@nocobase/plugin-multi-app-share-collection 0.12.0-alpha.5 → 0.13.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,6 +4,5 @@ export declare class MultiAppShareCollectionPlugin extends Plugin {
4
4
  beforeEnable(): Promise<void>;
5
5
  beforeLoad(): Promise<void>;
6
6
  load(): Promise<void>;
7
- requiredPlugins(): any[];
8
7
  }
9
8
  export default MultiAppShareCollectionPlugin;
@@ -76,6 +76,7 @@ class SubAppPlugin extends server.Plugin {
76
76
  await next();
77
77
  });
78
78
  subApp.on("beforeInstall", async () => {
79
+ await subApp.db.sync();
79
80
  const subAppPluginsCollection = subApp.db.getCollection("applicationPlugins");
80
81
  const mainAppPluginsCollection = mainApp.db.getCollection("applicationPlugins");
81
82
  await subApp.db.sequelize.query(`TRUNCATE ${subAppPluginsCollection.quotedTableName()}`);
@@ -90,6 +91,7 @@ class SubAppPlugin extends server.Plugin {
90
91
  await subApp.db.sequelize.query(`
91
92
  SELECT setval('${sequenceName[0]["pg_get_serial_sequence"]}', (SELECT max("id") FROM ${subAppPluginsCollection.quotedTableName()}));
92
93
  `);
94
+ await subApp.reload();
93
95
  console.log(`sync plugins from ${mainApp.name} app to sub app ${subApp.name}`);
94
96
  });
95
97
  }
@@ -101,6 +103,10 @@ class MultiAppShareCollectionPlugin extends server.Plugin {
101
103
  if (!this.db.inDialect("postgres")) {
102
104
  throw new Error("multi-app-share-collection plugin only support postgres");
103
105
  }
106
+ const plugin = this.pm.get("multi-app-manager");
107
+ if (!plugin.enabled) {
108
+ throw new Error(`${this.name} plugin need multi-app-manager plugin enabled`);
109
+ }
104
110
  }
