2022-02-11 23:59:03 +08:00
|
|
|
import { ACL } from '@nocobase/acl';
|
2021-09-16 00:38:48 +08:00
|
|
|
import { registerActions } from '@nocobase/actions';
|
2022-06-17 10:25:59 +08:00
|
|
|
import Database, { Collection, CollectionOptions, IDatabaseOptions } from '@nocobase/database';
|
2022-01-30 11:11:36 +08: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 19:32:59 +08:00
|
|
|
import { i18n, InitOptions } from 'i18next';
|
2022-01-30 11:11:36 +08:00
|
|
|
import Koa from 'koa';
|
|
|
|
import { isBoolean } from 'lodash';
|
2022-06-17 10:25:59 +08:00
|
|
|
import semver from 'semver';
|
2022-02-11 23:59:03 +08:00
|
|
|
import { createACL } from './acl';
|
2022-04-24 12:38:54 +08:00
|
|
|
import { AppManager } from './app-manager';
|
2022-05-19 00:40:55 +08:00
|
|
|
import { registerCli } from './commands';
|
2022-06-17 10:25:59 +08:00
|
|
|
import { createI18n, createResourcer, registerMiddlewares } from './helper';
|
2022-01-30 11:11:36 +08:00
|
|
|
import { Plugin } from './plugin';
|
2022-04-24 12:38:54 +08:00
|
|
|
import { InstallOptions, PluginManager } from './plugin-manager';
|
2021-09-09 13:05:33 +08:00
|
|
|
|
2022-05-19 00:40:55 +08:00
|
|
|
const packageJson = require('../package.json');
|
|
|
|
|
2022-04-17 10:00:42 +08:00
|
|
|
export type PluginConfiguration = string | [string, any];
|
|
|
|
export type PluginsConfigurations = Array<PluginConfiguration>;
|
|
|
|
|
2021-09-09 13:05:33 +08:00
|
|
|
export interface ResourcerOptions {
|
|
|
|
prefix?: string;
|
|
|
|
}
|
2020-11-12 11:48:27 +08:00
|
|
|
|
|
|
|
export interface ApplicationOptions {
|
2022-02-15 00:20:25 +08:00
|
|
|
database?: IDatabaseOptions | Database;
|
2021-09-09 13:05:33 +08:00
|
|
|
resourcer?: ResourcerOptions;
|
|
|
|
bodyParser?: any;
|
|
|
|
cors?: any;
|
|
|
|
dataWrapping?: boolean;
|
2021-10-18 12:49:37 +08:00
|
|
|
registerActions?: boolean;
|
2021-11-08 19:32:59 +08:00
|
|
|
i18n?: i18n | InitOptions;
|
2022-04-17 10:00:42 +08:00
|
|
|
plugins?: PluginsConfigurations;
|
2020-11-12 11:48:27 +08:00
|
|
|
}
|
2020-11-11 18:16:18 +08:00
|
|
|
|
2022-05-19 00:40:55 +08:00
|
|
|
export interface DefaultState {
|
2021-09-16 15:57:21 +08:00
|
|
|
currentUser?: any;
|
|
|
|
[key: string]: any;
|
|
|
|
}
|
|
|
|
|
2022-05-19 00:40:55 +08:00
|
|
|
export interface DefaultContext {
|
2021-09-16 15:57:21 +08: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 11:11:36 +08: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-07 01:14:00 +08:00
|
|
|
cliArgs?: any[];
|
2022-01-30 11:11:36 +08:00
|
|
|
listen?: ListenOptions;
|
|
|
|
}
|
|
|
|
|
2022-06-17 10:25:59 +08: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-18 00:18:12 +08:00
|
|
|
if (await this.app.db.collectionExistsInDb('applicationVersion')) {
|
2022-06-17 10:25:59 +08: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-18 00:18:12 +08:00
|
|
|
if (await this.app.db.collectionExistsInDb('applicationVersion')) {
|
2022-06-17 10:25:59 +08: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 11:11:36 +08:00
|
|
|
export class Application<StateT = DefaultState, ContextT = DefaultContext> extends Koa implements AsyncEmitter {
|
2021-09-14 11:09:26 +08:00
|
|
|
public readonly db: Database;
|
2021-01-13 16:23:15 +08:00
|
|
|
|
|
|
|
public readonly resourcer: Resourcer;
|
2020-11-11 18:16:18 +08:00
|
|
|
|
2021-09-05 23:59:38 +08:00
|
|
|
public readonly cli: Command;
|
|
|
|
|
2021-11-08 19:32:59 +08:00
|
|
|
public readonly i18n: i18n;
|
|
|
|
|
2022-01-30 11:11:36 +08:00
|
|
|
public readonly pm: PluginManager;
|
|
|
|
|
2022-02-11 23:59:03 +08:00
|
|
|
public readonly acl: ACL;
|
|
|
|
|
2022-03-28 22:01:10 +08:00
|
|
|
public readonly appManager: AppManager;
|
|
|
|
|
2022-06-17 10:25:59 +08:00
|
|
|
public readonly version: ApplicationVersion;
|
|
|
|
|
2021-09-14 11:09:26 +08:00
|
|
|
protected plugins = new Map<string, Plugin>();
|
2020-11-12 11:48:27 +08:00
|
|
|
|
2022-01-30 11:11:36 +08:00
|
|
|
public listenServer: Server;
|
|
|
|
|
2022-03-28 22:01:10 +08:00
|
|
|
constructor(public options: ApplicationOptions) {
|
2021-01-13 16:23:15 +08:00
|
|
|
super();
|
2021-09-09 13:05:33 +08:00
|
|
|
|
2022-02-11 23:59:03 +08:00
|
|
|
this.acl = createACL();
|
2022-06-17 10:25:59 +08:00
|
|
|
this.db = this.createDatabase(options);
|
2021-10-18 12:49:37 +08:00
|
|
|
this.resourcer = createResourcer(options);
|
2022-05-19 00:40:55 +08:00
|
|
|
this.cli = new Command('nocobase').usage('[command] [options]');
|
2021-11-08 19:32:59 +08:00
|
|
|
this.i18n = createI18n(options);
|
2021-09-05 23:59:38 +08:00
|
|
|
|
2022-01-30 11:11:36 +08:00
|
|
|
this.pm = new PluginManager({
|
|
|
|
app: this,
|
|
|
|
});
|
|
|
|
|
2022-03-28 22:01:10 +08:00
|
|
|
this.appManager = new AppManager(this);
|
|
|
|
|
2021-10-18 12:49:37 +08:00
|
|
|
registerMiddlewares(this, options);
|
2022-03-11 10:10:57 +08:00
|
|
|
|
2021-10-18 12:49:37 +08:00
|
|
|
if (options.registerActions !== false) {
|
|
|
|
registerActions(this);
|
2021-09-09 13:05:33 +08:00
|
|
|
}
|
2022-04-17 10:00:42 +08:00
|
|
|
|
|
|
|
this.loadPluginConfig(options.plugins || []);
|
2022-05-19 00:40:55 +08:00
|
|
|
|
|
|
|
registerCli(this);
|
2022-06-17 10:25:59 +08: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-19 00:40:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
getVersion() {
|
|
|
|
return packageJson.version;
|
2020-12-18 19:54:53 +08:00
|
|
|
}
|
|
|
|
|
2022-01-30 11:11:36 +08:00
|
|
|
plugin<O = any>(pluginClass: any, options?: O): Plugin<O> {
|
|
|
|
return this.pm.add(pluginClass, options);
|
|
|
|
}
|
|
|
|
|
2022-04-17 10:00:42 +08: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 15:57:21 +08:00
|
|
|
use<NewStateT = {}, NewContextT = {}>(
|
|
|
|
middleware: Koa.Middleware<StateT & NewStateT, ContextT & NewContextT>,
|
|
|
|
options?: MiddlewareOptions,
|
2021-09-23 00:16:04 +08:00
|
|
|
) {
|
2021-09-16 15:57:21 +08:00
|
|
|
// @ts-ignore
|
|
|
|
return super.use(middleware);
|
|
|
|
}
|
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
collection(options: CollectionOptions) {
|
|
|
|
return this.db.collection(options);
|
2021-09-14 11:09:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
resource(options: ResourceOptions) {
|
|
|
|
return this.resourcer.define(options);
|
|
|
|
}
|
|
|
|
|
2021-09-16 15:57:21 +08:00
|
|
|
actions(handlers: any, options?: ActionsOptions) {
|
2021-09-16 00:38:48 +08:00
|
|
|
return this.resourcer.registerActions(handlers);
|
|
|
|
}
|
|
|
|
|
2022-02-07 21:54:07 +08:00
|
|
|
command(name: string, desc?: string, opts?: CommandOptions): Command {
|
2022-05-19 00:40:55 +08:00
|
|
|
return this.cli.command(name, desc, opts).allowUnknownOption();
|
2021-09-14 11:09:26 +08:00
|
|
|
}
|
|
|
|
|
2021-10-18 12:49:37 +08:00
|
|
|
findCommand(name: string): Command {
|
2022-02-10 18:38:05 +08:00
|
|
|
return (<any>this.cli)._findCommand(name);
|
2021-10-18 12:49:37 +08:00
|
|
|
}
|
|
|
|
|
2021-09-14 11:09:26 +08:00
|
|
|
async load() {
|
2022-01-30 11:11:36 +08:00
|
|
|
await this.pm.load();
|
2021-09-14 11:09:26 +08:00
|
|
|
}
|
|
|
|
|
2022-01-24 14:10:35 +08:00
|
|
|
getPlugin<P extends Plugin>(name: string) {
|
2022-01-30 11:11:36 +08:00
|
|
|
return this.pm.get(name) as P;
|
2022-01-24 14:10:35 +08:00
|
|
|
}
|
|
|
|
|
2022-01-30 11:11:36 +08:00
|
|
|
async parse(argv = process.argv) {
|
|
|
|
await this.load();
|
|
|
|
return this.cli.parseAsync(argv);
|
|
|
|
}
|
|
|
|
|
2022-05-13 11:18:36 +08:00
|
|
|
async start(options: StartOptions = {}) {
|
2022-01-30 11:11:36 +08:00
|
|
|
// reconnect database
|
|
|
|
if (this.db.closed()) {
|
|
|
|
await this.db.reconnect();
|
2021-12-06 21:23:34 +08:00
|
|
|
}
|
2022-01-30 11:11:36 +08:00
|
|
|
|
|
|
|
await this.emitAsync('beforeStart', this, options);
|
|
|
|
|
|
|
|
if (options?.listen?.port) {
|
|
|
|
const listen = () =>
|
2022-05-13 11:20:30 +08:00
|
|
|
new Promise((resolve, reject) => {
|
2022-01-30 11:11:36 +08:00
|
|
|
const Server = this.listen(options?.listen, () => {
|
|
|
|
resolve(Server);
|
|
|
|
});
|
2022-05-13 11:20:30 +08:00
|
|
|
|
|
|
|
Server.on('error', (err) => {
|
|
|
|
reject(err);
|
|
|
|
});
|
2021-12-06 21:23:34 +08:00
|
|
|
});
|
2022-01-30 11:11:36 +08:00
|
|
|
|
2022-05-13 11:20:30 +08:00
|
|
|
try {
|
|
|
|
//@ts-ignore
|
|
|
|
this.listenServer = await listen();
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
2021-12-06 21:23:34 +08:00
|
|
|
}
|
|
|
|
|
2022-01-30 11:11:36 +08:00
|
|
|
await this.emitAsync('afterStart', this, options);
|
2021-12-06 21:23:34 +08:00
|
|
|
}
|
|
|
|
|
2022-03-28 22:01:10 +08:00
|
|
|
listen(...args): Server {
|
|
|
|
return this.appManager.listen(...args);
|
|
|
|
}
|
|
|
|
|
2022-05-13 11:18:36 +08:00
|
|
|
async stop(options: any = {}) {
|
2022-02-07 01:14:00 +08:00
|
|
|
await this.emitAsync('beforeStop', this, options);
|
2022-01-30 11:11:36 +08:00
|
|
|
|
2022-03-28 22:01:10 +08:00
|
|
|
try {
|
|
|
|
// close database connection
|
|
|
|
// silent if database already closed
|
|
|
|
await this.db.close();
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
}
|
2022-01-30 11:11:36 +08: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-07 01:14:00 +08:00
|
|
|
await this.emitAsync('afterStop', this, options);
|
2020-12-18 19:54:53 +08:00
|
|
|
}
|
|
|
|
|
2022-05-13 11:18:36 +08:00
|
|
|
async destroy(options: any = {}) {
|
2022-02-07 01:14:00 +08:00
|
|
|
await this.emitAsync('beforeDestroy', this, options);
|
|
|
|
await this.stop(options);
|
|
|
|
await this.emitAsync('afterDestroy', this, options);
|
2022-01-30 11:11:36 +08:00
|
|
|
}
|
|
|
|
|
2022-05-13 11:18:36 +08:00
|
|
|
async install(options: InstallOptions = {}) {
|
2022-02-07 10:07:39 +08:00
|
|
|
await this.emitAsync('beforeInstall', this, options);
|
2022-02-28 21:49:50 +08:00
|
|
|
|
2022-07-04 13:41:07 +08:00
|
|
|
const r = await this.db.version.satisfies({
|
|
|
|
mysql: '8.x',
|
|
|
|
sqlite: '3.x',
|
|
|
|
postgres: '>=10',
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!r) {
|
|
|
|
console.log('The database only supports MySQL 8.x, SQLite 3.x and PostgreSQL 10+');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-30 11:11:36 +08:00
|
|
|
if (options?.clean) {
|
|
|
|
await this.db.clean(isBoolean(options.clean) ? { drop: options.clean } : options.clean);
|
|
|
|
}
|
2022-02-28 21:49:50 +08:00
|
|
|
|
2022-01-30 11:11:36 +08:00
|
|
|
await this.db.sync(options?.sync);
|
2022-02-28 21:49:50 +08:00
|
|
|
await this.pm.install(options);
|
2022-06-17 10:25:59 +08:00
|
|
|
await this.version.update();
|
|
|
|
|
2022-01-30 11:11:36 +08:00
|
|
|
await this.emitAsync('afterInstall', this, options);
|
2021-09-09 13:05:33 +08:00
|
|
|
}
|
2022-01-30 11:11:36 +08:00
|
|
|
|
2022-06-17 10:25:59 +08: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-19 00:40:55 +08:00
|
|
|
declare emitAsync: (event: string | symbol, ...args: any[]) => Promise<boolean>;
|
2020-11-11 18:16:18 +08:00
|
|
|
}
|
|
|
|
|
2022-01-30 11:11:36 +08:00
|
|
|
applyMixins(Application, [AsyncEmitter]);
|
|
|
|
|
2020-11-11 18:16:18 +08:00
|
|
|
export default Application;
|