@nocobase/plugin-data-source-manager 1.5.0-beta.1 → 1.6.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.
@@ -58,6 +58,81 @@ const canRefreshStatus = ["loaded", "loading-failed", "reloading-failed"];
58
58
  class PluginDataSourceManagerServer extends import_server.Plugin {
59
59
  dataSourceErrors = {};
60
60
  dataSourceStatus = {};
61
+ async handleSyncMessage(message) {
62
+ const { type } = message;
63
+ if (type === "syncRole") {
64
+ const { roleName, dataSourceKey } = message;
65
+ const dataSource = this.app.dataSourceManager.dataSources.get(dataSourceKey);
66
+ const dataSourceRole = await this.app.db.getRepository("dataSourcesRoles").findOne({
67
+ filter: {
68
+ dataSourceKey,
69
+ roleName
70
+ }
71
+ });
72
+ await dataSourceRole.writeToAcl({
73
+ acl: dataSource.acl
74
+ });
75
+ }
76
+ if (type === "syncRoleResource") {
77
+ const { roleName, dataSourceKey, resourceName } = message;
78
+ const dataSource = this.app.dataSourceManager.dataSources.get(dataSourceKey);
79
+ const dataSourceRoleResource = await this.app.db.getRepository("dataSourcesRolesResources").findOne({
80
+ filter: {
81
+ dataSourceKey,
82
+ roleName,
83
+ name: resourceName
84
+ }
85
+ });
86
+ await dataSourceRoleResource.writeToACL({
87
+ acl: dataSource.acl
88
+ });
89
+ }
90
+ if (type === "loadDataSource") {
91
+ const { dataSourceKey } = message;
92
+ const dataSourceModel = await this.app.db.getRepository("dataSources").findOne({
93
+ filter: {
94
+ key: dataSourceKey
95
+ }
96
+ });
97
+ if (!dataSourceModel) {
98
+ return;
99
+ }
100
+ await dataSourceModel.loadIntoApplication({
101
+ app: this.app
102
+ });
103
+ }
104
+ if (type === "loadDataSourceField") {
105
+ const { key } = message;
106
+ const fieldModel = await this.app.db.getRepository("dataSourcesFields").findOne({
107
+ filter: {
108
+ key
109
+ }
110
+ });
111
+ fieldModel.load({
112
+ app: this.app
113
+ });
114
+ }
115
+ if (type === "removeDataSourceCollection") {
116
+ const { dataSourceKey, collectionName } = message;
117
+ const dataSource = this.app.dataSourceManager.dataSources.get(dataSourceKey);
118
+ dataSource.collectionManager.removeCollection(collectionName);
119
+ }
120
+ if (type === "removeDataSourceField") {
121
+ const { key } = message;
122
+ const fieldModel = await this.app.db.getRepository("dataSourcesFields").findOne({
123
+ filter: {
124
+ key
125
+ }
126
+ });
127
+ fieldModel.unload({
128
+ app: this.app
129
+ });
130
+ }
131
+ if (type === "removeDataSource") {
132
+ const { dataSourceKey } = message;
133
+ this.app.dataSourceManager.dataSources.delete(dataSourceKey);
134
+ }
135
+ }
61
136
  dataSourceLoadingProgress = {};
62
137
  async beforeLoad() {
63
138
  this.app.db.registerModels({
@@ -114,6 +189,15 @@ class PluginDataSourceManagerServer extends import_server.Plugin {
114
189
  model.loadIntoApplication({
115
190
  app: this.app
116
191
  });
192
+ this.sendSyncMessage(
193
+ {
194
+ type: "loadDataSource",
195
+ dataSourceKey: model.get("key")
196
+ },
197
+ {
198
+ transaction: options.transaction
199
+ }
200
+ );
117
201
  }
118
202
  });
119
203
  this.app.db.on("dataSources.afterCreate", async (model, options) => {
@@ -241,6 +325,7 @@ class PluginDataSourceManagerServer extends import_server.Plugin {
241
325
  }
242
326
  }
243
327
  });
328
+ const self = this;
244
329
  this.app.actions({
245
330
  async ["dataSources:listEnabled"](ctx, next) {
246
331
  const dataSources = await ctx.db.getRepository("dataSources").find({
@@ -280,6 +365,10 @@ class PluginDataSourceManagerServer extends import_server.Plugin {
280
365
  dataSourceModel.loadIntoApplication({
281
366
  app: ctx.app
282
367
  });
368
+ ctx.app.syncMessageManager.publish(self.name, {
369
+ type: "loadDataSource",
370
+ dataSourceKey: dataSourceModel.get("key")
371
+ });
283
372
  }
284
373
  ctx.body = {
285
374
  status: plugin.dataSourceStatus[filterByTk]
@@ -307,21 +396,49 @@ class PluginDataSourceManagerServer extends import_server.Plugin {
307
396
  model.set("dataSourceKey", collection.get("dataSourceKey"));
308
397
  }
309
398
  });
310
- this.app.db.on("dataSourcesCollections.afterDestroy", async (model) => {
399
+ this.app.db.on("dataSourcesCollections.afterDestroy", async (model, options) => {
311
400
  const dataSource = this.app.dataSourceManager.dataSources.get(model.get("dataSourceKey"));
312
401
  if (dataSource) {
313
402
  dataSource.collectionManager.removeCollection(model.get("name"));
314
403
  }
404
+ this.sendSyncMessage(
405
+ {
406
+ type: "removeDataSourceCollection",
407
+ dataSourceKey: model.get("dataSourceKey"),
408
+ collectionName: model.get("name")
409
+ },
410
+ {
411
+ transaction: options.transaction
412
+ }
413
+ );
315
414
  });
316
- this.app.db.on("dataSourcesFields.afterSaveWithAssociations", async (model) => {
415
+ this.app.db.on("dataSourcesFields.afterSaveWithAssociations", async (model, options) => {
317
416
  model.load({
318
417
  app: this.app
319
418
  });
419
+ this.sendSyncMessage(
420
+ {
421
+ type: "loadDataSourceField",
422
+ key: model.get("key")
423
+ },
424
+ {
425
+ transaction: options.transaction
426
+ }
427
+ );
320
428
  });
321
- this.app.db.on("dataSourcesFields.afterDestroy", async (model) => {
429
+ this.app.db.on("dataSourcesFields.afterDestroy", async (model, options) => {
322
430
  model.unload({
323
431
  app: this.app
324
432
  });
433
+ this.sendSyncMessage(
434
+ {
435
+ type: "removeDataSourceField",
436
+ key: model.get("key")
437
+ },
438
+ {
439
+ transaction: options.transaction
440
+ }
441
+ );
325
442
  });
326
443
  this.app.db.on(
327
444
  "dataSourcesCollections.afterSaveWithAssociations",
@@ -332,8 +449,17 @@ class PluginDataSourceManagerServer extends import_server.Plugin {
332
449
  });
333
450
  }
334
451
  );
335
- this.app.db.on("dataSources.afterDestroy", async (model) => {
452
+ this.app.db.on("dataSources.afterDestroy", async (model, options) => {
336
453
  this.app.dataSourceManager.dataSources.delete(model.get("key"));
454
+ this.sendSyncMessage(
455
+ {
456
+ type: "removeDataSource",
457
+ dataSourceKey: model.get("key")
458
+ },
459
+ {
460
+ transaction: options.transaction
461
+ }
462
+ );
337
463
  });
338
464
  this.app.on("afterStart", async (app2) => {
339
465
  const dataSourcesRecords = await this.app.db.getRepository("dataSources").find({
@@ -359,6 +485,17 @@ class PluginDataSourceManagerServer extends import_server.Plugin {
359
485
  acl: dataSource.acl,
360
486
  transaction
361
487
  });
488
+ this.sendSyncMessage(
489
+ {
490
+ type: "syncRoleResource",
491
+ roleName: model.get("roleName"),
492
+ dataSourceKey: model.get("dataSourceKey"),
493
+ resourceName: model.get("name")
494
+ },
495
+ {
496
+ transaction
497
+ }
498
+ );
362
499
  }
363
500
  );
364
501
  this.app.db.on(
@@ -373,6 +510,17 @@ class PluginDataSourceManagerServer extends import_server.Plugin {
373
510
  acl: dataSource.acl,
374
511
  transaction
375
512
  });
513
+ this.sendSyncMessage(
514
+ {
515
+ type: "syncRoleResource",
516
+ roleName: resource.get("roleName"),
517
+ dataSourceKey: resource.get("dataSourceKey"),
518
+ resourceName: resource.get("name")
519
+ },
520
+ {
521
+ transaction
522
+ }
523
+ );
376
524
  }
377
525
  );
378
526
  this.app.db.on("dataSourcesRolesResources.afterDestroy", async (model, options) => {
@@ -382,6 +530,17 @@ class PluginDataSourceManagerServer extends import_server.Plugin {
382
530
  if (role) {
383
531
  role.revokeResource(model.get("name"));
384
532
  }
533
+ this.sendSyncMessage(
534
+ {
535
+ type: "syncRoleResource",
536
+ roleName,
537
+ dataSourceKey: model.get("dataSourceKey"),
538
+ resourceName: model.get("name")
539
+ },
540
+ {
541
+ transaction: options.transaction
542
+ }
543
+ );
385
544
  });
386
545
  this.app.db.on("dataSourcesRoles.afterSave", async (model, options) => {
387
546
  const { transaction } = options;
@@ -400,6 +559,16 @@ class PluginDataSourceManagerServer extends import_server.Plugin {
400
559
  hooks: false,
401
560
  transaction
402
561
  });
562
+ this.sendSyncMessage(
563
+ {
564
+ type: "syncRole",
565
+ roleName: model.get("roleName"),
566
+ dataSourceKey: model.get("dataSourceKey")
567
+ },
568
+ {
569
+ transaction
570
+ }
571
+ );
403
572
  });
404
573
  this.app.on("acl:writeResources", async ({ roleName, transaction }) => {
405
574
  const dataSource = this.app.dataSourceManager.dataSources.get("main");
@@ -415,7 +584,6 @@ class PluginDataSourceManagerServer extends import_server.Plugin {
415
584
  transaction
416
585
  });
417
586
  });
418
- const self = this;
419
587
  this.app.resourceManager.use(async function appendDataToRolesCheck(ctx, next) {
420
588
  const action = ctx.action;
421
589
  await next();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nocobase/plugin-data-source-manager",
3
- "version": "1.5.0-beta.1",
3
+ "version": "1.6.0-alpha.1",
4
4
  "main": "dist/server/index.js",
5
5
  "displayName": "Data source manager",
6
6
  "displayName.zh-CN": "数据源管理",
@@ -17,5 +17,5 @@
17
17
  "keywords": [
18
18
  "Data model tools"
19
19
  ],
20
- "gitHead": "10c6f1f3d90e91f3aabfa80449c7ef062e90f6af"
20
+ "gitHead": "a4c91015e34ec0c9a427451b5fbdfb5fedc4f3d7"
21
21
  }