nocobase/packages/plugins/multi-app-share-collection/src/server/migrations/20230319111111-update-apps-collections.ts
ChengLei Shao ca95edf295
refactor: multi-app (#1578)
* feat: compact theme

* fix: theme

* fix: styling

* fix: margin

* feat: improve

* fix: remove console.log

* test: enable plugin test

* refactor: multi app

* test: lazy load sync plugin

* test: lazy load test

* fix: beforeGetApplication Event

* feat: loadFromDatabase options in traverseSubApps

* fix: test

* fix: multi app manager test

* chore: test

* test: should upgrade sub apps when main app upgrade

* feat: plugin require check

* chore: yarn.lock

* fix: sql typo

* feat: share collections

* fix: record name

* test: belongs to many repository

* fix: belongs to many with targetKey alias

* fix: extend collection error

* fix: transaction error

* feat: collection graph

* fix: update options in collection

* chore: collections graph

* chore: export uitls

* feat: connected nodes method in collections graph

* feat: exclude params in connected nodes

* chore: sub app collection list params

* fix: collections graph

* feat: syncToApps migration

* fix:  translation

---------

Co-authored-by: chenos <chenlinxh@gmail.com>
2023-03-19 23:40:42 +08:00

68 lines
1.9 KiB
TypeScript

import { Migration } from '@nocobase/server';
import { CollectionsGraph } from '@nocobase/utils';
export default class extends Migration {
async up() {
if (!this.app.db.getCollection('applications')) return;
await this.app.db.getCollection('collections').repository.destroy({
where: {
name: 'applications',
},
});
const appSyncedCollections: Map<string, Set<string>> = new Map();
const collections = await this.app.db.getCollection('collections').repository.find();
const collectionsData = collections.map((collection) => collection.toJSON());
for (const collection of collections) {
const collectionSyncToApps = collection.get('syncToApps');
if (collectionSyncToApps) {
for (const app of collectionSyncToApps) {
if (!appSyncedCollections.has(app)) {
appSyncedCollections.set(app, new Set());
}
appSyncedCollections.get(app).add(collection.name);
}
}
}
const allCollections = collections.map((collection) => collection.name);
const appCollectionBlacklist = this.app.db.getCollection('appCollectionBlacklist');
for (const [app, syncedCollections] of appSyncedCollections) {
const blackListCollections = allCollections.filter(
(collection) => !syncedCollections.has(collection) && !['users', 'roles'].includes(collection),
);
const connectedCollections = CollectionsGraph.connectedNodes({
collections: collectionsData,
nodes: blackListCollections,
direction: 'reverse',
});
console.log(
JSON.stringify(
{
app,
connectedCollections,
},
null,
2,
),
);
await appCollectionBlacklist.model.bulkCreate(
connectedCollections.map((collection) => {
return {
applicationName: app,
collectionName: collection,
};
}),
);
}
}
}