105
111
  async beforeLoad() {
106
112
  if (!this.db.inDialect("postgres")) {
@@ -110,24 +116,30 @@ class MultiAppShareCollectionPlugin extends server.Plugin {
110
116
  if (lodash__default.default.get(options, "loadFromDatabase")) {
111
117
  for (const application of await this.app.db.getCollection("applications").repository.find()) {
112
118
  const appName = application.get("name");
113
- const subApp = await this.app.appManager.getApplication(appName);
119
+ const subApp = await server.AppSupervisor.getInstance().getApp(appName);
114
120
  await callback(subApp);
115
121
  }
116
122
  return;
117
123
  }
118
- const subApps = [...this.app.appManager.applications.values()];
124
+ const subApps = [...server.AppSupervisor.getInstance().subApps()];
119
125
  for (const subApp of subApps) {
120
126
  await callback(subApp);
121
127
  }
122
128
  };
123
- this.app.on("afterSubAppAdded", (subApp) => {
124
- subApp.plugin(SubAppPlugin, { name: "sub-app", mainApp: this.app });
125
- });
129
+ const mainApp = this.app;
130
+ function addPluginToSubApp(app) {
131
+ if (app.name !== "main") {
132
+ app.plugin(SubAppPlugin, { name: "sub-app", mainApp });
133
+ }
134
+ }
135
+ if (server.AppSupervisor.getInstance().listeners("afterAppAdded").filter((f) => f.name == addPluginToSubApp.name).length == 0) {
136
+ server.AppSupervisor.getInstance().on("afterAppAdded", addPluginToSubApp);
137
+ }
126
138
  this.app.db.on("users.afterCreateWithAssociations", async (model, options) => {
127
139
  await traverseSubApps(async (subApp) => {
128
140
  const { transaction } = options;
129
141
  const repository = subApp.db.getRepository("roles");
130
- const subAppModel = await subApp.db.getCollection("users").repository.findOne({
142
+ const subAppUserModel = await subApp.db.getCollection("users").repository.findOne({
131
143
  filter: {
132
144
  id: model.get("id")
133
145
  },
@@ -139,72 +151,36 @@ class MultiAppShareCollectionPlugin extends server.Plugin {
139
151
  },
140
152
  transaction
141
153
  });
142
- if (defaultRole && await subAppModel.countRoles({ transaction }) == 0) {
143
- await subAppModel.addRoles(defaultRole, { transaction });
154
+ if (defaultRole && await subAppUserModel.countRoles({ transaction }) == 0) {
155
+ await subAppUserModel.addRoles(defaultRole, { transaction });
144
156
  }
145
157
  });
146
158
  });
147
- this.app.db.on("collection:loaded", async ({ transaction, collection }) => {
148
- await traverseSubApps(async (subApp) => {
149
- const name = collection.name;
150
- const collectionRecord = await subApp.db.getRepository("collections").findOne({
151
- filter: {
152
- name
153
- },
154
- transaction
155
- });
156
- await collectionRecord.load({ transaction });
159
+ this.app.on("__restarted", () => {
160
+ traverseSubApps((subApp) => {
161
+ subApp.runCommand("restart");
157
162
  });
158
163
  });
159
- this.app.db.on("field:loaded", async ({ transaction, fieldKey }) => {
160
- await traverseSubApps(async (subApp) => {
161
- const fieldRecord = await subApp.db.getRepository("fields").findOne({
162
- filterByTk: fieldKey,
163
- transaction
164
- });
165
- if (fieldRecord) {
166
- await fieldRecord.load({ transaction });
167
- }
168
- });
169
- });
170
- this.app.on("afterEnablePlugin", async (pluginName) => {
171
- await traverseSubApps(
172
- async (subApp) => {
164
+ this.app.on("afterEnablePlugin", (pluginNames) => {
165
+ traverseSubApps((subApp) => {
166
+ for (const pluginName of lodash__default.default.castArray(pluginNames)) {
173
167
  if (subAppFilteredPlugins.includes(pluginName))
174
168
  return;
175
- await subApp.pm.enable(pluginName);
176
- },
177
- {
178
- loadFromDatabase: true
169
+ subApp.runAsCLI(["pm", "enable", pluginName], { from: "user" });
179
170
  }
180
- );
171
+ });
181
172
  });
182
- this.app.on("afterDisablePlugin", async (pluginName) => {
183
- await traverseSubApps(
184
- async (subApp) => {
173
+ this.app.on("afterDisablePlugin", (pluginNames) => {
174
+ traverseSubApps((subApp) => {
175
+ for (const pluginName of lodash__default.default.castArray(pluginNames)) {
185
176
  if (subAppFilteredPlugins.includes(pluginName))
186
177
  return;
187
- await subApp.pm.disable(pluginName);
188
- },
189
- {
190
- loadFromDatabase: true
178
+ subApp.runAsCLI(["pm", "disable", pluginName], { from: "user" });
191
179
  }
192
- );
193
- });
194
- this.app.db.on("field.afterRemove", (removedField) => {
195
- const subApps = [...this.app.appManager.applications.values()];
196
- for (const subApp of subApps) {
197
- const collectionName = removedField.collection.name;
198
- const collection = subApp.db.getCollection(collectionName);
199
- if (!collection) {
200
- subApp.log.warn(`collection ${collectionName} not found in ${subApp.name}`);
201
- continue;
202
- }
203
- collection.removeField(removedField.name);
204
- }
180
+ });
205
181
  });
206
182
  this.app.db.on(`afterRemoveCollection`, (collection) => {
207
- const subApps = [...this.app.appManager.applications.values()];
183
+ const subApps = [...server.AppSupervisor.getInstance().subApps()];
208
184
  for (const subApp of subApps) {
209
185
  subApp.db.removeCollection(collection.name);
210
186
  }
@@ -235,7 +211,7 @@ class MultiAppShareCollectionPlugin extends server.Plugin {
235
211
  ...mainAppDbConfig,
236
212
  schema: appName
237
213
  };
238
- const plugins = [...mainApp.pm.getPlugins().keys()].filter(
214
+ const plugins = [...mainApp.pm.getAliases()].filter(
239
215
  (name) => name !== "multi-app-manager" && name !== "multi-app-share-collection"
240
216
  );
241
217
  return {
@@ -268,9 +244,6 @@ class MultiAppShareCollectionPlugin extends server.Plugin {
268
244
  await this.app.db.sequelize.query(`CREATE SCHEMA IF NOT EXISTS ${schema}`);
269
245
  });
270
246
  }
271
- requiredPlugins() {
272
- return ["multi-app-manager"];
273
- }
274
247
  }
275
248
  var plugin_default = MultiAppShareCollectionPlugin;
276
249
 
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "displayName.zh-CN": "多应用数据共享",
5
5
  "description": "multi app share collection",
6
6
  "description.zh-CN": "多应用数据共享",
7
- "version": "0.12.0-alpha.5",
7
+ "version": "0.13.0-alpha.1",
8
8
  "main": "./dist/server/index.js",
9
9
  "devDependencies": {
10
10
  "@formily/react": "2.x",
@@ -23,5 +23,5 @@
23
23
  "@nocobase/test": "0.x",
24
24
  "@nocobase/utils": "0.x"
25
25
  },
26
- "gitHead": "689cc16e83361c4d0b91907e0deac30bdb907692"
26
+ "gitHead": "0ebd4e85a1b0b0d0943768ab6cb5c3d824562239"
27
27
  }