tachybase_todo/packages/plugin-system-settings/src/server.ts

46 lines
1.3 KiB
TypeScript
Raw Normal View History

2021-08-13 23:14:07 +08:00
import path from 'path';
2021-09-23 00:16:04 +08:00
import { PluginOptions } from '@nocobase/server';
2021-08-13 23:14:07 +08:00
2021-09-23 00:16:04 +08:00
export default {
name: 'system-settings',
async load() {
const database = this.app.db;
const resourcer = this.app.resourcer;
2021-08-13 23:14:07 +08:00
2021-09-23 00:16:04 +08:00
database.import({
directory: path.resolve(__dirname, 'collections'),
});
2021-09-11 18:53:26 +08:00
2021-09-23 00:16:04 +08:00
const SystemSetting = database.getModel('system_settings');
resourcer.use(async (ctx, next) => {
const { actionName, resourceName, resourceIndex } = ctx.action.params;
2021-09-23 00:16:04 +08:00
if (resourceName === 'system_settings' && actionName === 'get') {
let model = await SystemSetting.findOne();
if (!model) {
model = await SystemSetting.create();
}
ctx.action.mergeParams({
resourceIndex: model.id,
2021-09-23 00:16:04 +08:00
});
2021-08-13 23:14:07 +08:00
}
2021-09-23 00:16:04 +08:00
await next();
2021-09-11 18:53:26 +08:00
});
2021-09-23 00:16:04 +08:00
this.app.on('db.init', async () => {
const setting = await SystemSetting.create({
title: 'NocoBase',
});
await setting.updateAssociations({
logo: {
title: 'nocobase-logo',
filename: '682e5ad037dd02a0fe4800a3e91c283b.png',
extname: '.png',
mimetype: 'image/png',
url: 'https://nocobase.oss-cn-beijing.aliyuncs.com/682e5ad037dd02a0fe4800a3e91c283b.png',
},
});
2021-09-11 18:53:26 +08:00
});
2021-09-23 00:16:04 +08:00
}
} as PluginOptions;