tachybase_todo/packages/plugin-users/src/server.ts

42 lines
1.2 KiB
TypeScript
Raw Normal View History

import path from 'path';
2021-09-09 23:58:01 +08:00
import Database, { registerFields, Table } from '@nocobase/database';
import Resourcer from '@nocobase/resourcer';
import * as fields from './fields';
import * as usersActions from './actions/users';
import * as middlewares from './middlewares';
export default async function (options = {}) {
const database: Database = this.database;
const resourcer: Resourcer = this.resourcer;
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',
});
}
});
database.import({
directory: path.resolve(__dirname, 'collections'),
});
for (const [key, action] of Object.entries(usersActions)) {
resourcer.registerActionHandler(`users:${key}`, action);
}
resourcer.use(middlewares.parseToken(options));
}