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';
|
2021-09-11 18:53:26 +08:00
|
|
|
import Application from '@nocobase/server';
|
2020-12-18 09:04:40 +08:00
|
|
|
|
2021-09-11 18:53:26 +08:00
|
|
|
export default async function (this: Application, options = {}) {
|
2020-11-11 15:23:39 +08:00
|
|
|
const database: Database = this.database;
|
|
|
|
const resourcer: Resourcer = this.resourcer;
|
|
|
|
|
2020-12-18 09:04:40 +08:00
|
|
|
registerFields(fields);
|
|
|
|
|
2021-09-11 18:53:26 +08:00
|
|
|
this.on('users.init', async () => {
|
|
|
|
const User = database.getModel('users');
|
|
|
|
await User.create({
|
|
|
|
nickname: '超级管理员',
|
|
|
|
email: process.env.ADMIN_EMAIL,
|
|
|
|
password: process.env.ADMIN_PASSWORD,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
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
|
|
|
}
|