From 2484c8ffb22edc358df38485f9f1ce2127ccb5e6 Mon Sep 17 00:00:00 2001 From: ChengLei Shao Date: Wed, 6 Sep 2023 14:36:13 +0800 Subject: [PATCH] fix: sync roles collection in share plugin (#2601) --- .../collection-manager/src/server/server.ts | 12 +++++++-- .../server/__tests__/collection-sync.test.ts | 27 +++++++++++++++++++ .../src/server/plugin.ts | 20 +++++++------- 3 files changed, 48 insertions(+), 11 deletions(-) diff --git a/packages/plugins/collection-manager/src/server/server.ts b/packages/plugins/collection-manager/src/server/server.ts index 0394930f6..0fbfbc500 100644 --- a/packages/plugins/collection-manager/src/server/server.ts +++ b/packages/plugins/collection-manager/src/server/server.ts @@ -1,4 +1,4 @@ -import { InheritedCollection, UniqueConstraintError } from '@nocobase/database'; +import { Filter, InheritedCollection, UniqueConstraintError } from '@nocobase/database'; import PluginErrorHandler from '@nocobase/plugin-error-handler'; import { Plugin } from '@nocobase/server'; import { Mutex } from 'async-mutex'; @@ -21,6 +21,12 @@ import viewResourcer from './resourcers/views'; export class CollectionManagerPlugin extends Plugin { public schema: string; + private loadFilter: Filter = {}; + + setLoadFilter(filter: Filter) { + this.loadFilter = filter; + } + async beforeLoad() { if (this.app.db.inDialect('postgres')) { this.schema = process.env.COLLECTION_MANAGER_SCHEMA || this.db.options.schema || 'public'; @@ -211,7 +217,9 @@ export class CollectionManagerPlugin extends Plugin { const loadCollections = async () => { this.app.log.debug('loading custom collections'); this.app.setMaintainingMessage('loading custom collections'); - await this.app.db.getRepository('collections').load(); + await this.app.db.getRepository('collections').load({ + filter: this.loadFilter, + }); }; this.app.on('beforeStart', loadCollections); diff --git a/packages/plugins/multi-app-share-collection/src/server/__tests__/collection-sync.test.ts b/packages/plugins/multi-app-share-collection/src/server/__tests__/collection-sync.test.ts index f1c556128..3c70e3b2d 100644 --- a/packages/plugins/multi-app-share-collection/src/server/__tests__/collection-sync.test.ts +++ b/packages/plugins/multi-app-share-collection/src/server/__tests__/collection-sync.test.ts @@ -240,6 +240,33 @@ pgOnly()('collection sync', () => { ).toBeFalsy(); }); + it('should use sub app role model', async () => { + await mainApp.db.getRepository('collections.fields', 'roles').create({ + values: { + name: 'role-field', + type: 'string', + }, + context: {}, + }); + + await mainDb.getRepository('applications').create({ + values: { + name: 'sub1', + }, + context: { + waitSubAppInstall: true, + }, + }); + + const subApp1 = await AppSupervisor.getInstance().getApp('sub1'); + + await subApp1.db.getRepository('roles').create({ + values: { + name: 'role1', + }, + }); + }); + it('should sync user collections', async () => { const subApp1Record = await mainDb.getRepository('applications').create({ values: { diff --git a/packages/plugins/multi-app-share-collection/src/server/plugin.ts b/packages/plugins/multi-app-share-collection/src/server/plugin.ts index 963bda1b2..295f58a5c 100644 --- a/packages/plugins/multi-app-share-collection/src/server/plugin.ts +++ b/packages/plugins/multi-app-share-collection/src/server/plugin.ts @@ -37,18 +37,20 @@ class SubAppPlugin extends Plugin { const sharedCollections = [...sharedCollectionGroupsCollections.flat(), 'users', 'users_jobs']; - subApp.db.on('beforeDefineCollection', (options) => { - const name = options.name; - - // 指向主应用的 系统schema - if (sharedCollections.includes(name)) { - options.schema = mainApp.db.options.schema || 'public'; + subApp.on('beforeLoadPlugin', (plugin) => { + if (plugin.name === 'collection-manager') { + plugin.setLoadFilter({ + 'name.$ne': 'roles', + }); } }); - subApp.db.on('beforeUpdateCollection', (collection, newOptions) => { - if (collection.name === 'roles') { - newOptions.schema = subApp.db.options.schema; + subApp.db.on('beforeDefineCollection', (options) => { + const name = options.name; + + // 共享的Collection指向主应用的 系统schema + if (sharedCollections.includes(name)) { + options.schema = mainApp.db.options.schema || 'public'; } });