@nocobase/plugin-field-m2m-array 2.0.0-alpha.43 → 2.0.0-alpha.44

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.
@@ -11,10 +11,11 @@ module.exports = {
11
11
  "@formily/react": "2.3.7",
12
12
  "antd": "5.24.2",
13
13
  "react": "18.2.0",
14
- "@nocobase/client": "2.0.0-alpha.43",
14
+ "@nocobase/client": "2.0.0-alpha.44",
15
15
  "react-router-dom": "6.30.1",
16
- "@nocobase/utils": "2.0.0-alpha.43",
17
- "@nocobase/database": "2.0.0-alpha.43",
18
- "@nocobase/server": "2.0.0-alpha.43",
19
- "@nocobase/data-source-manager": "2.0.0-alpha.43"
16
+ "@nocobase/utils": "2.0.0-alpha.44",
17
+ "@nocobase/database": "2.0.0-alpha.44",
18
+ "lodash": "4.17.21",
19
+ "@nocobase/server": "2.0.0-alpha.44",
20
+ "@nocobase/data-source-manager": "2.0.0-alpha.44"
20
21
  };
@@ -7,9 +7,11 @@
7
7
  * For more information, please refer to: https://www.nocobase.com/agreement.
8
8
  */
9
9
 
10
+ var __create = Object.create;
10
11
  var __defProp = Object.defineProperty;
11
12
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
12
13
  var __getOwnPropNames = Object.getOwnPropertyNames;
14
+ var __getProtoOf = Object.getPrototypeOf;
13
15
  var __hasOwnProp = Object.prototype.hasOwnProperty;
14
16
  var __export = (target, all) => {
15
17
  for (var name in all)
@@ -23,6 +25,14 @@ var __copyProps = (to, from, except, desc) => {
23
25
  }
24
26
  return to;
25
27
  };
28
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
29
+ // If the importer is in node compatibility mode or this is not an ESM
30
+ // file that has been converted to a CommonJS file using a Babel-
31
+ // compatible transform (i.e. "__esModule" has not been set), then set
32
+ // "default" to the CommonJS "module.exports" for node compatibility.
33
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
34
+ mod
35
+ ));
26
36
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
27
37
  var belongs_to_array_field_exports = {};
28
38
  __export(belongs_to_array_field_exports, {
@@ -31,6 +41,7 @@ __export(belongs_to_array_field_exports, {
31
41
  });
32
42
  module.exports = __toCommonJS(belongs_to_array_field_exports);
33
43
  var import_database = require("@nocobase/database");
44
+ var import_lodash = __toESM(require("lodash"));
34
45
  const elementTypeMap = {
35
46
  nanoid: "string",
36
47
  sequence: "string",
@@ -40,14 +51,32 @@ class BelongsToArrayField extends import_database.RelationField {
40
51
  get dataType() {
41
52
  return "BelongsToArray";
42
53
  }
43
- setForeignKeyArray = async (model, { values, transaction }) => {
44
- const { name, foreignKey, target, targetKey } = this.options;
45
- if (!values || values[name] === void 0) {
54
+ setForeignKeyArray = async (model, { values, context, transaction, associationKey }) => {
55
+ var _a, _b, _c, _d;
56
+ const { type, name, foreignKey, target, targetKey } = this.options;
57
+ if (type !== "belongsToArray") {
46
58
  return;
47
59
  }
48
- let value = values[name] || [];
49
- if (!Array.isArray(value)) {
50
- value = [value];
60
+ let value;
61
+ let valuesInParams = values;
62
+ let valuesInCtx = (_b = (_a = context == null ? void 0 : context.action) == null ? void 0 : _a.params) == null ? void 0 : _b.values;
63
+ if (associationKey) {
64
+ valuesInParams = (_c = values == null ? void 0 : values[associationKey]) == null ? void 0 : _c[name];
65
+ valuesInCtx = (_d = valuesInCtx == null ? void 0 : valuesInCtx[associationKey]) == null ? void 0 : _d[name];
66
+ } else {
67
+ valuesInParams = values == null ? void 0 : values[name];
68
+ valuesInCtx = valuesInCtx == null ? void 0 : valuesInCtx[name];
69
+ }
70
+ if (valuesInParams !== void 0) {
71
+ value = import_lodash.default.castArray(valuesInParams || []);
72
+ } else if (valuesInCtx !== void 0) {
73
+ value = import_lodash.default.castArray(valuesInCtx || []);
74
+ } else {
75
+ return;
76
+ }
77
+ if (value.length === 0) {
78
+ model.set(foreignKey, []);
79
+ return;
51
80
  }
52
81
  const tks = [];
53
82
  const items = [];
@@ -67,10 +96,19 @@ class BelongsToArrayField extends import_database.RelationField {
67
96
  transaction
68
97
  });
69
98
  tks.push(...instances.map((instance) => instance[targetKey]));
70
- const toCreate = items.filter((item) => !item[targetKey] || !tks.includes(item[targetKey]));
71
99
  const m = this.database.getModel(target);
100
+ const toCreate = items.filter((item) => !item[targetKey] || !tks.includes(item[targetKey]));
72
101
  const newInstances = await m.bulkCreate(toCreate, { transaction });
73
102
  tks.push(...newInstances.map((instance) => instance[targetKey]));
103
+ const toUpdate = items.filter((item) => item[targetKey] && tks.includes(item[targetKey]));
104
+ for (const item of toUpdate) {
105
+ await m.update(item, {
106
+ where: {
107
+ [targetKey]: item[targetKey]
108
+ },
109
+ transaction
110
+ });
111
+ }
74
112
  model.set(foreignKey, tks);
75
113
  };
76
114
  checkTargetCollection() {
package/package.json CHANGED
@@ -6,7 +6,7 @@
6
6
  "description": "Allows to create many to many relationships between two models by storing an array of unique keys of the target model.",
7
7
  "description.ru-RU": "Позволяет создавать отношения «многие ко многим» между двумя моделями, сохраняя массив уникальных ключей целевой модели.",
8
8
  "description.zh-CN": "支持通过在数组中存储目标表唯一键的方式建立多对多关系。",
9
- "version": "2.0.0-alpha.43",
9
+ "version": "2.0.0-alpha.44",
10
10
  "main": "dist/server/index.js",
11
11
  "peerDependencies": {
12
12
  "@nocobase/client": "2.x",
@@ -16,5 +16,5 @@
16
16
  "keywords": [
17
17
  "Collection fields"
18
18
  ],
19
- "gitHead": "5e1b7446db50446b80427240a38a9417f3742c05"
19
+ "gitHead": "075bd484c270740fe9b02f1989818da91139b6b9"
20
20
  }