diff --git a/packages/core/server/src/plugin-manager/PluginManager.ts b/packages/core/server/src/plugin-manager/PluginManager.ts index c2681d3bd..2f24814ba 100644 --- a/packages/core/server/src/plugin-manager/PluginManager.ts +++ b/packages/core/server/src/plugin-manager/PluginManager.ts @@ -43,6 +43,7 @@ export class PluginManager { this.repository.setPluginManager(this); this.app.resourcer.define(resourceOptions); this.app.acl.allow('pm', ['enable', 'disable', 'remove'], 'allowConfigure'); + this.app.acl.allow('applicationPlugins', 'list', 'allowConfigure'); this.server = net.createServer((socket) => { socket.on('data', async (data) => { const { method, plugins } = JSON.parse(data.toString()); @@ -66,7 +67,9 @@ export class PluginManager { if (options?.method !== 'install' || options.reload) { await this.repository.load(); } - this.app.acl.allow('applicationPlugins', 'list'); + }); + this.app.on('beforeUpgrade', async () => { + await this.collection.sync(); }); this.addStaticMultiple(options.plugins); } @@ -209,23 +212,22 @@ export class PluginManager { transaction, filter: { name: plugin }, }); - if (model) { - throw new Error(`${plugin} plugin already exists`); - } - const { enabled, builtIn, installed, ...others } = options; - await this.repository.create({ - transaction, - values: { - name: plugin, - version: packageJson.version, - enabled: !!enabled, - builtIn: !!builtIn, - installed: !!installed, - options: { - ...others, + if (!model) { + const { enabled, builtIn, installed, ...others } = options; + model = await this.repository.create({ + transaction, + values: { + name: plugin, + version: packageJson.version, + enabled: !!enabled, + builtIn: !!builtIn, + installed: !!installed, + options: { + ...others, + }, }, - }, - }); + }); + } const file = resolve( process.cwd(), 'packages', diff --git a/packages/core/server/src/plugin.ts b/packages/core/server/src/plugin.ts index 3af567d7b..70b2d6339 100644 --- a/packages/core/server/src/plugin.ts +++ b/packages/core/server/src/plugin.ts @@ -1,4 +1,3 @@ -import { Database } from '@nocobase/database'; import { Application } from './application'; import { InstallOptions } from './plugin-manager'; @@ -25,15 +24,17 @@ export type PluginType = typeof Plugin; export abstract class Plugin implements PluginInterface { options: any; app: Application; - db: Database; constructor(app: Application, options?: any) { this.app = app; - this.db = app.db; this.setOptions(options); this.afterAdd(); } + get db() { + return this.app.db; + } + get enabled() { return this.options.enabled; } diff --git a/packages/plugins/collection-manager/src/server.ts b/packages/plugins/collection-manager/src/server.ts index b73d724ce..cf75feefb 100644 --- a/packages/plugins/collection-manager/src/server.ts +++ b/packages/plugins/collection-manager/src/server.ts @@ -13,11 +13,11 @@ import { beforeCreateForChildrenCollection, beforeCreateForReverseField, beforeDestroyForeignKey, - beforeInitOptions, + beforeInitOptions } from './hooks'; -import { CollectionModel, FieldModel } from './models'; import { InheritedCollection } from '@nocobase/database'; +import { CollectionModel, FieldModel } from './models'; export class CollectionManagerPlugin extends Plugin { async beforeLoad() { diff --git a/packages/presets/nocobase/src/index.ts b/packages/presets/nocobase/src/index.ts index e70c60cc0..eb70efb7b 100644 --- a/packages/presets/nocobase/src/index.ts +++ b/packages/presets/nocobase/src/index.ts @@ -69,7 +69,7 @@ export class PresetNocoBase extends Plugin { await this.addBuiltInPlugins(); } const builtInPlugins = this.getBuiltInPlugins(); - const plugins = await this.db.getRepository('applicationPlugins').find(); + const plugins = await this.app.db.getRepository('applicationPlugins').find(); const pluginNames = plugins.map((p) => p.name); await this.app.pm.add( builtInPlugins.filter((plugin) => !pluginNames.includes(plugin)),