@nocobase/plugin-data-source-main 1.2.12-alpha → 1.2.14-alpha

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.
@@ -8,14 +8,14 @@
8
8
  */
9
9
 
10
10
  module.exports = {
11
- "@nocobase/client": "1.2.12-alpha",
12
- "@nocobase/database": "1.2.12-alpha",
13
- "@nocobase/plugin-error-handler": "1.2.12-alpha",
14
- "@nocobase/server": "1.2.12-alpha",
11
+ "@nocobase/client": "1.2.14-alpha",
12
+ "@nocobase/database": "1.2.14-alpha",
13
+ "@nocobase/plugin-error-handler": "1.2.14-alpha",
14
+ "@nocobase/server": "1.2.14-alpha",
15
15
  "async-mutex": "0.3.2",
16
16
  "lodash": "4.17.21",
17
- "@nocobase/test": "1.2.12-alpha",
18
- "@nocobase/utils": "1.2.12-alpha",
17
+ "@nocobase/test": "1.2.14-alpha",
18
+ "@nocobase/utils": "1.2.14-alpha",
19
19
  "sequelize": "6.35.2",
20
20
  "dayjs": "1.11.10"
21
21
  };
@@ -1,3 +1,4 @@
1
1
  {
2
- "field-name-exists": "Field name \"{{name}}\" already exists in collection \"{{collectionName}}\""
2
+ "field-name-exists": "Field name \"{{name}}\" already exists in collection \"{{collectionName}}\"",
3
+ "field-is-depended-on-by-other": "Can not delete field \"{{fieldName}}\" in \"{{fieldCollectionName}}\", it is used by field \"{{dependedFieldName}}\" in \"{{dependedFieldCollectionName}}\" as \"{{dependedFieldAs}}\""
3
4
  }
@@ -1,3 +1,4 @@
1
1
  {
2
- "field-name-exists": "字段标识 \"{{name}}\" 已存在"
2
+ "field-name-exists": "字段标识 \"{{name}}\" 已存在",
3
+ "field-is-depended-on-by-other": "无法删除 \"{{fieldCollectionName}}\" 中的 \"{{fieldName}}\" 字段,它被 \"{{dependedFieldCollectionName}}\" 中的 \"{{dependedFieldName}}\" 字段用作 \"{{dependedFieldAs}}\""
3
4
  }
@@ -0,0 +1,12 @@
1
+ type FieldIsDependedOnByOtherErrorOptions = {
2
+ fieldName: string;
3
+ fieldCollectionName: string;
4
+ dependedFieldName: string;
5
+ dependedFieldCollectionName: string;
6
+ dependedFieldAs: string;
7
+ };
8
+ export declare class FieldIsDependedOnByOtherError extends Error {
9
+ options: FieldIsDependedOnByOtherErrorOptions;
10
+ constructor(options: FieldIsDependedOnByOtherErrorOptions);
11
+ }
12
+ export {};
@@ -0,0 +1,44 @@
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 field_is_depended_on_by_other_exports = {};
28
+ __export(field_is_depended_on_by_other_exports, {
29
+ FieldIsDependedOnByOtherError: () => FieldIsDependedOnByOtherError
30
+ });
31
+ module.exports = __toCommonJS(field_is_depended_on_by_other_exports);
32
+ class FieldIsDependedOnByOtherError extends Error {
33
+ constructor(options) {
34
+ super(
35
+ `Can't delete field ${options.fieldName} of ${options.fieldCollectionName}, it is used by field ${options.dependedFieldName} in collection ${options.dependedFieldCollectionName} as ${options.dependedFieldAs}`
36
+ );
37
+ this.options = options;
38
+ this.name = "FieldIsDependedOnByOtherError";
39
+ }
40
+ }
41
+ // Annotate the CommonJS export names for ESM import in node:
42
+ 0 && (module.exports = {
43
+ FieldIsDependedOnByOtherError
44
+ });
@@ -0,0 +1,2 @@
1
+ import { Database } from '@nocobase/database';
2
+ export declare function beforeDestoryField(db: Database): (model: any, opts: any) => Promise<void>;
@@ -0,0 +1,80 @@
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 beforeDestoryField_exports = {};
28
+ __export(beforeDestoryField_exports, {
29
+ beforeDestoryField: () => beforeDestoryField
30
+ });
31
+ module.exports = __toCommonJS(beforeDestoryField_exports);
32
+ var import_field_is_depended_on_by_other = require("../errors/field-is-depended-on-by-other");
33
+ function beforeDestoryField(db) {
34
+ return async (model, opts) => {
35
+ const { transaction } = opts;
36
+ const { name, type, collectionName } = model.get();
37
+ if (["belongsTo", "hasOne", "hasMany", "belongsToMany"].includes(type)) {
38
+ return;
39
+ }
40
+ const relatedFields = await db.getRepository("fields").find({
41
+ filter: {
42
+ $or: [
43
+ {
44
+ ["options.sourceKey"]: name,
45
+ collectionName
46
+ },
47
+ {
48
+ ["options.targetKey"]: name,
49
+ ["options.target"]: collectionName
50
+ }
51
+ ]
52
+ },
53
+ transaction
54
+ });
55
+ for (const field of relatedFields) {
56
+ const keys = [
57
+ {
58
+ name: "sourceKey",
59
+ condition: (associationField) => associationField.options["sourceKey"] === name && associationField.collectionName === collectionName
60
+ },
61
+ {
62
+ name: "targetKey",
63
+ condition: (associationField) => associationField.options["targetKey"] === name && associationField.options["target"] === collectionName
64
+ }
65
+ ];
66
+ const usedAs = keys.find((key) => key.condition(field))["name"];
67
+ throw new import_field_is_depended_on_by_other.FieldIsDependedOnByOtherError({
68
+ fieldName: name,
69
+ fieldCollectionName: collectionName,
70
+ dependedFieldName: field.get("name"),
71
+ dependedFieldCollectionName: field.get("collectionName"),
72
+ dependedFieldAs: usedAs
73
+ });
74
+ }
75
+ };
76
+ }
77
+ // Annotate the CommonJS export names for ESM import in node:
78
+ 0 && (module.exports = {
79
+ beforeDestoryField
80
+ });
@@ -147,6 +147,9 @@ class CollectionModel extends import_database.MagicAttributeModel {
147
147
  } else if (field.get("through") && field.get("through") === name) {
148
148
  await field.destroy({ transaction });
149
149
  }
