2020-11-11 15:23:39 +08:00
|
|
|
import path from 'path';
|
2021-09-09 23:58:01 +08:00
|
|
|
import Database, { registerFields, Table } 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';
|
2021-03-22 13:41:00 +08:00
|
|
|
import * as usersActions from './actions/users';
|
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);
|
|
|
|
|
2021-09-09 23:58:01 +08:00
|
|
|
database.on('afterTableInit', (table: Table) => {
|
|
|
|
let { createdBy, updatedBy } = table.getOptions();
|
|
|
|
if (createdBy !== false) {
|
|
|
|
table.addField({
|
|
|
|
type: 'createdBy',
|
|
|
|
name: typeof createdBy === 'string' ? createdBy : 'createdBy',
|
|
|
|
target: 'users',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (updatedBy !== false) {
|
|
|
|
table.addField({
|
|
|
|
type: 'updatedBy',
|
|
|
|
name: typeof updatedBy === 'string' ? updatedBy : 'updatedBy',
|
|
|
|
target: 'users',
|
|
|
|
});
|
2020-12-18 19:41:40 +08:00
|
|
|
}
|
2020-12-18 15:50:03 +08:00
|
|
|
});
|
|
|
|
|
2021-01-29 23:53:50 +08:00
|
|
|
database.import({
|
|
|
|
directory: path.resolve(__dirname, 'collections'),
|
|
|
|
});
|
|
|
|
|
2021-03-22 13:41:00 +08:00
|
|
|
for (const [key, action] of Object.entries(usersActions)) {
|
|
|
|
resourcer.registerActionHandler(`users:${key}`, action);
|
|
|
|
}
|
2020-11-29 16:26:53 +08:00
|
|
|
|
2021-01-13 16:23:15 +08:00
|
|
|
resourcer.use(middlewares.parseToken(options));
|
2020-11-11 15:23:39 +08:00
|
|
|
}
|