@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,88 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+ var data_sources_resources_exports = {};
19
+ __export(data_sources_resources_exports, {
20
+ default: () => data_sources_resources_default
21
+ });
22
+ module.exports = __toCommonJS(data_sources_resources_exports);
23
+ var data_sources_resources_default = {
24
+ name: "roles.dataSourceResources",
25
+ actions: {
26
+ async create(ctx, next) {
27
+ const { associatedIndex: roleName } = ctx.action.params;
28
+ const db = ctx.db;
29
+ const transaction = await db.sequelize.transaction();
30
+ const dataSourceKey = ctx.action.params.values.dataSourceKey;
31
+ if (!dataSourceKey) {
32
+ throw new Error("dataSourceKey is required");
33
+ }
34
+ const connectionRole = await db.getRepository("dataSourcesRoles").findOne({
35
+ filter: {
36
+ roleName,
37
+ dataSourceKey
38
+ },
39
+ transaction
40
+ });
41
+ if (!connectionRole) {
42
+ await db.getRepository("dataSourcesRoles").create({
43
+ values: {
44
+ roleName,
45
+ dataSourceKey
46
+ },
47
+ transaction
48
+ });
49
+ }
50
+ const record = await db.getRepository("dataSourcesRolesResources").create({
51
+ values: {
52
+ roleName,
53
+ ...ctx.action.params.values
54
+ },
55
+ transaction
56
+ });
57
+ await transaction.commit();
58
+ ctx.body = record.toJSON();
59
+ await next();
60
+ },
61
+ async update(ctx, next) {
62
+ const { associatedIndex: roleName } = ctx.action.params;
63
+ ctx.body = await ctx.db.getRepository("dataSourcesRolesResources").update({
64
+ filter: {
65
+ roleName,
66
+ dataSourceKey: ctx.action.params.filter.dataSourceKey,
67
+ name: ctx.action.params.filter.name
68
+ },
69
+ values: ctx.action.params.values,
70
+ updateAssociationValues: ["actions"]
71
+ });
72
+ await next();
73
+ },
74
+ async get(ctx, next) {
75
+ const { associatedIndex: roleName } = ctx.action.params;
76
+ const record = await ctx.db.getRepository("dataSourcesRolesResources").findOne({
77
+ filter: {
78
+ roleName,
79
+ dataSourceKey: ctx.action.params.filter.dataSourceKey,
80
+ name: ctx.action.params.filter.name
81
+ },
82
+ appends: ctx.action.params.appends
83
+ });
84
+ ctx.body = record;
85
+ await next();
86
+ }
87
+ }
88
+ };
@@ -0,0 +1,8 @@
1
+ declare const _default: {
2
+ name: string;
3
+ actions: {
4
+ update(ctx: any, next: any): Promise<void>;
5
+ get(ctx: any, next: any): Promise<void>;
6
+ };
7
+ };
8
+ export default _default;
@@ -0,0 +1,72 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+ var data_sources_roles_exports = {};
19
+ __export(data_sources_roles_exports, {
20
+ default: () => data_sources_roles_default
21
+ });
22
+ module.exports = __toCommonJS(data_sources_roles_exports);
23
+ var data_sources_roles_default = {
24
+ name: "dataSources.roles",
25
+ actions: {
26
+ async update(ctx, next) {
27
+ const params = ctx.action.params;
28
+ const { filterByTk: name, associatedIndex: dataSourceKey } = params;
29
+ let connectionRoleRecord = await ctx.db.getRepository("dataSourcesRoles").findOne({
30
+ filter: {
31
+ roleName: name,
32
+ dataSourceKey
33
+ }
34
+ });
35
+ if (!connectionRoleRecord) {
36
+ connectionRoleRecord = await ctx.db.getRepository("dataSourcesRoles").create({
37
+ values: {
38
+ ...params.values,
39
+ roleName: name,
40
+ dataSourceKey
41
+ }
42
+ });
43
+ } else {
44
+ await connectionRoleRecord.update({
45
+ ...params.values
46
+ });
47
+ }
48
+ ctx.body = connectionRoleRecord.toJSON();
49
+ await next();
50
+ },
51
+ async get(ctx, next) {
52
+ const params = ctx.action.params;
53
+ const { filterByTk: name, associatedIndex: dataSourceKey } = params;
54
+ let connectionRoleRecord = await ctx.db.getRepository("dataSourcesRoles").findOne({
55
+ filter: {
56
+ roleName: name,
57
+ dataSourceKey
58
+ }
59
+ });
60
+ if (!connectionRoleRecord) {
61
+ connectionRoleRecord = await ctx.db.getRepository("dataSourcesRoles").create({
62
+ values: {
63
+ roleName: name,
64
+ dataSourceKey
65
+ }
66
+ });
67
+ }
68
+ ctx.body = connectionRoleRecord.toJSON();
69
+ await next();
70
+ }
71
+ }
72
+ };
@@ -0,0 +1,7 @@
1
+ declare const rolesRemoteCollectionsResourcer: {
2
+ name: string;
3
+ actions: {
4
+ list(ctx: any, next: any): Promise<void>;
5
+ };
6
+ };
7
+ export { rolesRemoteCollectionsResourcer };
@@ -0,0 +1,101 @@
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 roles_data_sources_collections_exports = {};
29
+ __export(roles_data_sources_collections_exports, {
30
+ rolesRemoteCollectionsResourcer: () => rolesRemoteCollectionsResourcer
31
+ });
32
+ module.exports = __toCommonJS(roles_data_sources_collections_exports);
33
+ var import_full_data_repository = require("../services/full-data-repository");
34
+ var import_lodash = __toESM(require("lodash"));
35
+ function totalPage(total, pageSize) {
36
+ return Math.ceil(total / pageSize);
37
+ }
38
+ const rolesRemoteCollectionsResourcer = {
39
+ name: "roles.dataSourcesCollections",
40
+ actions: {
41
+ async list(ctx, next) {
42
+ var _a, _b;
43
+ const role = ctx.action.params.associatedIndex;
44
+ const { page = 1, pageSize = 20 } = ctx.action.params;
45
+ const { filter } = ctx.action.params;
46
+ const { dataSourceKey } = filter;
47
+ const dataSource = ctx.app.dataSourceManager.dataSources.get(dataSourceKey);
48
+ const collectionRepository = new import_full_data_repository.FullDataRepository(dataSource.collectionManager.getCollections());
49
+ const [collections, count] = await collectionRepository.findAndCount();
50
+ const filterItem = import_lodash.default.get(filter, "$and");
51
+ const filterByTitle = filterItem == null ? void 0 : filterItem.find((item) => item.title);
52
+ const filterByName = filterItem == null ? void 0 : filterItem.find((item) => item.name);
53
+ const filterTitle = (_a = import_lodash.default.get(filterByTitle, "title.$includes")) == null ? void 0 : _a.toLowerCase();
54
+ const filterName = (_b = import_lodash.default.get(filterByName, "name.$includes")) == null ? void 0 : _b.toLowerCase();
55
+ const roleResources = await ctx.app.db.getRepository("dataSourcesRolesResources").find({
56
+ filter: {
57
+ roleName: role,
58
+ dataSourceKey
59
+ }
60
+ });
61
+ const roleResourcesNames = roleResources.map((roleResource) => roleResource.get("name"));
62
+ const roleResourceActionResourceNames = roleResources.filter((roleResources2) => roleResources2.get("usingActionsConfig")).map((roleResources2) => roleResources2.get("name"));
63
+ const items = import_lodash.default.sortBy(
64
+ collections.filter((collection) => {
65
+ var _a2;
66
+ return (!filterTitle || ((_a2 = import_lodash.default.get(collection, "options.title")) == null ? void 0 : _a2.toLowerCase().includes(filterTitle))) && (!filterName || collection.options.name.toLowerCase().includes(filterName));
67
+ }).map((collection, i) => {
68
+ var _a2;
69
+ const collectionName = collection.options.name;
70
+ const exists = roleResourcesNames.includes(collectionName);
71
+ const usingConfig = roleResourceActionResourceNames.includes(collectionName) ? "resourceAction" : "strategy";
72
+ return {
73
+ type: "collection",
74
+ name: collectionName,
75
+ collectionName,
76
+ title: ((_a2 = collection.options.uiSchema) == null ? void 0 : _a2.title) || collection.options.title,
77
+ roleName: role,
78
+ usingConfig,
79
+ exists,
80
+ fields: [...collection.fields.values()].map((field) => {
81
+ return field.options;
82
+ })
83
+ };
84
+ }),
85
+ "name"
86
+ );
87
+ ctx.body = {
88
+ count,
89
+ rows: items,
90
+ page: Number(page),
91
+ pageSize: Number(pageSize),
92
+ totalPage: totalPage(count, pageSize)
93
+ };
94
+ await next();
95
+ }
96
+ }
97
+ };
98
+ // Annotate the CommonJS export names for ESM import in node:
99
+ 0 && (module.exports = {
100
+ rolesRemoteCollectionsResourcer
101
+ });
@@ -0,0 +1,6 @@
1
+ import { Database } from '@nocobase/database';
2
+ export declare class CollectionManager {
3
+ private db;
4
+ constructor(db: Database);
5
+ findAndCount(): void;
6
+ }
@@ -0,0 +1,33 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+ var collection_manager_exports = {};
19
+ __export(collection_manager_exports, {
20
+ CollectionManager: () => CollectionManager
21
+ });
22
+ module.exports = __toCommonJS(collection_manager_exports);
23
+ class CollectionManager {
24
+ constructor(db) {
25
+ this.db = db;
26
+ }
27
+ findAndCount() {
28
+ }
29
+ }
30
+ // Annotate the CommonJS export names for ESM import in node:
31
+ 0 && (module.exports = {
32
+ CollectionManager
33
+ });
@@ -0,0 +1,70 @@
1
+ import { Database } from '@nocobase/database';
2
+ type tableInfo = {
3
+ tableName: string;
4
+ schema?: string;
5
+ };
6
+ type GetCollectionOptions = {
7
+ tableInfo: tableInfo;
8
+ localOptions?: PartialCollectionOptions;
9
+ };
10
+ export type LocalData = {
11
+ [collectionName: string]: PartialCollectionOptions;
12
+ };
13
+ type GetCollectionsOptions = {
14
+ localData?: LocalData;
15
+ };
16
+ export type PartialCollectionOptions = Partial<Omit<CollectionOptions, 'fields'> & {
17
+ fields?: Partial<FieldOptions>[];
18
+ }>;
19
+ interface CollectionOptions {
20
+ name: string;
21
+ schema?: string;
22
+ tableName: string;
23
+ title?: string;
24
+ timestamps?: boolean;
25
+ filterTargetKey?: string;
26
+ fields: FieldOptions[];
27
+ unsupportedFields?: UnsupportedFieldOptions[];
28
+ }
29
+ interface UnsupportedFieldOptions {
30
+ rawType: string;
31
+ name: string;
32
+ supported: false;
33
+ }
34
+ interface FieldOptions {
35
+ name: string;
36
+ field: string;
37
+ rawType: string;
38
+ type: string;
39
+ description?: string;
40
+ interface?: string;
41
+ uiSchema?: any;
42
+ possibleTypes?: string[];
43
+ defaultValue?: any;
44
+ primaryKey: boolean;
45
+ unique: boolean;
46
+ allowNull?: boolean;
47
+ autoIncrement?: boolean;
48
+ }
49
+ export declare class DatabaseIntrospector {
50
+ private db;
51
+ constructor(db: Database);
52
+ getCollections(options?: GetCollectionsOptions): Promise<Array<CollectionOptions>>;
53
+ getCollection(options: GetCollectionOptions): Promise<CollectionOptions>;
54
+ loadCollection(options: CollectionOptions): void;
55
+ loadCollections(options: {
56
+ collections: CollectionOptions[];
57
+ }): void;
58
+ tableInfoToCollectionOptions(tableInfo: tableInfo): {
59
+ name: string;
60
+ schema?: string;
61
+ tableName: string;
62
+ title?: string;
63
+ };
64
+ private collectionOptionsByFields;
65
+ private mergeLocalDataIntoCollectionOptions;
66
+ private columnInfoToFieldOptions;
67
+ private getDefaultInterfaceByType;
68
+ private columnAttribute;
69
+ }
70
+ export {};
@@ -0,0 +1,235 @@
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 database_introspector_exports = {};
29
+ __export(database_introspector_exports, {
30
+ DatabaseIntrospector: () => DatabaseIntrospector
31
+ });
32
+ module.exports = __toCommonJS(database_introspector_exports);
33
+ var import_database = require("@nocobase/database");
34
+ var import_lodash = __toESM(require("lodash"));
35
+ var import_type_interface_map = __toESM(require("./type-interface-map"));
36
+ class DatabaseIntrospector {
37
+ constructor(db) {
38
+ this.db = db;
39
+ }
40
+ async getCollections(options = {}) {
41
+ const tableList = await this.db.sequelize.getQueryInterface().showAllTables();
42
+ const batchSize = 5;
43
+ const results = [];
44
+ for (let i = 0; i < tableList.length; i += batchSize) {
45
+ const batch = tableList.slice(i, i + batchSize);
46
+ const batchResults = await Promise.all(
47
+ batch.map(async (tableName) => {
48
+ var _a;
49
+ const tableInfo = {
50
+ tableName
51
+ };
52
+ const collectionOptions = this.tableInfoToCollectionOptions(tableInfo);
53
+ const localOptions = (_a = options.localData) == null ? void 0 : _a[collectionOptions.name];
54
+ try {
55
+ return await this.getCollection({
56
+ tableInfo,
57
+ localOptions
58
+ });
59
+ } catch (e) {
60
+ if (e.message.includes("No description found for")) {
61
+ return false;
62
+ }
63
+ throw e;
64
+ }
65
+ })
66
+ );
67
+ results.push(...batchResults);
68
+ }
69
+ return results.filter(Boolean);
70
+ }
71
+ async getCollection(options) {
72
+ const { tableInfo } = options;
73
+ const columnsInfo = await this.db.sequelize.getQueryInterface().describeTable(tableInfo);
74
+ const collectionOptions = this.tableInfoToCollectionOptions(tableInfo);
75
+ const constraints = await this.db.sequelize.getQueryInterface().showIndex(this.db.inDialect("postgres") ? tableInfo : tableInfo.tableName);
76
+ try {
77
+ const fields = Object.keys(columnsInfo).map((columnName) => {
78
+ return this.columnInfoToFieldOptions(columnsInfo, columnName, constraints);
79
+ });
80
+ const unsupportedFields = fields.filter((field) => {
81
+ return field.supported === false;
82
+ });
83
+ const supportFields = fields.filter((field) => {
84
+ return field.supported !== false;
85
+ });
86
+ const remoteCollectionInfo = {
87
+ ...collectionOptions,
88
+ ...this.collectionOptionsByFields(supportFields),
89
+ fields: supportFields
90
+ };
91
+ if (unsupportedFields.length) {
92
+ remoteCollectionInfo.unsupportedFields = unsupportedFields;
93
+ }
94
+ return this.mergeLocalDataIntoCollectionOptions(remoteCollectionInfo, options.localOptions);
95
+ } catch (e) {
96
+ throw new Error(`table ${tableInfo.tableName} introspection error: ${e.message}`);
97
+ }
98
+ }
99
+ loadCollection(options) {
100
+ this.db.collection({
101
+ ...options,
102
+ introspected: true
103
+ });
104
+ }
105
+ loadCollections(options) {
106
+ options.collections.forEach((collection) => {
107
+ this.loadCollection(collection);
108
+ });
109
+ }
110
+ tableInfoToCollectionOptions(tableInfo) {
111
+ return {
112
+ name: tableInfo.tableName,
113
+ title: tableInfo.tableName,
114
+ schema: tableInfo.schema,
115
+ tableName: tableInfo.tableName
116
+ };
117
+ }
118
+ collectionOptionsByFields(fields) {
119
+ const options = {
120
+ timestamps: false
121
+ };
122
+ const autoIncrementField = fields.find((field) => field.autoIncrement);
123
+ if (autoIncrementField) {
124
+ options.filterTargetKey = autoIncrementField.name;
125
+ }
126
+ const primaryKeys = fields.filter((field) => field.primaryKey);
127
+ if (!options.filterTargetKey && primaryKeys.length == 1) {
128
+ options.filterTargetKey = primaryKeys[0].name;
129
+ }
130
+ const uniques = fields.filter((field) => field.unique);
131
+ if (!options.filterTargetKey && uniques.length == 1) {
132
+ options.filterTargetKey = uniques[0].name;
133
+ }
134
+ return options;
135
+ }
136
+ mergeLocalDataIntoCollectionOptions(collectionOptions, localData) {
137
+ var _a;
138
+ if (!localData) {
139
+ return collectionOptions;
140
+ }
141
+ const collectionFields = collectionOptions.fields || [];
142
+ const localFieldsAsObject = import_lodash.default.keyBy(localData.fields, "name");
143
+ const newFields = collectionFields.map((field) => {
144
+ const localField = localFieldsAsObject[field.name];
145
+ if (!localField) {
146
+ return field;
147
+ }
148
+ return {
149
+ ...field,
150
+ ...localField
151
+ };
152
+ });
153
+ const localAssociationFields = (_a = localData.fields) == null ? void 0 : _a.filter((field) => {
154
+ return ["belongsTo", "belongsToMany", "hasMany", "hasOne"].includes(field.type);
155
+ });
156
+ if (localAssociationFields) {
157
+ newFields.push(...localAssociationFields);
158
+ }
159
+ return {
160
+ ...collectionOptions,
161
+ ...import_lodash.default.omit(localData, ["fields"]),
162
+ fields: newFields
163
+ };
164
+ }
165
+ columnInfoToFieldOptions(columnsInfo, columnName, indexes) {
166
+ const columnInfo = columnsInfo[columnName];
167
+ let fieldOptions = {
168
+ ...this.columnAttribute(columnsInfo, columnName, indexes),
169
+ ...import_database.ViewFieldInference.inferToFieldType({
170
+ dialect: this.db.options.dialect,
171
+ type: columnInfo.type,
172
+ name: columnName
173
+ }),
174
+ rawType: columnInfo.type,
175
+ name: columnName
176
+ };
177
+ const interfaceConfig = this.getDefaultInterfaceByType(columnsInfo, columnName, fieldOptions.type);
178
+ if (typeof interfaceConfig === "string") {
179
+ fieldOptions.interface = interfaceConfig;
180
+ } else {
181
+ fieldOptions = {
182
+ ...fieldOptions,
183
+ ...interfaceConfig
184
+ };
185
+ }
186
+ if (!fieldOptions.type) {
187
+ return {
188
+ rawType: columnInfo.type,
189
+ name: columnName,
190
+ supported: false
191
+ };
192
+ }
193
+ import_lodash.default.set(fieldOptions, "uiSchema.title", columnName);
194
+ return fieldOptions;
195
+ }
196
+ getDefaultInterfaceByType(columnsInfo, columnName, type) {
197
+ const interfaceConfig = import_type_interface_map.default[type];
198
+ if (typeof interfaceConfig === "function") {
199
+ return interfaceConfig(columnsInfo[columnName]);
200
+ }
201
+ return interfaceConfig;
202
+ }
203
+ columnAttribute(columnsInfo, columnName, indexes) {
204
+ const columnInfo = columnsInfo[columnName];
205
+ const attr = {
206
+ type: columnInfo.type,
207
+ allowNull: columnInfo.allowNull,
208
+ primaryKey: columnInfo.primaryKey,
209
+ unique: false
210
+ };
211
+ if (columnInfo.defaultValue && typeof columnInfo.defaultValue === "string") {
212
+ const isSerial = columnInfo.defaultValue.match(/^nextval\(/);
213
+ const isUUID = columnInfo.defaultValue.match(/^uuid_generate_v4\(/);
214
+ if (!isSerial && !isUUID) {
215
+ attr.defaultValue = columnInfo.defaultValue;
216
+ }
217
+ if (isSerial || isUUID) {
218
+ attr.autoIncrement = true;
219
+ }
220
+ }
221
+ for (const index of indexes) {
222
+ if (index.fields.length == 1 && index.fields[0].attribute == columnName && index.unique) {
223
+ attr.unique = true;
224
+ }
225
+ }
226
+ if (attr.primaryKey && columnName == "id") {
227
+ attr.autoIncrement = true;
228
+ }
229
+ return attr;
230
+ }
231
+ }
232
+ // Annotate the CommonJS export names for ESM import in node:
233
+ 0 && (module.exports = {
234
+ DatabaseIntrospector
235
+ });
@@ -0,0 +1,8 @@
1
+ import { CountOptions, FindOptions } from '@nocobase/database';
2
+ export declare class FullDataRepository<T> {
3
+ data: Array<T>;
4
+ constructor(data: Array<T>);
5
+ count(countOptions?: CountOptions): Promise<number>;
6
+ find(options?: FindOptions): Promise<Array<T>>;
7
+ findAndCount(options?: {}): Promise<[Array<T>, number]>;
8
+ }
@@ -0,0 +1,51 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+ var full_data_repository_exports = {};
19
+ __export(full_data_repository_exports, {
20
+ FullDataRepository: () => FullDataRepository
21
+ });
22
+ module.exports = __toCommonJS(full_data_repository_exports);
23
+ class FullDataRepository {
24
+ data = [];
25
+ constructor(data) {
26
+ this.data = data;
27
+ }
28
+ async count(countOptions) {
29
+ return this.data.length;
30
+ }
31
+ async find(options) {
32
+ const { limit, offset } = options || {};
33
+ let results = this.data;
34
+ if (offset) {
35
+ results = results.slice(offset);
36
+ }
37
+ if (limit) {
38
+ results = results.slice(0, limit);
39
+ }
40
+ return results;
41
+ }
42
+ async findAndCount(options = {}) {
43
+ const count = await this.count();
44
+ const results = count ? await this.find(options) : [];
45
+ return [results, count];
46
+ }
47
+ }
48
+ // Annotate the CommonJS export names for ESM import in node:
49
+ 0 && (module.exports = {
50
+ FullDataRepository
51
+ });