chore: revert #1794 for now until permanent init architecture is established
This commit is contained in:
@@ -25,7 +25,8 @@ payload.init({
|
||||
secret: 'PAYLOAD_SECRET_KEY',
|
||||
mongoURL: 'mongodb://localhost/payload',
|
||||
express: app,
|
||||
onInit: async () => {
|
||||
});
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
router.use(payload.authenticate); // highlight-line
|
||||
@@ -43,7 +44,5 @@ payload.init({
|
||||
app.listen(3000, async () => {
|
||||
payload.logger.info(`listening on ${3000}...`);
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
```
|
||||
|
||||
@@ -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) 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#init) docs.
|
||||
|
||||
To initialize Payload, update your `server.js` file to reflect the following code:
|
||||
|
||||
@@ -80,12 +80,11 @@ payload.init({
|
||||
secret: 'SECRET_KEY',
|
||||
mongoURL: 'mongodb://localhost/payload',
|
||||
express: app,
|
||||
onInit: () => {
|
||||
})
|
||||
|
||||
app.listen(3000, async () => {
|
||||
console.log('Express is now listening for incoming connections on port 3000.')
|
||||
});
|
||||
}
|
||||
})
|
||||
|
||||
```
|
||||
|
||||
|
||||
@@ -5,11 +5,11 @@ import Logger from '../utilities/logger';
|
||||
import loadConfig from '../config/load';
|
||||
import payload from '..';
|
||||
|
||||
export async function generateGraphQLSchema(): Promise<void> {
|
||||
export function generateGraphQLSchema(): void {
|
||||
const logger = Logger();
|
||||
const config = loadConfig();
|
||||
|
||||
await payload.init({
|
||||
payload.init({
|
||||
secret: '--unused--',
|
||||
mongoURL: false,
|
||||
local: true,
|
||||
|
||||
10
src/index.ts
10
src/index.ts
@@ -44,7 +44,7 @@ import { Result as ResetPasswordResult } from './auth/operations/resetPassword';
|
||||
import { Result as LoginResult } from './auth/operations/login';
|
||||
import { Options as FindGlobalOptions } from './globals/operations/local/findOne';
|
||||
import { Options as UpdateGlobalOptions } from './globals/operations/local/update';
|
||||
import { initPayload } from './init';
|
||||
import { initSync, initAsync } from './init';
|
||||
|
||||
require('isomorphic-fetch');
|
||||
|
||||
@@ -121,8 +121,12 @@ export class Payload {
|
||||
* @description Initializes Payload
|
||||
* @param options
|
||||
*/
|
||||
async init(options: InitOptions): Promise<void> {
|
||||
await initPayload(this, options);
|
||||
init(options: InitOptions): void {
|
||||
initSync(this, options);
|
||||
}
|
||||
|
||||
async initAsync(options: InitOptions): Promise<void> {
|
||||
await initAsync(this, options);
|
||||
}
|
||||
|
||||
getAdminURL = (): string => `${this.config.serverURL}${this.config.routes.admin}`;
|
||||
|
||||
37
src/init.ts
37
src/init.ts
@@ -33,15 +33,7 @@ import mountEndpoints from './express/mountEndpoints';
|
||||
import PreferencesModel from './preferences/model';
|
||||
import findConfig from './config/find';
|
||||
|
||||
export const initPayload = async (payload: Payload, options: InitOptions): Promise<void> => {
|
||||
payload.logger = Logger('payload', options.loggerOptions);
|
||||
payload.mongoURL = options.mongoURL;
|
||||
|
||||
if (payload.mongoURL) {
|
||||
mongoose.set('strictQuery', false);
|
||||
payload.mongoMemoryServer = await connectMongoose(payload.mongoURL, options.mongoOptions, payload.logger);
|
||||
}
|
||||
|
||||
export const init = (payload: Payload, options: InitOptions): void => {
|
||||
payload.logger.info('Starting Payload...');
|
||||
if (!options.secret) {
|
||||
throw new Error(
|
||||
@@ -153,7 +145,34 @@ export const initPayload = async (payload: Payload, options: InitOptions): Promi
|
||||
}
|
||||
|
||||
serverInitTelemetry(payload);
|
||||
};
|
||||
|
||||
export const initAsync = async (payload: Payload, options: InitOptions): Promise<void> => {
|
||||
payload.logger = Logger('payload', options.loggerOptions);
|
||||
payload.mongoURL = options.mongoURL;
|
||||
|
||||
if (payload.mongoURL) {
|
||||
mongoose.set('strictQuery', false);
|
||||
payload.mongoMemoryServer = await connectMongoose(payload.mongoURL, options.mongoOptions, payload.logger);
|
||||
}
|
||||
|
||||
init(payload, options);
|
||||
|
||||
if (typeof options.onInit === 'function') await options.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);
|
||||
};
|
||||
|
||||
@@ -4,8 +4,9 @@ import { v4 as uuid } from 'uuid';
|
||||
import payload from '../src';
|
||||
|
||||
const expressApp = express();
|
||||
|
||||
const init = async () => {
|
||||
await payload.init({
|
||||
await payload.initAsync({
|
||||
secret: uuid(),
|
||||
mongoURL: process.env.MONGO_URL || 'mongodb://localhost/payload',
|
||||
express: expressApp,
|
||||
|
||||
@@ -40,7 +40,7 @@ export async function initPayloadTest(options: Options): Promise<{ serverURL: st
|
||||
initOptions.express = express();
|
||||
}
|
||||
|
||||
await payload.init(initOptions);
|
||||
await payload.initAsync(initOptions);
|
||||
|
||||
if (initOptions.express) {
|
||||
initOptions.express.listen(port);
|
||||
|
||||
Reference in New Issue
Block a user