feat!: enforces payload.init is async

* Run connectMongoose before starting payload init

* - reverted changes
- added deprecated to init
- docs: changed all payload.init to payload.initAsync
- changed all internal init calls

* forgotten inits in docs

* reverted back - removed init and renamed initAsync to init
This commit is contained in:
Christian Schurr
2023-01-09 17:24:22 +01:00
committed by GitHub
parent 7583289d24
commit 82961767e3
7 changed files with 26 additions and 43 deletions

View File

@@ -25,24 +25,25 @@ payload.init({
secret: 'PAYLOAD_SECRET_KEY', secret: 'PAYLOAD_SECRET_KEY',
mongoURL: 'mongodb://localhost/payload', mongoURL: 'mongodb://localhost/payload',
express: app, express: app,
}); onInit: async () => {
const router = express.Router();
const router = express.Router(); router.use(payload.authenticate); // highlight-line
router.use(payload.authenticate); // highlight-line router.get('/', (req, res) => {
if (req.user) {
return res.send(`Authenticated successfully as ${req.user.email}.`);
}
router.get('/', (req, res) => { return res.send('Not authenticated');
if (req.user) { });
return res.send(`Authenticated successfully as ${req.user.email}.`);
}
return res.send('Not authenticated'); app.use('/some-route-here', router);
});
app.use('/some-route-here', router); app.listen(3000, async () => {
payload.logger.info(`listening on ${3000}...`);
app.listen(3000, async () => { });
payload.logger.info(`listening on ${3000}...`); },
}); });
``` ```

View File

@@ -66,7 +66,7 @@ app.listen(3000, async () => {
}); });
``` ```
This server doesn't do anything just yet. But, after you have this in place, we can initialize Payload via its `init()` method, which accepts a small set of arguments to tell it how to operate. For a full list of `init` arguments, please consult the [main configuration](/docs/configuration/overview#init) docs. This server doesn't do anything just yet. But, after you have this in place, we can initialize Payload via its `init()` method, which accepts a small set of arguments to tell it how to operate. For a full list of `init` arguments, please consult the [main configuration](/docs/configuration/overview) docs.
To initialize Payload, update your `server.js` file to reflect the following code: To initialize Payload, update your `server.js` file to reflect the following code:
@@ -80,12 +80,13 @@ payload.init({
secret: 'SECRET_KEY', secret: 'SECRET_KEY',
mongoURL: 'mongodb://localhost/payload', mongoURL: 'mongodb://localhost/payload',
express: app, express: app,
onInit: () => {
app.listen(3000, async () => {
console.log('Express is now listening for incoming connections on port 3000.')
});
}
}) })
app.listen(3000, async () => {
console.log('Express is now listening for incoming connections on port 3000.')
});
``` ```
Here is a list of all properties available to pass through `payload.init`: Here is a list of all properties available to pass through `payload.init`:

View File

@@ -5,11 +5,11 @@ import Logger from '../utilities/logger';
import loadConfig from '../config/load'; import loadConfig from '../config/load';
import payload from '..'; import payload from '..';
export function generateGraphQLSchema(): void { export async function generateGraphQLSchema(): Promise<void> {
const logger = Logger(); const logger = Logger();
const config = loadConfig(); const config = loadConfig();
payload.init({ await payload.init({
secret: '--unused--', secret: '--unused--',
mongoURL: false, mongoURL: false,
local: true, local: true,

View File

@@ -44,7 +44,7 @@ import { Result as ResetPasswordResult } from './auth/operations/resetPassword';
import { Result as LoginResult } from './auth/operations/login'; import { Result as LoginResult } from './auth/operations/login';
import { Options as FindGlobalOptions } from './globals/operations/local/findOne'; import { Options as FindGlobalOptions } from './globals/operations/local/findOne';
import { Options as UpdateGlobalOptions } from './globals/operations/local/update'; import { Options as UpdateGlobalOptions } from './globals/operations/local/update';
import { initSync, initAsync } from './init'; import { initAsync } from './init';
require('isomorphic-fetch'); require('isomorphic-fetch');
@@ -121,11 +121,7 @@ export class Payload {
* @description Initializes Payload * @description Initializes Payload
* @param options * @param options
*/ */
init(options: InitOptions): void { async init(options: InitOptions): Promise<void> {
initSync(this, options);
}
async initAsync(options: InitOptions): Promise<void> {
await initAsync(this, options); await initAsync(this, options);
} }

View File

@@ -161,18 +161,3 @@ export const initAsync = async (payload: Payload, options: InitOptions): Promise
if (typeof options.onInit === 'function') await options.onInit(payload); if (typeof options.onInit === 'function') await options.onInit(payload);
if (typeof payload.config.onInit === 'function') await payload.config.onInit(payload); if (typeof payload.config.onInit === 'function') await payload.config.onInit(payload);
}; };
export const initSync = (payload: Payload, options: InitOptions): void => {
payload.logger = Logger('payload', options.loggerOptions);
payload.mongoURL = options.mongoURL;
if (payload.mongoURL) {
mongoose.set('strictQuery', false);
connectMongoose(payload.mongoURL, options.mongoOptions, payload.logger);
}
init(payload, options);
if (typeof options.onInit === 'function') options.onInit(payload);
if (typeof payload.config.onInit === 'function') payload.config.onInit(payload);
};

View File

@@ -5,7 +5,7 @@ import payload from '../src';
const expressApp = express(); const expressApp = express();
const init = async () => { const init = async () => {
await payload.initAsync({ await payload.init({
secret: uuid(), secret: uuid(),
mongoURL: process.env.MONGO_URL || 'mongodb://localhost/payload', mongoURL: process.env.MONGO_URL || 'mongodb://localhost/payload',
express: expressApp, express: expressApp,

View File

@@ -40,7 +40,7 @@ export async function initPayloadTest(options: Options): Promise<{ serverURL: st
initOptions.express = express(); initOptions.express = express();
} }
await payload.initAsync(initOptions); await payload.init(initOptions);
if (initOptions.express) { if (initOptions.express) {
initOptions.express.listen(port); initOptions.express.listen(port);