@nocobase/plugin-data-source-manager 0.21.0-alpha.8 → 1.0.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.
@@ -1,7 +1,7 @@
1
1
  module.exports = {
2
- "@nocobase/client": "0.21.0-alpha.8",
2
+ "@nocobase/client": "1.0.0-alpha.1",
3
3
  "react": "18.2.0",
4
- "@nocobase/server": "0.21.0-alpha.8",
4
+ "@nocobase/server": "1.0.0-alpha.1",
5
5
  "lodash": "4.17.21",
6
6
  "@ant-design/icons": "5.2.6",
7
7
  "antd": "5.12.8",
@@ -10,10 +10,10 @@ module.exports = {
10
10
  "react-i18next": "11.18.6",
11
11
  "@formily/react": "2.3.0",
12
12
  "@emotion/css": "11.11.2",
13
- "@nocobase/database": "0.21.0-alpha.8",
14
- "@nocobase/utils": "0.21.0-alpha.8",
15
- "@nocobase/plugin-acl": "0.21.0-alpha.8",
16
- "@nocobase/acl": "0.21.0-alpha.8",
13
+ "@nocobase/database": "1.0.0-alpha.1",
14
+ "@nocobase/utils": "1.0.0-alpha.1",
15
+ "@nocobase/plugin-acl": "1.0.0-alpha.1",
16
+ "@nocobase/acl": "1.0.0-alpha.1",
17
17
  "sequelize": "6.35.2",
18
18
  "@formily/antd-v5": "1.1.9",
19
19
  "@formily/core": "2.3.0",
@@ -22,6 +22,7 @@ __export(migrate_acl_resources_exports, {
22
22
  module.exports = __toCommonJS(migrate_acl_resources_exports);
23
23
  var import_server = require("@nocobase/server");
24
24
  var import_utils = require("@nocobase/utils");
25
+ /* istanbul ignore file -- @preserve */
25
26
  class migrate_acl_resources_default extends import_server.Migration {
26
27
  on = "afterSync";
27
28
  // 'beforeLoad' or 'afterLoad'
@@ -63,10 +63,7 @@ class DataSourceModel extends import_database.Model {
63
63
  async loadIntoACL(options) {
64
64
  const { app, acl } = options;
65
65
  const loadRoleIntoACL = async (model) => {
66
- const pluginACL = app.pm.get("acl");
67
66
  await model.writeToAcl({
68
- grantHelper: pluginACL.grantHelper,
69
- associationFieldsActions: pluginACL.associationFieldsActions,
70
67
  acl
71
68
  });
72
69
  };
@@ -93,7 +90,8 @@ class DataSourceModel extends import_database.Model {
93
90
  const createOptions = this.get("options");
94
91
  const dataSource = app.dataSourceManager.factory.create(type, {
95
92
  ...createOptions,
96
- name: this.get("key")
93
+ name: dataSourceKey,
94
+ logger: app.logger.child({ dataSourceKey })
97
95
  });
98
96
  if (loadAtAfterStart) {
99
97
  dataSource.on("loadMessage", ({ message }) => {
@@ -5,7 +5,5 @@ export declare class DataSourcesRolesModel extends Model {
5
5
  writeToAcl(options: {
6
6
  acl: ACL;
7
7
  transaction?: Transaction;
8
- associationFieldsActions: any;
9
- grantHelper: any;
10
8
  }): Promise<void>;
11
9
  }
@@ -44,9 +44,7 @@ class DataSourcesRolesModel extends import_database.Model {
44
44
  for (const resource of resources) {
45
45
  await resource.writeToACL({
46
46
  acl,
47
- transaction,
48
- grantHelper: options.grantHelper,
49
- associationFieldsActions: options.associationFieldsActions
47
+ transaction
50
48
  });
51
49
  }
52
50
  }
@@ -303,31 +303,31 @@ class PluginDataSourceManagerServer extends import_server.Plugin {
303
303
  this.app.setMaintainingMessage("Loading data sources...");
304
304
  await Promise.all(loadPromises);
305
305
  });
306
- this.app.db.on("dataSourcesRolesResources.afterSaveWithAssociations", async (model, options) => {
307
- const { transaction } = options;
308
- const pluginACL = this.app.pm.get("acl");
309
- const dataSource = this.app.dataSourceManager.dataSources.get(model.get("dataSourceKey"));
310
- await model.writeToACL({
311
- acl: dataSource.acl,
312
- associationFieldsActions: pluginACL.associationFieldsActions,
313
- transaction,
314
- grantHelper: pluginACL.grantHelper
315
- });
316
- });
317
- this.app.db.on("dataSourcesRolesResourcesActions.afterUpdateWithAssociations", async (model, options) => {
318
- const { transaction } = options;
319
- const resource = await model.getResource({
320
- transaction
321
- });
322
- const pluginACL = this.app.pm.get("acl");
323
- const dataSource = this.app.dataSourceManager.dataSources.get(resource.get("dataSourceKey"));
324
- await resource.writeToACL({
325
- acl: dataSource.acl,
326
- associationFieldsActions: pluginACL.associationFieldsActions,
327
- transaction,
328
- grantHelper: pluginACL.grantHelper
329
- });
330
- });
306
+ this.app.db.on(
307
+ "dataSourcesRolesResources.afterSaveWithAssociations",
308
+ async (model, options) => {
309
+ const { transaction } = options;
310
+ const dataSource = this.app.dataSourceManager.dataSources.get(model.get("dataSourceKey"));
311
+ await model.writeToACL({
312
+ acl: dataSource.acl,
313
+ transaction
314
+ });
315
+ }
316
+ );
317
+ this.app.db.on(
318
+ "dataSourcesRolesResourcesActions.afterUpdateWithAssociations",
319
+ async (model, options) => {
320
+ const { transaction } = options;
321
+ const resource = await model.getResource({
322
+ transaction
323
+ });
324
+ const dataSource = this.app.dataSourceManager.dataSources.get(resource.get("dataSourceKey"));
325
+ await resource.writeToACL({
326
+ acl: dataSource.acl,
327
+ transaction
328
+ });
329
+ }
330
+ );
331
331
  this.app.db.on("dataSourcesRolesResources.afterDestroy", async (model, options) => {
332
332
  const dataSource = this.app.dataSourceManager.dataSources.get(model.get("dataSourceKey"));
333
333
  const roleName = model.get("roleName");
@@ -338,11 +338,8 @@ class PluginDataSourceManagerServer extends import_server.Plugin {
338
338
  });
339
339
  this.app.db.on("dataSourcesRoles.afterSave", async (model, options) => {
340
340
  const { transaction } = options;
341
- const pluginACL = this.app.pm.get("acl");
342
341
  const dataSource = this.app.dataSourceManager.dataSources.get(model.get("dataSourceKey"));
343
342
  await model.writeToAcl({
344
- grantHelper: pluginACL.grantHelper,
345
- associationFieldsActions: pluginACL.associationFieldsActions,
346
343
  acl: dataSource.acl,
347
344
  transaction
348
345
  });
@@ -359,7 +356,6 @@ class PluginDataSourceManagerServer extends import_server.Plugin {
359
356
  });
360
357
  this.app.on("acl:writeResources", async ({ roleName, transaction }) => {
361
358
  const dataSource = this.app.dataSourceManager.dataSources.get("main");
362
- const pluginACL = this.app.pm.get("acl");
363
359
  const dataSourceRole = await this.app.db.getRepository("dataSourcesRoles").findOne({
364
360
  filter: {
365
361
  dataSourceKey: "main",
@@ -368,8 +364,6 @@ class PluginDataSourceManagerServer extends import_server.Plugin {
368
364
  transaction
369
365
  });
370
366
  await dataSourceRole.writeToAcl({
371
- grantHelper: pluginACL.grantHelper,
372
- associationFieldsActions: pluginACL.associationFieldsActions,
373
367
  acl: dataSource.acl,
374
368
  transaction
375
369
  });
@@ -20,6 +20,7 @@ __export(type_interface_map_exports, {
20
20
  default: () => type_interface_map_default
21
21
  });
22
22
  module.exports = __toCommonJS(type_interface_map_exports);
23
+ /* istanbul ignore file -- @preserve */
23
24
  const typeInterfaceMap = {
24
25
  array: "",
25
26
  belongsTo: "",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nocobase/plugin-data-source-manager",
3
- "version": "0.21.0-alpha.8",
3
+ "version": "1.0.0-alpha.1",
4
4
  "main": "dist/server/index.js",
5
5
  "displayName": "Data source manager",
6
6
  "displayName.zh-CN": "数据源管理",
@@ -9,12 +9,12 @@
9
9
  "homepage": "https://docs.nocobase.com/handbook/data-source-manager",
10
10
  "homepage.zh-CN": "https://docs-cn.nocobase.com/handbook/data-source-manager",
11
11
  "peerDependencies": {
12
- "@nocobase/client": "0.x",
13
- "@nocobase/server": "0.x",
14
- "@nocobase/test": "0.x"
12
+ "@nocobase/client": "1.x",
13
+ "@nocobase/server": "1.x",
14
+ "@nocobase/test": "1.x"
15
15
  },
16
16
  "keywords": [
17
17
  "Data model tools"
18
18
  ],
19
- "gitHead": "bed69485709bc8e268285b50debfc1ebfd7a30c7"
19
+ "gitHead": "c73b6d2032a6151fdfeebeb0ec923d766578c53c"
20
20
  }
@@ -1,6 +0,0 @@
1
- import { Database } from '@nocobase/database';
2
- export declare class CollectionManager {
3
- private db;
4
- constructor(db: Database);
5
- findAndCount(): void;
6
- }
@@ -1,33 +0,0 @@
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
- });
@@ -1,70 +0,0 @@
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 {};
@@ -1,235 +0,0 @@
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
- });