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 { 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<CollectionRepository>('collections').load();
await this.app.db.getRepository<CollectionRepository>('collections').load({
filter: this.loadFilter,
});
};
this.app.on('beforeStart', loadCollections);

View File

@ -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: {

View File

@ -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';
}
});