@tachybase/module-data-source 0.23.8

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 (122) hide show
  1. package/.turbo/turbo-build.log +14 -0
  2. package/LICENSE +201 -0
  3. package/README.md +1 -0
  4. package/client.d.ts +2 -0
  5. package/client.js +1 -0
  6. package/dist/client/DatabaseConnectionProvider.d.ts +3 -0
  7. package/dist/client/ThridDataSource.d.ts +4 -0
  8. package/dist/client/component/BreadcumbTitle.d.ts +2 -0
  9. package/dist/client/component/CollectionsManager/AddFieldAction.d.ts +5 -0
  10. package/dist/client/component/CollectionsManager/CollectionFields.d.ts +8 -0
  11. package/dist/client/component/CollectionsManager/ConfigurationTable.d.ts +2 -0
  12. package/dist/client/component/CollectionsManager/ConfigurationTabs.d.ts +2 -0
  13. package/dist/client/component/CollectionsManager/EditCollectionAction.d.ts +6 -0
  14. package/dist/client/component/CollectionsManager/EditFieldAction.d.ts +2 -0
  15. package/dist/client/component/CollectionsManager/components/CollectionFieldInterfaceSelect.d.ts +2 -0
  16. package/dist/client/component/CollectionsManager/components/CollectionName.d.ts +1 -0
  17. package/dist/client/component/CollectionsManager/components/FieldTitleInput.d.ts +2 -0
  18. package/dist/client/component/CollectionsManager/components/FieldType.d.ts +2 -0
  19. package/dist/client/component/CollectionsManager/components/TitleField.d.ts +2 -0
  20. package/dist/client/component/CollectionsManager/components/UnSupportFields.d.ts +2 -0
  21. package/dist/client/component/CollectionsManager/components/index.d.ts +6 -0
  22. package/dist/client/component/CollectionsManager/hooks.d.ts +12 -0
  23. package/dist/client/component/CollectionsManager/index.d.ts +2 -0
  24. package/dist/client/component/CollectionsManager/schema/collectionFields.d.ts +4 -0
  25. package/dist/client/component/CollectionsManager/schema/collections.d.ts +8 -0
  26. package/dist/client/component/CreateDatabaseConnectAction.d.ts +2 -0
  27. package/dist/client/component/DatabaseConnectionManager.d.ts +2 -0
  28. package/dist/client/component/DatasourceLink.d.ts +2 -0
  29. package/dist/client/component/EditDatabaseConnectionAction.d.ts +2 -0
  30. package/dist/client/component/MainDataSourceManager/Configuration/AddCategoryAction.d.ts +3 -0
  31. package/dist/client/component/MainDataSourceManager/Configuration/CollectionFields.d.ts +2 -0
  32. package/dist/client/component/MainDataSourceManager/Configuration/CollectionFieldsTable.d.ts +15 -0
  33. package/dist/client/component/MainDataSourceManager/Configuration/CollectionFieldsTableArray.d.ts +8 -0
  34. package/dist/client/component/MainDataSourceManager/Configuration/ConfigurationTable.d.ts +2 -0
  35. package/dist/client/component/MainDataSourceManager/Configuration/ConfigurationTabs.d.ts +2 -0
  36. package/dist/client/component/MainDataSourceManager/Configuration/EditCategoryAction.d.ts +3 -0
  37. package/dist/client/component/MainDataSourceManager/Configuration/index.d.ts +6 -0
  38. package/dist/client/component/MainDataSourceManager/Configuration/interfaces.d.ts +16 -0
  39. package/dist/client/component/MainDataSourceManager/Configuration/schemas/collectionFields.d.ts +5 -0
  40. package/dist/client/component/MainDataSourceManager/Configuration/schemas/collections.d.ts +7 -0
  41. package/dist/client/component/MainDataSourceManager/index.d.ts +2 -0
  42. package/dist/client/component/PermissionManager/DataSourceTable.d.ts +3 -0
  43. package/dist/client/component/PermissionManager/PermisionProvider.d.ts +5 -0
  44. package/dist/client/component/PermissionManager/RoleConfigure.d.ts +2 -0
  45. package/dist/client/component/PermissionManager/RolesResourcesActions.d.ts +3 -0
  46. package/dist/client/component/PermissionManager/ScopeSelect.d.ts +3 -0
  47. package/dist/client/component/PermissionManager/StrategyActions.d.ts +2 -0
  48. package/dist/client/component/PermissionManager/index.d.ts +3 -0
  49. package/dist/client/component/PermissionManager/schemas/dataSourceTable.d.ts +2 -0
  50. package/dist/client/component/PermissionManager/schemas/roleCollections.d.ts +2 -0
  51. package/dist/client/component/PermissionManager/schemas/scopes.d.ts +11 -0
  52. package/dist/client/component/PermissionManager/schemas/useRoleResourceValues.d.ts +8 -0
  53. package/dist/client/component/PermissionManager/schemas/useSaveRoleResourceAction.d.ts +3 -0
  54. package/dist/client/component/PermissionManager/style.d.ts +1 -0
  55. package/dist/client/component/ViewDatabaseConnectionAction.d.ts +2 -0
  56. package/dist/client/constant.d.ts +4 -0
  57. package/dist/client/hooks/index.d.ts +6 -0
  58. package/dist/client/index.d.ts +9 -0
  59. package/dist/client/index.js +56 -0
  60. package/dist/client/locale/index.d.ts +3 -0
  61. package/dist/client/schema/index.d.ts +7 -0
  62. package/dist/externalVersion.js +19 -0
  63. package/dist/index.d.ts +2 -0
  64. package/dist/index.js +39 -0
  65. package/dist/locale/zh-CN.json +35 -0
  66. package/dist/server/collections/data-sources-collections.d.ts +2 -0
  67. package/dist/server/collections/data-sources-collections.js +66 -0
  68. package/dist/server/collections/data-sources-fields.d.ts +2 -0
  69. package/dist/server/collections/data-sources-fields.js +87 -0
  70. package/dist/server/collections/data-sources-roles-resources-actions.d.ts +2 -0
  71. package/dist/server/collections/data-sources-roles-resources-actions.js +52 -0
  72. package/dist/server/collections/data-sources-roles-resources-scopes.d.ts +2 -0
  73. package/dist/server/collections/data-sources-roles-resources-scopes.js +56 -0
  74. package/dist/server/collections/data-sources-roles-resources.d.ts +2 -0
  75. package/dist/server/collections/data-sources-roles-resources.js +62 -0
  76. package/dist/server/collections/data-sources-roles.d.ts +2 -0
  77. package/dist/server/collections/data-sources-roles.js +54 -0
  78. package/dist/server/collections/data-sources.d.ts +2 -0
  79. package/dist/server/collections/data-sources.js +71 -0
  80. package/dist/server/errors/type-infer-error.d.ts +2 -0
  81. package/dist/server/errors/type-infer-error.js +28 -0
  82. package/dist/server/index.d.ts +2 -0
  83. package/dist/server/index.js +39 -0
  84. package/dist/server/migrations/20240301164024-migrate-acl-resources.d.ts +7 -0
  85. package/dist/server/migrations/20240301164024-migrate-acl-resources.js +117 -0
  86. package/dist/server/models/connections-roles-resources-action.d.ts +3 -0
  87. package/dist/server/models/connections-roles-resources-action.js +29 -0
  88. package/dist/server/models/connections-roles-resources.d.ts +3 -0
  89. package/dist/server/models/connections-roles-resources.js +29 -0
  90. package/dist/server/models/data-source.d.ts +18 -0
  91. package/dist/server/models/data-source.js +170 -0
  92. package/dist/server/models/data-sources-collection-model.d.ts +7 -0
  93. package/dist/server/models/data-sources-collection-model.js +37 -0
  94. package/dist/server/models/data-sources-field-model.d.ts +10 -0
  95. package/dist/server/models/data-sources-field-model.js +54 -0
  96. package/dist/server/models/data-sources-roles-model.d.ts +13 -0
  97. package/dist/server/models/data-sources-roles-model.js +58 -0
  98. package/dist/server/plugin.d.ts +13 -0
  99. package/dist/server/plugin.js +448 -0
  100. package/dist/server/resourcers/data-sources-collections-fields.d.ts +11 -0
  101. package/dist/server/resourcers/data-sources-collections-fields.js +131 -0
  102. package/dist/server/resourcers/data-sources-collections.d.ts +8 -0
  103. package/dist/server/resourcers/data-sources-collections.js +106 -0
  104. package/dist/server/resourcers/data-sources-resources.d.ts +9 -0
  105. package/dist/server/resourcers/data-sources-resources.js +88 -0
  106. package/dist/server/resourcers/data-sources-roles.d.ts +8 -0
  107. package/dist/server/resourcers/data-sources-roles.js +89 -0
  108. package/dist/server/resourcers/roles-data-sources-collections.d.ts +7 -0
  109. package/dist/server/resourcers/roles-data-sources-collections.js +102 -0
  110. package/dist/server/services/collection-manager.d.ts +6 -0
  111. package/dist/server/services/collection-manager.js +33 -0
  112. package/dist/server/services/database-introspector.d.ts +70 -0
  113. package/dist/server/services/database-introspector.js +235 -0
  114. package/dist/server/services/full-data-repository.d.ts +8 -0
  115. package/dist/server/services/full-data-repository.js +51 -0
  116. package/dist/server/services/type-interface-map.d.ts +183 -0
  117. package/dist/server/services/type-interface-map.js +243 -0
  118. package/dist/server/utils.d.ts +1 -0
  119. package/dist/server/utils.js +38 -0
  120. package/package.json +36 -0
  121. package/server.d.ts +2 -0
  122. package/server.js +1 -0
