@nocobase/plugin-data-source-main 1.2.3-alpha → 1.2.5-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.3-alpha",
12
- "@nocobase/database": "1.2.3-alpha",
13
- "@nocobase/plugin-error-handler": "1.2.3-alpha",
14
- "@nocobase/server": "1.2.3-alpha",
11
+ "@nocobase/client": "1.2.5-alpha",
12
+ "@nocobase/database": "1.2.5-alpha",
13
+ "@nocobase/plugin-error-handler": "1.2.5-alpha",
14
+ "@nocobase/server": "1.2.5-alpha",
15
15
  "async-mutex": "0.3.2",
16
16
  "lodash": "4.17.21",
17
- "@nocobase/test": "1.2.3-alpha",
18
- "@nocobase/utils": "1.2.3-alpha",
17
+ "@nocobase/test": "1.2.5-alpha",
18
+ "@nocobase/utils": "1.2.5-alpha",
19
19
  "sequelize": "6.35.2",
20
20
  "dayjs": "1.11.10"
21
21
  };
@@ -0,0 +1,3 @@
1
+ {
2
+ "field-name-exists": "Field name \"{{name}}\" already exists in collection \"{{collectionName}}\""
3
+ }
@@ -0,0 +1,3 @@
1
+ {
2
+ "field-name-exists": "字段标识 \"{{name}}\" 已存在"
3
+ }
@@ -0,0 +1,13 @@
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
+ export declare class FieldNameExistsError extends Error {
10
+ value: string;
11
+ collectionName: string;
12
+ constructor(value: string, collectionName: string);
13
+ }
@@ -0,0 +1,45 @@
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_name_exists_error_exports = {};
28
+ __export(field_name_exists_error_exports, {
29
+ FieldNameExistsError: () => FieldNameExistsError
30
+ });
31
+ module.exports = __toCommonJS(field_name_exists_error_exports);
32
+ class FieldNameExistsError extends Error {
33
+ value;
34
+ collectionName;
35
+ constructor(value, collectionName) {
36
+ super(`Field name "${value}" already exists in collection "${collectionName}"`);
37
+ this.value = value;
38
+ this.collectionName = collectionName;
39
+ this.name = "FieldNameExistsError";
40
+ }
41
+ }
42
+ // Annotate the CommonJS export names for ESM import in node:
43
+ 0 && (module.exports = {
44
+ FieldNameExistsError
45
+ });
@@ -53,6 +53,7 @@ var import_beforeCreateForViewCollection = require("./hooks/beforeCreateForViewC
53
53
  var import_models = require("./models");
54
54
  var import_collections = __toESM(require("./resourcers/collections"));
55
55
  var import_views = __toESM(require("./resourcers/views"));
56
+ var import_field_name_exists_error = require("./errors/field-name-exists-error");
56
57
  class PluginDataSourceMainServer extends import_server.Plugin {
57
58
  schema;
58
59
  loadFilter = {};
@@ -132,6 +133,24 @@ class PluginDataSourceMainServer extends import_server.Plugin {
132
133
  });
133
134
  this.app.db.on("fields.beforeCreate", (0, import_beforeCreateForValidateField.beforeCreateForValidateField)(this.app.db));
134
135
  this.app.db.on("fields.afterCreate", (0, import_hooks.afterCreateForReverseField)(this.app.db));
136
+ this.app.db.on("fields.beforeCreate", async (model, options) => {
137
+ const { transaction } = options;
138
+ const collectionName = model.get("collectionName");
139
+ const name = model.get("name");
140
+ if (!collectionName || !name) {
141
+ return;
142
+ }
143
+ const exists = await this.app.db.getRepository("fields").findOne({
144
+ filter: {
145
+ collectionName,
146
+ name
147
+ },
148
+ transaction
149
+ });
150
+ if (exists) {
151
+ throw new import_field_name_exists_error.FieldNameExistsError(name, collectionName);
152
+ }
153
+ });
135
154
  this.app.db.on("fields.beforeUpdate", (0, import_beforeCreateForValidateField.beforeUpdateForValidateField)(this.app.db));
136
155
  this.app.db.on("fields.beforeUpdate", async (model, options) => {
137
156
  const newValue = options.values;
@@ -271,6 +290,23 @@ class PluginDataSourceMainServer extends import_server.Plugin {
271
290
  return ctx.throw(400, ctx.t(`The value of ${Object.keys(err.fields)} field duplicated`));
272
291
  }
273
292
  );
293
+ errorHandlerPlugin.errorHandler.register(
294
+ (err) => err instanceof import_field_name_exists_error.FieldNameExistsError,
295
+ (err, ctx) => {
296
+ ctx.status = 400;
297
+ ctx.body = {
298
+ errors: [
299
+ {
300
+ message: ctx.i18n.t("field-name-exists", {
301
+ name: err.value,
302
+ collectionName: err.collectionName,
303
+ ns: "data-source-main"
304
+ })
305
+ }
306
+ ]
307
+ };
308
+ }
309
+ );
274
310
  this.app.resourcer.use(async (ctx, next) => {
275
311
  if (ctx.action.resourceName === "collections.fields" && ["create", "update"].includes(ctx.action.actionName)) {
276
312
  ctx.action.mergeParams({
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.3-alpha",
7
+ "version": "1.2.5-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": "a9167db4051c276bdad81f1a98c3d77987af983d",
25
+ "gitHead": "ca7baa2d4dffe0e2c02f676b96ff841f3ee31e7e",
26
26
  "keywords": [
27
27
  "Data sources"
28
28
  ]