2020-11-11 15:23:39 +08:00
|
|
|
import path from 'path';
|
2020-12-18 09:04:40 +08:00
|
|
|
|
|
|
|
import Database, { registerFields } from '@nocobase/database';
|
2020-11-11 15:23:39 +08:00
|
|
|
import Resourcer from '@nocobase/resourcer';
|
2020-12-18 09:04:40 +08:00
|
|
|
|
|
|
|
import * as fields from './fields';
|
2020-12-18 15:50:03 +08:00
|
|
|
// import hooks from './hooks';
|
2020-11-29 16:26:53 +08:00
|
|
|
import login from './actions/login';
|
|
|
|
import register from './actions/register';
|
|
|
|
import logout from './actions/logout';
|
|
|
|
import check from './actions/check';
|
2020-12-18 15:50:03 +08:00
|
|
|
import { makeOptions } from './hooks/collection-after-create';
|
2021-01-13 16:23:15 +08:00
|
|
|
import * as middlewares from './middlewares';
|
2020-12-18 09:04:40 +08:00
|
|
|
|
2020-11-11 15:23:39 +08:00
|
|
|
export default async function (options = {}) {
|
|
|
|
const database: Database = this.database;
|
|
|
|
const resourcer: Resourcer = this.resourcer;
|
|
|
|
|
2020-12-18 09:04:40 +08:00
|
|
|
registerFields(fields);
|
|
|
|
|
2020-11-11 20:57:18 +08:00
|
|
|
database.import({
|
2020-11-11 15:23:39 +08:00
|
|
|
directory: path.resolve(__dirname, 'collections'),
|
|
|
|
});
|
2020-11-29 16:26:53 +08:00
|
|
|
|
2020-12-18 15:50:03 +08:00
|
|
|
database.addHook('afterTableInit', (table) => {
|
2020-12-18 19:41:40 +08:00
|
|
|
let { createdBy, updatedBy, internal } = table.getOptions();
|
|
|
|
// 非内置表,默认创建 createdBy 和 updatedBy
|
|
|
|
if (!internal) {
|
|
|
|
if (typeof createdBy === 'undefined') {
|
|
|
|
createdBy = true;
|
|
|
|
}
|
|
|
|
if (typeof updatedBy === 'undefined') {
|
|
|
|
updatedBy = true;
|
|
|
|
}
|
|
|
|
}
|
2020-12-18 15:50:03 +08:00
|
|
|
const fieldsToMake = { createdBy, updatedBy };
|
|
|
|
Object.keys(fieldsToMake)
|
|
|
|
.filter(type => Boolean(fieldsToMake[type]))
|
|
|
|
.map(type => table.addField(makeOptions(type, fieldsToMake[type])));
|
|
|
|
});
|
|
|
|
|
2020-12-02 13:48:19 +08:00
|
|
|
resourcer.registerActionHandlers({
|
2020-11-29 16:26:53 +08:00
|
|
|
'users:login': login,
|
|
|
|
'users:register': register,
|
|
|
|
'users:logout': logout,
|
|
|
|
'users:check': check,
|
|
|
|
});
|
|
|
|
|
2021-01-13 16:23:15 +08:00
|
|
|
resourcer.use(middlewares.parseToken(options));
|
2020-11-11 15:23:39 +08:00
|
|
|
}
|