@nocobase/plugin-data-source-manager 0.20.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.
Files changed (119) hide show
  1. package/README.md +1 -0
  2. package/client.d.ts +2 -0
  3. package/client.js +1 -0
  4. package/dist/client/DatabaseConnectionProvider.d.ts +3 -0
  5. package/dist/client/ThridDataSource.d.ts +4 -0
  6. package/dist/client/component/BreadcumbTitle.d.ts +2 -0
  7. package/dist/client/component/CollectionsManager/AddFieldAction.d.ts +5 -0
  8. package/dist/client/component/CollectionsManager/CollectionFields.d.ts +8 -0
  9. package/dist/client/component/CollectionsManager/ConfigurationTable.d.ts +2 -0
  10. package/dist/client/component/CollectionsManager/ConfigurationTabs.d.ts +2 -0
  11. package/dist/client/component/CollectionsManager/EditCollectionAction.d.ts +6 -0
  12. package/dist/client/component/CollectionsManager/EditFieldAction.d.ts +2 -0
  13. package/dist/client/component/CollectionsManager/components/CollectionFieldInterfaceSelect.d.ts +2 -0
  14. package/dist/client/component/CollectionsManager/components/CollectionName.d.ts +1 -0
  15. package/dist/client/component/CollectionsManager/components/FieldTitleInput.d.ts +2 -0
  16. package/dist/client/component/CollectionsManager/components/FieldType.d.ts +2 -0
  17. package/dist/client/component/CollectionsManager/components/TitleField.d.ts +2 -0
  18. package/dist/client/component/CollectionsManager/components/UnSupportFields.d.ts +2 -0
  19. package/dist/client/component/CollectionsManager/components/index.d.ts +6 -0
  20. package/dist/client/component/CollectionsManager/hooks.d.ts +12 -0
  21. package/dist/client/component/CollectionsManager/index.d.ts +2 -0
  22. package/dist/client/component/CollectionsManager/schema/collectionFields.d.ts +4 -0
  23. package/dist/client/component/CollectionsManager/schema/collections.d.ts +64 -0
  24. package/dist/client/component/CreateDatabaseConnectAction.d.ts +2 -0
  25. package/dist/client/component/DatabaseConnectionManager.d.ts +2 -0
  26. package/dist/client/component/EditDatabaseConnectionAction.d.ts +2 -0
  27. package/dist/client/component/MainDataSourceManager/Configuration/AddCategoryAction.d.ts +3 -0
  28. package/dist/client/component/MainDataSourceManager/Configuration/CollectionFields.d.ts +2 -0
  29. package/dist/client/component/MainDataSourceManager/Configuration/CollectionFieldsTable.d.ts +15 -0
  30. package/dist/client/component/MainDataSourceManager/Configuration/CollectionFieldsTableArray.d.ts +8 -0
  31. package/dist/client/component/MainDataSourceManager/Configuration/ConfigurationTable.d.ts +2 -0
  32. package/dist/client/component/MainDataSourceManager/Configuration/ConfigurationTabs.d.ts +2 -0
  33. package/dist/client/component/MainDataSourceManager/Configuration/EditCategoryAction.d.ts +3 -0
  34. package/dist/client/component/MainDataSourceManager/Configuration/index.d.ts +6 -0
  35. package/dist/client/component/MainDataSourceManager/Configuration/interfaces.d.ts +16 -0
  36. package/dist/client/component/MainDataSourceManager/Configuration/schemas/collectionFields.d.ts +5 -0
  37. package/dist/client/component/MainDataSourceManager/Configuration/schemas/collections.d.ts +7 -0
  38. package/dist/client/component/MainDataSourceManager/index.d.ts +2 -0
  39. package/dist/client/component/PermissionManager/DataSourceTable.d.ts +3 -0
  40. package/dist/client/component/PermissionManager/PermisionProvider.d.ts +5 -0
  41. package/dist/client/component/PermissionManager/RoleConfigure.d.ts +2 -0
  42. package/dist/client/component/PermissionManager/RolesResourcesActions.d.ts +3 -0
  43. package/dist/client/component/PermissionManager/ScopeSelect.d.ts +3 -0
  44. package/dist/client/component/PermissionManager/StrategyActions.d.ts +2 -0
  45. package/dist/client/component/PermissionManager/index.d.ts +3 -0
  46. package/dist/client/component/PermissionManager/schemas/dataSourceTable.d.ts +2 -0
  47. package/dist/client/component/PermissionManager/schemas/roleCollections.d.ts +2 -0
  48. package/dist/client/component/PermissionManager/schemas/scopes.d.ts +122 -0
  49. package/dist/client/component/PermissionManager/schemas/useRoleResourceValues.d.ts +8 -0
  50. package/dist/client/component/PermissionManager/schemas/useSaveRoleResourceAction.d.ts +3 -0
  51. package/dist/client/component/PermissionManager/style.d.ts +1 -0
  52. package/dist/client/component/ViewDatabaseConnectionAction.d.ts +2 -0
  53. package/dist/client/constant.d.ts +1 -0
  54. package/dist/client/hooks/index.d.ts +6 -0
  55. package/dist/client/index.d.ts +9 -0
  56. package/dist/client/index.js +67 -0
  57. package/dist/client/locale/index.d.ts +2 -0
  58. package/dist/client/schema/index.d.ts +7 -0
  59. package/dist/externalVersion.js +24 -0
  60. package/dist/index.d.ts +2 -0
  61. package/dist/index.js +39 -0
  62. package/dist/locale/zh-CN.json +33 -0
  63. package/dist/server/collections/data-sources-collections.d.ts +2 -0
  64. package/dist/server/collections/data-sources-collections.js +60 -0
  65. package/dist/server/collections/data-sources-fields.d.ts +2 -0
  66. package/dist/server/collections/data-sources-fields.js +86 -0
  67. package/dist/server/collections/data-sources-roles-resources-actions.d.ts +2 -0
  68. package/dist/server/collections/data-sources-roles-resources-actions.js +52 -0
  69. package/dist/server/collections/data-sources-roles-resources-scopes.d.ts +2 -0
  70. package/dist/server/collections/data-sources-roles-resources-scopes.js +56 -0
  71. package/dist/server/collections/data-sources-roles-resources.d.ts +2 -0
  72. package/dist/server/collections/data-sources-roles-resources.js +62 -0
  73. package/dist/server/collections/data-sources-roles.d.ts +2 -0
  74. package/dist/server/collections/data-sources-roles.js +53 -0
  75. package/dist/server/collections/data-sources.d.ts +2 -0
  76. package/dist/server/collections/data-sources.js +70 -0
  77. package/dist/server/errors/type-infer-error.d.ts +2 -0
  78. package/dist/server/errors/type-infer-error.js +28 -0
  79. package/dist/server/index.d.ts +2 -0
  80. package/dist/server/index.js +39 -0
  81. package/dist/server/migrations/20240301164024-migrate-acl-resources.d.ts +7 -0
  82. package/dist/server/migrations/20240301164024-migrate-acl-resources.js +117 -0
  83. package/dist/server/models/connections-roles-resources-action.d.ts +3 -0
  84. package/dist/server/models/connections-roles-resources-action.js +29 -0
  85. package/dist/server/models/connections-roles-resources.d.ts +3 -0
  86. package/dist/server/models/connections-roles-resources.js +29 -0
  87. package/dist/server/models/data-source.d.ts +17 -0
  88. package/dist/server/models/data-source.js +166 -0
  89. package/dist/server/models/data-sources-collection-model.d.ts +7 -0
  90. package/dist/server/models/data-sources-collection-model.js +37 -0
  91. package/dist/server/models/data-sources-field-model.d.ts +10 -0
  92. package/dist/server/models/data-sources-field-model.js +48 -0
  93. package/dist/server/models/data-sources-roles-model.d.ts +11 -0
  94. package/dist/server/models/data-sources-roles-model.js +57 -0
  95. package/dist/server/plugin.d.ts +13 -0
  96. package/dist/server/plugin.js +416 -0
  97. package/dist/server/resourcers/data-sources-collections-fields.d.ts +11 -0
  98. package/dist/server/resourcers/data-sources-collections-fields.js +131 -0
  99. package/dist/server/resourcers/data-sources-collections.d.ts +8 -0
  100. package/dist/server/resourcers/data-sources-collections.js +106 -0
  101. package/dist/server/resourcers/data-sources-resources.d.ts +9 -0
  102. package/dist/server/resourcers/data-sources-resources.js +88 -0
  103. package/dist/server/resourcers/data-sources-roles.d.ts +8 -0
  104. package/dist/server/resourcers/data-sources-roles.js +72 -0
  105. package/dist/server/resourcers/roles-data-sources-collections.d.ts +7 -0
  106. package/dist/server/resourcers/roles-data-sources-collections.js +101 -0
  107. package/dist/server/services/collection-manager.d.ts +6 -0
  108. package/dist/server/services/collection-manager.js +33 -0
  109. package/dist/server/services/database-introspector.d.ts +70 -0
  110. package/dist/server/services/database-introspector.js +235 -0
  111. package/dist/server/services/full-data-repository.d.ts +8 -0
  112. package/dist/server/services/full-data-repository.js +51 -0
  113. package/dist/server/services/type-interface-map.d.ts +183 -0
  114. package/dist/server/services/type-interface-map.js +243 -0
  115. package/dist/server/utils.d.ts +1 -0
  116. package/dist/server/utils.js +38 -0
  117. package/package.json +18 -0
  118. package/server.d.ts +2 -0
  119. package/server.js +1 -0