150
+ if (field.get("collectionName") === name) {
151
+ await field.destroy({ transaction });
152
+ }
150
153
  }
151
154
  await collection.removeFromDb(options);
152
155
  }
@@ -54,6 +54,8 @@ var import_models = require("./models");
54
54
  var import_collections = __toESM(require("./resourcers/collections"));
55
55
  var import_views = __toESM(require("./resourcers/views"));
56
56
  var import_field_name_exists_error = require("./errors/field-name-exists-error");
57
+ var import_beforeDestoryField = require("./hooks/beforeDestoryField");
58
+ var import_field_is_depended_on_by_other = require("./errors/field-is-depended-on-by-other");
57
59
  class PluginDataSourceMainServer extends import_server.Plugin {
58
60
  schema;
59
61
  loadFilter = {};
@@ -99,7 +101,7 @@ class PluginDataSourceMainServer extends import_server.Plugin {
99
101
  if (options.transaction) {
100
102
  removeOptions["transaction"] = options.transaction;
101
103
  }
102
- const cascade = import_lodash.default.get(options, "context.action.params.cascade", false);
104
+ const cascade = options.cascade || import_lodash.default.get(options, "context.action.params.cascade", false);
103
105
  if (cascade === true || cascade === "true") {
104
106
  removeOptions["cascade"] = true;
105
107
  }
@@ -216,6 +218,7 @@ class PluginDataSourceMainServer extends import_server.Plugin {
216
218
  await collection.sync(syncOptions);
217
219
  }
218
220
  });
221
+ this.app.db.on("fields.beforeDestroy", (0, import_beforeDestoryField.beforeDestoryField)(this.app.db));
219
222
  this.app.db.on("fields.beforeDestroy", (0, import_hooks.beforeDestroyForeignKey)(this.app.db));
220
223
  const mutex = new import_async_mutex.Mutex();
221
224
  this.app.db.on("fields.beforeDestroy", async (model, options) => {
@@ -290,6 +293,26 @@ class PluginDataSourceMainServer extends import_server.Plugin {
290
293
  return ctx.throw(400, ctx.t(`The value of ${Object.keys(err.fields)} field duplicated`));
291
294
  }
292
295
  );
296
+ errorHandlerPlugin.errorHandler.register(
297
+ (err) => err instanceof import_field_is_depended_on_by_other.FieldIsDependedOnByOtherError,
298
+ (err, ctx) => {
299
+ ctx.status = 400;
300
+ ctx.body = {
301
+ errors: [
302
+ {
303
+ message: ctx.i18n.t("field-is-depended-on-by-other", {
304
+ fieldName: err.options.fieldName,
305
+ fieldCollectionName: err.options.fieldCollectionName,
306
+ dependedFieldName: err.options.dependedFieldName,
307
+ dependedFieldCollectionName: err.options.dependedFieldCollectionName,
308
+ dependedFieldAs: err.options.dependedFieldAs,
309
+ ns: "data-source-main"
310
+ })
311
+ }
312
+ ]
313
+ };
314
+ }
315
+ );
293
316
  errorHandlerPlugin.errorHandler.register(
294
317
  (err) => err instanceof import_field_name_exists_error.FieldNameExistsError,
295
318
  (err, ctx) => {
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "displayName.zh-CN": "数据源:主数据库",
5
5
  "description": "NocoBase main database, supports relational databases such as MySQL, PostgreSQL, SQLite and so on.",
6
6
  "description.zh-CN": "NocoBase 主数据库,支持 MySQL、PostgreSQL、SQLite 等关系型数据库。",
7
- "version": "1.2.12-alpha",
7
+ "version": "1.2.14-alpha",
8
8
  "main": "./dist/server/index.js",
9
9
  "homepage": "https://docs.nocobase.com/handbook/data-source-main",
10
10
  "homepage.zh-CN": "https://docs-cn.nocobase.com/handbook/data-source-main",
@@ -22,7 +22,7 @@
22
22
  "@nocobase/test": "1.x",
23
23
  "@nocobase/utils": "1.x"
24
24
  },
25
- "gitHead": "553231d4882496cb7f1f17fa7541899ce82a27ab",
25
+ "gitHead": "717c71eee8cec8d230f1856759d97d6a843f8be6",
26
26
  "keywords": [
27
27
  "Data sources"
28
28
  ]