From d8ad98e2f75b236ac43185f513cc24aa2497f34b Mon Sep 17 00:00:00 2001 From: ChengLei Shao Date: Tue, 20 Jun 2023 11:28:54 +0800 Subject: [PATCH] fix(collection-manager): share collection when COLLECTION_MANAGER_SCHEMA not set (#2081) * fix: share collection when COLLECTION_MANAGER_SCHEMA not set * fix: test --- .../plugins/collection-manager/src/server.ts | 4 +-- .../server/__tests__/collection-sync.test.ts | 25 +++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/packages/plugins/collection-manager/src/server.ts b/packages/plugins/collection-manager/src/server.ts index b4a6d695a0..3f655b8566 100644 --- a/packages/plugins/collection-manager/src/server.ts +++ b/packages/plugins/collection-manager/src/server.ts @@ -27,8 +27,8 @@ export class CollectionManagerPlugin extends Plugin { public schema: string; async beforeLoad() { - if (process.env.COLLECTION_MANAGER_SCHEMA) { - this.schema = process.env.COLLECTION_MANAGER_SCHEMA; + if (this.app.db.inDialect('postgres')) { + this.schema = process.env.COLLECTION_MANAGER_SCHEMA || this.db.options.schema || 'public'; } this.app.db.registerModels({ 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 dfd01692bf..8846af73fa 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 @@ -1,5 +1,6 @@ import { BelongsToManyRepository, Database } from '@nocobase/database'; import { MockServer, mockServer, pgOnly } from '@nocobase/test'; +import * as process from 'process'; pgOnly()('enable plugin', () => { let mainDb: Database; @@ -255,6 +256,30 @@ pgOnly()('collection sync', () => { expect(user).toBeTruthy(); }); + it('should sync custom collections', async () => { + const subApp1Record = await mainDb.getRepository('applications').create({ + values: { + name: 'sub1', + }, + }); + + const subApp1 = await mainApp.appManager.getApplication(subApp1Record.name); + + await mainApp.db.getRepository('collections').create({ + values: { + name: 'posts', + fields: [{ type: 'string', title: 'title' }], + }, + context: {}, + }); + + const postCollection = subApp1.db.getCollection('posts'); + + expect(postCollection.options.schema).toBe( + process.env.COLLECTION_MANAGER_SCHEMA || mainDb.options.schema || 'public', + ); + }); + it('should support syncToApps with wildcard value', async () => { const subApp1Record = await mainDb.getRepository('applications').create({ values: {