feat: reorganizes mongo connection

This commit is contained in:
James
2022-07-13 13:17:00 -07:00
parent 8ba25e8602
commit 8589fdefda
4 changed files with 14 additions and 52 deletions

44
.vscode/launch.json vendored
View File

@@ -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": [
"<node_internals>/**"
],
"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": [
"<node_internals>/**"
],
"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"
]
},
]

View File

@@ -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);

View File

@@ -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) {

View File

@@ -14,5 +14,6 @@ export function buildConfig(overrides?: Partial<Config>): SanitizedConfig {
}),
};
}
return buildPayloadConfig(merge(baseConfig, overrides || {}));
}