From 8589fdefdad4856f4d642c2d9ed8d6ca815c81f6 Mon Sep 17 00:00:00 2001 From: James Date: Wed, 13 Jul 2022 13:17:00 -0700 Subject: [PATCH] feat: reorganizes mongo connection --- .vscode/launch.json | 44 +++-------------------------------------- src/index.ts | 9 +++++---- src/mongoose/connect.ts | 12 +++++------ test/e2e/buildConfig.ts | 1 + 4 files changed, 14 insertions(+), 52 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 8604344c7..688631f7c 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -29,55 +29,17 @@ "request": "launch", "name": "Launch Program", "env": { - "PAYLOAD_CONFIG_PATH": "demo/payload.config.ts", "BABEL_ENV": "development" }, - "program": "${workspaceFolder}/demo/index.js", + "program": "${workspaceFolder}/test/dev/index.js", "skipFiles": [ "/**" ], - "runtimeExecutable": "${workspaceFolder}/node_modules/.bin/babel-node", "runtimeArgs": [ "--nolazy" ], - }, - { - "type": "node", - "request": "launch", - "name": "Launch Program - Production", - "env": { - "PAYLOAD_CONFIG_PATH": "demo/payload.config.ts", - "NODE_ENV": "production", - "BABEL_ENV": "development" - }, - "program": "${workspaceFolder}/demo/index.js", - "skipFiles": [ - "/**" - ], - "runtimeExecutable": "${workspaceFolder}/node_modules/.bin/babel-node", - "outputCapture": "std", - "runtimeArgs": [ - "--nolazy" - ], - }, - { - "type": "chrome", - "request": "launch", - "name": "Launch Chrome against Localhost", - "url": "http://localhost:3000/admin", - "webRoot": "${workspaceFolder}" - }, - { - "type": "node", - "request": "launch", - "name": "Debug Payload Generate Types", - "program": "${workspaceFolder}/src/bin/generateTypes.ts", - "env": { - "PAYLOAD_CONFIG_PATH": "demo/payload.config.ts", - }, - "outFiles": [ - "${workspaceFolder}/dist/**/*.js", - "!**/node_modules/**" + "args": [ + "e2e/fields" ] }, ] diff --git a/src/index.ts b/src/index.ts index 50ece2f45..93ce32a9d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -165,6 +165,11 @@ export class Payload { this.config = loadConfig(this.logger); + // Connect to database + if (this.mongoURL) { + this.mongoMemoryServer = await connectMongoose(this.mongoURL, options.mongoOptions, this.logger); + } + // If not initializing locally, scaffold router if (!this.local) { this.router = express.Router(); @@ -222,10 +227,6 @@ export class Payload { this.authenticate = authenticate(this.config); } - // Connect to database - if (this.mongoURL) { - this.mongoMemoryServer = await connectMongoose(this.mongoURL, options.mongoOptions, this.logger); - } if (typeof options.onInit === 'function') await options.onInit(this); if (typeof this.config.onInit === 'function') await this.config.onInit(this); diff --git a/src/mongoose/connect.ts b/src/mongoose/connect.ts index 3a6ddb583..6220e00ab 100644 --- a/src/mongoose/connect.ts +++ b/src/mongoose/connect.ts @@ -35,15 +35,13 @@ const connectMongoose = async ( } try { - if (process.env.PAYLOAD_DROP_DATABASE === 'true') { - mongoose.connection.once('connected', () => { - logger.info('---- DROPPING DATABASE ----'); - mongoose.connection.dropDatabase(); - }); - } - await mongoose.connect(urlToConnect, connectionOptions); + if (process.env.PAYLOAD_DROP_DATABASE === 'true') { + logger.info('---- DROPPING DATABASE ----'); + await mongoose.connection.dropDatabase(); + logger.info('---- DROPPED DATABASE ----'); + } logger.info(successfulConnectionMessage); } catch (err) { diff --git a/test/e2e/buildConfig.ts b/test/e2e/buildConfig.ts index 6b30ba5b5..cece6dec5 100644 --- a/test/e2e/buildConfig.ts +++ b/test/e2e/buildConfig.ts @@ -14,5 +14,6 @@ export function buildConfig(overrides?: Partial): SanitizedConfig { }), }; } + return buildPayloadConfig(merge(baseConfig, overrides || {})); }