fix: sync roles collection in share plugin (#2601)

This commit is contained in:
ChengLei Shao 2023-09-06 14:36:13 +08:00 committed by GitHub
parent 22f681024d
commit 2484c8ffb2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 11 deletions

View File

@ -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 PluginErrorHandler from '@nocobase/plugin-error-handler';
import { Plugin } from '@nocobase/server'; import { Plugin } from '@nocobase/server';
import { Mutex } from 'async-mutex'; import { Mutex } from 'async-mutex';
@ -21,6 +21,12 @@ import viewResourcer from './resourcers/views';
export class CollectionManagerPlugin extends Plugin { export class CollectionManagerPlugin extends Plugin {
public schema: string; public schema: string;
private loadFilter: Filter = {};
setLoadFilter(filter: Filter) {
this.loadFilter = filter;
}
async beforeLoad() { async beforeLoad() {
if (this.app.db.inDialect('postgres')) { if (this.app.db.inDialect('postgres')) {
this.schema = process.env.COLLECTION_MANAGER_SCHEMA || this.db.options.schema || 'public'; this.schema = process.env.COLLECTION_MANAGER_SCHEMA || this.db.options.schema || 'public';
@ -211,7 +217,9 @@ export class CollectionManagerPlugin extends Plugin {
const loadCollections = async () => { const loadCollections = async () => {
this.app.log.debug('loading custom collections'); this.app.log.debug('loading custom collections');
this.app.setMaintainingMessage('loading custom collections'); this.app.setMaintainingMessage('loading custom collections');
await this.app.db.getRepository<CollectionRepository>('collections').load(); await this.app.db.getRepository<CollectionRepository>('collections').load({
filter: this.loadFilter,
});
}; };
this.app.on('beforeStart', loadCollections); this.app.on('beforeStart', loadCollections);

View File

@ -240,6 +240,33 @@ pgOnly()('collection sync', () => {
).toBeFalsy(); ).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 () => { it('should sync user collections', async () => {
const subApp1Record = await mainDb.getRepository('applications').create({ const subApp1Record = await mainDb.getRepository('applications').create({
values: { values: {

View File

@ -37,18 +37,20 @@ class SubAppPlugin extends Plugin {
const sharedCollections = [...sharedCollectionGroupsCollections.flat(), 'users', 'users_jobs']; const sharedCollections = [...sharedCollectionGroupsCollections.flat(), 'users', 'users_jobs'];
subApp.db.on('beforeDefineCollection', (options) => { subApp.on('beforeLoadPlugin', (plugin) => {
const name = options.name; if (plugin.name === 'collection-manager') {
plugin.setLoadFilter({
// 指向主应用的 系统schema 'name.$ne': 'roles',
if (sharedCollections.includes(name)) { });
options.schema = mainApp.db.options.schema || 'public';
} }
}); });
subApp.db.on('beforeUpdateCollection', (collection, newOptions) => { subApp.db.on('beforeDefineCollection', (options) => {
if (collection.name === 'roles') { const name = options.name;
newOptions.schema = subApp.db.options.schema;
// 共享的Collection指向主应用的 系统schema
if (sharedCollections.includes(name)) {
options.schema = mainApp.db.options.schema || 'public';
} }
}); });