chore: abstracts dev / test suites to accept incoming database specification
This commit is contained in:
@@ -552,17 +552,16 @@ export type Config = {
|
|||||||
/** Extension point to add your custom data. */
|
/** Extension point to add your custom data. */
|
||||||
custom?: Record<string, any>;
|
custom?: Record<string, any>;
|
||||||
/** Pass in a database adapter for use on this project. */
|
/** Pass in a database adapter for use on this project. */
|
||||||
db?: DatabaseAdapter
|
db: (args: { payload: Payload }) => DatabaseAdapter
|
||||||
};
|
};
|
||||||
|
|
||||||
export type SanitizedConfig = Omit<
|
export type SanitizedConfig = Omit<
|
||||||
DeepRequired<Config>,
|
DeepRequired<Config>,
|
||||||
'collections' | 'globals' | 'endpoint' | 'db'
|
'collections' | 'globals' | 'endpoint'
|
||||||
> & {
|
> & {
|
||||||
collections: SanitizedCollectionConfig[];
|
collections: SanitizedCollectionConfig[];
|
||||||
globals: SanitizedGlobalConfig[];
|
globals: SanitizedGlobalConfig[];
|
||||||
endpoints: Endpoint[];
|
endpoints: Endpoint[];
|
||||||
db: DatabaseAdapter
|
|
||||||
paths: {
|
paths: {
|
||||||
configDir: string
|
configDir: string
|
||||||
config: string
|
config: string
|
||||||
|
|||||||
@@ -12,6 +12,11 @@ export const connect: Connect = async function connect(
|
|||||||
if (this.url === false) {
|
if (this.url === false) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!payload.local && typeof this.url !== 'string') {
|
||||||
|
throw new Error('Error: missing MongoDB connection URL.');
|
||||||
|
}
|
||||||
|
|
||||||
let urlToConnect = this.url;
|
let urlToConnect = this.url;
|
||||||
let successfulConnectionMessage = 'Connected to MongoDB server successfully!';
|
let successfulConnectionMessage = 'Connected to MongoDB server successfully!';
|
||||||
|
|
||||||
|
|||||||
@@ -24,12 +24,11 @@ import { updateGlobal } from './updateGlobal';
|
|||||||
import { updateOne } from './updateOne';
|
import { updateOne } from './updateOne';
|
||||||
import { updateVersion } from './updateVersion';
|
import { updateVersion } from './updateVersion';
|
||||||
import { deleteMany } from './deleteMany';
|
import { deleteMany } from './deleteMany';
|
||||||
import { baseDatabaseAdapter } from '../../baseDatabaseAdapter';
|
import { withBaseDatabaseAdapter } from '../../baseDatabaseAdapter';
|
||||||
import { destroy } from './destroy';
|
import { destroy } from './destroy';
|
||||||
import type { CollectionModel, GlobalModel } from './types';
|
import type { CollectionModel, GlobalModel } from './types';
|
||||||
|
|
||||||
export interface Args {
|
export interface Args {
|
||||||
payload: Payload;
|
|
||||||
/** The URL to connect to MongoDB or false to start payload and prevent connecting */
|
/** The URL to connect to MongoDB or false to start payload and prevent connecting */
|
||||||
url: string | false;
|
url: string | false;
|
||||||
migrationDir?: string;
|
migrationDir?: string;
|
||||||
@@ -54,23 +53,22 @@ export type MongooseAdapter = DatabaseAdapter &
|
|||||||
connection: Connection
|
connection: Connection
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type MongooseAdapterResult = (args: { payload: Payload }) => MongooseAdapter
|
||||||
|
|
||||||
export function mongooseAdapter({
|
export function mongooseAdapter({
|
||||||
payload,
|
|
||||||
url,
|
url,
|
||||||
connectOptions,
|
connectOptions,
|
||||||
migrationDir,
|
migrationDir,
|
||||||
}: Args): MongooseAdapter {
|
}: Args): MongooseAdapterResult {
|
||||||
const adapter = baseDatabaseAdapter({
|
function adapter({ payload }: { payload: Payload }) {
|
||||||
|
mongoose.set('strictQuery', false);
|
||||||
|
|
||||||
|
return withBaseDatabaseAdapter<MongooseAdapter>({
|
||||||
payload,
|
payload,
|
||||||
migrationDir,
|
migrationDir,
|
||||||
});
|
|
||||||
mongoose.set('strictQuery', false);
|
|
||||||
return {
|
|
||||||
...adapter,
|
|
||||||
connection: undefined,
|
connection: undefined,
|
||||||
mongoMemoryServer: undefined,
|
mongoMemoryServer: undefined,
|
||||||
sessions: {},
|
sessions: {},
|
||||||
payload,
|
|
||||||
url,
|
url,
|
||||||
connectOptions: connectOptions || {},
|
connectOptions: connectOptions || {},
|
||||||
globals: undefined,
|
globals: undefined,
|
||||||
@@ -99,5 +97,8 @@ export function mongooseAdapter({
|
|||||||
createVersion,
|
createVersion,
|
||||||
updateVersion,
|
updateVersion,
|
||||||
deleteVersions,
|
deleteVersions,
|
||||||
};
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return adapter;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
exports.mongooseAdapter = () => ({});
|
||||||
|
|||||||
@@ -1,13 +1,15 @@
|
|||||||
import path from 'path';
|
import path from 'path';
|
||||||
import type { Webpack } from '../../types';
|
import type { Webpack } from '../../types';
|
||||||
|
|
||||||
export const webpack: Webpack = (config) => ({
|
export const webpack: Webpack = (config) => {
|
||||||
|
return {
|
||||||
...config,
|
...config,
|
||||||
resolve: {
|
resolve: {
|
||||||
...config.resolve || {},
|
...config.resolve || {},
|
||||||
alias: {
|
alias: {
|
||||||
...config.resolve?.alias || {},
|
...config.resolve?.alias || {},
|
||||||
[path.resolve(__filename)]: path.resolve(__dirname, 'mock.js'),
|
[path.resolve(__dirname, './index')]: path.resolve(__dirname, 'mock'),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
});
|
};
|
||||||
|
};
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
/* eslint-disable no-param-reassign */
|
||||||
|
import { Configuration } from 'webpack';
|
||||||
import { transaction } from './transaction';
|
import { transaction } from './transaction';
|
||||||
import { migrate } from './migrations/migrate';
|
import { migrate } from './migrations/migrate';
|
||||||
import { migrateStatus } from './migrations/migrateStatus';
|
import { migrateStatus } from './migrations/migrateStatus';
|
||||||
@@ -8,8 +10,7 @@ import { DatabaseAdapter } from './types';
|
|||||||
import type { Payload } from '../index';
|
import type { Payload } from '../index';
|
||||||
import { createMigration } from './migrations/createMigration';
|
import { createMigration } from './migrations/createMigration';
|
||||||
|
|
||||||
|
type BaseDatabaseAdapter = Omit<DatabaseAdapter,
|
||||||
type BaseDatabaseAdapter = Pick<DatabaseAdapter, 'payload'
|
|
||||||
| 'transaction'
|
| 'transaction'
|
||||||
| 'migrate'
|
| 'migrate'
|
||||||
| 'createMigration'
|
| 'createMigration'
|
||||||
@@ -19,18 +20,24 @@ type BaseDatabaseAdapter = Pick<DatabaseAdapter, 'payload'
|
|||||||
| 'migrateReset'
|
| 'migrateReset'
|
||||||
| 'migrateFresh'
|
| 'migrateFresh'
|
||||||
| 'migrationDir'
|
| 'migrationDir'
|
||||||
>
|
>
|
||||||
|
|
||||||
type Args = {
|
type Args = {
|
||||||
payload: Payload,
|
payload: Payload,
|
||||||
migrationDir?: string,
|
migrationDir?: string,
|
||||||
}
|
}
|
||||||
export function baseDatabaseAdapter({
|
export function withBaseDatabaseAdapter<T extends BaseDatabaseAdapter>(args: T): T {
|
||||||
payload,
|
// Need to implement DB Webpack config extensions here
|
||||||
migrationDir = '.migrations',
|
if (args.webpack) {
|
||||||
}: Args): BaseDatabaseAdapter {
|
const existingWebpackConfig = args.payload.config.admin.webpack ? args.payload.config.admin.webpack : (webpackConfig) => webpackConfig;
|
||||||
|
args.payload.config.admin.webpack = (webpackConfig: Configuration) => {
|
||||||
|
return args.webpack(
|
||||||
|
existingWebpackConfig(webpackConfig),
|
||||||
|
);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
payload,
|
|
||||||
transaction,
|
transaction,
|
||||||
migrate,
|
migrate,
|
||||||
createMigration,
|
createMigration,
|
||||||
@@ -39,6 +46,6 @@ export function baseDatabaseAdapter({
|
|||||||
migrateRefresh,
|
migrateRefresh,
|
||||||
migrateReset,
|
migrateReset,
|
||||||
migrateFresh: async () => null,
|
migrateFresh: async () => null,
|
||||||
migrationDir,
|
...args,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -58,7 +58,6 @@ import findConfig from './config/find';
|
|||||||
|
|
||||||
import { defaults as emailDefaults } from './email/defaults';
|
import { defaults as emailDefaults } from './email/defaults';
|
||||||
import type { DatabaseAdapter } from '.';
|
import type { DatabaseAdapter } from '.';
|
||||||
import { mongooseAdapter } from './database/adapters/mongoose';
|
|
||||||
import type { PaginatedDocs } from './database/types';
|
import type { PaginatedDocs } from './database/types';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -159,10 +158,6 @@ export class BasePayload<TGeneratedTypes extends GeneratedTypes> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!options.local && options.mongoURL !== false && typeof options.mongoURL !== 'string') {
|
|
||||||
throw new Error('Error: missing MongoDB connection URL.');
|
|
||||||
}
|
|
||||||
|
|
||||||
this.secret = crypto
|
this.secret = crypto
|
||||||
.createHash('sha256')
|
.createHash('sha256')
|
||||||
.update(options.secret)
|
.update(options.secret)
|
||||||
@@ -198,21 +193,10 @@ export class BasePayload<TGeneratedTypes extends GeneratedTypes> {
|
|||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
// THIS BLOCK IS TEMPORARY UNTIL 2.0.0
|
this.db = this.config.db({ payload: this });
|
||||||
// We automatically add the Mongoose adapter
|
|
||||||
// if there is no defined database adapter
|
|
||||||
if (!this.config.db) {
|
|
||||||
this.config.db = mongooseAdapter({
|
|
||||||
payload: this,
|
|
||||||
url: this.mongoURL ? this.mongoURL : false,
|
|
||||||
connectOptions: options.mongoOptions,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
this.db = this.config.db;
|
|
||||||
this.db.payload = this;
|
this.db.payload = this;
|
||||||
|
|
||||||
if (this.db?.connect) {
|
if (this.db.connect) {
|
||||||
await this.db.connect(this);
|
await this.db.connect(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,16 @@
|
|||||||
import { Config, SanitizedConfig } from '../src/config/types';
|
import { Config, SanitizedConfig } from '../src/config/types';
|
||||||
import { buildConfig as buildPayloadConfig } from '../src/config/build';
|
import { buildConfig as buildPayloadConfig } from '../src/config/build';
|
||||||
|
import { mongooseAdapter } from '../src/database/adapters/mongoose';
|
||||||
|
|
||||||
|
const databaseAdapters = {
|
||||||
|
mongoose: mongooseAdapter({
|
||||||
|
url: 'mongodb://127.0.0.1/payload',
|
||||||
|
}),
|
||||||
|
};
|
||||||
|
|
||||||
export function buildConfigWithDefaults(testConfig?: Partial<Config>): Promise<SanitizedConfig> {
|
export function buildConfigWithDefaults(testConfig?: Partial<Config>): Promise<SanitizedConfig> {
|
||||||
const [name] = process.argv.slice(2);
|
const [name] = process.argv.slice(2);
|
||||||
|
|
||||||
const config: Config = {
|
const config: Config = {
|
||||||
telemetry: false,
|
telemetry: false,
|
||||||
rateLimit: {
|
rateLimit: {
|
||||||
@@ -10,6 +18,7 @@ export function buildConfigWithDefaults(testConfig?: Partial<Config>): Promise<S
|
|||||||
max: 9999999999,
|
max: 9999999999,
|
||||||
},
|
},
|
||||||
...testConfig,
|
...testConfig,
|
||||||
|
db: databaseAdapters[process.env.PAYLOAD_DATABASE || 'mongoose'],
|
||||||
};
|
};
|
||||||
|
|
||||||
config.admin = {
|
config.admin = {
|
||||||
|
|||||||
@@ -34,7 +34,6 @@ const expressApp = express();
|
|||||||
const startDev = async () => {
|
const startDev = async () => {
|
||||||
await payload.init({
|
await payload.init({
|
||||||
secret: uuid(),
|
secret: uuid(),
|
||||||
mongoURL: 'mongodb://127.0.0.1/payload',
|
|
||||||
express: expressApp,
|
express: expressApp,
|
||||||
email: {
|
email: {
|
||||||
logMockCredentials: true,
|
logMockCredentials: true,
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ import { defaultNumber, numberDoc } from './collections/Number';
|
|||||||
import { dateDoc } from './collections/Date';
|
import { dateDoc } from './collections/Date';
|
||||||
import type { RichTextField } from './payload-types';
|
import type { RichTextField } from './payload-types';
|
||||||
import type { PaginatedDocs } from '../../src/database/types';
|
import type { PaginatedDocs } from '../../src/database/types';
|
||||||
import type { MongooseAdapter } from '../../src/mongoose';
|
import type { MongooseAdapter } from '../../src/database/adapters/mongoose';
|
||||||
|
|
||||||
let client;
|
let client;
|
||||||
let graphQLClient: GraphQLClient;
|
let graphQLClient: GraphQLClient;
|
||||||
|
|||||||
Reference in New Issue
Block a user