From 9fd41ab33e564f3f83c47e81391d550f519311a0 Mon Sep 17 00:00:00 2001 From: chenos Date: Fri, 13 Aug 2021 23:14:07 +0800 Subject: [PATCH] feat: system settings --- packages/plugin-system-settings/.npmignore | 7 +++++ packages/plugin-system-settings/package.json | 15 +++++++++++ .../src/collections/system_settings.ts | 16 ++++++++++++ packages/plugin-system-settings/src/server.ts | 26 +++++++++++++++++++ 4 files changed, 64 insertions(+) create mode 100644 packages/plugin-system-settings/.npmignore create mode 100644 packages/plugin-system-settings/package.json create mode 100644 packages/plugin-system-settings/src/collections/system_settings.ts create mode 100644 packages/plugin-system-settings/src/server.ts diff --git a/packages/plugin-system-settings/.npmignore b/packages/plugin-system-settings/.npmignore new file mode 100644 index 000000000..461574b2f --- /dev/null +++ b/packages/plugin-system-settings/.npmignore @@ -0,0 +1,7 @@ +node_modules +*.log +docs +__tests__ +tsconfig.json +src +.fatherrc.ts \ No newline at end of file diff --git a/packages/plugin-system-settings/package.json b/packages/plugin-system-settings/package.json new file mode 100644 index 000000000..7aed1d653 --- /dev/null +++ b/packages/plugin-system-settings/package.json @@ -0,0 +1,15 @@ +{ + "name": "@nocobase/plugin-system-settings", + "version": "0.4.0-alpha.7", + "main": "lib/index.js", + "license": "MIT", + "dependencies": { + "@nocobase/database": "^0.4.0-alpha.7", + "@nocobase/resourcer": "^0.4.0-alpha.7", + "@nocobase/server": "^0.4.0-alpha.7" + }, + "devDependencies": { + "@nocobase/actions": "^0.4.0-alpha.7" + }, + "gitHead": "f0b335ac30f29f25c95d7d137655fa64d8d67f1e" +} diff --git a/packages/plugin-system-settings/src/collections/system_settings.ts b/packages/plugin-system-settings/src/collections/system_settings.ts new file mode 100644 index 000000000..a49bdd3ae --- /dev/null +++ b/packages/plugin-system-settings/src/collections/system_settings.ts @@ -0,0 +1,16 @@ +import { TableOptions } from '@nocobase/database'; + +export default { + name: 'system_settings', + fields: [ + { + type: 'string', + name: 'title', + }, + { + type: 'belongsTo', + name: 'logo', + target: 'attachments', + }, + ], +} as TableOptions; diff --git a/packages/plugin-system-settings/src/server.ts b/packages/plugin-system-settings/src/server.ts new file mode 100644 index 000000000..b1992f49b --- /dev/null +++ b/packages/plugin-system-settings/src/server.ts @@ -0,0 +1,26 @@ +import path from 'path'; +import { Application } from '@nocobase/server'; + +export default async function (this: Application, options = {}) { + const database = this.database; + const resourcer = this.resourcer; + + database.import({ + directory: path.resolve(__dirname, 'collections'), + }); + + resourcer.use(async (ctx, next) => { + const { actionName, resourceName, resourceKey } = ctx.action.params; + if (resourceName === 'system_settings' && actionName === 'get') { + const SystemSetting = database.getModel('system_settings'); + let model = await SystemSetting.findOne(); + if (!model) { + model = await SystemSetting.create(); + } + ctx.action.mergeParams({ + resourceKey: model.id, + }); + } + await next(); + }); +}