nocobase/packages/server/src/application.ts

278 lines
6.5 KiB
TypeScript
Raw Normal View History

import Koa from 'koa';
import cors from '@koa/cors';
import bodyParser from 'koa-bodyparser';
2021-09-14 03:09:26 +00:00
import { Command, CommandOptions } from 'commander';
import Database, { DatabaseOptions, TableOptions } from '@nocobase/database';
import Resourcer, { ResourceOptions } from '@nocobase/resourcer';
2021-09-09 05:05:33 +00:00
import { dataWrapping, table2resource } from './middlewares';
2021-09-14 03:09:26 +00:00
import { PluginType, Plugin, PluginOptions } from './plugin';
2021-09-15 16:38:48 +00:00
import { registerActions } from '@nocobase/actions';
2021-09-09 05:05:33 +00:00
export interface ResourcerOptions {
prefix?: string;
}
export interface ApplicationOptions {
2021-09-09 05:05:33 +00:00
database?: DatabaseOptions;
resourcer?: ResourcerOptions;
bodyParser?: any;
cors?: any;
dataWrapping?: boolean;
}
2021-09-16 07:57:21 +00:00
interface DefaultState {
currentUser?: any;
[key: string]: any;
}
interface DefaultContext {
db: Database;
resourcer: Resourcer;
[key: string]: any;
}
interface MiddlewareOptions {
name?: string;
resourceName?: string;
resourceNames?: string[];
insertBefore?: string;
insertAfter?: string;
}
interface ActionsOptions {
resourceName?: string;
resourceNames?: string[];
}
export class Application<
StateT = DefaultState,
ContextT = DefaultContext
> extends Koa {
2021-09-15 16:38:48 +00:00
2021-09-14 03:09:26 +00:00
public readonly db: Database;
public readonly resourcer: Resourcer;
public readonly cli: Command;
2021-09-14 03:09:26 +00:00
protected plugins = new Map<string, Plugin>();
constructor(options: ApplicationOptions) {
super();
2021-09-09 05:05:33 +00:00
2021-09-15 16:38:48 +00:00
if (options.database instanceof Database) {
this.db = options.database;
} else {
this.db = new Database(options.database);
}
2021-09-09 05:05:33 +00:00
this.resourcer = new Resourcer({ ...options.resourcer });
this.cli = new Command();
2021-09-09 05:05:33 +00:00
this.use(
bodyParser({
...options.bodyParser,
}),
);
2021-09-09 05:05:33 +00:00
this.use(
cors({
exposeHeaders: ['content-disposition'],
...options.cors,
}),
);
2021-09-16 07:57:21 +00:00
this.use<DefaultState, DefaultContext>(async (ctx, next) => {
2021-09-14 03:09:26 +00:00
ctx.db = this.db;
2021-09-09 05:05:33 +00:00
ctx.resourcer = this.resourcer;
await next();
});
2021-09-09 05:05:33 +00:00
if (options.dataWrapping !== false) {
2021-09-15 16:38:48 +00:00
this.use(dataWrapping());
2021-09-09 05:05:33 +00:00
}
2021-09-15 16:38:48 +00:00
this.use(table2resource());
2021-09-09 05:05:33 +00:00
this.use(this.resourcer.restApiMiddleware());
2021-09-15 16:38:48 +00:00
registerActions(this);
this.cli
2021-09-16 07:57:21 +00:00
.command('db:sync')
.option('-f, --force')
.action(async (...args) => {
2021-09-15 16:38:48 +00:00
console.log('db sync...');
const cli = args.pop();
2021-09-14 03:09:26 +00:00
const force = cli.opts()?.force;
await this.load();
await this.db.sync(
force
? {
2021-09-15 16:38:48 +00:00
force: true,
alter: {
drop: true,
},
}
2021-09-14 03:09:26 +00:00
: {},
);
await this.destroy();
});
this.cli
2021-09-16 07:57:21 +00:00
.command('init')
// .option('-f, --force')
.action(async (...args) => {
const cli = args.pop();
2021-09-14 03:09:26 +00:00
await this.load();
await this.db.sync({
force: true,
alter: {
drop: true,
},
});
await this.emitAsync('db.init');
2021-09-09 05:05:33 +00:00
await this.destroy();
});
this.cli
.command('start')
.option('-p, --port [port]')
.action(async (...args) => {
const cli = args.pop();
console.log(args);
const opts = cli.opts();
2021-09-14 03:09:26 +00:00
await this.load();
await this.emitAsync('server.beforeStart');
this.listen(opts.port || 3000);
console.log(`http://localhost:${opts.port || 3000}/`);
});
2020-12-18 11:54:53 +00:00
}
2021-09-16 07:57:21 +00:00
// @ts-ignore
use<NewStateT = {}, NewContextT = {}>(
middleware: Koa.Middleware<StateT & NewStateT, ContextT & NewContextT>,
options?: MiddlewareOptions,
): Application<StateT & NewStateT, ContextT & NewContextT> {
// @ts-ignore
return super.use(middleware);
}
2021-09-14 03:09:26 +00:00
collection(options: TableOptions) {
return this.db.table(options);
}
resource(options: ResourceOptions) {
return this.resourcer.define(options);
}
2021-09-16 07:57:21 +00:00
actions(handlers: any, options?: ActionsOptions) {
2021-09-15 16:38:48 +00:00
return this.resourcer.registerActions(handlers);
}
2021-09-14 03:09:26 +00:00
command(nameAndArgs: string, opts?: CommandOptions) {
return this.cli.command(nameAndArgs, opts);
}
plugin(plugin: PluginType, options?: PluginOptions): Plugin {
if (typeof plugin === 'string') {
plugin = require(plugin).default;
}
let instance: Plugin;
try {
// @ts-ignore
const p = new plugin();
if (p instanceof Plugin) {
// @ts-ignore
instance = new plugin({}, {
...options,
app: this,
});
} else {
throw new Error('plugin must be instanceof Plugin');
}
} catch (err) {
instance = new Plugin(plugin, {
...options,
app: this,
});
}
const name = instance.getName();
if (this.plugins.has(name)) {
throw new Error(`plugin name [${name}] is repeated`);
}
this.plugins.set(name, instance);
return instance;
}
async load() {
await this.emitAsync('plugins.beforeLoad');
for (const [name, plugin] of this.plugins) {
await this.emitAsync(`plugins.${name}.beforeLoad`);
await plugin.load();
await this.emitAsync(`plugins.${name}.afterLoad`);
}
await this.emitAsync('plugins.afterLoad');
}
2021-09-05 06:17:45 +00:00
async emitAsync(event: string | symbol, ...args: any[]): Promise<boolean> {
// @ts-ignore
const events = this._events;
let callbacks = events[event];
if (!callbacks) {
return false;
}
// helper function to reuse as much code as possible
const run = (cb) => {
switch (args.length) {
// fast cases
case 0:
cb = cb.call(this);
break;
case 1:
cb = cb.call(this, args[0]);
break;
case 2:
cb = cb.call(this, args[0], args[1]);
break;
case 3:
cb = cb.call(this, args[0], args[1], args[2]);
break;
// slower
default:
cb = cb.apply(this, args);
}
2021-09-09 05:05:33 +00:00
if (cb && (cb instanceof Promise || typeof cb.then === 'function')) {
2021-09-05 06:17:45 +00:00
return cb;
}
return Promise.resolve(true);
};
if (typeof callbacks === 'function') {
await run(callbacks);
} else if (typeof callbacks === 'object') {
callbacks = callbacks.slice().filter(Boolean);
await callbacks.reduce((prev, next) => {
return prev.then((res) => {
2021-09-09 05:05:33 +00:00
return run(next).then((result) =>
Promise.resolve(res.concat(result)),
);
2021-09-05 06:17:45 +00:00
});
}, Promise.resolve([]));
}
return true;
}
2021-09-15 16:38:48 +00:00
async parse(argv = process.argv) {
return this.cli.parseAsync(argv);
2020-12-18 11:54:53 +00:00
}
2021-09-09 05:05:33 +00:00
async destroy() {
2021-09-14 03:09:26 +00:00
await this.db.close();
2021-09-09 05:05:33 +00:00
}
}
export default Application;