2022-02-11 15:59:03 +00:00
|
|
|
import { ACL } from '@nocobase/acl';
|
2021-09-15 16:38:48 +00:00
|
|
|
import { registerActions } from '@nocobase/actions';
|
2022-06-17 02:25:59 +00:00
|
|
|
import Database, { Collection, CollectionOptions, IDatabaseOptions } from '@nocobase/database';
|
2022-01-30 03:11:36 +00:00
|
|
|
import Resourcer, { ResourceOptions } from '@nocobase/resourcer';
|
|
|
|
import { applyMixins, AsyncEmitter } from '@nocobase/utils';
|
|
|
|
import { Command, CommandOptions } from 'commander';
|
|
|
|
import { Server } from 'http';
|
2021-11-08 11:32:59 +00:00
|
|
|
import { i18n, InitOptions } from 'i18next';
|
2022-01-30 03:11:36 +00:00
|
|
|
import Koa from 'koa';
|
|
|
|
import { isBoolean } from 'lodash';
|
2022-06-17 02:25:59 +00:00
|
|
|
import semver from 'semver';
|
2022-02-11 15:59:03 +00:00
|
|
|
import { createACL } from './acl';
|
2022-04-24 04:38:54 +00:00
|
|
|
import { AppManager } from './app-manager';
|
2022-05-18 16:40:55 +00:00
|
|
|
import { registerCli } from './commands';
|
2022-06-17 02:25:59 +00:00
|
|
|
import { createI18n, createResourcer, registerMiddlewares } from './helper';
|
2022-01-30 03:11:36 +00:00
|
|
|
import { Plugin } from './plugin';
|
2022-04-24 04:38:54 +00:00
|
|
|
import { InstallOptions, PluginManager } from './plugin-manager';
|
2021-09-09 05:05:33 +00:00
|
|
|
|
2022-05-18 16:40:55 +00:00
|
|
|
const packageJson = require('../package.json');
|
|
|
|
|
2022-04-17 02:00:42 +00:00
|
|
|
export type PluginConfiguration = string | [string, any];
|
|
|
|
export type PluginsConfigurations = Array<PluginConfiguration>;
|
|
|
|
|
2021-09-09 05:05:33 +00:00
|
|
|
export interface ResourcerOptions {
|
|
|
|
prefix?: string;
|
|
|
|
}
|
2020-11-12 03:48:27 +00:00
|
|
|
|
|
|
|
export interface ApplicationOptions {
|
2022-02-14 16:20:25 +00:00
|
|
|
database?: IDatabaseOptions | Database;
|
2021-09-09 05:05:33 +00:00
|
|
|
resourcer?: ResourcerOptions;
|
|
|
|
bodyParser?: any;
|
|
|
|
cors?: any;
|
|
|
|
dataWrapping?: boolean;
|
2021-10-18 04:49:37 +00:00
|
|
|
registerActions?: boolean;
|
2021-11-08 11:32:59 +00:00
|
|
|
i18n?: i18n | InitOptions;
|
2022-04-17 02:00:42 +00:00
|
|
|
plugins?: PluginsConfigurations;
|
2020-11-12 03:48:27 +00:00
|
|
|
}
|
2020-11-11 10:16:18 +00:00
|
|
|
|
2022-05-18 16:40:55 +00:00
|
|
|
export interface DefaultState {
|
2021-09-16 07:57:21 +00:00
|
|
|
currentUser?: any;
|
|
|
|
[key: string]: any;
|
|
|
|
}
|
|
|
|
|
2022-05-18 16:40:55 +00:00
|
|
|
export interface DefaultContext {
|
2021-09-16 07:57:21 +00:00
|
|
|
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[];
|
|
|
|
}
|
|
|
|
|
2022-01-30 03:11:36 +00:00
|
|
|
interface ListenOptions {
|
|
|
|
port?: number | undefined;
|
|
|
|
host?: string | undefined;
|
|
|
|
backlog?: number | undefined;
|
|
|
|
path?: string | undefined;
|
|
|
|
exclusive?: boolean | undefined;
|
|
|
|
readableAll?: boolean | undefined;
|
|
|
|
writableAll?: boolean | undefined;
|
|
|
|
/**
|
|
|
|
* @default false
|
|
|
|
*/
|
|
|
|
ipv6Only?: boolean | undefined;
|
|
|
|
signal?: AbortSignal | undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface StartOptions {
|
2022-02-06 17:14:00 +00:00
|
|
|
cliArgs?: any[];
|
2022-01-30 03:11:36 +00:00
|
|
|
listen?: ListenOptions;
|
|
|
|
}
|
|
|
|
|
2022-06-17 02:25:59 +00:00
|
|
|
export class ApplicationVersion {
|
|
|
|
protected app: Application;
|
|
|
|
protected collection: Collection;
|
|
|
|
|
|
|
|
constructor(app: Application) {
|
|
|
|
this.app = app;
|
|
|
|
if (!app.db.hasCollection('applicationVersion')) {
|
|
|
|
app.db.collection({
|
|
|
|
name: 'applicationVersion',
|
|
|
|
timestamps: false,
|
|
|
|
fields: [{ name: 'value', type: 'string' }],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
this.collection = this.app.db.getCollection('applicationVersion');
|
|
|
|
}
|
|
|
|
|
|
|
|
async get() {
|
2022-06-17 16:18:12 +00:00
|
|
|
if (await this.app.db.collectionExistsInDb('applicationVersion')) {
|
2022-06-17 02:25:59 +00:00
|
|
|
const model = await this.collection.model.findOne();
|
|
|
|
return model.get('value') as any;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
async update() {
|
|
|
|
await this.collection.sync();
|
|
|
|
await this.collection.model.destroy({
|
|
|
|
truncate: true,
|
|
|
|
});
|
|
|
|
await this.collection.model.create({
|
|
|
|
value: this.app.getVersion(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async satisfies(range: string) {
|
2022-06-17 16:18:12 +00:00
|
|
|
if (await this.app.db.collectionExistsInDb('applicationVersion')) {
|
2022-06-17 02:25:59 +00:00
|
|
|
const model = await this.collection.model.findOne();
|
|
|
|
const version = model.get('value') as any;
|
|
|
|
return semver.satisfies(version, range);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-30 03:11:36 +00:00
|
|
|
export class Application<StateT = DefaultState, ContextT = DefaultContext> extends Koa implements AsyncEmitter {
|
2021-09-14 03:09:26 +00:00
|
|
|
public readonly db: Database;
|
2021-01-13 08:23:15 +00:00
|
|
|
|
|
|
|
public readonly resourcer: Resourcer;
|
2020-11-11 10:16:18 +00:00
|
|
|
|
2021-09-05 15:59:38 +00:00
|
|
|
public readonly cli: Command;
|
|
|
|
|
2021-11-08 11:32:59 +00:00
|
|
|
public readonly i18n: i18n;
|
|
|
|
|
2022-01-30 03:11:36 +00:00
|
|
|
public readonly pm: PluginManager;
|
|
|
|
|
2022-02-11 15:59:03 +00:00
|
|
|
public readonly acl: ACL;
|
|
|
|
|
2022-03-28 14:01:10 +00:00
|
|
|
public readonly appManager: AppManager;
|
|
|
|
|
2022-06-17 02:25:59 +00:00
|
|
|
public readonly version: ApplicationVersion;
|
|
|
|
|
2021-09-14 03:09:26 +00:00
|
|
|
protected plugins = new Map<string, Plugin>();
|
2020-11-12 03:48:27 +00:00
|
|
|
|
2022-01-30 03:11:36 +00:00
|
|
|
public listenServer: Server;
|
|
|
|
|
2022-03-28 14:01:10 +00:00
|
|
|
constructor(public options: ApplicationOptions) {
|
2021-01-13 08:23:15 +00:00
|
|
|
super();
|
2021-09-09 05:05:33 +00:00
|
|
|
|
2022-02-11 15:59:03 +00:00
|
|
|
this.acl = createACL();
|
2022-06-17 02:25:59 +00:00
|
|
|
this.db = this.createDatabase(options);
|
2021-10-18 04:49:37 +00:00
|
|
|
this.resourcer = createResourcer(options);
|
2022-05-18 16:40:55 +00:00
|
|
|
this.cli = new Command('nocobase').usage('[command] [options]');
|
2021-11-08 11:32:59 +00:00
|
|
|
this.i18n = createI18n(options);
|
2021-09-05 15:59:38 +00:00
|
|
|
|
2022-01-30 03:11:36 +00:00
|
|
|
this.pm = new PluginManager({
|
|
|
|
app: this,
|
|
|
|
});
|
|
|
|
|
2022-03-28 14:01:10 +00:00
|
|
|
this.appManager = new AppManager(this);
|
|
|
|
|
2021-10-18 04:49:37 +00:00
|
|
|
registerMiddlewares(this, options);
|
2022-03-11 02:10:57 +00:00
|
|
|
|
2021-10-18 04:49:37 +00:00
|
|
|
if (options.registerActions !== false) {
|
|
|
|
registerActions(this);
|
2021-09-09 05:05:33 +00:00
|
|
|
}
|
2022-04-17 02:00:42 +00:00
|
|
|
|
|
|
|
this.loadPluginConfig(options.plugins || []);
|
2022-05-18 16:40:55 +00:00
|
|
|
|
|
|
|
registerCli(this);
|
2022-06-17 02:25:59 +00:00
|
|
|
|
|
|
|
this.version = new ApplicationVersion(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
private createDatabase(options: ApplicationOptions) {
|
|
|
|
if (options.database instanceof Database) {
|
|
|
|
return options.database;
|
|
|
|
} else {
|
|
|
|
return new Database({
|
|
|
|
...options.database,
|
|
|
|
migrator: {
|
|
|
|
context: { app: this },
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2022-05-18 16:40:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getVersion() {
|
|
|
|
return packageJson.version;
|
2020-12-18 11:54:53 +00:00
|
|
|
}
|
|
|
|
|
2022-01-30 03:11:36 +00:00
|
|
|
plugin<O = any>(pluginClass: any, options?: O): Plugin<O> {
|
|
|
|
return this.pm.add(pluginClass, options);
|
|
|
|
}
|
|
|
|
|
2022-04-17 02:00:42 +00:00
|
|
|
loadPluginConfig(pluginsConfigurations: PluginsConfigurations) {
|
|
|
|
for (let pluginConfiguration of pluginsConfigurations) {
|
|
|
|
if (typeof pluginConfiguration == 'string') {
|
|
|
|
pluginConfiguration = [pluginConfiguration, {}];
|
|
|
|
}
|
|
|
|
|
|
|
|
const plugin = PluginManager.resolvePlugin(pluginConfiguration[0]);
|
|
|
|
const pluginOptions = pluginConfiguration[1];
|
|
|
|
|
|
|
|
this.plugin(plugin, pluginOptions);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-16 07:57:21 +00:00
|
|
|
use<NewStateT = {}, NewContextT = {}>(
|
|
|
|
middleware: Koa.Middleware<StateT & NewStateT, ContextT & NewContextT>,
|
|
|
|
options?: MiddlewareOptions,
|
2021-09-22 16:16:04 +00:00
|
|
|
) {
|
2021-09-16 07:57:21 +00:00
|
|
|
// @ts-ignore
|
|
|
|
return super.use(middleware);
|
|
|
|
}
|
|
|
|
|
2021-12-06 13:23:34 +00:00
|
|
|
collection(options: CollectionOptions) {
|
|
|
|
return this.db.collection(options);
|
2021-09-14 03:09:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-02-07 13:54:07 +00:00
|
|
|
command(name: string, desc?: string, opts?: CommandOptions): Command {
|
2022-05-18 16:40:55 +00:00
|
|
|
return this.cli.command(name, desc, opts).allowUnknownOption();
|
2021-09-14 03:09:26 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 04:49:37 +00:00
|
|
|
findCommand(name: string): Command {
|
2022-02-10 10:38:05 +00:00
|
|
|
return (<any>this.cli)._findCommand(name);
|
2021-10-18 04:49:37 +00:00
|
|
|
}
|
|
|
|
|
2021-09-14 03:09:26 +00:00
|
|
|
async load() {
|
2022-01-30 03:11:36 +00:00
|
|
|
await this.pm.load();
|
2021-09-14 03:09:26 +00:00
|
|
|
}
|
|
|
|
|
2022-01-24 06:10:35 +00:00
|
|
|
getPlugin<P extends Plugin>(name: string) {
|
2022-01-30 03:11:36 +00:00
|
|
|
return this.pm.get(name) as P;
|
2022-01-24 06:10:35 +00:00
|
|
|
}
|
|
|
|
|
2022-01-30 03:11:36 +00:00
|
|
|
async parse(argv = process.argv) {
|
|
|
|
await this.load();
|
|
|
|
return this.cli.parseAsync(argv);
|
|
|
|
}
|
|
|
|
|
2022-05-13 03:18:36 +00:00
|
|
|
async start(options: StartOptions = {}) {
|
2022-01-30 03:11:36 +00:00
|
|
|
// reconnect database
|
|
|
|
if (this.db.closed()) {
|
|
|
|
await this.db.reconnect();
|
2021-12-06 13:23:34 +00:00
|
|
|
}
|
2022-01-30 03:11:36 +00:00
|
|
|
|
|
|
|
await this.emitAsync('beforeStart', this, options);
|
|
|
|
|
|
|
|
if (options?.listen?.port) {
|
|
|
|
const listen = () =>
|
2022-05-13 03:20:30 +00:00
|
|
|
new Promise((resolve, reject) => {
|
2022-01-30 03:11:36 +00:00
|
|
|
const Server = this.listen(options?.listen, () => {
|
|
|
|
resolve(Server);
|
|
|
|
});
|
2022-05-13 03:20:30 +00:00
|
|
|
|
|
|
|
Server.on('error', (err) => {
|
|
|
|
reject(err);
|
|
|
|
});
|
2021-12-06 13:23:34 +00:00
|
|
|
});
|
2022-01-30 03:11:36 +00:00
|
|
|
|
2022-05-13 03:20:30 +00:00
|
|
|
try {
|
|
|
|
//@ts-ignore
|
|
|
|
this.listenServer = await listen();
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
2021-12-06 13:23:34 +00:00
|
|
|
}
|
|
|
|
|
2022-01-30 03:11:36 +00:00
|
|
|
await this.emitAsync('afterStart', this, options);
|
2021-12-06 13:23:34 +00:00
|
|
|
}
|
|
|
|
|
2022-03-28 14:01:10 +00:00
|
|
|
listen(...args): Server {
|
|
|
|
return this.appManager.listen(...args);
|
|
|
|
}
|
|
|
|
|
2022-05-13 03:18:36 +00:00
|
|
|
async stop(options: any = {}) {
|
2022-02-06 17:14:00 +00:00
|
|
|
await this.emitAsync('beforeStop', this, options);
|
2022-01-30 03:11:36 +00:00
|
|
|
|
2022-03-28 14:01:10 +00:00
|
|
|
try {
|
|
|
|
// close database connection
|
|
|
|
// silent if database already closed
|
|
|
|
await this.db.close();
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
}
|
2022-01-30 03:11:36 +00:00
|
|
|
|
|
|
|
// close http server
|
|
|
|
if (this.listenServer) {
|
|
|
|
const closeServer = () =>
|
|
|
|
new Promise((resolve, reject) => {
|
|
|
|
this.listenServer.close((err) => {
|
|
|
|
if (err) {
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
this.listenServer = null;
|
|
|
|
resolve(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
await closeServer();
|
|
|
|
}
|
|
|
|
|
2022-02-06 17:14:00 +00:00
|
|
|
await this.emitAsync('afterStop', this, options);
|
2020-12-18 11:54:53 +00:00
|
|
|
}
|
|
|
|
|
2022-05-13 03:18:36 +00:00
|
|
|
async destroy(options: any = {}) {
|
2022-02-06 17:14:00 +00:00
|
|
|
await this.emitAsync('beforeDestroy', this, options);
|
|
|
|
await this.stop(options);
|
|
|
|
await this.emitAsync('afterDestroy', this, options);
|
2022-01-30 03:11:36 +00:00
|
|
|
}
|
|
|
|
|
2022-05-13 03:18:36 +00:00
|
|
|
async install(options: InstallOptions = {}) {
|
2022-02-07 02:07:39 +00:00
|
|
|
await this.emitAsync('beforeInstall', this, options);
|
2022-02-28 13:49:50 +00:00
|
|
|
|
2022-01-30 03:11:36 +00:00
|
|
|
if (options?.clean) {
|
|
|
|
await this.db.clean(isBoolean(options.clean) ? { drop: options.clean } : options.clean);
|
|
|
|
}
|
2022-02-28 13:49:50 +00:00
|
|
|
|
2022-01-30 03:11:36 +00:00
|
|
|
await this.db.sync(options?.sync);
|
2022-02-28 13:49:50 +00:00
|
|
|
await this.pm.install(options);
|
2022-06-17 02:25:59 +00:00
|
|
|
await this.version.update();
|
|
|
|
|
2022-01-30 03:11:36 +00:00
|
|
|
await this.emitAsync('afterInstall', this, options);
|
2021-09-09 05:05:33 +00:00
|
|
|
}
|
2022-01-30 03:11:36 +00:00
|
|
|
|
2022-06-17 02:25:59 +00:00
|
|
|
async upgrade(options: any = {}) {
|
|
|
|
const force = false;
|
|
|
|
await this.db.migrator.up();
|
|
|
|
await this.db.sync({
|
|
|
|
force,
|
|
|
|
alter: {
|
|
|
|
drop: force,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
await this.version.update();
|
|
|
|
}
|
|
|
|
|
2022-05-18 16:40:55 +00:00
|
|
|
declare emitAsync: (event: string | symbol, ...args: any[]) => Promise<boolean>;
|
2020-11-11 10:16:18 +00:00
|
|
|
}
|
|
|
|
|
2022-01-30 03:11:36 +00:00
|
|
|
applyMixins(Application, [AsyncEmitter]);
|
|
|
|
|
2020-11-11 10:16:18 +00:00
|
|
|
export default Application;
|