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. */
|
||||
custom?: Record<string, any>;
|
||||
/** Pass in a database adapter for use on this project. */
|
||||
db?: DatabaseAdapter
|
||||
db: (args: { payload: Payload }) => DatabaseAdapter
|
||||
};
|
||||
|
||||
export type SanitizedConfig = Omit<
|
||||
DeepRequired<Config>,
|
||||
'collections' | 'globals' | 'endpoint' | 'db'
|
||||
'collections' | 'globals' | 'endpoint'
|
||||
> & {
|
||||
collections: SanitizedCollectionConfig[];
|
||||
globals: SanitizedGlobalConfig[];
|
||||
endpoints: Endpoint[];
|
||||
db: DatabaseAdapter
|
||||
paths: {
|
||||
configDir: string
|
||||
config: string
|
||||
|
||||
@@ -12,6 +12,11 @@ export const connect: Connect = async function connect(
|
||||
if (this.url === false) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!payload.local && typeof this.url !== 'string') {
|
||||
throw new Error('Error: missing MongoDB connection URL.');
|
||||
}
|
||||
|
||||
let urlToConnect = this.url;
|
||||
let successfulConnectionMessage = 'Connected to MongoDB server successfully!';
|
||||
|
||||
|
||||
@@ -24,12 +24,11 @@ import { updateGlobal } from './updateGlobal';
|
||||
import { updateOne } from './updateOne';
|
||||
import { updateVersion } from './updateVersion';
|
||||
import { deleteMany } from './deleteMany';
|
||||
import { baseDatabaseAdapter } from '../../baseDatabaseAdapter';
|
||||
import { withBaseDatabaseAdapter } from '../../baseDatabaseAdapter';
|
||||
import { destroy } from './destroy';
|
||||
import type { CollectionModel, GlobalModel } from './types';
|
||||
|
||||
export interface Args {
|
||||
payload: Payload;
|
||||
/** The URL to connect to MongoDB or false to start payload and prevent connecting */
|
||||
url: string | false;
|
||||
migrationDir?: string;
|
||||
@@ -54,50 +53,52 @@ export type MongooseAdapter = DatabaseAdapter &
|
||||
connection: Connection
|
||||
}
|
||||
|
||||
type MongooseAdapterResult = (args: { payload: Payload }) => MongooseAdapter
|
||||
|
||||
export function mongooseAdapter({
|
||||
payload,
|
||||
url,
|
||||
connectOptions,
|
||||
migrationDir,
|
||||
}: Args): MongooseAdapter {
|
||||
const adapter = baseDatabaseAdapter({
|
||||
payload,
|
||||
migrationDir,
|
||||
});
|
||||
mongoose.set('strictQuery', false);
|
||||
return {
|
||||
...adapter,
|
||||
connection: undefined,
|
||||
mongoMemoryServer: undefined,
|
||||
sessions: {},
|
||||
payload,
|
||||
url,
|
||||
connectOptions: connectOptions || {},
|
||||
globals: undefined,
|
||||
collections: {},
|
||||
versions: {},
|
||||
connect,
|
||||
destroy,
|
||||
init,
|
||||
webpack,
|
||||
createMigration: async (migrationName) => createMigration({ payload, migrationDir, migrationName }),
|
||||
beginTransaction,
|
||||
rollbackTransaction,
|
||||
commitTransaction,
|
||||
queryDrafts,
|
||||
findOne,
|
||||
find,
|
||||
create,
|
||||
updateOne,
|
||||
deleteOne,
|
||||
deleteMany,
|
||||
findGlobal,
|
||||
createGlobal,
|
||||
updateGlobal,
|
||||
findVersions,
|
||||
findGlobalVersions,
|
||||
createVersion,
|
||||
updateVersion,
|
||||
deleteVersions,
|
||||
};
|
||||
}: Args): MongooseAdapterResult {
|
||||
function adapter({ payload }: { payload: Payload }) {
|
||||
mongoose.set('strictQuery', false);
|
||||
|
||||
return withBaseDatabaseAdapter<MongooseAdapter>({
|
||||
payload,
|
||||
migrationDir,
|
||||
connection: undefined,
|
||||
mongoMemoryServer: undefined,
|
||||
sessions: {},
|
||||
url,
|
||||
connectOptions: connectOptions || {},
|
||||
globals: undefined,
|
||||
collections: {},
|
||||
versions: {},
|
||||
connect,
|
||||
destroy,
|
||||
init,
|
||||
webpack,
|
||||
createMigration: async (migrationName) => createMigration({ payload, migrationDir, migrationName }),
|
||||
beginTransaction,
|
||||
rollbackTransaction,
|
||||
commitTransaction,
|
||||
queryDrafts,
|
||||
findOne,
|
||||
find,
|
||||
create,
|
||||
updateOne,
|
||||
deleteOne,
|
||||
deleteMany,
|
||||
findGlobal,
|
||||
createGlobal,
|
||||
updateGlobal,
|
||||
findVersions,
|
||||
findGlobalVersions,
|
||||
createVersion,
|
||||
updateVersion,
|
||||
deleteVersions,
|
||||
});
|
||||
}
|
||||
|
||||
return adapter;
|
||||
}
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
exports.mongooseAdapter = () => ({});
|
||||
|
||||
@@ -1,13 +1,15 @@
|
||||
import path from 'path';
|
||||
import type { Webpack } from '../../types';
|
||||
|
||||
export const webpack: Webpack = (config) => ({
|
||||
...config,
|
||||
resolve: {
|
||||
...config.resolve || {},
|
||||
alias: {
|
||||
...config.resolve?.alias || {},
|
||||
[path.resolve(__filename)]: path.resolve(__dirname, 'mock.js'),
|
||||
export const webpack: Webpack = (config) => {
|
||||
return {
|
||||
...config,
|
||||
resolve: {
|
||||
...config.resolve || {},
|
||||
alias: {
|
||||
...config.resolve?.alias || {},
|
||||
[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 { migrate } from './migrations/migrate';
|
||||
import { migrateStatus } from './migrations/migrateStatus';
|
||||
@@ -8,8 +10,7 @@ import { DatabaseAdapter } from './types';
|
||||
import type { Payload } from '../index';
|
||||
import { createMigration } from './migrations/createMigration';
|
||||
|
||||
|
||||
type BaseDatabaseAdapter = Pick<DatabaseAdapter, 'payload'
|
||||
type BaseDatabaseAdapter = Omit<DatabaseAdapter,
|
||||
| 'transaction'
|
||||
| 'migrate'
|
||||
| 'createMigration'
|
||||
@@ -19,18 +20,24 @@ type BaseDatabaseAdapter = Pick<DatabaseAdapter, 'payload'
|
||||
| 'migrateReset'
|
||||
| 'migrateFresh'
|
||||
| 'migrationDir'
|
||||
>
|
||||
>
|
||||
|
||||
type Args = {
|
||||
payload: Payload,
|
||||
migrationDir?: string,
|
||||
}
|
||||
export function baseDatabaseAdapter({
|
||||
payload,
|
||||
migrationDir = '.migrations',
|
||||
}: Args): BaseDatabaseAdapter {
|
||||
export function withBaseDatabaseAdapter<T extends BaseDatabaseAdapter>(args: T): T {
|
||||
// Need to implement DB Webpack config extensions here
|
||||
if (args.webpack) {
|
||||
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 {
|
||||
payload,
|
||||
transaction,
|
||||
migrate,
|
||||
createMigration,
|
||||
@@ -39,6 +46,6 @@ export function baseDatabaseAdapter({
|
||||
migrateRefresh,
|
||||
migrateReset,
|
||||
migrateFresh: async () => null,
|
||||
migrationDir,
|
||||
...args,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -58,7 +58,6 @@ import findConfig from './config/find';
|
||||
|
||||
import { defaults as emailDefaults } from './email/defaults';
|
||||
import type { DatabaseAdapter } from '.';
|
||||
import { mongooseAdapter } from './database/adapters/mongoose';
|
||||
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
|
||||
.createHash('sha256')
|
||||
.update(options.secret)
|
||||
@@ -198,21 +193,10 @@ export class BasePayload<TGeneratedTypes extends GeneratedTypes> {
|
||||
};
|
||||
});
|
||||
|
||||
// THIS BLOCK IS TEMPORARY UNTIL 2.0.0
|
||||
// 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 = this.config.db({ payload: this });
|
||||
this.db.payload = this;
|
||||
|
||||
if (this.db?.connect) {
|
||||
if (this.db.connect) {
|
||||
await this.db.connect(this);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,16 @@
|
||||
import { Config, SanitizedConfig } from '../src/config/types';
|
||||
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> {
|
||||
const [name] = process.argv.slice(2);
|
||||
|
||||
const config: Config = {
|
||||
telemetry: false,
|
||||
rateLimit: {
|
||||
@@ -10,6 +18,7 @@ export function buildConfigWithDefaults(testConfig?: Partial<Config>): Promise<S
|
||||
max: 9999999999,
|
||||
},
|
||||
...testConfig,
|
||||
db: databaseAdapters[process.env.PAYLOAD_DATABASE || 'mongoose'],
|
||||
};
|
||||
|
||||
config.admin = {
|
||||
|
||||
@@ -34,7 +34,6 @@ const expressApp = express();
|
||||
const startDev = async () => {
|
||||
await payload.init({
|
||||
secret: uuid(),
|
||||
mongoURL: 'mongodb://127.0.0.1/payload',
|
||||
express: expressApp,
|
||||
email: {
|
||||
logMockCredentials: true,
|
||||
|
||||
@@ -14,7 +14,7 @@ import { defaultNumber, numberDoc } from './collections/Number';
|
||||
import { dateDoc } from './collections/Date';
|
||||
import type { RichTextField } from './payload-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 graphQLClient: GraphQLClient;
|
||||
|
||||
Reference in New Issue
Block a user