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';
|
2021-03-22 13:41:00 +08:00
|
|
|
import * as usersActions from './actions/users';
|
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-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])));
|
|
|
|
});
|
|
|
|
|
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
|
|
|
}
|