tachybase_todo/packages/plugin-system-settings/src/server.ts
chenos c1b560e928
feat: rename resourceKey & associatedKey to resourceIndex & associatedIndex (#126)
* resourceIndex & associatedIndex

* resourceIndex & associatedIndex
2021-12-04 16:28:52 +08:00

46 lines
1.3 KiB
TypeScript

import path from 'path';
import { PluginOptions } from '@nocobase/server';
export default {
name: 'system-settings',
async load() {
const database = this.app.db;
const resourcer = this.app.resourcer;
database.import({
directory: path.resolve(__dirname, 'collections'),
});
const SystemSetting = database.getModel('system_settings');
resourcer.use(async (ctx, next) => {
const { actionName, resourceName, resourceIndex } = ctx.action.params;
if (resourceName === 'system_settings' && actionName === 'get') {
let model = await SystemSetting.findOne();
if (!model) {
model = await SystemSetting.create();
}
ctx.action.mergeParams({
resourceIndex: model.id,
});
}
await next();
});
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',
},
});
});
}
} as PluginOptions;