@nocobase/plugin-data-source-manager 2.0.0-alpha.3 → 2.0.0-alpha.31
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.
- package/LICENSE.txt +31 -20
- package/dist/client/1a8cce8035a89dd9.js +1 -1
- package/dist/client/2695e7fbad1ec078.js +10 -0
- package/dist/client/2f7a418e7935984d.js +10 -0
- package/dist/client/a279f7ca19e59f87.js +10 -0
- package/dist/client/component/MainDataSourceManager/Configuration/db-sync/LoadCollectionAction.d.ts +15 -0
- package/dist/client/component/MainDataSourceManager/Configuration/db-sync/SyncFieldChangesAction.d.ts +10 -0
- package/dist/client/component/MainDataSourceManager/Configuration/db-sync/SyncFromDatabaseAction.d.ts +10 -0
- package/dist/client/dbf5ddf434ab29c0.js +10 -0
- package/dist/client/e7a513a5b431455b.js +10 -0
- package/dist/client/fdae18574fda07af.js +10 -0
- package/dist/client/hooks/index.d.ts +3 -4
- package/dist/client/hooks/useDataSourceActions.d.ts +14 -0
- package/dist/client/hooks/useDataSourceRefresh.d.ts +18 -0
- package/dist/client/hooks/useResourceData.d.ts +31 -0
- package/dist/client/index.js +1 -1
- package/dist/externalVersion.js +9 -8
- package/dist/locale/en-US.json +7 -0
- package/dist/locale/zh-CN.json +7 -2
- package/dist/server/actions/data-sources.d.ts +17 -0
- package/dist/server/actions/data-sources.js +114 -0
- package/dist/server/middlewares/load-tables.d.ts +10 -0
- package/dist/server/middlewares/load-tables.js +74 -0
- package/dist/server/plugin.js +10 -96
- package/dist/server/resourcers/data-sources-collections.d.ts +0 -2
- package/dist/server/resourcers/data-sources-collections.js +0 -83
- package/dist/server/utils.d.ts +3 -0
- package/dist/server/utils.js +45 -0
- package/package.json +2 -2
- package/dist/client/0e6cf640dde183c8.js +0 -10
- package/dist/client/7b6cb59b157f5087.js +0 -10
- package/dist/client/9aff90e19fbd41b6.js +0 -10
- package/dist/client/b3873b8560b4de87.js +0 -10
- package/dist/client/cab6d8ac1e107ea5.js +0 -10
- package/dist/client/cc4271412c5e6495.js +0 -10
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
var __defProp = Object.defineProperty;
|
|
11
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
12
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
13
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
14
|
+
var __export = (target, all) => {
|
|
15
|
+
for (var name in all)
|
|
16
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
17
|
+
};
|
|
18
|
+
var __copyProps = (to, from, except, desc) => {
|
|
19
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
20
|
+
for (let key of __getOwnPropNames(from))
|
|
21
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
22
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
23
|
+
}
|
|
24
|
+
return to;
|
|
25
|
+
};
|
|
26
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
27
|
+
var data_sources_exports = {};
|
|
28
|
+
__export(data_sources_exports, {
|
|
29
|
+
default: () => data_sources_default
|
|
30
|
+
});
|
|
31
|
+
module.exports = __toCommonJS(data_sources_exports);
|
|
32
|
+
var import_utils = require("../utils");
|
|
33
|
+
const canRefreshStatus = ["loaded", "loading-failed", "reloading-failed"];
|
|
34
|
+
var data_sources_default = {
|
|
35
|
+
async ["dataSources:listEnabled"](ctx, next) {
|
|
36
|
+
const dataSources = await ctx.db.getRepository("dataSources").find({
|
|
37
|
+
filter: {
|
|
38
|
+
enabled: true,
|
|
39
|
+
"type.$ne": "main"
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
ctx.body = dataSources.map((dataSourceModel) => {
|
|
43
|
+
return (0, import_utils.mapDataSourceWithCollection)(ctx.app, dataSourceModel);
|
|
44
|
+
});
|
|
45
|
+
await next();
|
|
46
|
+
},
|
|
47
|
+
async ["dataSources:testConnection"](ctx, next) {
|
|
48
|
+
const { values } = ctx.action.params;
|
|
49
|
+
const { options, type } = values;
|
|
50
|
+
const klass = ctx.app.dataSourceManager.factory.getClass(type);
|
|
51
|
+
try {
|
|
52
|
+
await klass.testConnection(ctx.app.environment.renderJsonTemplate(options));
|
|
53
|
+
} catch (error) {
|
|
54
|
+
throw new Error(`Test connection failed: ${error.message}`);
|
|
55
|
+
}
|
|
56
|
+
ctx.body = {
|
|
57
|
+
success: true
|
|
58
|
+
};
|
|
59
|
+
await next();
|
|
60
|
+
},
|
|
61
|
+
async ["dataSources:refresh"](ctx, next) {
|
|
62
|
+
const plugin = ctx.app.pm.get("data-source-manager");
|
|
63
|
+
const { filterByTk, clientStatus } = ctx.action.params;
|
|
64
|
+
const dataSourceModel = await ctx.db.getRepository("dataSources").findOne({
|
|
65
|
+
filter: {
|
|
66
|
+
key: filterByTk
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
const currentStatus = plugin.dataSourceStatus[filterByTk];
|
|
70
|
+
if (canRefreshStatus.includes(currentStatus) && (clientStatus ? clientStatus && canRefreshStatus.includes(clientStatus) : true)) {
|
|
71
|
+
dataSourceModel.loadIntoApplication({
|
|
72
|
+
app: ctx.app,
|
|
73
|
+
refresh: true
|
|
74
|
+
});
|
|
75
|
+
ctx.app.syncMessageManager.publish(plugin.name, {
|
|
76
|
+
type: "loadDataSource",
|
|
77
|
+
dataSourceKey: dataSourceModel.get("key")
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
ctx.body = {
|
|
81
|
+
status: plugin.dataSourceStatus[filterByTk]
|
|
82
|
+
};
|
|
83
|
+
await next();
|
|
84
|
+
},
|
|
85
|
+
async ["dataSources:readTables"](ctx, next) {
|
|
86
|
+
const { dataSourceKey, dbOptions } = ctx.action.params.values || {};
|
|
87
|
+
const dataSourceManager = ctx.app.dataSourceManager;
|
|
88
|
+
let dataSource;
|
|
89
|
+
if (dbOptions) {
|
|
90
|
+
dataSource = dataSourceManager.factory.create(dbOptions.type, {
|
|
91
|
+
name: dataSourceKey,
|
|
92
|
+
...dbOptions
|
|
93
|
+
});
|
|
94
|
+
} else {
|
|
95
|
+
dataSource = dataSourceManager.dataSources.get(dataSourceKey);
|
|
96
|
+
if (!dataSource) {
|
|
97
|
+
throw new Error(`dataSource ${dataSourceKey} not found`);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
const tables = await dataSource.readTables();
|
|
101
|
+
ctx.body = tables;
|
|
102
|
+
await next();
|
|
103
|
+
},
|
|
104
|
+
async ["dataSources:loadTables"](ctx, next) {
|
|
105
|
+
const { dataSourceKey, tables } = ctx.action.params.values || {};
|
|
106
|
+
const dataSourceManager = ctx.app.dataSourceManager;
|
|
107
|
+
const dataSource = dataSourceManager.dataSources.get(dataSourceKey);
|
|
108
|
+
if (!dataSource) {
|
|
109
|
+
throw new Error(`dataSource ${dataSourceKey} not found`);
|
|
110
|
+
}
|
|
111
|
+
await dataSource.loadTables(ctx, tables);
|
|
112
|
+
await next();
|
|
113
|
+
}
|
|
114
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
import { Context, Next } from '@nocobase/actions';
|
|
10
|
+
export declare function loadDataSourceTablesIntoCollections(ctx: Context, next: Next): Promise<any>;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
var __defProp = Object.defineProperty;
|
|
11
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
12
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
13
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
14
|
+
var __export = (target, all) => {
|
|
15
|
+
for (var name in all)
|
|
16
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
17
|
+
};
|
|
18
|
+
var __copyProps = (to, from, except, desc) => {
|
|
19
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
20
|
+
for (let key of __getOwnPropNames(from))
|
|
21
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
22
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
23
|
+
}
|
|
24
|
+
return to;
|
|
25
|
+
};
|
|
26
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
27
|
+
var load_tables_exports = {};
|
|
28
|
+
__export(load_tables_exports, {
|
|
29
|
+
loadDataSourceTablesIntoCollections: () => loadDataSourceTablesIntoCollections
|
|
30
|
+
});
|
|
31
|
+
module.exports = __toCommonJS(load_tables_exports);
|
|
32
|
+
var import_data_source_manager = require("@nocobase/data-source-manager");
|
|
33
|
+
var import_constants = require("../constants");
|
|
34
|
+
async function loadDataSourceTablesIntoCollections(ctx, next) {
|
|
35
|
+
const { actionName, resourceName, params } = ctx.action;
|
|
36
|
+
if (resourceName === "dataSources" && (actionName === "create" || actionName === "update")) {
|
|
37
|
+
const { options, type, collections, key } = params.values || {};
|
|
38
|
+
let dataSource;
|
|
39
|
+
if (actionName === "create") {
|
|
40
|
+
dataSource = ctx.app.dataSourceManager.factory.create(type, {
|
|
41
|
+
name: key,
|
|
42
|
+
...options
|
|
43
|
+
});
|
|
44
|
+
dataSource.setLogger(ctx.logger);
|
|
45
|
+
} else {
|
|
46
|
+
const { filterByTk: dataSourceKey } = params;
|
|
47
|
+
dataSource = ctx.app.dataSourceManager.dataSources.get(dataSourceKey);
|
|
48
|
+
if (!dataSource) {
|
|
49
|
+
throw new Error(`dataSource ${dataSourceKey} not found`);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
if (!(dataSource instanceof import_data_source_manager.DatabaseDataSource)) {
|
|
53
|
+
return next();
|
|
54
|
+
}
|
|
55
|
+
if (options == null ? void 0 : options.addAllCollections) {
|
|
56
|
+
const allTables = await dataSource.introspector.getTables();
|
|
57
|
+
if (allTables.length > import_constants.ALLOW_MAX_COLLECTIONS_COUNT) {
|
|
58
|
+
throw new Error(
|
|
59
|
+
`The number of collections exceeds the limit of ${import_constants.ALLOW_MAX_COLLECTIONS_COUNT}. Please remove some collections before adding new ones.`
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
} else {
|
|
63
|
+
await dataSource.loadTables(ctx, collections);
|
|
64
|
+
}
|
|
65
|
+
if (collections) {
|
|
66
|
+
delete ctx.action.params.values.collections;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
await next();
|
|
70
|
+
}
|
|
71
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
72
|
+
0 && (module.exports = {
|
|
73
|
+
loadDataSourceTablesIntoCollections
|
|
74
|
+
});
|
package/dist/server/plugin.js
CHANGED
|
@@ -41,7 +41,6 @@ __export(plugin_exports, {
|
|
|
41
41
|
});
|
|
42
42
|
module.exports = __toCommonJS(plugin_exports);
|
|
43
43
|
var import_server = require("@nocobase/server");
|
|
44
|
-
var import_path = require("path");
|
|
45
44
|
var import_data_sources_collection_model = require("./models/data-sources-collection-model");
|
|
46
45
|
var import_data_sources_field_model = require("./models/data-sources-field-model");
|
|
47
46
|
var import_data_sources_collections = __toESM(require("./resourcers/data-sources-collections"));
|
|
@@ -49,14 +48,14 @@ var import_data_sources_collections_fields = __toESM(require("./resourcers/data-
|
|
|
49
48
|
var import_data_sources_resources = __toESM(require("./resourcers/data-sources-resources"));
|
|
50
49
|
var import_data_sources_roles = __toESM(require("./resourcers/data-sources-roles"));
|
|
51
50
|
var import_roles_data_sources_collections = require("./resourcers/roles-data-sources-collections");
|
|
51
|
+
var import_data_sources = __toESM(require("./actions/data-sources"));
|
|
52
52
|
var import_lodash = __toESM(require("lodash"));
|
|
53
53
|
var import_connections_roles_resources = require("./models/connections-roles-resources");
|
|
54
54
|
var import_connections_roles_resources_action = require("./models/connections-roles-resources-action");
|
|
55
55
|
var import_data_source = require("./models/data-source");
|
|
56
56
|
var import_data_sources_roles_model = require("./models/data-sources-roles-model");
|
|
57
57
|
var import_acl = require("@nocobase/acl");
|
|
58
|
-
var
|
|
59
|
-
const canRefreshStatus = ["loaded", "loading-failed", "reloading-failed"];
|
|
58
|
+
var import_load_tables = require("./middlewares/load-tables");
|
|
60
59
|
class PluginDataSourceManagerServer extends import_server.Plugin {
|
|
61
60
|
dataSourceErrors = {};
|
|
62
61
|
dataSourceStatus = {};
|
|
@@ -312,41 +311,7 @@ class PluginDataSourceManagerServer extends import_server.Plugin {
|
|
|
312
311
|
}
|
|
313
312
|
await next();
|
|
314
313
|
});
|
|
315
|
-
this.app.resourceManager.use(
|
|
316
|
-
var _a;
|
|
317
|
-
if (!ctx.action) {
|
|
318
|
-
await next();
|
|
319
|
-
return;
|
|
320
|
-
}
|
|
321
|
-
const { actionName, resourceName, params } = ctx.action;
|
|
322
|
-
if (resourceName === "dataSources" && (actionName === "add" || actionName === "update")) {
|
|
323
|
-
const { values, filterByTk: dataSourceKey } = params;
|
|
324
|
-
if ((_a = values.options) == null ? void 0 : _a.addAllCollections) {
|
|
325
|
-
let introspector = null;
|
|
326
|
-
const dataSourceManager = ctx.app["dataSourceManager"];
|
|
327
|
-
if (actionName === "add") {
|
|
328
|
-
const klass = dataSourceManager.factory.getClass(values.options.type);
|
|
329
|
-
const dataSource = new klass(dbOptions);
|
|
330
|
-
introspector = dataSource.collectionManager.dataSource.createDatabaseIntrospector(
|
|
331
|
-
dataSource.collectionManager.db
|
|
332
|
-
);
|
|
333
|
-
} else {
|
|
334
|
-
const dataSource = dataSourceManager.dataSources.get(dataSourceKey);
|
|
335
|
-
if (!dataSource) {
|
|
336
|
-
throw new Error(`dataSource ${dataSourceKey} not found`);
|
|
337
|
-
}
|
|
338
|
-
introspector = dataSource["introspector"];
|
|
339
|
-
}
|
|
340
|
-
const allCollections = await introspector.getCollections();
|
|
341
|
-
if (allCollections.length > import_constants.ALLOW_MAX_COLLECTIONS_COUNT) {
|
|
342
|
-
throw new Error(
|
|
343
|
-
`The number of collections exceeds the limit of ${import_constants.ALLOW_MAX_COLLECTIONS_COUNT}. Please remove some collections before adding new ones.`
|
|
344
|
-
);
|
|
345
|
-
}
|
|
346
|
-
}
|
|
347
|
-
}
|
|
348
|
-
await next();
|
|
349
|
-
});
|
|
314
|
+
this.app.resourceManager.use(import_load_tables.loadDataSourceTablesIntoCollections);
|
|
350
315
|
this.app.use(async function handleAppendDataSourceCollection(ctx, next) {
|
|
351
316
|
await next();
|
|
352
317
|
if (!ctx.action) {
|
|
@@ -367,63 +332,13 @@ class PluginDataSourceManagerServer extends import_server.Plugin {
|
|
|
367
332
|
}
|
|
368
333
|
});
|
|
369
334
|
const self = this;
|
|
370
|
-
this.app.
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
});
|
|
378
|
-
ctx.body = dataSources.map((dataSourceModel) => {
|
|
379
|
-
return mapDataSourceWithCollection(dataSourceModel);
|
|
380
|
-
});
|
|
381
|
-
await next();
|
|
382
|
-
},
|
|
383
|
-
async ["dataSources:testConnection"](ctx, next) {
|
|
384
|
-
const { values } = ctx.action.params;
|
|
385
|
-
const { options, type } = values;
|
|
386
|
-
const klass = ctx.app.dataSourceManager.factory.getClass(type);
|
|
387
|
-
try {
|
|
388
|
-
await klass.testConnection(self.renderJsonTemplate(options));
|
|
389
|
-
} catch (error) {
|
|
390
|
-
throw new Error(`Test connection failed: ${error.message}`);
|
|
391
|
-
}
|
|
392
|
-
ctx.body = {
|
|
393
|
-
success: true
|
|
394
|
-
};
|
|
395
|
-
await next();
|
|
396
|
-
},
|
|
397
|
-
async ["dataSources:refresh"](ctx, next) {
|
|
398
|
-
const { filterByTk, clientStatus } = ctx.action.params;
|
|
399
|
-
const dataSourceModel = await ctx.db.getRepository("dataSources").findOne({
|
|
400
|
-
filter: {
|
|
401
|
-
key: filterByTk
|
|
402
|
-
}
|
|
403
|
-
});
|
|
404
|
-
const currentStatus = plugin.dataSourceStatus[filterByTk];
|
|
405
|
-
if (canRefreshStatus.includes(currentStatus) && (clientStatus ? clientStatus && canRefreshStatus.includes(clientStatus) : true)) {
|
|
406
|
-
dataSourceModel.loadIntoApplication({
|
|
407
|
-
app: ctx.app,
|
|
408
|
-
refresh: true
|
|
409
|
-
});
|
|
410
|
-
ctx.app.syncMessageManager.publish(self.name, {
|
|
411
|
-
type: "loadDataSource",
|
|
412
|
-
dataSourceKey: dataSourceModel.get("key")
|
|
413
|
-
});
|
|
414
|
-
}
|
|
415
|
-
ctx.body = {
|
|
416
|
-
status: plugin.dataSourceStatus[filterByTk]
|
|
417
|
-
};
|
|
418
|
-
await next();
|
|
419
|
-
}
|
|
420
|
-
});
|
|
421
|
-
this.app.resourcer.define(import_data_sources_collections.default);
|
|
422
|
-
this.app.resourcer.define(import_data_sources_collections_fields.default);
|
|
423
|
-
this.app.resourcer.define(import_roles_data_sources_collections.rolesRemoteCollectionsResourcer);
|
|
424
|
-
this.app.resourcer.define(import_data_sources_roles.default);
|
|
425
|
-
this.app.resourcer.define(import_data_sources_resources.default);
|
|
426
|
-
this.app.resourcer.define({
|
|
335
|
+
this.app.resourceManager.registerActionHandlers(import_data_sources.default);
|
|
336
|
+
this.app.resourceManager.define(import_data_sources_collections.default);
|
|
337
|
+
this.app.resourceManager.define(import_data_sources_collections_fields.default);
|
|
338
|
+
this.app.resourceManager.define(import_roles_data_sources_collections.rolesRemoteCollectionsResourcer);
|
|
339
|
+
this.app.resourceManager.define(import_data_sources_roles.default);
|
|
340
|
+
this.app.resourceManager.define(import_data_sources_resources.default);
|
|
341
|
+
this.app.resourceManager.define({
|
|
427
342
|
name: "dataSources"
|
|
428
343
|
});
|
|
429
344
|
this.app.db.on("dataSourcesFields.beforeSave", async (model, options) => {
|
|
@@ -692,7 +607,6 @@ class PluginDataSourceManagerServer extends import_server.Plugin {
|
|
|
692
607
|
});
|
|
693
608
|
}
|
|
694
609
|
async load() {
|
|
695
|
-
await this.importCollections((0, import_path.resolve)(__dirname, "collections"));
|
|
696
610
|
}
|
|
697
611
|
}
|
|
698
612
|
var plugin_default = PluginDataSourceManagerServer;
|
|
@@ -41,8 +41,6 @@ __export(data_sources_collections_exports, {
|
|
|
41
41
|
module.exports = __toCommonJS(data_sources_collections_exports);
|
|
42
42
|
var import_lodash = __toESM(require("lodash"));
|
|
43
43
|
var import_database = require("@nocobase/database");
|
|
44
|
-
var import_lodash2 = __toESM(require("lodash"));
|
|
45
|
-
var import_constants = require("../constants");
|
|
46
44
|
var data_sources_collections_default = {
|
|
47
45
|
name: "dataSources.collections",
|
|
48
46
|
actions: {
|
|
@@ -136,87 +134,6 @@ var data_sources_collections_default = {
|
|
|
136
134
|
});
|
|
137
135
|
ctx.body = dataSourceCollectionRecord.toJSON();
|
|
138
136
|
await next();
|
|
139
|
-
},
|
|
140
|
-
async all(ctx, next) {
|
|
141
|
-
const params = ctx.action.params;
|
|
142
|
-
const { associatedIndex: dataSourceKey, isFirst, dbOptions } = params;
|
|
143
|
-
const dataSourceManager = ctx.app.dataSourceManager;
|
|
144
|
-
let introspector = null;
|
|
145
|
-
if (isFirst) {
|
|
146
|
-
const klass = dataSourceManager.factory.getClass(dbOptions.type);
|
|
147
|
-
const dataSource = new klass(dbOptions);
|
|
148
|
-
introspector = dataSource.collectionManager.dataSource.createDatabaseIntrospector(
|
|
149
|
-
dataSource.collectionManager.db
|
|
150
|
-
);
|
|
151
|
-
} else {
|
|
152
|
-
const dataSource = dataSourceManager.dataSources.get(dataSourceKey);
|
|
153
|
-
if (!dataSource) {
|
|
154
|
-
throw new Error(`dataSource ${dataSourceKey} not found`);
|
|
155
|
-
}
|
|
156
|
-
introspector = dataSource["introspector"];
|
|
157
|
-
}
|
|
158
|
-
const allCollections = await introspector.getCollections();
|
|
159
|
-
const selectedCollections = await ctx.db.getRepository("dataSourcesCollections").find({
|
|
160
|
-
filter: { dataSourceKey }
|
|
161
|
-
});
|
|
162
|
-
const selectedMap = import_lodash2.default.keyBy(selectedCollections, (x) => x.name);
|
|
163
|
-
const result = allCollections.map((collection) => {
|
|
164
|
-
return {
|
|
165
|
-
name: collection,
|
|
166
|
-
selected: !!selectedMap[collection]
|
|
167
|
-
};
|
|
168
|
-
});
|
|
169
|
-
ctx.body = result;
|
|
170
|
-
await next();
|
|
171
|
-
},
|
|
172
|
-
async add(ctx, next) {
|
|
173
|
-
const params = ctx.action.params;
|
|
174
|
-
const { associatedIndex: dataSourceKey, values } = params;
|
|
175
|
-
const collections = values.collections || [];
|
|
176
|
-
const dbOptions = values.dbOptions || {};
|
|
177
|
-
if (dbOptions.addAllCollections !== false) {
|
|
178
|
-
await next();
|
|
179
|
-
return;
|
|
180
|
-
}
|
|
181
|
-
if (collections.length > import_constants.ALLOW_MAX_COLLECTIONS_COUNT) {
|
|
182
|
-
throw new Error(
|
|
183
|
-
`The number of collections exceeds the limit of ${import_constants.ALLOW_MAX_COLLECTIONS_COUNT}. Please remove some collections before adding new ones.`
|
|
184
|
-
);
|
|
185
|
-
}
|
|
186
|
-
const transaction = await ctx.db.sequelize.transaction();
|
|
187
|
-
const repo = ctx.db.getRepository("dataSourcesCollections");
|
|
188
|
-
const alreadyInserted = await repo.find({
|
|
189
|
-
filter: {
|
|
190
|
-
dataSourceKey
|
|
191
|
-
},
|
|
192
|
-
transaction
|
|
193
|
-
});
|
|
194
|
-
const alreadyInsertedNames = import_lodash2.default.keyBy(alreadyInserted, (x) => x.name);
|
|
195
|
-
const incomingCollections = import_lodash2.default.keyBy(collections);
|
|
196
|
-
const toBeInserted = collections.filter((collection) => !alreadyInsertedNames[collection]);
|
|
197
|
-
const toBeDeleted = Object.keys(alreadyInsertedNames).filter((name) => !incomingCollections[name]);
|
|
198
|
-
if (toBeInserted.length > 0) {
|
|
199
|
-
const insertCollections = toBeInserted.map((collection) => {
|
|
200
|
-
return { name: collection, dataSourceKey };
|
|
201
|
-
});
|
|
202
|
-
await repo.model.bulkCreate(insertCollections, { transaction });
|
|
203
|
-
}
|
|
204
|
-
if (toBeDeleted.length > 0) {
|
|
205
|
-
await repo.model.destroy({
|
|
206
|
-
where: {
|
|
207
|
-
dataSourceKey,
|
|
208
|
-
name: toBeDeleted
|
|
209
|
-
},
|
|
210
|
-
transaction
|
|
211
|
-
});
|
|
212
|
-
}
|
|
213
|
-
await transaction.commit();
|
|
214
|
-
const dataSource = ctx.app.dataSourceManager.dataSources.get(dataSourceKey);
|
|
215
|
-
if (dataSource) {
|
|
216
|
-
await dataSource.load({ refresh: true, condition: toBeInserted ? { name: { $in: toBeInserted } } : {} });
|
|
217
|
-
}
|
|
218
|
-
ctx.body = true;
|
|
219
|
-
await next();
|
|
220
137
|
}
|
|
221
138
|
}
|
|
222
139
|
};
|
package/dist/server/utils.d.ts
CHANGED
|
@@ -1 +1,4 @@
|
|
|
1
|
+
import { DataSourceModel } from './models/data-source';
|
|
2
|
+
import { Application } from '@nocobase/server';
|
|
1
3
|
export declare function mergeOptions(fieldOptions: any, modelOptions: any): any;
|
|
4
|
+
export declare const mapDataSourceWithCollection: (app: Application, dataSourceModel: DataSourceModel, appendCollections?: boolean) => any;
|
package/dist/server/utils.js
CHANGED
|
@@ -26,6 +26,7 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
26
26
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
27
27
|
var utils_exports = {};
|
|
28
28
|
__export(utils_exports, {
|
|
29
|
+
mapDataSourceWithCollection: () => mapDataSourceWithCollection,
|
|
29
30
|
mergeOptions: () => mergeOptions
|
|
30
31
|
});
|
|
31
32
|
module.exports = __toCommonJS(utils_exports);
|
|
@@ -41,7 +42,51 @@ function mergeOptions(fieldOptions, modelOptions) {
|
|
|
41
42
|
}
|
|
42
43
|
return newOptions;
|
|
43
44
|
}
|
|
45
|
+
const mapDataSourceWithCollection = (app, dataSourceModel, appendCollections = true) => {
|
|
46
|
+
const plugin = app.pm.get("data-source-manager");
|
|
47
|
+
const dataSource = app.dataSourceManager.dataSources.get(dataSourceModel.get("key"));
|
|
48
|
+
const dataSourceStatus = plugin.dataSourceStatus[dataSourceModel.get("key")];
|
|
49
|
+
const item = {
|
|
50
|
+
key: dataSourceModel.get("key"),
|
|
51
|
+
displayName: dataSourceModel.get("displayName"),
|
|
52
|
+
status: dataSourceStatus,
|
|
53
|
+
type: dataSourceModel.get("type"),
|
|
54
|
+
// @ts-ignore
|
|
55
|
+
isDBInstance: !!(dataSource == null ? void 0 : dataSource.collectionManager.db)
|
|
56
|
+
};
|
|
57
|
+
const publicOptions = dataSource == null ? void 0 : dataSource.publicOptions();
|
|
58
|
+
if (publicOptions) {
|
|
59
|
+
item["options"] = publicOptions;
|
|
60
|
+
}
|
|
61
|
+
if (dataSourceStatus === "loading-failed" || dataSourceStatus === "reloading-failed") {
|
|
62
|
+
item["errorMessage"] = plugin.dataSourceErrors[dataSourceModel.get("key")].message;
|
|
63
|
+
}
|
|
64
|
+
if (!dataSource) {
|
|
65
|
+
return item;
|
|
66
|
+
}
|
|
67
|
+
if (appendCollections) {
|
|
68
|
+
const collections = dataSource.collectionManager.getCollections();
|
|
69
|
+
item.collections = collections.map((collection) => {
|
|
70
|
+
const collectionOptions = collection.options;
|
|
71
|
+
const collectionInstance = dataSource.collectionManager.getCollection(collectionOptions.name);
|
|
72
|
+
const fields = [...collection.fields.values()].map((field) => field.options);
|
|
73
|
+
const results = {
|
|
74
|
+
...collectionOptions,
|
|
75
|
+
fields
|
|
76
|
+
};
|
|
77
|
+
if (collectionInstance && collectionInstance.availableActions) {
|
|
78
|
+
results["availableActions"] = collectionInstance.availableActions();
|
|
79
|
+
}
|
|
80
|
+
if (collectionInstance && collectionInstance.unavailableActions) {
|
|
81
|
+
results["unavailableActions"] = collectionInstance.unavailableActions();
|
|
82
|
+
}
|
|
83
|
+
return results;
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
return item;
|
|
87
|
+
};
|
|
44
88
|
// Annotate the CommonJS export names for ESM import in node:
|
|
45
89
|
0 && (module.exports = {
|
|
90
|
+
mapDataSourceWithCollection,
|
|
46
91
|
mergeOptions
|
|
47
92
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nocobase/plugin-data-source-manager",
|
|
3
|
-
"version": "2.0.0-alpha.
|
|
3
|
+
"version": "2.0.0-alpha.31",
|
|
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": "
|
|
20
|
+
"gitHead": "1ccdc0a8af73f6c2e3e83df75741947a5a8c1984"
|
|
21
21
|
}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This file is part of the NocoBase (R) project.
|
|
3
|
-
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
-
* Authors: NocoBase Team.
|
|
5
|
-
*
|
|
6
|
-
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
-
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
"use strict";(self.webpackChunk_nocobase_plugin_data_source_manager=self.webpackChunk_nocobase_plugin_data_source_manager||[]).push([["65"],{612:function(e,t,n){n.r(t),n.d(t,{DataSourceContext:function(){return l},DatabaseConnectionProvider:function(){return u}});var o=n(772),r=n(156),a=n.n(r),c=n(239);function i(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,o=Array(t);n<t;n++)o[n]=e[n];return o}var l=(0,r.createContext)(null);l.displayName="DataSourceContext";var u=function(e){var t,n=(t=(0,r.useState)(null),function(e){if(Array.isArray(e))return e}(t)||function(e,t){var n,o,r=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=r){var a=[],c=!0,i=!1;try{for(r=r.call(e);!(c=(n=r.next()).done)&&(a.push(n.value),a.length!==t);c=!0);}catch(e){i=!0,o=e}finally{try{c||null==r.return||r.return()}finally{if(i)throw o}}return a}}(t,2)||function(e,t){if(e){if("string"==typeof e)return i(e,2);var n=Object.prototype.toString.call(e).slice(8,-1);if("Object"===n&&e.constructor&&(n=e.constructor.name),"Map"===n||"Set"===n)return Array.from(n);if("Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n))return i(e,t)}}(t,2)||function(){throw TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()),u=n[0],s=n[1];return a().createElement(l.Provider,{value:{dataSource:u,setDataSource:s}},a().createElement(o.SchemaComponentOptions,{scope:c,components:{}},e.children))}},130:function(e,t,n){n.r(t),n.d(t,{BreadcumbTitle:function(){return d}});var o=n(482),r=n(772),a=n(721),c=n(156),i=n.n(c),l=n(128),u=n(612),s=n(551),p=n(469),d=function(){var e=(0,r.useApp)(),t=(0,l.useParams)().name,n=(0,r.useCompile)(),d=(0,r.useDataSourceManager)(),m=(d.getDataSource(t)||{}).displayName,y=(0,c.useContext)(u.DataSourceContext).dataSource,f=(0,c.useMemo)(function(){return y&&(null==y?void 0:y.name)===t?y:d.getDataSource(t)},[y,t]),b=(0,c.useMemo)(function(){var t=null==f?void 0:f.status,o=p.$.find(function(e){return e.value===t}),r=[{title:i().createElement(l.Link,{to:e.pluginSettingsManager.getRoutePath(s.A7)},(0,s.KQ)("Data source manager"))}];return f&&r.push({title:i().createElement(a.Space,null,i().createElement("span",null,n(m)),t&&i().createElement(a.Tag,{key:t,color:null==o?void 0:o.color},n(null==o?void 0:o.label)))}),r},[f]);return i().createElement(a.Breadcrumb,{separator:i().createElement(o.RightOutlined,null),items:b})}},239:function(e,t,n){n.r(t),n.d(t,{addDatasourceCollections:function(){return b},useCreateDatabaseServer:function(){return m},useLoadCollections:function(){return f},useTestConnectionAction:function(){return y}});var o,r=n(505),a=n(721),c=n(238),i=n(772),l=n(551);function u(e,t,n,o,r,a,c){try{var i=e[a](c),l=i.value}catch(e){n(e);return}i.done?t(l):Promise.resolve(l).then(o,r)}function s(e){return function(){var t=this,n=arguments;return new Promise(function(o,r){var a=e.apply(t,n);function c(e){u(a,o,r,c,i,"next",e)}function i(e){u(a,o,r,c,i,"throw",e)}c(void 0)})}}function p(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{},o=Object.keys(n);"function"==typeof Object.getOwnPropertySymbols&&(o=o.concat(Object.getOwnPropertySymbols(n).filter(function(e){return Object.getOwnPropertyDescriptor(n,e).enumerable}))),o.forEach(function(t){var o;o=n[t],t in e?Object.defineProperty(e,t,{value:o,enumerable:!0,configurable:!0,writable:!0}):e[t]=o})}return e}function d(e,t){var n,o,r,a,c={label:0,sent:function(){if(1&r[0])throw r[1];return r[1]},trys:[],ops:[]};return a={next:i(0),throw:i(1),return:i(2)},"function"==typeof Symbol&&(a[Symbol.iterator]=function(){return this}),a;function i(a){return function(i){var l=[a,i];if(n)throw TypeError("Generator is already executing.");for(;c;)try{if(n=1,o&&(r=2&l[0]?o.return:l[0]?o.throw||((r=o.return)&&r.call(o),0):o.next)&&!(r=r.call(o,l[1])).done)return r;switch(o=0,r&&(l=[2&l[0],r.value]),l[0]){case 0:case 1:r=l;break;case 4:return c.label++,{value:l[1],done:!1};case 5:c.label++,o=l[1],l=[0];continue;case 7:l=c.ops.pop(),c.trys.pop();continue;default:if(!(r=(r=c.trys).length>0&&r[r.length-1])&&(6===l[0]||2===l[0])){c=0;continue}if(3===l[0]&&(!r||l[1]>r[0]&&l[1]<r[3])){c.label=l[1];break}if(6===l[0]&&c.label<r[1]){c.label=r[1],r=l;break}if(r&&c.label<r[2]){c.label=r[2],c.ops.push(l);break}r[2]&&c.ops.pop(),c.trys.pop();continue}l=t.call(e,c)}catch(e){l=[6,e],o=0}finally{n=r=0}if(5&l[0])throw l[1];return{value:l[0]?l[1]:void 0,done:!0}}}}var m=function(e){var t=(0,r.useForm)(),n=(0,i.useActionContext)(),o=(0,i.useAPIClient)(),l=(0,c.useTranslation)().t,u=(0,r.useField)();return u.data=u.data||{},{run:function(){return s(function(){var r,c;return d(this,function(i){switch(i.label){case 0:return[4,t.submit()];case 1:i.sent(),i.label=2;case 2:return i.trys.push([2,5,,6]),u.data.loading=!0,[4,o.resource("databaseServers").create({values:p({},t.values)})];case 3:return r=i.sent().data,u.data.loading=!1,n.setVisible(!1),[4,t.reset()];case 4:return i.sent(),null==e||e(null==r?void 0:r.data),a.message.success(l("Saved successfully")),[3,6];case 5:return c=i.sent(),u.data.loading=!1,console.log(c),[3,6];case 6:return[2]}})})()}}},y=function(){var e=(0,r.useForm)(),t=(0,i.useAPIClient)(),n=(0,c.useTranslation)().t,o=(0,r.useField)();return o.data=o.data||{},{run:function(){return s(function(){var r;return d(this,function(c){switch(c.label){case 0:return[4,e.submit()];case 1:c.sent(),c.label=2;case 2:return c.trys.push([2,4,,5]),o.data.loading=!0,[4,t.resource("dataSources").testConnection({values:p({},e.values)})];case 3:return c.sent(),o.data.loading=!1,a.message.success(n("Connection successful",{ns:l.A7})),[3,5];case 4:return r=c.sent(),o.data.loading=!1,console.log(r),[3,5];case 5:return[2]}})})()}}},f=function(){var e,t=(0,i.useAPIClient)();return e=s(function(e){return d(this,function(n){switch(n.label){case 0:return[4,t.request({url:"dataSources/".concat(e,"/collections:all"),method:"get"})];case 1:return[2,n.sent().data]}})}),function(t){return e.apply(this,arguments)}},b=(o=s(function(e,t,n){var o,r,a,c,i,l,u,s,p,m,y;return d(this,function(d){switch(d.label){case 0:if(o="dataSources/".concat(t,"/collections:add"),r=n.collections,a=n.dbOptions,!r.length)return[3,2];c=[],i=!0,l=!1,u=void 0;try{for(s=r[Symbol.iterator]();!(i=(p=s.next()).done);i=!0)y=(m=p.value).name,m.selected&&c.push(y)}catch(e){l=!0,u=e}finally{try{i||null==s.return||s.return()}finally{if(l)throw u}}return[4,e.request({url:o,method:"post",data:{dbOptions:a,collections:c}})];case 1:d.sent(),d.label=2;case 2:return[2]}})}),function(e,t,n){return o.apply(this,arguments)})},469:function(e,t,n){n.d(t,{$:function(){return l},j:function(){return s}});var o,r,a,c=n(875),i=n(551),l=[{value:"loading",label:'{{t("Loading",{ns:"'.concat(i.A7,'"})}}'),color:"orange"},{value:"loading-failed",label:'{{t("Failed",{ns:"'.concat(i.A7,'"})}}'),color:"red"},{value:"loaded",label:'{{t("Loaded",{ns:"'.concat(i.A7,'"})}}'),color:"green"},{value:"reloading",label:'{{t("Reloading",{ns:"'.concat(i.A7,'"})}}'),color:"orange"}],u={name:"collections-"+(0,c.uid)(),fields:[{type:"string",name:"key",interface:"input",uiSchema:{title:'{{t("Data source name",{ ns: "'.concat(i.A7,'" })}}'),type:"string","x-component":"Input",required:!0}},{type:"string",name:"displayName",interface:"input",uiSchema:{title:'{{t("Data source display name",{ ns: "'.concat(i.A7,'" })}}'),type:"string","x-component":"Input",required:!0}},{type:"string",name:"type",interface:"select",uiSchema:{title:'{{t("Type", { ns: "'.concat(i.A7,'" })}}'),type:"string","x-component":"Select",enum:"{{types}}"}},{type:"string",name:"status",interface:"select",uiSchema:{title:'{{t("Status", { ns: "'.concat(i.A7,'" })}}'),type:"string","x-component":"Select",enum:l}},{type:"boolean",name:"enabled",uiSchema:{type:"boolean",title:'{{t("Enabled")}}',"x-component":"Checkbox"}}]},s={type:"object",properties:(o={},r=(0,c.uid)(),a={type:"void","x-decorator":"ResourceActionProvider","x-decorator-props":{collection:u,resourceName:"dataSources",request:{resource:"dataSources",action:"list",params:{pageSize:50,appends:[]}}},"x-component":"CollectionProvider_deprecated","x-component-props":{collection:u},properties:{actions:{type:"void","x-component":"ActionBar","x-component-props":{style:{marginBottom:16}},properties:{refresh:{type:"void",title:'{{ t("Refresh") }}',"x-component":"Action","x-use-component-props":"useRefreshActionProps","x-component-props":{icon:"ReloadOutlined"}},delete:{type:"void",title:'{{ t("Delete") }}',"x-component":"Action","x-component-props":{icon:"DeleteOutlined",useAction:"{{ cm.useBulkDestroyAction }}",confirm:{title:"{{t('Delete')}}",content:"{{t('Are you sure you want to delete it?')}}"},actionCallback:"{{ dataSourceDeleteCallback }}"}},create:{type:"void",title:'{{t("Add new")}}',"x-component":"CreateDatabaseConnectAction","x-component-props":{type:"primary"}}}},table:{type:"void","x-uid":"input","x-component":"Table.Void","x-component-props":{rowKey:"key",rowSelection:{type:"checkbox"},useDataSource:"{{ cm.useDataSourceFromRAC }}"},properties:{key:{type:"void","x-decorator":"Table.Column.Decorator","x-component":"Table.Column",properties:{key:{type:"string","x-component":"CollectionField","x-read-pretty":!0}}},displayName:{type:"void","x-decorator":"Table.Column.Decorator","x-component":"Table.Column",properties:{displayName:{type:"string","x-component":"CollectionField","x-read-pretty":!0}}},type:{type:"void","x-decorator":"Table.Column.Decorator","x-component":"Table.Column",properties:{type:{type:"string","x-component":"CollectionField","x-read-pretty":!0}}},status:{type:"void","x-decorator":"Table.Column.Decorator","x-component":"Table.Column",properties:{status:{type:"string","x-component":"CollectionField","x-read-pretty":!0}}},enabled:{type:"void","x-decorator":"Table.Column.Decorator","x-component":"Table.Column",properties:{enabled:{type:"string","x-component":"CollectionField","x-read-pretty":!0}}},actions:{type:"void",title:'{{t("Actions")}}',"x-component":"Table.Column",properties:{actions:{type:"void","x-component":"Space","x-component-props":{},properties:{view:{type:"void",title:'{{t("View")}}',"x-component":"ViewDatabaseConnectionAction","x-component-props":{type:"primary"}},update:{type:"void",title:'{{t("Edit")}}',"x-component":"EditDatabaseConnectionAction","x-component-props":{type:"primary"},"x-reactions":["{{useIsAbleDelete($self)}}"]},delete:{type:"void",title:'{{ t("Delete") }}',"x-component":"Action.Link","x-component-props":{confirm:{title:'{{t("Delete")}}',content:'{{t("Are you sure you want to delete it?")}}'},useAction:"{{useDestroyAction}}"},"x-reactions":["{{useIsAbleDelete($self)}}"]}}}}}}}}},r in o?Object.defineProperty(o,r,{value:a,enumerable:!0,configurable:!0,writable:!0}):o[r]=a,o)}}}]);
|