@@ -0,0 +1,448 @@
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_path = require("path");
35
+ var import_server = require("@tachybase/server");
36
+ var import_lodash = __toESM(require("lodash"));
37
+ var import_connections_roles_resources = require("./models/connections-roles-resources");
38
+ var import_connections_roles_resources_action = require("./models/connections-roles-resources-action");
39
+ var import_data_source = require("./models/data-source");
40
+ var import_data_sources_collection_model = require("./models/data-sources-collection-model");
41
+ var import_data_sources_field_model = require("./models/data-sources-field-model");
42
+ var import_data_sources_roles_model = require("./models/data-sources-roles-model");
43
+ var import_data_sources_collections = __toESM(require("./resourcers/data-sources-collections"));
44
+ var import_data_sources_collections_fields = __toESM(require("./resourcers/data-sources-collections-fields"));
45
+ var import_data_sources_resources = __toESM(require("./resourcers/data-sources-resources"));
46
+ var import_data_sources_roles = __toESM(require("./resourcers/data-sources-roles"));
47
+ var import_roles_data_sources_collections = require("./resourcers/roles-data-sources-collections");
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(
123
+ async (ctx, next) => {
124
+ await next();
125
+ if (!ctx.action) {
126
+ return;
127
+ }
128
+ const { actionName, resourceName, params } = ctx.action;
129
+ if (resourceName === "dataSources" && actionName === "list") {
130
+ let dataPath = "body";
131
+ if (Array.isArray(ctx.body["data"])) {
132
+ dataPath = "body.data";
133
+ }
134
+ const items = import_lodash.default.get(ctx, dataPath);
135
+ import_lodash.default.set(
136
+ ctx,
137
+ dataPath,
138
+ items.map((item) => {
139
+ const data = item.toJSON();
140
+ if (item.isMainRecord()) {
141
+ data["status"] = "loaded";
142
+ return data;
143
+ }
144
+ const dataSourceStatus = this.dataSourceStatus[item.get("key")];
145
+ data["status"] = dataSourceStatus;
146
+ if (dataSourceStatus === "loading-failed" || dataSourceStatus === "reloading-failed") {
147
+ data["errorMessage"] = this.dataSourceErrors[item.get("key")].message;
148
+ }
149
+ return data;
150
+ })
151
+ );
152
+ }
153
+ },
154
+ { tag: "processDataSourcesList" }
155
+ );
156
+ const plugin = this;
157
+ const mapDataSourceWithCollection = (dataSourceModel, appendCollections = true) => {
158
+ const dataSource = app.dataSourceManager.dataSources.get(dataSourceModel.get("key"));
159
+ const dataSourceStatus = plugin.dataSourceStatus[dataSourceModel.get("key")];
160
+ const item = {
161
+ key: dataSourceModel.get("key"),
162
+ displayName: dataSourceModel.get("displayName"),
163
+ status: dataSourceStatus,
164
+ type: dataSourceModel.get("type"),
165
+ // @ts-ignore
166
+ isDBInstance: !!(dataSource == null ? void 0 : dataSource.collectionManager.db)
167
+ };
168
+ if (dataSourceStatus === "loading-failed" || dataSourceStatus === "reloading-failed") {
169
+ item["errorMessage"] = plugin.dataSourceErrors[dataSourceModel.get("key")].message;
170
+ }
171
+ if (!dataSource) {
172
+ return item;
173
+ }
174
+ if (appendCollections) {
175
+ const collections = dataSource.collectionManager.getCollections();
176
+ item.collections = collections.map((collection) => {
177
+ const collectionOptions = collection.options;
178
+ const fields = [...collection.fields.values()].map((field) => field.options);
179
+ return {
180
+ ...collectionOptions,
181
+ fields
182
+ };
183
+ });
184
+ }
185
+ return item;
186
+ };
187
+ this.app.resourcer.use(
188
+ async (ctx, next) => {
189
+ if (!ctx.action) {
190
+ await next();
191
+ return;
192
+ }
193
+ const { actionName, resourceName, params } = ctx.action;
194
+ if (resourceName === "dataSources" && actionName === "list") {
195
+ if (!params.sort) {
196
+ params.sort = ["-fixed", "createdAt"];
197
+ }
198
+ }
199
+ await next();
200
+ },
201
+ { tag: "defaultSorting" }
202
+ );
203
+ this.app.use(
204
+ async (ctx, next) => {
205
+ await next();
206
+ if (!ctx.action) {
207
+ return;
208
+ }
209
+ const { actionName, resourceName, params } = ctx.action;
210
+ if (resourceName === "dataSources" && actionName === "get") {
211
+ let appendCollections = false;
212
+ const appends = ctx.action.params.appends;
213
+ if (appends && appends.includes("collections")) {
214
+ appendCollections = true;
215
+ }
216
+ if (ctx.body.data) {
217
+ ctx.body.data = mapDataSourceWithCollection(ctx.body.data, appendCollections);
218
+ } else {
219
+ ctx.body = mapDataSourceWithCollection(ctx.body, appendCollections);
220
+ }
221
+ }
222
+ },
223
+ { tag: "dataSourcesget" }
224
+ );
225
+ this.app.actions({
226
+ async ["dataSources:listEnabled"](ctx, next) {
227
+ const dataSources = await ctx.db.getRepository("dataSources").find({
228
+ filter: {
229
+ enabled: true,
230
+ "type.$ne": "main"
231
+ }
232
+ });
233
+ ctx.body = dataSources.map((dataSourceModel) => {
234
+ return mapDataSourceWithCollection(dataSourceModel);
235
+ });
236
+ await next();
237
+ },
238
+ async ["dataSources:testConnection"](ctx, next) {
239
+ const { values } = ctx.action.params;
240
+ const { options, type } = values;
241
+ const klass = ctx.app.dataSourceManager.factory.getClass(type);
242
+ try {
243
+ await klass.testConnection(options);
244
+ } catch (error) {
245
+ throw new Error(`Test connection failed: ${error.message}`);
246
+ }
247
+ ctx.body = {
248
+ success: true
249
+ };
250
+ await next();
251
+ },
252
+ async ["dataSources:refresh"](ctx, next) {
253
+ const { filterByTk, clientStatus } = ctx.action.params;
254
+ const dataSourceModel = await ctx.db.getRepository("dataSources").findOne({
255
+ filter: {
256
+ key: filterByTk
257
+ }
258
+ });
259
+ const currentStatus = plugin.dataSourceStatus[filterByTk];
260
+ if (canRefreshStatus.includes(currentStatus) && (clientStatus ? clientStatus && canRefreshStatus.includes(clientStatus) : true)) {
261
+ dataSourceModel.loadIntoApplication({
262
+ app: ctx.app
263
+ });
264
+ }
265
+ ctx.body = {
266
+ status: plugin.dataSourceStatus[filterByTk]
267
+ };
268
+ await next();
269
+ }
270
+ });
271
+ this.app.resourcer.define(import_data_sources_collections.default);
272
+ this.app.resourcer.define(import_data_sources_collections_fields.default);
273
+ this.app.resourcer.define(import_roles_data_sources_collections.rolesRemoteCollectionsResourcer);
274
+ this.app.resourcer.define(import_data_sources_roles.default);
275
+ this.app.resourcer.define(import_data_sources_resources.default);
276
+ this.app.resourcer.define({
277
+ name: "dataSources"
278
+ });
279
+ this.app.db.on("dataSourcesFields.afterSave", async (model) => {
280
+ model.load({
281
+ app: this.app
282
+ });
283
+ });
284
+ this.app.db.on("dataSourcesFields.afterDestroy", async (model) => {
285
+ model.unload({
286
+ app: this.app
287
+ });
288
+ });
289
+ this.app.db.on("dataSourcesCollections.afterSave", async (model) => {
290
+ model.load({
291
+ app: this.app
292
+ });
293
+ });
294
+ this.app.db.on("dataSources.afterDestroy", async (model) => {
295
+ this.app.dataSourceManager.dataSources.delete(model.get("key"));
296
+ });
297
+ this.app.on("afterStart", async (app2) => {
298
+ const dataSourcesRecords = await this.app.db.getRepository("dataSources").find({
299
+ filter: {
300
+ enabled: true
301
+ }
302
+ });
303
+ const loadPromises = dataSourcesRecords.map((dataSourceRecord) => {
304
+ if (dataSourceRecord.isMainRecord()) {
305
+ return dataSourceRecord.loadIntoACL({ app: app2, acl: app2.acl, loadAtAfterStart: true });
306
+ }
307
+ return dataSourceRecord.loadIntoApplication({ app: app2, loadAtAfterStart: true });
308
+ });
309
+ this.app.setMaintainingMessage("Loading data sources...");
310
+ Promise.allSettled(loadPromises).then(() => {
311
+ return this.app.emitAsync("dataSourceAfterStart");
312
+ });
313
+ });
314
+ this.app.db.on("dataSourcesRolesResources.afterSaveWithAssociations", async (model, options) => {
315
+ const { transaction } = options;
316
+ const pluginACL = this.app.pm.get("acl");
317
+ const dataSource = this.app.dataSourceManager.dataSources.get(model.get("dataSourceKey"));
318
+ await model.writeToACL({
319
+ acl: dataSource.acl,
320
+ associationFieldsActions: pluginACL.associationFieldsActions,
321
+ transaction,
322
+ grantHelper: pluginACL.grantHelper,
323
+ app: this.app
324
+ });
325
+ });
326
+ this.app.db.on("dataSourcesRolesResourcesActions.afterUpdateWithAssociations", async (model, options) => {
327
+ const { transaction } = options;
328
+ const resource = await model.getResource({
329
+ transaction
330
+ });
331
+ const pluginACL = this.app.pm.get("acl");
332
+ const dataSource = this.app.dataSourceManager.dataSources.get(resource.get("dataSourceKey"));
333
+ await resource.writeToACL({
334
+ acl: dataSource.acl,
335
+ associationFieldsActions: pluginACL.associationFieldsActions,
336
+ transaction,
337
+ grantHelper: pluginACL.grantHelper,
338
+ app: this.app
339
+ });
340
+ });
341
+ this.app.db.on("dataSourcesRolesResources.afterDestroy", async (model, options) => {
342
+ const dataSource = this.app.dataSourceManager.dataSources.get(model.get("dataSourceKey"));
343
+ const roleName = model.get("roleName");
344
+ const role = dataSource.acl.getRole(roleName);
345
+ if (role) {
346
+ role.revokeResource(model.get("name"));
347
+ }
348
+ });
349
+ this.app.db.on("dataSourcesRoles.afterSave", async (model, options) => {
350
+ const { transaction } = options;
351
+ const pluginACL = this.app.pm.get("acl");
352
+ const dataSource = this.app.dataSourceManager.dataSources.get(model.get("dataSourceKey"));
353
+ await model.writeToAcl({
354
+ grantHelper: pluginACL.grantHelper,
355
+ associationFieldsActions: pluginACL.associationFieldsActions,
356
+ acl: dataSource.acl,
357
+ transaction,
358
+ app: this.app
359
+ });
360
+ });
361
+ this.app.on("acl:writeResources", async ({ roleName, transaction }) => {
362
+ const dataSource = this.app.dataSourceManager.dataSources.get("main");
363
+ const pluginACL = this.app.pm.get("acl");
364
+ const dataSourceRole = await this.app.db.getRepository("dataSourcesRoles").findOne({
365
+ filter: {
366
+ dataSourceKey: "main",
367
+ roleName
368
+ },
369
+ transaction
370
+ });
371
+ await dataSourceRole.writeToAcl({
372
+ grantHelper: pluginACL.grantHelper,
373
+ associationFieldsActions: pluginACL.associationFieldsActions,
374
+ acl: dataSource.acl,
375
+ transaction,
376
+ app: this.app
377
+ });
378
+ });
379
+ this.app.resourcer.use(
380
+ async (ctx, next) => {
381
+ const action = ctx.action;
382
+ await next();
383
+ const { resourceName, actionName } = action.params;
384
+ if (resourceName === "roles" && actionName === "check") {
385
+ const roleName = ctx.state.currentRole;
386
+ const dataSources = await ctx.db.getRepository("dataSources").find();
387
+ ctx.bodyMeta = {
388
+ dataSources: dataSources.reduce((carry, dataSourceModel) => {
389
+ const dataSource = this.app.dataSourceManager.dataSources.get(dataSourceModel.get("key"));
390
+ if (!dataSource) {
391
+ return carry;
392
+ }
393
+ const dataSourceStatus = this.dataSourceStatus[dataSourceModel.get("key")];
394
+ if (dataSourceStatus !== "loaded") {
395
+ return carry;
396
+ }
397
+ const aclInstance = dataSource.acl;
398
+ const roleInstance = aclInstance.getRole(roleName);
399
+ const dataObj = {
400
+ strategy: {},
401
+ resources: roleInstance ? [...roleInstance.resources.keys()] : [],
402
+ actions: {}
403
+ };
404
+ if (roleInstance) {
405
+ const data = roleInstance.toJSON();
406
+ dataObj["name"] = data["name"];
407
+ dataObj["strategy"] = data["strategy"];
408
+ dataObj["actions"] = data["actions"];
409
+ }
410
+ carry[dataSourceModel.get("key")] = dataObj;
411
+ return carry;
412
+ }, {})
413
+ };
414
+ }
415
+ },
416
+ { tag: "dataSourceAccessCheck" }
417
+ );
418
+ this.app.acl.registerSnippet({
419
+ name: `pm.${this.name}`,
420
+ actions: [
421
+ "dataSources:*",
422
+ "roles.dataSourceResources",
423
+ "collections:*",
424
+ "collections.fields:*",
425
+ "dbViews:*",
426
+ "collectionCategories:*",
427
+ "sqlCollection:*"
428
+ ]
429
+ });
430
+ this.app.acl.allow("dataSources", "listEnabled", "loggedIn");
431
+ this.app.acl.allow("dataSources", "get", "loggedIn");
432
+ this.app.acl.addFixedParams("dataSources", "destroy", () => {
433
+ return {
434
+ filter: {
435
+ "key.$ne": "main"
436
+ }
437
+ };
438
+ });
439
+ }
440
+ async load() {
441
+ await this.importCollections((0, import_path.resolve)(__dirname, "collections"));
442
+ }
443
+ }
444
+ var plugin_default = PluginDataSourceManagerServer;
445
+ // Annotate the CommonJS export names for ESM import in node:
446
+ 0 && (module.exports = {
447
+ PluginDataSourceManagerServer
448
+ });
@@ -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;