@@ -0,0 +1,416 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
24
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
25
+ mod
26
+ ));
27
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
+ var plugin_exports = {};
29
+ __export(plugin_exports, {
30
+ PluginDataSourceManagerServer: () => PluginDataSourceManagerServer,
31
+ default: () => plugin_default
32
+ });
33
+ module.exports = __toCommonJS(plugin_exports);
34
+ var import_server = require("@nocobase/server");
35
+ var import_path = require("path");
36
+ var import_data_sources_collection_model = require("./models/data-sources-collection-model");
37
+ var import_data_sources_field_model = require("./models/data-sources-field-model");
38
+ var import_data_sources_collections = __toESM(require("./resourcers/data-sources-collections"));
39
+ var import_data_sources_collections_fields = __toESM(require("./resourcers/data-sources-collections-fields"));
40
+ var import_data_sources_resources = __toESM(require("./resourcers/data-sources-resources"));
41
+ var import_data_sources_roles = __toESM(require("./resourcers/data-sources-roles"));
42
+ var import_roles_data_sources_collections = require("./resourcers/roles-data-sources-collections");
43
+ var import_lodash = __toESM(require("lodash"));
44
+ var import_connections_roles_resources = require("./models/connections-roles-resources");
45
+ var import_connections_roles_resources_action = require("./models/connections-roles-resources-action");
46
+ var import_data_source = require("./models/data-source");
47
+ var import_data_sources_roles_model = require("./models/data-sources-roles-model");
48
+ const canRefreshStatus = ["loaded", "loading-failed", "reloading-failed"];
49
+ class PluginDataSourceManagerServer extends import_server.Plugin {
50
+ dataSourceErrors = {};
51
+ dataSourceStatus = {};
52
+ async beforeLoad() {
53
+ this.app.db.registerModels({
54
+ DataSourcesCollectionModel: import_data_sources_collection_model.DataSourcesCollectionModel,
55
+ DataSourcesFieldModel: import_data_sources_field_model.DataSourcesFieldModel,
56
+ DataSourcesRolesModel: import_data_sources_roles_model.DataSourcesRolesModel,
57
+ DataSourcesRolesResourcesModel: import_connections_roles_resources.DataSourcesRolesResourcesModel,
58
+ DataSourcesRolesResourcesActionModel: import_connections_roles_resources_action.DataSourcesRolesResourcesActionModel,
59
+ DataSourceModel: import_data_source.DataSourceModel
60
+ });
61
+ this.app.db.on("dataSourcesFields.beforeCreate", async (model, options) => {
62
+ const validatePresent = (name) => {
63
+ if (!model.get(name)) {
64
+ throw new Error(`"${name}" is required`);
65
+ }
66
+ };
67
+ const validatePresents = (names) => {
68
+ names.forEach((name) => validatePresent(name));
69
+ };
70
+ const type = model.get("type");
71
+ if (type === "belongsTo") {
72
+ validatePresents(["foreignKey", "targetKey", "target"]);
73
+ }
74
+ if (type === "hasMany") {
75
+ validatePresents(["foreignKey", "sourceKey", "target"]);
76
+ }
77
+ if (type == "hasOne") {
78
+ validatePresents(["foreignKey", "sourceKey", "target"]);
79
+ }
80
+ if (type === "belongsToMany") {
81
+ validatePresents(["foreignKey", "otherKey", "sourceKey", "targetKey", "through", "target"]);
82
+ }
83
+ });
84
+ this.app.db.on("dataSources.beforeCreate", async (model, options) => {
85
+ this.dataSourceStatus[model.get("key")] = "loading";
86
+ });
87
+ this.app.db.on("dataSources.beforeSave", async (model) => {
88
+ if (model.changed("options") && !model.isMainRecord()) {
89
+ const dataSourceOptions = model.get("options");
90
+ const type = model.get("type");
91
+ const klass = this.app.dataSourceManager.factory.getClass(type);
92
+ try {
93
+ await klass.testConnection(dataSourceOptions);
94
+ } catch (error) {
95
+ throw new Error(`Test connection failed: ${error.message}`);
96
+ }
97
+ }
98
+ });
99
+ this.app.db.on("dataSources.afterSave", async (model, options) => {
100
+ if (model.changed("options") && !model.isMainRecord()) {
101
+ model.loadIntoApplication({
102
+ app: this.app
103
+ });
104
+ }
105
+ });
106
+ this.app.db.on("dataSources.afterCreate", async (model, options) => {
107
+ if (model.isMainRecord()) {
108
+ return;
109
+ }
110
+ const { transaction } = options;
111
+ await this.app.db.getRepository("dataSourcesRolesResourcesScopes").create({
112
+ values: {
113
+ dataSourceKey: model.get("key"),
114
+ key: "all",
115
+ name: '{{t("All records")}}',
116
+ scope: {}
117
+ },
118
+ transaction
119
+ });
120
+ });
121
+ const app = this.app;
122
+ this.app.use(async (ctx, next) => {
123
+ await next();
124
+ if (!ctx.action) {
125
+ return;
126
+ }
127
+ const { actionName, resourceName, params } = ctx.action;
128
+ if (resourceName === "dataSources" && actionName == "list") {
129
+ let dataPath = "body";
130
+ if (Array.isArray(ctx.body["data"])) {
131
+ dataPath = "body.data";
132
+ }
133
+ const items = import_lodash.default.get(ctx, dataPath);
134
+ import_lodash.default.set(
135
+ ctx,
136
+ dataPath,
137
+ items.map((item) => {
138
+ const data = item.toJSON();
139
+ if (item.isMainRecord()) {
140
+ data["status"] = "loaded";
141
+ return data;
142
+ }
143
+ const dataSourceStatus = this.dataSourceStatus[item.get("key")];
144
+ data["status"] = dataSourceStatus;
145
+ if (dataSourceStatus === "loading-failed" || dataSourceStatus === "reloading-failed") {
146
+ data["errorMessage"] = this.dataSourceErrors[item.get("key")].message;
147
+ }
148
+ return data;
149
+ })
150
+ );
151
+ }
152
+ });
153
+ const plugin = this;
154
+ const mapDataSourceWithCollection = (dataSourceModel, appendCollections = true) => {
155
+ const dataSourceStatus = plugin.dataSourceStatus[dataSourceModel.get("key")];
156
+ const item = {
157
+ key: dataSourceModel.get("key"),
158
+ displayName: dataSourceModel.get("displayName"),
159
+ status: dataSourceStatus
160
+ };
161
+ if (dataSourceStatus === "loading-failed" || dataSourceStatus === "reloading-failed") {
162
+ item["errorMessage"] = plugin.dataSourceErrors[dataSourceModel.get("key")].message;
163
+ }
164
+ const dataSource = app.dataSourceManager.dataSources.get(dataSourceModel.get("key"));
165
+ if (!dataSource) {
166
+ return item;
167
+ }
168
+ if (appendCollections) {
169
+ const collections = dataSource.collectionManager.getCollections();
170
+ item.collections = collections.map((collection) => {
171
+ const collectionOptions = collection.options;
172
+ const fields = [...collection.fields.values()].map((field) => field.options);
173
+ return {
174
+ ...collectionOptions,
175
+ fields
176
+ };
177
+ });
178
+ }
179
+ return item;
180
+ };
181
+ this.app.resourcer.use(async (ctx, next) => {
182
+ if (!ctx.action) {
183
+ await next();
184
+ return;
185
+ }
186
+ const { actionName, resourceName, params } = ctx.action;
187
+ if (resourceName === "dataSources" && actionName == "list") {
188
+ if (!params.sort) {
189
+ params.sort = ["-fixed", "createdAt"];
190
+ }
191
+ }
192
+ await next();
193
+ });
194
+ this.app.use(async (ctx, next) => {
195
+ await next();
196
+ if (!ctx.action) {
197
+ return;
198
+ }
199
+ const { actionName, resourceName, params } = ctx.action;
200
+ if (resourceName === "dataSources" && actionName == "get") {
201
+ let appendCollections = false;
202
+ const appends = ctx.action.params.appends;
203
+ if (appends && appends.includes("collections")) {
204
+ appendCollections = true;
205
+ }
206
+ if (ctx.body.data) {
207
+ ctx.body.data = mapDataSourceWithCollection(ctx.body.data, appendCollections);
208
+ } else {
209
+ ctx.body = mapDataSourceWithCollection(ctx.body, appendCollections);
210
+ }
211
+ }
212
+ });
213
+ this.app.actions({
214
+ async ["dataSources:listEnabled"](ctx, next) {
215
+ const dataSources = await ctx.db.getRepository("dataSources").find({
216
+ filter: {
217
+ enabled: true,
218
+ "type.$ne": "main"
219
+ }
220
+ });
221
+ ctx.body = dataSources.map((dataSourceModel) => {
222
+ return mapDataSourceWithCollection(dataSourceModel);
223
+ });
224
+ await next();
225
+ },
226
+ async ["dataSources:testConnection"](ctx, next) {
227
+ const { values } = ctx.action.params;
228
+ const { options, type } = values;
229
+ const klass = ctx.app.dataSourceManager.factory.getClass(type);
230
+ try {
231
+ await klass.testConnection(options);
232
+ } catch (error) {
233
+ throw new Error(`Test connection failed: ${error.message}`);
234
+ }
235
+ ctx.body = {
236
+ success: true
237
+ };
238
+ await next();
239
+ },
240
+ async ["dataSources:refresh"](ctx, next) {
241
+ const { filterByTk, clientStatus } = ctx.action.params;
242
+ const dataSourceModel = await ctx.db.getRepository("dataSources").findOne({
243
+ filter: {
244
+ key: filterByTk
245
+ }
246
+ });
247
+ const currentStatus = plugin.dataSourceStatus[filterByTk];
248
+ if (canRefreshStatus.includes(currentStatus) && (clientStatus ? clientStatus && canRefreshStatus.includes(clientStatus) : true)) {
249
+ dataSourceModel.loadIntoApplication({
250
+ app: ctx.app
251
+ });
252
+ }
253
+ ctx.body = {
254
+ status: plugin.dataSourceStatus[filterByTk]
255
+ };
256
+ await next();
257
+ }
258
+ });
259
+ this.app.resourcer.define(import_data_sources_collections.default);
260
+ this.app.resourcer.define(import_data_sources_collections_fields.default);
261
+ this.app.resourcer.define(import_roles_data_sources_collections.rolesRemoteCollectionsResourcer);
262
+ this.app.resourcer.define(import_data_sources_roles.default);
263
+ this.app.resourcer.define(import_data_sources_resources.default);
264
+ this.app.resourcer.define({
265
+ name: "dataSources"
266
+ });
267
+ this.app.db.on("dataSourcesFields.afterSave", async (model) => {
268
+ model.load({
269
+ app: this.app
270
+ });
271
+ });
272
+ this.app.db.on("dataSourcesFields.afterDestroy", async (model) => {
273
+ model.unload({
274
+ app: this.app
275
+ });
276
+ });
277
+ this.app.db.on("dataSourcesCollections.afterSave", async (model) => {
278
+ model.load({
279
+ app: this.app
280
+ });
281
+ });
282
+ this.app.on("afterStart", async (app2) => {
283
+ const dataSourcesRecords = await this.app.db.getRepository("dataSources").find({
284
+ filter: {
285
+ enabled: true
286
+ }
287
+ });
288
+ const loadPromises = dataSourcesRecords.map((dataSourceRecord) => {
289
+ if (dataSourceRecord.isMainRecord()) {
290
+ return dataSourceRecord.loadIntoACL({ app: app2, acl: app2.acl });
291
+ }
292
+ return dataSourceRecord.loadIntoApplication({ app: app2, loadAtAfterStart: true });
293
+ });
294
+ this.app.setMaintainingMessage("Loading data sources...");
295
+ await Promise.all(loadPromises);
296
+ });
297
+ this.app.db.on("dataSourcesRolesResources.afterSaveWithAssociations", async (model, options) => {
298
+ const { transaction } = options;
299
+ const pluginACL = this.app.pm.get("acl");
300
+ const dataSource = this.app.dataSourceManager.dataSources.get(model.get("dataSourceKey"));
301
+ await model.writeToACL({
302
+ acl: dataSource.acl,
303
+ associationFieldsActions: pluginACL.associationFieldsActions,
304
+ transaction,
305
+ grantHelper: pluginACL.grantHelper
306
+ });
307
+ });
308
+ this.app.db.on("dataSourcesRolesResourcesActions.afterUpdateWithAssociations", async (model, options) => {
309
+ const { transaction } = options;
310
+ const resource = await model.getResource({
311
+ transaction
312
+ });
313
+ const pluginACL = this.app.pm.get("acl");
314
+ const dataSource = this.app.dataSourceManager.dataSources.get(resource.get("dataSourceKey"));
315
+ await resource.writeToACL({
316
+ acl: dataSource.acl,
317
+ associationFieldsActions: pluginACL.associationFieldsActions,
318
+ transaction,
319
+ grantHelper: pluginACL.grantHelper
320
+ });
321
+ });
322
+ this.app.db.on("dataSourcesRolesResources.afterDestroy", async (model, options) => {
323
+ const dataSource = this.app.dataSourceManager.dataSources.get(model.get("dataSourceKey"));
324
+ const roleName = model.get("roleName");
325
+ const role = dataSource.acl.getRole(roleName);
326
+ if (role) {
327
+ role.revokeResource(model.get("name"));
328
+ }
329
+ });
330
+ this.app.db.on("dataSourcesRoles.afterSave", async (model, options) => {
331
+ const { transaction } = options;
332
+ const pluginACL = this.app.pm.get("acl");
333
+ const dataSource = this.app.dataSourceManager.dataSources.get(model.get("dataSourceKey"));
334
+ await model.writeToAcl({
335
+ grantHelper: pluginACL.grantHelper,
336
+ associationFieldsActions: pluginACL.associationFieldsActions,
337
+ acl: dataSource.acl,
338
+ transaction
339
+ });
340
+ });
341
+ this.app.on("acl:writeResources", async ({ roleName, transaction }) => {
342
+ const dataSource = this.app.dataSourceManager.dataSources.get("main");
343
+ const pluginACL = this.app.pm.get("acl");
344
+ const dataSourceRole = await this.app.db.getRepository("dataSourcesRoles").findOne({
345
+ filter: {
346
+ dataSourceKey: "main",
347
+ roleName
348
+ },
349
+ transaction
350
+ });
351
+ await dataSourceRole.writeToAcl({
352
+ grantHelper: pluginACL.grantHelper,
353
+ associationFieldsActions: pluginACL.associationFieldsActions,
354
+ acl: dataSource.acl,
355
+ transaction
356
+ });
357
+ });
358
+ this.app.resourcer.use(async (ctx, next) => {
359
+ const action = ctx.action;
360
+ await next();
361
+ const { resourceName, actionName } = action.params;
362
+ if (resourceName === "roles" && actionName == "check") {
363
+ const roleName = ctx.state.currentRole;
364
+ const dataSources = await ctx.db.getRepository("dataSources").find();
365
+ ctx.bodyMeta = {
366
+ dataSources: dataSources.reduce((carry, dataSourceModel) => {
367
+ const dataSource = this.app.dataSourceManager.dataSources.get(dataSourceModel.get("key"));
368
+ if (!dataSource) {
369
+ return carry;
370
+ }
371
+ const dataSourceStatus = this.dataSourceStatus[dataSourceModel.get("key")];
372
+ if (dataSourceStatus !== "loaded") {
373
+ return carry;
374
+ }
375
+ const aclInstance = dataSource.acl;
376
+ const roleInstance = aclInstance.getRole(roleName);
377
+ const dataObj = {
378
+ strategy: {},
379
+ resources: roleInstance ? [...roleInstance.resources.keys()] : [],
380
+ actions: {}
381
+ };
382
+ if (roleInstance) {
383
+ const data = roleInstance.toJSON();
384
+ dataObj["name"] = data["name"];
385
+ dataObj["strategy"] = data["strategy"];
386
+ dataObj["actions"] = data["actions"];
387
+ }
388
+ carry[dataSourceModel.get("key")] = dataObj;
389
+ return carry;
390
+ }, {})
391
+ };
392
+ }
393
+ });
394
+ this.app.acl.registerSnippet({
395
+ name: `pm.${this.name}`,
396
+ actions: ["dataSources:*", "roles.dataSourceResources"]
397
+ });
398
+ this.app.acl.allow("dataSources", "listEnabled", "loggedIn");
399
+ this.app.acl.allow("dataSources", "get", "loggedIn");
400
+ this.app.acl.addFixedParams("dataSources", "destroy", () => {
401
+ return {
402
+ filter: {
403
+ "key.$ne": "main"
404
+ }
405
+ };
406
+ });
407
+ }
408
+ async load() {
409
+ await this.importCollections((0, import_path.resolve)(__dirname, "collections"));
410
+ }
411
+ }
412
+ var plugin_default = PluginDataSourceManagerServer;
413
+ // Annotate the CommonJS export names for ESM import in node:
414
+ 0 && (module.exports = {
415
+ PluginDataSourceManagerServer
416
+ });
@@ -0,0 +1,11 @@
1
+ declare const _default: {
2
+ name: string;
3
+ actions: {
4
+ list(ctx: any, next: any): Promise<void>;
5
+ get(ctx: any, next: any): Promise<void>;
6
+ update(ctx: any, next: any): Promise<void>;
7
+ create(ctx: any, next: any): Promise<void>;
8
+ destroy(ctx: any, next: any): Promise<void>;
9
+ };
10
+ };
11
+ export default _default;
@@ -0,0 +1,131 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
24
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
25
+ mod
26
+ ));
27
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
+ var data_sources_collections_fields_exports = {};
29
+ __export(data_sources_collections_fields_exports, {
30
+ default: () => data_sources_collections_fields_default
31
+ });
32
+ module.exports = __toCommonJS(data_sources_collections_fields_exports);
33
+ var import_lodash = __toESM(require("lodash"));
34
+ var data_sources_collections_fields_default = {
35
+ name: "dataSourcesCollections.fields",
36
+ actions: {
37
+ async list(ctx, next) {
38
+ const { associatedIndex: collectionNameWithDataSourceKey } = ctx.action.params;
39
+ const [dataSourceKey, collectionName] = collectionNameWithDataSourceKey.split(".");
40
+ const dataSource = ctx.app.dataSourceManager.dataSources.get(dataSourceKey);
41
+ const collection = dataSource.collectionManager.getCollection(collectionName);
42
+ const fields = collection.getFields();
43
+ ctx.body = import_lodash.default.sortBy(
44
+ fields.map((field) => field.options),
45
+ "name"
46
+ );
47
+ await next();
48
+ },
49
+ async get(ctx, next) {
50
+ const { associatedIndex: collectionNameWithDataSourceKey, filterByTk: name } = ctx.action.params;
51
+ const [dataSourceKey, collectionName] = collectionNameWithDataSourceKey.split(".");
52
+ const dataSource = ctx.app.dataSourceManager.dataSources.get(dataSourceKey);
53
+ const collection = dataSource.collectionManager.getCollection(collectionName);
54
+ const field = collection.getField(name);
55
+ ctx.body = field.options;
56
+ await next();
57
+ },
58
+ async update(ctx, next) {
59
+ const { associatedIndex: collectionNameWithDataSourceKey, filterByTk: name, values } = ctx.action.params;
60
+ const [dataSourceKey, collectionName] = collectionNameWithDataSourceKey.split(".");
61
+ const mainDb = ctx.app.db;
62
+ let fieldRecord = await mainDb.getRepository("dataSourcesFields").findOne({
63
+ filter: {
64
+ name,
65
+ collectionName,
66
+ dataSourceKey
67
+ }
68
+ });
69
+ if (!fieldRecord) {
70
+ fieldRecord = await mainDb.getRepository("dataSourcesFields").create({
71
+ values: {
72
+ ...values,
73
+ name,
74
+ collectionName,
75
+ dataSourceKey
76
+ }
77
+ });
78
+ } else {
79
+ await fieldRecord.update({
80
+ ...values
81
+ });
82
+ }
83
+ const field = ctx.app.dataSourceManager.dataSources.get(dataSourceKey).collectionManager.getCollection(collectionName).getField(name);
84
+ ctx.body = field.options;
85
+ await next();
86
+ },
87
+ async create(ctx, next) {
88
+ const { associatedIndex: collectionNameWithDataSourceKey, values } = ctx.action.params;
89
+ const [dataSourceKey, collectionName] = collectionNameWithDataSourceKey.split(".");
90
+ const mainDb = ctx.app.db;
91
+ const name = values.name;
92
+ if (await mainDb.getRepository("dataSourcesFields").findOne({
93
+ filter: {
94
+ name,
95
+ collectionName,
96
+ dataSourceKey
97
+ }
98
+ })) {
99
+ throw new Error(
100
+ `Field name ${name} already exists in collection ${collectionName} of data source ${dataSourceKey}`
101
+ );
102
+ }
103
+ const fieldRecord = await mainDb.getRepository("dataSourcesFields").create({
104
+ values: {
105
+ ...values,
106
+ collectionName,
107
+ dataSourceKey
108
+ }
109
+ });
110
+ ctx.body = fieldRecord.toJSON();
111
+ await next();
112
+ },
113
+ async destroy(ctx, next) {
114
+ const { associatedIndex: collectionNameWithDataSourceKey, filterByTk: name } = ctx.action.params;
115
+ const [dataSourceKey, collectionName] = collectionNameWithDataSourceKey.split(".");
116
+ const mainDb = ctx.app.db;
117
+ const fieldRecord = await mainDb.getRepository("dataSourcesFields").findOne({
118
+ filter: {
119
+ name,
120
+ collectionName,
121
+ dataSourceKey
122
+ }
123
+ });
124
+ if (fieldRecord) {
125
+ await fieldRecord.destroy();
126
+ }
127
+ ctx.body = "ok";
128
+ await next();
129
+ }
130
+ }
131
+ };
@@ -0,0 +1,8 @@
1
+ declare const _default: {
2
+ name: string;
3
+ actions: {
4
+ list(ctx: any, next: any): Promise<void>;
5
+ update(ctx: any, next: any): Promise<void>;
6
+ };
7
+ };
8
+ export default _default;
@@ -0,0 +1,106 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
24
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
25
+ mod
26
+ ));
27
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
+ var data_sources_collections_exports = {};
29
+ __export(data_sources_collections_exports, {
30
+ default: () => data_sources_collections_default
31
+ });
32
+ module.exports = __toCommonJS(data_sources_collections_exports);
33
+ var import_lodash = __toESM(require("lodash"));
34
+ var data_sources_collections_default = {
35
+ name: "dataSources.collections",
36
+ actions: {
37
+ async list(ctx, next) {
38
+ var _a, _b;
39
+ const params = ctx.action.params;
40
+ const { associatedIndex: dataSourceKey } = params;
41
+ const dataSource = ctx.app.dataSourceManager.dataSources.get(dataSourceKey);
42
+ if (!dataSource) {
43
+ throw new Error(`dataSource ${dataSourceKey} not found`);
44
+ }
45
+ const { paginate, filter } = ctx.action.params;
46
+ const filterTitle = (_a = import_lodash.default.get(filter, "$and.0.title.$includes")) == null ? void 0 : _a.toLowerCase();
47
+ const filterName = (_b = import_lodash.default.get(filter, "$and.0.name.$includes")) == null ? void 0 : _b.toLowerCase();
48
+ const collections = import_lodash.default.sortBy(
49
+ dataSource.collectionManager.getCollections().filter((collection) => {
50
+ var _a2;
51
+ return (!filterTitle || ((_a2 = import_lodash.default.get(collection, "options.title")) == null ? void 0 : _a2.toLowerCase().includes(filterTitle))) && (!filterName || collection.options.name.toLowerCase().includes(filterName));
52
+ }),
53
+ "name"
54
+ );
55
+ const mapCollection = (collections2) => {
56
+ return collections2.map((collection) => {
57
+ return {
58
+ ...collection.options,
59
+ fields: collection.getFields().map((field) => field.options)
60
+ };
61
+ });
62
+ };
63
+ if (paginate === false || paginate === "false") {
64
+ ctx.body = mapCollection(collections);
65
+ } else {
66
+ const { page = 1, pageSize = 20 } = ctx.action.params;
67
+ ctx.withoutDataWrapping = true;
68
+ ctx.body = {
69
+ data: mapCollection(collections.slice((page - 1) * pageSize, page * pageSize)),
70
+ meta: {
71
+ count: collections.length,
72
+ page,
73
+ pageSize,
74
+ totalPage: Math.ceil(collections.length / pageSize)
75
+ }
76
+ };
77
+ }
78
+ await next();
79
+ },
80
+ async update(ctx, next) {
81
+ const params = ctx.action.params;
82
+ const { filterByTk: collectionName, associatedIndex: dataSourceKey } = params;
83
+ let dataSourceCollectionRecord = await ctx.db.getRepository("dataSourcesCollections").findOne({
84
+ filter: {
85
+ name: collectionName,
86
+ dataSourceKey
87
+ }
88
+ });
89
+ if (!dataSourceCollectionRecord) {
90
+ dataSourceCollectionRecord = await ctx.db.getRepository("dataSourcesCollections").create({
91
+ values: {
92
+ ...params.values,
93
+ name: collectionName,
94
+ dataSourceKey
95
+ }
96
+ });
97
+ } else {
98
+ await dataSourceCollectionRecord.update({
99
+ ...params.values
100
+ });
101
+ }
102
+ ctx.body = dataSourceCollectionRecord.toJSON();
103
+ await next();
104
+ }
105
+ }
106
+ };
@@ -0,0 +1,9 @@
1
+ declare const _default: {
2
+ name: string;
3
+ actions: {
4
+ create(ctx: any, next: any): Promise<void>;
5
+ update(ctx: any, next: any): Promise<void>;
6
+ get(ctx: any, next: any): Promise<void>;
7
+ };
8
+ };
9
+ export default _default;