chore: change db adapter args
This commit is contained in:
@@ -43,8 +43,7 @@ const getExecuteStaticAccess = (config: SanitizedCollectionConfig) => async (req
|
||||
}
|
||||
|
||||
const { docs } = await req.payload.db.find({
|
||||
payload: req.payload,
|
||||
collection: config,
|
||||
collection: config.slug,
|
||||
where: queryToBuild,
|
||||
limit: 1,
|
||||
});
|
||||
|
||||
@@ -69,8 +69,7 @@ async function forgotPassword(incomingArgs: Arguments): Promise<string | null> {
|
||||
}
|
||||
|
||||
const { docs } = await payload.db.find<UserDoc>({
|
||||
payload,
|
||||
collection: collectionConfig,
|
||||
collection: collectionConfig.slug,
|
||||
where: { email: { equals: (data.email as string).toLowerCase() } },
|
||||
limit: 1,
|
||||
});
|
||||
|
||||
@@ -9,8 +9,7 @@ async function init(args: { req: PayloadRequest, collection: string }): Promise<
|
||||
const collection = payload.collections[slug].config;
|
||||
|
||||
const { docs } = await payload.db.find({
|
||||
payload,
|
||||
collection,
|
||||
collection: slug,
|
||||
limit: 1,
|
||||
pagination: false,
|
||||
});
|
||||
|
||||
@@ -80,8 +80,7 @@ async function login<TSlug extends keyof GeneratedTypes['collections']>(
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore Improper typing in library, additional args should be optional
|
||||
const { docs } = await payload.db.find<any>({
|
||||
payload,
|
||||
collection: collectionConfig,
|
||||
collection: collectionConfig.slug,
|
||||
where: { email: { equals: email.toLowerCase() } },
|
||||
limit: 1,
|
||||
});
|
||||
|
||||
@@ -41,8 +41,7 @@ async function registerFirstUser<TSlug extends keyof GeneratedTypes['collections
|
||||
} = args;
|
||||
|
||||
const { docs } = await payload.db.find({
|
||||
payload,
|
||||
collection: config,
|
||||
collection: config.slug,
|
||||
limit: 1,
|
||||
pagination: false,
|
||||
});
|
||||
|
||||
@@ -51,8 +51,7 @@ async function resetPassword(args: Arguments): Promise<Result> {
|
||||
// /////////////////////////////////////
|
||||
|
||||
const { docs } = await payload.db.find<any>({
|
||||
payload,
|
||||
collection: collectionConfig,
|
||||
collection: collectionConfig.slug,
|
||||
limit: 1,
|
||||
where: {
|
||||
resetPasswordToken: { equals: data.token },
|
||||
@@ -77,8 +76,7 @@ async function resetPassword(args: Arguments): Promise<Result> {
|
||||
}
|
||||
|
||||
const { updatedDocs } = await payload.db.update({
|
||||
payload,
|
||||
collection: collectionConfig,
|
||||
collection: collectionConfig.slug,
|
||||
where: {
|
||||
id: { equals: user.id },
|
||||
},
|
||||
|
||||
@@ -46,8 +46,7 @@ async function unlock(args: Args): Promise<boolean> {
|
||||
// /////////////////////////////////////
|
||||
|
||||
const { docs } = await req.payload.db.find({
|
||||
payload,
|
||||
collection: collectionConfig,
|
||||
collection: collectionConfig.slug,
|
||||
where: { email: { equals: data.email.toLowerCase() } },
|
||||
limit: 1,
|
||||
});
|
||||
|
||||
@@ -12,9 +12,6 @@ export type Args = {
|
||||
async function verifyEmail(args: Args): Promise<boolean> {
|
||||
const {
|
||||
req,
|
||||
req: {
|
||||
payload,
|
||||
},
|
||||
token,
|
||||
collection,
|
||||
} = args;
|
||||
@@ -23,8 +20,7 @@ async function verifyEmail(args: Args): Promise<boolean> {
|
||||
}
|
||||
|
||||
const { docs } = await req.payload.db.find<any>({
|
||||
payload,
|
||||
collection: collection.config,
|
||||
collection: collection.config.slug,
|
||||
limit: 1,
|
||||
where: {
|
||||
_verificationToken: { equals: token },
|
||||
@@ -37,8 +33,7 @@ async function verifyEmail(args: Args): Promise<boolean> {
|
||||
if (user && user._verified === true) throw new APIError('This account has already been activated.', httpStatus.ACCEPTED);
|
||||
|
||||
await req.payload.db.update({
|
||||
payload,
|
||||
collection: collection.config,
|
||||
collection: collection.config.slug,
|
||||
where: {
|
||||
id: user.id,
|
||||
},
|
||||
|
||||
@@ -93,7 +93,11 @@ async function deleteOperation<TSlug extends keyof GeneratedTypes['collections']
|
||||
// Retrieve documents
|
||||
// /////////////////////////////////////
|
||||
|
||||
const { docs } = await payload.db.find<GeneratedTypes['collections'][TSlug]>({ payload, locale, where: fullWhere, collection: collectionConfig });
|
||||
const { docs } = await payload.db.find<GeneratedTypes['collections'][TSlug]>({
|
||||
locale,
|
||||
where: fullWhere,
|
||||
collection: collectionConfig.slug,
|
||||
});
|
||||
|
||||
const errors = [];
|
||||
|
||||
|
||||
@@ -125,8 +125,7 @@ async function find<T extends TypeWithID & Record<string, unknown>>(
|
||||
});
|
||||
|
||||
result = await payload.db.queryDrafts<T>({
|
||||
payload,
|
||||
collection: collectionConfig,
|
||||
collection: collectionConfig.slug,
|
||||
where: fullWhere,
|
||||
page: sanitizedPage,
|
||||
limit: sanitizedLimit,
|
||||
@@ -144,8 +143,7 @@ async function find<T extends TypeWithID & Record<string, unknown>>(
|
||||
});
|
||||
|
||||
result = await payload.db.find<T>({
|
||||
payload,
|
||||
collection: collectionConfig,
|
||||
collection: collectionConfig.slug,
|
||||
where: fullWhere,
|
||||
page: sanitizedPage,
|
||||
limit: sanitizedLimit,
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
/* eslint-disable no-underscore-dangle */
|
||||
import httpStatus from 'http-status';
|
||||
import { PayloadRequest } from '../../express/types';
|
||||
import { Collection, CollectionModel } from '../config/types';
|
||||
import sanitizeInternalFields from '../../utilities/sanitizeInternalFields';
|
||||
import { Collection } from '../config/types';
|
||||
import { APIError, Forbidden, NotFound } from '../../errors';
|
||||
import executeAccess from '../../auth/executeAccess';
|
||||
import { TypeWithVersion } from '../../versions/types';
|
||||
@@ -61,9 +60,8 @@ async function findVersionByID<T extends TypeWithVersion<T> = any>(args: Argumen
|
||||
// /////////////////////////////////////
|
||||
|
||||
const versionsQuery = await payload.db.findVersions<T>({
|
||||
payload,
|
||||
locale,
|
||||
collection: collectionConfig,
|
||||
collection: collectionConfig.slug,
|
||||
limit: 1,
|
||||
pagination: false,
|
||||
where: fullWhere,
|
||||
|
||||
@@ -78,11 +78,10 @@ async function findVersions<T extends TypeWithVersion<T>>(
|
||||
});
|
||||
|
||||
const paginatedDocs = await payload.db.findVersions<T>({
|
||||
payload,
|
||||
where: fullWhere,
|
||||
page: page || 1,
|
||||
limit: limit ?? 10,
|
||||
collection: collectionConfig,
|
||||
collection: collectionConfig.slug,
|
||||
sortProperty,
|
||||
sortOrder,
|
||||
locale,
|
||||
|
||||
@@ -118,8 +118,7 @@ async function update<TSlug extends keyof GeneratedTypes['collections']>(
|
||||
});
|
||||
|
||||
const query = await payload.db.queryDrafts<GeneratedTypes['collections'][TSlug]>({
|
||||
payload,
|
||||
collection: collectionConfig,
|
||||
collection: collectionConfig.slug,
|
||||
where: versionsWhere,
|
||||
locale,
|
||||
});
|
||||
@@ -127,9 +126,8 @@ async function update<TSlug extends keyof GeneratedTypes['collections']>(
|
||||
docs = query.docs;
|
||||
} else {
|
||||
const query = await payload.db.find({
|
||||
payload,
|
||||
locale,
|
||||
collection: collectionConfig,
|
||||
collection: collectionConfig.slug,
|
||||
where: fullWhere,
|
||||
pagination: false,
|
||||
limit: 0,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import type { SchemaOptions } from 'mongoose';
|
||||
import { Configuration } from 'webpack';
|
||||
import type { Configuration } from 'webpack';
|
||||
import type { Config, SanitizedConfig } from '../config/types';
|
||||
import {
|
||||
import type {
|
||||
ArrayField,
|
||||
BlockField,
|
||||
CheckboxField,
|
||||
@@ -24,107 +24,109 @@ import {
|
||||
TextField,
|
||||
UploadField,
|
||||
} from '../fields/config/types';
|
||||
import { SanitizedCollectionConfig, TypeWithID } from '../collections/config/types';
|
||||
import { Payload } from '../payload';
|
||||
import { Document, Where } from '../types';
|
||||
import { SanitizedGlobalConfig } from '../globals/config/types';
|
||||
import type { TypeWithID } from '../collections/config/types';
|
||||
import type { Payload } from '../payload';
|
||||
import type { Document, Where } from '../types';
|
||||
|
||||
export interface DatabaseAdapter {
|
||||
/**
|
||||
* reference to the instance of payload
|
||||
*/
|
||||
payload: Payload;
|
||||
/**
|
||||
* Open the connection to the database
|
||||
*/
|
||||
connect?: ({ payload, config }: { payload: Payload, config: SanitizedConfig }) => Promise<void>
|
||||
connect?: ({ config }: { config: SanitizedConfig }) => Promise<void>;
|
||||
|
||||
/**
|
||||
* Perform startup tasks required to interact with the database such as building Schema and models
|
||||
*/
|
||||
init?: ({ payload, config }: { payload: Payload, config: SanitizedConfig }) => Promise<void>
|
||||
init?: ({ config }: { config: SanitizedConfig }) => Promise<void>;
|
||||
|
||||
/**
|
||||
* Terminate the connection with the database
|
||||
*/
|
||||
destroy?: () => Promise<void>
|
||||
destroy?: () => Promise<void>;
|
||||
|
||||
/**
|
||||
* Used to alias server only modules or make other changes to webpack configuration
|
||||
*/
|
||||
webpack?: (config: Configuration) => Configuration
|
||||
webpack?: (config: Configuration) => Configuration;
|
||||
|
||||
// migrations
|
||||
/**
|
||||
* Output a migration file
|
||||
*/
|
||||
createMigration: ({ payload }: { payload: Payload }) => Promise<void>
|
||||
createMigration: () => Promise<void>;
|
||||
|
||||
/**
|
||||
* Run any migration up functions that have not yet been performed and update the status
|
||||
*/
|
||||
migrate: ({ payload }: { payload: Payload }) => Promise<void>
|
||||
migrate: () => Promise<void>;
|
||||
|
||||
/**
|
||||
* Read the current state of migrations and output the result to show which have been run
|
||||
*/
|
||||
migrateStatus: ({ payload }: { payload: Payload }) => Promise<void>
|
||||
migrateStatus: () => Promise<void>;
|
||||
|
||||
/**
|
||||
* Run any migration down functions that have been performed
|
||||
*/
|
||||
migrateDown: ({ payload }: { payload: Payload }) => Promise<void>
|
||||
migrateDown: () => Promise<void>;
|
||||
|
||||
/**
|
||||
* Run all migration down functions before running up
|
||||
*/
|
||||
migrateRefresh: ({ payload }: { payload: Payload }) => Promise<void>
|
||||
migrateRefresh: () => Promise<void>;
|
||||
|
||||
/**
|
||||
* Run all migrate down functions
|
||||
*/
|
||||
migrateReset: ({ payload }: { payload: Payload }) => Promise<void>
|
||||
migrateReset: () => Promise<void>;
|
||||
|
||||
/**
|
||||
* Drop the current database and run all migrate up functions
|
||||
*/
|
||||
migrateFresh: ({ payload }: { payload: Payload }) => Promise<void>
|
||||
migrateFresh: () => Promise<void>;
|
||||
|
||||
// transactions
|
||||
/**
|
||||
* Perform many database interactions in a single, all-or-nothing operation.
|
||||
*/
|
||||
transaction?: ({ payload }: { payload: Payload }) => Promise<boolean>
|
||||
transaction?: () => Promise<boolean>;
|
||||
|
||||
/**
|
||||
* Start a transaction, requiring commit() to be called for any changes to be made.
|
||||
*/
|
||||
beginTransaction?: ({ payload }: { payload: Payload }) => Promise<boolean>
|
||||
beginTransaction?: () => Promise<boolean>;
|
||||
|
||||
/**
|
||||
* Cancel any changes since the beginning of the transaction.
|
||||
*/
|
||||
rollbackTransaction?: ({ payload }: { payload: Payload }) => Promise<boolean>
|
||||
rollbackTransaction?: () => Promise<boolean>;
|
||||
|
||||
/**
|
||||
* Instruct the database to complete the changes made in the transaction.
|
||||
*/
|
||||
commitTransaction?: ({ payload }: { payload: Payload }) => Promise<boolean>
|
||||
commitTransaction?: () => Promise<boolean>;
|
||||
|
||||
// versions
|
||||
queryDrafts: <T = TypeWithID>(args: QueryDraftsArgs) => Promise<PaginatedDocs<T>>
|
||||
queryDrafts: <T = TypeWithID>(args: QueryDraftsArgs) => Promise<PaginatedDocs<T>>;
|
||||
|
||||
// operations
|
||||
find: <T = TypeWithID>(args: FindArgs) => Promise<PaginatedDocs<T>>
|
||||
findVersions: <T = TypeWithID>(args: FindVersionArgs) => Promise<PaginatedDocs<T>>
|
||||
findGlobalVersions: <T = TypeWithID>(args: FindGlobalVersionArgs) => Promise<PaginatedDocs<T>>
|
||||
findOne: FindOne
|
||||
create: Create
|
||||
update: Update
|
||||
updateOne: UpdateOne
|
||||
deleteOne: DeleteOne
|
||||
deleteMany: DeleteMany
|
||||
find: <T = TypeWithID>(args: FindArgs) => Promise<PaginatedDocs<T>>;
|
||||
findVersions: <T = TypeWithID>(args: FindVersionArgs) => Promise<PaginatedDocs<T>>;
|
||||
findGlobalVersions: <T = TypeWithID>(args: FindGlobalVersionArgs) => Promise<PaginatedDocs<T>>;
|
||||
findOne: FindOne;
|
||||
create: Create;
|
||||
update: Update;
|
||||
updateOne: UpdateOne;
|
||||
deleteOne: DeleteOne;
|
||||
deleteMany: DeleteMany;
|
||||
}
|
||||
|
||||
export type QueryDraftsArgs = {
|
||||
payload: Payload
|
||||
collection: SanitizedCollectionConfig
|
||||
collection: string
|
||||
where?: Where
|
||||
page?: number
|
||||
limit?: number
|
||||
@@ -135,8 +137,7 @@ export type QueryDraftsArgs = {
|
||||
}
|
||||
|
||||
export type FindArgs = {
|
||||
payload: Payload
|
||||
collection: SanitizedCollectionConfig
|
||||
collection: string
|
||||
where?: Where
|
||||
page?: number
|
||||
skip?: number
|
||||
@@ -149,8 +150,7 @@ export type FindArgs = {
|
||||
}
|
||||
|
||||
export type FindVersionArgs = {
|
||||
payload: Payload
|
||||
collection: SanitizedCollectionConfig
|
||||
collection: string
|
||||
where?: Where
|
||||
page?: number
|
||||
skip?: number
|
||||
@@ -163,8 +163,7 @@ export type FindVersionArgs = {
|
||||
}
|
||||
|
||||
export type FindGlobalVersionArgs = {
|
||||
payload: Payload
|
||||
global: SanitizedGlobalConfig
|
||||
global: string
|
||||
where?: Where
|
||||
page?: number
|
||||
skip?: number
|
||||
@@ -177,8 +176,7 @@ export type FindGlobalVersionArgs = {
|
||||
}
|
||||
|
||||
export type FindOneArgs = {
|
||||
payload: Payload
|
||||
collection: SanitizedCollectionConfig
|
||||
collection: string
|
||||
where: Where
|
||||
locale?: string
|
||||
sort?: {
|
||||
@@ -189,8 +187,7 @@ export type FindOneArgs = {
|
||||
type FindOne = (args: FindOneArgs) => Promise<PaginatedDocs>
|
||||
|
||||
type CreateArgs = {
|
||||
payload: Payload
|
||||
collection: SanitizedCollectionConfig
|
||||
collection: string
|
||||
data: Record<string, unknown>
|
||||
draft?: boolean
|
||||
locale?: string
|
||||
@@ -199,8 +196,7 @@ type CreateArgs = {
|
||||
type Create = (args: CreateArgs) => Promise<Document>
|
||||
|
||||
type UpdateArgs = {
|
||||
payload: Payload
|
||||
collection: SanitizedCollectionConfig
|
||||
collection: string
|
||||
data: Record<string, unknown>
|
||||
where: Where
|
||||
draft?: boolean
|
||||
@@ -210,8 +206,7 @@ type UpdateArgs = {
|
||||
type Update = (args: UpdateArgs) => Promise<Document>
|
||||
|
||||
type UpdateOneArgs = {
|
||||
payload: Payload,
|
||||
collection: SanitizedCollectionConfig,
|
||||
collection: string,
|
||||
data: Record<string, unknown>,
|
||||
where: Where,
|
||||
draft?: boolean
|
||||
@@ -221,8 +216,7 @@ type UpdateOneArgs = {
|
||||
type UpdateOne = (args: UpdateOneArgs) => Promise<Document>
|
||||
|
||||
type DeleteOneArgs = {
|
||||
payload: Payload,
|
||||
collection: SanitizedCollectionConfig,
|
||||
collection: string,
|
||||
data: Record<string, unknown>,
|
||||
where: Where,
|
||||
}
|
||||
@@ -230,8 +224,7 @@ type DeleteOneArgs = {
|
||||
type DeleteOne = (args: DeleteOneArgs) => Promise<Document>
|
||||
|
||||
type DeleteManyArgs = {
|
||||
payload: Payload,
|
||||
collection: SanitizedCollectionConfig,
|
||||
collection: string,
|
||||
where: Where,
|
||||
}
|
||||
|
||||
|
||||
@@ -72,13 +72,12 @@ async function findVersions<T extends TypeWithVersion<T>>(
|
||||
});
|
||||
|
||||
const paginatedDocs = await payload.db.findGlobalVersions<T>({
|
||||
payload,
|
||||
where: fullWhere,
|
||||
page: page || 1,
|
||||
limit: limit ?? 10,
|
||||
sortProperty,
|
||||
sortOrder,
|
||||
global: globalConfig,
|
||||
global: globalConfig.slug,
|
||||
locale,
|
||||
});
|
||||
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
/* eslint-disable @typescript-eslint/no-var-requires */
|
||||
import type { ConnectOptions } from 'mongoose';
|
||||
import mongoose from 'mongoose';
|
||||
import type { Payload } from '..';
|
||||
|
||||
import { SanitizedConfig } from 'payload/config';
|
||||
import type { MongooseAdapter } from '.';
|
||||
|
||||
export async function connect(
|
||||
this: MongooseAdapter,
|
||||
{ payload }: { payload: Payload },
|
||||
{ config }: { config: SanitizedConfig },
|
||||
): Promise<void> {
|
||||
let urlToConnect = this.url;
|
||||
let successfulConnectionMessage = 'Connected to MongoDB server successfully!';
|
||||
@@ -45,13 +45,13 @@ export async function connect(
|
||||
await mongoose.connect(urlToConnect, connectionOptions);
|
||||
|
||||
if (process.env.PAYLOAD_DROP_DATABASE === 'true') {
|
||||
payload.logger.info('---- DROPPING DATABASE ----');
|
||||
this.payload.logger.info('---- DROPPING DATABASE ----');
|
||||
await mongoose.connection.dropDatabase();
|
||||
payload.logger.info('---- DROPPED DATABASE ----');
|
||||
this.payload.logger.info('---- DROPPED DATABASE ----');
|
||||
}
|
||||
payload.logger.info(successfulConnectionMessage);
|
||||
this.payload.logger.info(successfulConnectionMessage);
|
||||
} catch (err) {
|
||||
payload.logger.error(
|
||||
this.payload.logger.error(
|
||||
`Error: cannot connect to MongoDB. Details: ${err.message}`,
|
||||
err,
|
||||
);
|
||||
|
||||
@@ -6,9 +6,9 @@ import flattenWhereToOperators from '../database/flattenWhereToOperators';
|
||||
|
||||
export async function find<T = unknown>(
|
||||
this: MongooseAdapter,
|
||||
{ payload, collection, where, page, limit, sortProperty, sortOrder, locale, pagination }: FindArgs,
|
||||
{ collection, where, page, limit, sortProperty, sortOrder, locale, pagination }: FindArgs,
|
||||
): Promise<PaginatedDocs<T>> {
|
||||
const Model = this.collections[collection.slug];
|
||||
const Model = this.collections[collection];
|
||||
|
||||
let useEstimatedCount = false;
|
||||
|
||||
@@ -18,7 +18,7 @@ export async function find<T = unknown>(
|
||||
}
|
||||
|
||||
const query = await Model.buildQuery({
|
||||
payload,
|
||||
payload: this.payload,
|
||||
locale,
|
||||
where,
|
||||
});
|
||||
|
||||
@@ -6,9 +6,9 @@ import flattenWhereToOperators from '../database/flattenWhereToOperators';
|
||||
|
||||
export async function findGlobalVersions<T = unknown>(
|
||||
this: MongooseAdapter,
|
||||
{ payload, global, where, page, limit, sortProperty, sortOrder, locale, pagination, skip }: FindGlobalVersionArgs,
|
||||
{ global, where, page, limit, sortProperty, sortOrder, locale, pagination, skip }: FindGlobalVersionArgs,
|
||||
): Promise<PaginatedDocs<T>> {
|
||||
const Model = this.versions[global.slug];
|
||||
const Model = this.versions[global];
|
||||
|
||||
let useEstimatedCount = false;
|
||||
|
||||
@@ -18,10 +18,10 @@ export async function findGlobalVersions<T = unknown>(
|
||||
}
|
||||
|
||||
const query = await Model.buildQuery({
|
||||
payload,
|
||||
payload: this.payload,
|
||||
locale,
|
||||
where,
|
||||
globalSlug: global.slug,
|
||||
globalSlug: global,
|
||||
});
|
||||
|
||||
const paginationOptions = {
|
||||
|
||||
@@ -6,9 +6,9 @@ import flattenWhereToOperators from '../database/flattenWhereToOperators';
|
||||
|
||||
export async function findVersions<T = unknown>(
|
||||
this: MongooseAdapter,
|
||||
{ payload, collection, where, page, limit, sortProperty, sortOrder, locale, pagination, skip }: FindVersionArgs,
|
||||
{ collection, where, page, limit, sortProperty, sortOrder, locale, pagination, skip }: FindVersionArgs,
|
||||
): Promise<PaginatedDocs<T>> {
|
||||
const Model = this.versions[collection.slug];
|
||||
const Model = this.versions[collection];
|
||||
|
||||
let useEstimatedCount = false;
|
||||
|
||||
@@ -18,7 +18,7 @@ export async function findVersions<T = unknown>(
|
||||
}
|
||||
|
||||
const query = await Model.buildQuery({
|
||||
payload,
|
||||
payload: this.payload,
|
||||
locale,
|
||||
where,
|
||||
});
|
||||
|
||||
@@ -9,8 +9,10 @@ import { GlobalModel } from '../globals/config/types';
|
||||
import { find } from './find';
|
||||
import { findVersions } from './findVersions';
|
||||
import { findGlobalVersions } from './findGlobalVersions';
|
||||
import type { Payload } from '../index';
|
||||
|
||||
export interface Args {
|
||||
payload: Payload,
|
||||
/** The URL to connect to MongoDB */
|
||||
url: string
|
||||
connectOptions?: ConnectOptions & {
|
||||
@@ -31,10 +33,11 @@ export type MongooseAdapter = DatabaseAdapter &
|
||||
}
|
||||
}
|
||||
|
||||
export function mongooseAdapter({ url, connectOptions }: Args): MongooseAdapter {
|
||||
export function mongooseAdapter({ payload, url, connectOptions }: Args): MongooseAdapter {
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
return {
|
||||
payload,
|
||||
url,
|
||||
connectOptions: connectOptions || {},
|
||||
collections: {},
|
||||
|
||||
@@ -2,23 +2,23 @@
|
||||
import mongoose from 'mongoose';
|
||||
import paginate from 'mongoose-paginate-v2';
|
||||
import mongooseAggregatePaginate from 'mongoose-aggregate-paginate-v2';
|
||||
import { SanitizedConfig } from 'payload/config';
|
||||
import { buildVersionCollectionFields } from '../versions/buildCollectionFields';
|
||||
import getBuildQueryPlugin from './queries/buildQuery';
|
||||
import buildCollectionSchema from './models/buildCollectionSchema';
|
||||
import buildSchema from './models/buildSchema';
|
||||
import { CollectionModel, SanitizedCollectionConfig } from '../collections/config/types';
|
||||
import { getVersionsModelName } from '../versions/getVersionsModelName';
|
||||
import type { Payload } from '..';
|
||||
import type { MongooseAdapter } from '.';
|
||||
import { buildGlobalModel } from './models/buildGlobalModel';
|
||||
import { buildVersionGlobalFields } from '../versions/buildGlobalFields';
|
||||
|
||||
export async function init(
|
||||
this: MongooseAdapter,
|
||||
{ payload }: { payload: Payload },
|
||||
{ config }: { config: SanitizedConfig },
|
||||
): Promise<void> {
|
||||
payload.config.collections.forEach((collection: SanitizedCollectionConfig) => {
|
||||
const schema = buildCollectionSchema(collection, payload.config);
|
||||
this.payload.config.collections.forEach((collection: SanitizedCollectionConfig) => {
|
||||
const schema = buildCollectionSchema(collection, this.payload.config);
|
||||
|
||||
if (collection.versions) {
|
||||
const versionModelName = getVersionsModelName(collection);
|
||||
@@ -26,7 +26,7 @@ export async function init(
|
||||
const versionCollectionFields = buildVersionCollectionFields(collection);
|
||||
|
||||
const versionSchema = buildSchema(
|
||||
payload.config,
|
||||
this.payload.config,
|
||||
versionCollectionFields,
|
||||
{
|
||||
disableUnique: true,
|
||||
@@ -64,35 +64,35 @@ export async function init(
|
||||
}
|
||||
|
||||
const model = mongoose.model(versionModelName, versionSchema) as CollectionModel;
|
||||
payload.versions[collection.slug] = model;
|
||||
this.payload.versions[collection.slug] = model;
|
||||
this.versions[collection.slug] = model;
|
||||
}
|
||||
|
||||
const model = mongoose.model(collection.slug, schema) as CollectionModel;
|
||||
this.collections[collection.slug] = model;
|
||||
|
||||
payload.collections[collection.slug] = {
|
||||
this.payload.collections[collection.slug] = {
|
||||
Model: model,
|
||||
config: collection,
|
||||
};
|
||||
});
|
||||
|
||||
const model = buildGlobalModel(payload.config);
|
||||
const model = buildGlobalModel(this.payload.config);
|
||||
this.globals = model;
|
||||
|
||||
payload.globals.Model = model;
|
||||
this.payload.globals.Model = model;
|
||||
|
||||
payload.config.globals.forEach((global) => {
|
||||
this.payload.config.globals.forEach((global) => {
|
||||
if (global.versions) {
|
||||
const versionModelName = getVersionsModelName(global);
|
||||
|
||||
const versionGlobalFields = buildVersionGlobalFields(global);
|
||||
|
||||
const versionSchema = buildSchema(
|
||||
payload.config,
|
||||
this.payload.config,
|
||||
versionGlobalFields,
|
||||
{
|
||||
indexSortableFields: payload.config.indexSortableFields,
|
||||
indexSortableFields: this.payload.config.indexSortableFields,
|
||||
disableUnique: true,
|
||||
draftsEnabled: true,
|
||||
options: {
|
||||
@@ -108,7 +108,7 @@ export async function init(
|
||||
const versionsModel = mongoose.model(versionModelName, versionSchema) as CollectionModel;
|
||||
this.versions[global.slug] = versionsModel;
|
||||
|
||||
payload.versions[global.slug] = versionsModel;
|
||||
this.payload.versions[global.slug] = versionsModel;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -13,14 +13,14 @@ type AggregateVersion<T> = {
|
||||
|
||||
export async function queryDrafts<T = unknown>(
|
||||
this: MongooseAdapter,
|
||||
{ payload, collection, where, page, limit, sortProperty, sortOrder, locale, pagination }: QueryDraftsArgs,
|
||||
{ collection, where, page, limit, sortProperty, sortOrder, locale, pagination }: QueryDraftsArgs,
|
||||
): Promise<PaginatedDocs<T>> {
|
||||
const VersionModel = this.versions[collection.slug];
|
||||
const VersionModel = this.versions[collection];
|
||||
|
||||
const versionQuery = await VersionModel.buildQuery({
|
||||
where,
|
||||
locale,
|
||||
payload,
|
||||
payload: this.payload,
|
||||
});
|
||||
|
||||
const aggregate = VersionModel.aggregate<AggregateVersion<T>>([
|
||||
|
||||
@@ -207,6 +207,7 @@ export class BasePayload<TGeneratedTypes extends GeneratedTypes> {
|
||||
|
||||
if (!this.config.db) {
|
||||
this.config.db = mongooseAdapter({
|
||||
payload: this,
|
||||
url: this.mongoURL,
|
||||
connectOptions: options.mongoOptions,
|
||||
});
|
||||
@@ -215,7 +216,7 @@ export class BasePayload<TGeneratedTypes extends GeneratedTypes> {
|
||||
|
||||
this.db = this.config.db;
|
||||
if (this.db?.connect) {
|
||||
this.mongoMemoryServer = await this.db.connect({ payload: this, config: this.config });
|
||||
this.mongoMemoryServer = await this.db.connect({ config: this.config });
|
||||
}
|
||||
|
||||
// Configure email service
|
||||
@@ -233,7 +234,7 @@ export class BasePayload<TGeneratedTypes extends GeneratedTypes> {
|
||||
}
|
||||
|
||||
if (this.db.init) {
|
||||
await this.db?.init({ payload: this, config: this.config });
|
||||
await this.db?.init({ config: this.config });
|
||||
}
|
||||
|
||||
serverInitTelemetry(this);
|
||||
|
||||
@@ -34,9 +34,8 @@ export const enforceMaxVersions = async ({
|
||||
};
|
||||
|
||||
const query = await payload.db.findVersions({
|
||||
payload,
|
||||
where,
|
||||
collection,
|
||||
collection: collection.slug,
|
||||
skip: max,
|
||||
sortProperty: 'updatedAt',
|
||||
sortOrder: 'desc',
|
||||
@@ -46,9 +45,8 @@ export const enforceMaxVersions = async ({
|
||||
[oldestAllowedDoc] = query.docs;
|
||||
} else if (global) {
|
||||
const query = await payload.db.findGlobalVersions({
|
||||
payload,
|
||||
where,
|
||||
global,
|
||||
global: global.slug,
|
||||
skip: max,
|
||||
sortProperty: 'updatedAt',
|
||||
sortOrder: 'desc',
|
||||
|
||||
@@ -185,7 +185,6 @@ describe('Auth', () => {
|
||||
// const db = client.db(mongoDBName);
|
||||
const { db } = mongoose.connection;
|
||||
const userResult = await db.collection('public-users').findOne({ email: emailToVerify });
|
||||
// @ts-expect-error trust
|
||||
const { _verified, _verificationToken } = userResult;
|
||||
|
||||
expect(_verified).toBe(false);
|
||||
|
||||
Reference in New Issue
Block a user