@nocobase/database 1.2.13-alpha → 1.3.0-alpha.20240710084543

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 (247) hide show
  1. package/package.json +4 -4
  2. package/lib/collection-factory.d.ts +0 -25
  3. package/lib/collection-factory.js +0 -61
  4. package/lib/collection-group-manager.d.ts +0 -34
  5. package/lib/collection-group-manager.js +0 -66
  6. package/lib/collection-importer.d.ts +0 -15
  7. package/lib/collection-importer.js +0 -83
  8. package/lib/collection.d.ts +0 -145
  9. package/lib/collection.js +0 -663
  10. package/lib/database-utils/index.d.ts +0 -16
  11. package/lib/database-utils/index.js +0 -75
  12. package/lib/database.d.ts +0 -228
  13. package/lib/database.js +0 -795
  14. package/lib/decorators/must-have-filter-decorator.d.ts +0 -10
  15. package/lib/decorators/must-have-filter-decorator.js +0 -48
  16. package/lib/decorators/target-collection-decorator.d.ts +0 -10
  17. package/lib/decorators/target-collection-decorator.js +0 -56
  18. package/lib/decorators/transaction-decorator.d.ts +0 -9
  19. package/lib/decorators/transaction-decorator.js +0 -95
  20. package/lib/eager-loading/eager-loading-tree.d.ts +0 -39
  21. package/lib/eager-loading/eager-loading-tree.js +0 -448
  22. package/lib/errors/identifier-error.d.ts +0 -11
  23. package/lib/errors/identifier-error.js +0 -44
  24. package/lib/errors/zero-column-table-error.d.ts +0 -10
  25. package/lib/errors/zero-column-table-error.js +0 -40
  26. package/lib/features/references-map.d.ts +0 -29
  27. package/lib/features/references-map.js +0 -120
  28. package/lib/features/referential-integrity-check.d.ts +0 -16
  29. package/lib/features/referential-integrity-check.js +0 -86
  30. package/lib/field-repository/array-field-repository.d.ts +0 -36
  31. package/lib/field-repository/array-field-repository.js +0 -179
  32. package/lib/fields/array-field.d.ts +0 -19
  33. package/lib/fields/array-field.js +0 -66
  34. package/lib/fields/belongs-to-field.d.ts +0 -25
  35. package/lib/fields/belongs-to-field.js +0 -166
  36. package/lib/fields/belongs-to-many-field.d.ts +0 -28
  37. package/lib/fields/belongs-to-many-field.js +0 -196
  38. package/lib/fields/boolean-field.d.ts +0 -16
  39. package/lib/fields/boolean-field.js +0 -45
  40. package/lib/fields/context-field.d.ts +0 -22
  41. package/lib/fields/context-field.js +0 -79
  42. package/lib/fields/date-field.d.ts +0 -21
  43. package/lib/fields/date-field.js +0 -73
  44. package/lib/fields/field.d.ts +0 -52
  45. package/lib/fields/field.js +0 -168
  46. package/lib/fields/has-inverse-field.d.ts +0 -12
  47. package/lib/fields/has-inverse-field.js +0 -24
  48. package/lib/fields/has-many-field.d.ts +0 -76
  49. package/lib/fields/has-many-field.js +0 -165
  50. package/lib/fields/has-one-field.d.ts +0 -76
  51. package/lib/fields/has-one-field.js +0 -159
  52. package/lib/fields/index.d.ts +0 -54
  53. package/lib/fields/index.js +0 -74
  54. package/lib/fields/json-field.d.ts +0 -22
  55. package/lib/fields/json-field.js +0 -63
  56. package/lib/fields/nanoid-field.d.ts +0 -22
  57. package/lib/fields/nanoid-field.js +0 -65
  58. package/lib/fields/number-field.d.ts +0 -49
  59. package/lib/fields/number-field.js +0 -94
  60. package/lib/fields/password-field.d.ts +0 -29
  61. package/lib/fields/password-field.js +0 -106
  62. package/lib/fields/radio-field.d.ts +0 -24
  63. package/lib/fields/radio-field.js +0 -75
  64. package/lib/fields/relation-field.d.ts +0 -31
  65. package/lib/fields/relation-field.js +0 -94
  66. package/lib/fields/set-field.d.ts +0 -18
  67. package/lib/fields/set-field.js +0 -55
  68. package/lib/fields/sort-field.d.ts +0 -22
  69. package/lib/fields/sort-field.js +0 -197
  70. package/lib/fields/string-field.d.ts +0 -16
  71. package/lib/fields/string-field.js +0 -45
  72. package/lib/fields/text-field.d.ts +0 -18
  73. package/lib/fields/text-field.js +0 -53
  74. package/lib/fields/time-field.d.ts +0 -16
  75. package/lib/fields/time-field.js +0 -45
  76. package/lib/fields/uid-field.d.ts +0 -21
  77. package/lib/fields/uid-field.js +0 -72
  78. package/lib/fields/uuid-field.d.ts +0 -20
  79. package/lib/fields/uuid-field.js +0 -63
  80. package/lib/fields/virtual-field.d.ts +0 -16
  81. package/lib/fields/virtual-field.js +0 -45
  82. package/lib/filter-match.d.ts +0 -9
  83. package/lib/filter-match.js +0 -75
  84. package/lib/filter-parser.d.ts +0 -29
  85. package/lib/filter-parser.js +0 -238
  86. package/lib/helpers.d.ts +0 -11
  87. package/lib/helpers.js +0 -182
  88. package/lib/index.d.ts +0 -37
  89. package/lib/index.js +0 -138
  90. package/lib/inherited-collection.d.ts +0 -22
  91. package/lib/inherited-collection.js +0 -147
  92. package/lib/inherited-map.d.ts +0 -29
  93. package/lib/inherited-map.js +0 -125
  94. package/lib/inherited-sync-runner.d.ts +0 -12
  95. package/lib/inherited-sync-runner.js +0 -184
  96. package/lib/interface-manager.d.ts +0 -17
  97. package/lib/interface-manager.js +0 -50
  98. package/lib/interfaces/base-interface.d.ts +0 -33
  99. package/lib/interfaces/base-interface.js +0 -80
  100. package/lib/interfaces/boolean-interface.d.ts +0 -13
  101. package/lib/interfaces/boolean-interface.js +0 -70
  102. package/lib/interfaces/checkboxes-interface.d.ts +0 -11
  103. package/lib/interfaces/checkboxes-interface.js +0 -41
  104. package/lib/interfaces/datetime-interface.d.ts +0 -13
  105. package/lib/interfaces/datetime-interface.js +0 -108
  106. package/lib/interfaces/index.d.ts +0 -14
  107. package/lib/interfaces/index.js +0 -40
  108. package/lib/interfaces/integer-interface.d.ts +0 -12
  109. package/lib/interfaces/integer-interface.js +0 -44
  110. package/lib/interfaces/json-interface.d.ts +0 -13
  111. package/lib/interfaces/json-interface.js +0 -47
  112. package/lib/interfaces/many-to-many-interface.d.ts +0 -11
  113. package/lib/interfaces/many-to-many-interface.js +0 -41
  114. package/lib/interfaces/many-to-one-interface.d.ts +0 -11
  115. package/lib/interfaces/many-to-one-interface.js +0 -41
  116. package/lib/interfaces/multiple-select-interface.d.ts +0 -13
  117. package/lib/interfaces/multiple-select-interface.js +0 -76
  118. package/lib/interfaces/number-interface.d.ts +0 -15
  119. package/lib/interfaces/number-interface.js +0 -72
  120. package/lib/interfaces/one-belongs-to-one-interface.d.ts +0 -11
  121. package/lib/interfaces/one-belongs-to-one-interface.js +0 -41
  122. package/lib/interfaces/one-has-one-interface.d.ts +0 -11
  123. package/lib/interfaces/one-has-one-interface.js +0 -41
  124. package/lib/interfaces/one-to-many-interface.d.ts +0 -11
  125. package/lib/interfaces/one-to-many-interface.js +0 -41
  126. package/lib/interfaces/percent-interface.d.ts +0 -13
  127. package/lib/interfaces/percent-interface.js +0 -55
  128. package/lib/interfaces/select-interface.d.ts +0 -13
  129. package/lib/interfaces/select-interface.js +0 -63
  130. package/lib/interfaces/to-many-interface.d.ts +0 -12
  131. package/lib/interfaces/to-many-interface.js +0 -61
  132. package/lib/interfaces/to-one-interface.d.ts +0 -13
  133. package/lib/interfaces/to-one-interface.js +0 -61
  134. package/lib/interfaces/utils.d.ts +0 -10
  135. package/lib/interfaces/utils.js +0 -72
  136. package/lib/listeners/adjacency-list.d.ts +0 -10
  137. package/lib/listeners/adjacency-list.js +0 -51
  138. package/lib/listeners/append-child-collection-name-after-repository-find.d.ts +0 -14
  139. package/lib/listeners/append-child-collection-name-after-repository-find.js +0 -75
  140. package/lib/listeners/index.d.ts +0 -10
  141. package/lib/listeners/index.js +0 -42
  142. package/lib/magic-attribute-model.d.ts +0 -16
  143. package/lib/magic-attribute-model.js +0 -213
  144. package/lib/migration.d.ts +0 -43
  145. package/lib/migration.js +0 -112
  146. package/lib/mock-database.d.ts +0 -33
  147. package/lib/mock-database.js +0 -136
  148. package/lib/model-hook.d.ts +0 -20
  149. package/lib/model-hook.js +0 -96
  150. package/lib/model.d.ts +0 -32
  151. package/lib/model.js +0 -179
  152. package/lib/operators/array.d.ts +0 -10
  153. package/lib/operators/array.js +0 -196
  154. package/lib/operators/association.d.ts +0 -10
  155. package/lib/operators/association.js +0 -44
  156. package/lib/operators/boolean.d.ts +0 -10
  157. package/lib/operators/boolean.js +0 -47
  158. package/lib/operators/child-collection.d.ts +0 -10
  159. package/lib/operators/child-collection.js +0 -77
  160. package/lib/operators/date.d.ts +0 -10
  161. package/lib/operators/date.js +0 -149
  162. package/lib/operators/empty.d.ts +0 -13
  163. package/lib/operators/empty.js +0 -102
  164. package/lib/operators/eq.d.ts +0 -10
  165. package/lib/operators/eq.js +0 -44
  166. package/lib/operators/index.d.ts +0 -13
  167. package/lib/operators/index.js +0 -63
  168. package/lib/operators/ne.d.ts +0 -10
  169. package/lib/operators/ne.js +0 -49
  170. package/lib/operators/notIn.d.ts +0 -10
  171. package/lib/operators/notIn.js +0 -42
  172. package/lib/operators/string.d.ts +0 -10
  173. package/lib/operators/string.js +0 -118
  174. package/lib/operators/utils.d.ts +0 -12
  175. package/lib/operators/utils.js +0 -53
  176. package/lib/options-parser.d.ts +0 -45
  177. package/lib/options-parser.js +0 -350
  178. package/lib/query-interface/mysql-query-interface.d.ts +0 -44
  179. package/lib/query-interface/mysql-query-interface.js +0 -134
  180. package/lib/query-interface/postgres-query-interface.d.ts +0 -41
  181. package/lib/query-interface/postgres-query-interface.js +0 -207
  182. package/lib/query-interface/query-interface-builder.d.ts +0 -10
  183. package/lib/query-interface/query-interface-builder.js +0 -55
  184. package/lib/query-interface/query-interface.d.ts +0 -52
  185. package/lib/query-interface/query-interface.js +0 -59
  186. package/lib/query-interface/sqlite-query-interface.d.ts +0 -44
  187. package/lib/query-interface/sqlite-query-interface.js +0 -141
  188. package/lib/relation-repository/belongs-to-many-repository.d.ts +0 -28
  189. package/lib/relation-repository/belongs-to-many-repository.js +0 -248
  190. package/lib/relation-repository/belongs-to-repository.d.ts +0 -17
  191. package/lib/relation-repository/belongs-to-repository.js +0 -51
  192. package/lib/relation-repository/hasmany-repository.d.ts +0 -21
  193. package/lib/relation-repository/hasmany-repository.js +0 -158
  194. package/lib/relation-repository/hasone-repository.d.ts +0 -17
  195. package/lib/relation-repository/hasone-repository.js +0 -51
  196. package/lib/relation-repository/multiple-relation-repository.d.ts +0 -27
  197. package/lib/relation-repository/multiple-relation-repository.js +0 -200
  198. package/lib/relation-repository/relation-repository.d.ts +0 -38
  199. package/lib/relation-repository/relation-repository.js +0 -175
  200. package/lib/relation-repository/single-relation-repository.d.ts +0 -36
  201. package/lib/relation-repository/single-relation-repository.js +0 -151
  202. package/lib/relation-repository/types.d.ts +0 -15
  203. package/lib/relation-repository/types.js +0 -24
  204. package/lib/repositories/tree-repository/adjacency-list-repository.d.ts +0 -26
  205. package/lib/repositories/tree-repository/adjacency-list-repository.js +0 -192
  206. package/lib/repositories/view-repository.d.ts +0 -11
  207. package/lib/repositories/view-repository.js +0 -41
  208. package/lib/repository.d.ts +0 -214
  209. package/lib/repository.js +0 -609
  210. package/lib/sql-parser/index.d.ts +0 -13
  211. package/lib/sql-parser/index.js +0 -33
  212. package/lib/sync-runner.d.ts +0 -41
  213. package/lib/sync-runner.js +0 -335
  214. package/lib/types.d.ts +0 -57
  215. package/lib/types.js +0 -24
  216. package/lib/update-associations.d.ts +0 -68
  217. package/lib/update-associations.js +0 -429
  218. package/lib/update-guard.d.ts +0 -36
  219. package/lib/update-guard.js +0 -212
  220. package/lib/utils.d.ts +0 -15
  221. package/lib/utils.js +0 -136
  222. package/lib/value-parsers/array-value-parser.d.ts +0 -16
  223. package/lib/value-parsers/array-value-parser.js +0 -67
  224. package/lib/value-parsers/base-value-parser.d.ts +0 -20
  225. package/lib/value-parsers/base-value-parser.js +0 -72
  226. package/lib/value-parsers/boolean-value-parser.d.ts +0 -12
  227. package/lib/value-parsers/boolean-value-parser.js +0 -61
  228. package/lib/value-parsers/date-value-parser.d.ts +0 -13
  229. package/lib/value-parsers/date-value-parser.js +0 -99
  230. package/lib/value-parsers/index.d.ts +0 -20
  231. package/lib/value-parsers/index.js +0 -87
  232. package/lib/value-parsers/json-value-parser.d.ts +0 -12
  233. package/lib/value-parsers/json-value-parser.js +0 -56
  234. package/lib/value-parsers/number-value-parser.d.ts +0 -12
  235. package/lib/value-parsers/number-value-parser.js +0 -67
  236. package/lib/value-parsers/string-value-parser.d.ts +0 -16
  237. package/lib/value-parsers/string-value-parser.js +0 -73
  238. package/lib/value-parsers/to-many-value-parser.d.ts +0 -21
  239. package/lib/value-parsers/to-many-value-parser.js +0 -117
  240. package/lib/value-parsers/to-one-value-parser.d.ts +0 -12
  241. package/lib/value-parsers/to-one-value-parser.js +0 -57
  242. package/lib/view/field-type-map.d.ts +0 -109
  243. package/lib/view/field-type-map.js +0 -101
  244. package/lib/view/view-inference.d.ts +0 -40
  245. package/lib/view/view-inference.js +0 -157
  246. package/lib/view-collection.d.ts +0 -14
  247. package/lib/view-collection.js +0 -54
@@ -1,429 +0,0 @@
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 __create = Object.create;
11
- var __defProp = Object.defineProperty;
12
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
13
- var __getOwnPropNames = Object.getOwnPropertyNames;
14
- var __getProtoOf = Object.getPrototypeOf;
15
- var __hasOwnProp = Object.prototype.hasOwnProperty;
16
- var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
17
- var __export = (target, all) => {
18
- for (var name in all)
19
- __defProp(target, name, { get: all[name], enumerable: true });
20
- };
21
- var __copyProps = (to, from, except, desc) => {
22
- if (from && typeof from === "object" || typeof from === "function") {
23
- for (let key of __getOwnPropNames(from))
24
- if (!__hasOwnProp.call(to, key) && key !== except)
25
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
26
- }
27
- return to;
28
- };
29
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
30
- // If the importer is in node compatibility mode or this is not an ESM
31
- // file that has been converted to a CommonJS file using a Babel-
32
- // compatible transform (i.e. "__esModule" has not been set), then set
33
- // "default" to the CommonJS "module.exports" for node compatibility.
34
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
35
- mod
36
- ));
37
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
38
- var update_associations_exports = {};
39
- __export(update_associations_exports, {
40
- belongsToManyAssociations: () => belongsToManyAssociations,
41
- modelAssociationByKey: () => modelAssociationByKey,
42
- modelAssociations: () => modelAssociations,
43
- updateAssociation: () => updateAssociation,
44
- updateAssociations: () => updateAssociations,
45
- updateModelByValues: () => updateModelByValues,
46
- updateMultipleAssociation: () => updateMultipleAssociation,
47
- updateSingleAssociation: () => updateSingleAssociation,
48
- updateThroughTableValue: () => updateThroughTableValue
49
- });
50
- module.exports = __toCommonJS(update_associations_exports);
51
- var import_lodash = __toESM(require("lodash"));
52
- var import_model = require("./model");
53
- var import_update_guard = require("./update-guard");
54
- function isUndefinedOrNull(value) {
55
- return typeof value === "undefined" || value === null;
56
- }
57
- __name(isUndefinedOrNull, "isUndefinedOrNull");
58
- function isStringOrNumber(value) {
59
- return typeof value === "string" || typeof value === "number";
60
- }
61
- __name(isStringOrNumber, "isStringOrNumber");
62
- function getKeysByPrefix(keys, prefix) {
63
- return keys.filter((key) => key.startsWith(`${prefix}.`)).map((key) => key.substring(prefix.length + 1));
64
- }
65
- __name(getKeysByPrefix, "getKeysByPrefix");
66
- function modelAssociations(instance) {
67
- return instance.constructor.associations;
68
- }
69
- __name(modelAssociations, "modelAssociations");
70
- function belongsToManyAssociations(instance) {
71
- const associations = modelAssociations(instance);
72
- return Object.entries(associations).filter((entry) => {
73
- const [key, association] = entry;
74
- return association.associationType == "BelongsToMany";
75
- }).map((association) => {
76
- return association[1];
77
- });
78
- }
79
- __name(belongsToManyAssociations, "belongsToManyAssociations");
80
- function modelAssociationByKey(instance, key) {
81
- return modelAssociations(instance)[key];
82
- }
83
- __name(modelAssociationByKey, "modelAssociationByKey");
84
- async function updateModelByValues(instance, values, options) {
85
- if (!(options == null ? void 0 : options.sanitized)) {
86
- const guard = new import_update_guard.UpdateGuard();
87
- guard.setModel(instance.constructor);
88
- guard.setBlackList(options.blacklist);
89
- guard.setWhiteList(options.whitelist);
90
- guard.setAssociationKeysToBeUpdate(options.updateAssociationValues);
91
- values = guard.sanitize(values);
92
- }
93
- await instance.update(values, options);
94
- await updateAssociations(instance, values, options);
95
- }
96
- __name(updateModelByValues, "updateModelByValues");
97
- async function updateThroughTableValue(instance, throughName, throughValues, source, transaction = null) {
98
- for (const belongsToMany of belongsToManyAssociations(instance)) {
99
- const throughModel = belongsToMany.through.model;
100
- const throughModelName = throughModel.name;
101
- if (throughModelName === throughModelName) {
102
- const where = {
103
- [belongsToMany.foreignKey]: instance.get(belongsToMany.sourceKey),
104
- [belongsToMany.otherKey]: source.get(belongsToMany.targetKey)
105
- };
106
- return await throughModel.update(throughValues, {
107
- where,
108
- transaction
109
- });
110
- }
111
- }
112
- }
113
- __name(updateThroughTableValue, "updateThroughTableValue");
114
- async function updateAssociations(instance, values, options = {}) {
115
- if (!values) {
116
- return;
117
- }
118
- if (options == null ? void 0 : options.updateAssociationValues) {
119
- options.recursive = true;
120
- }
121
- let newTransaction = false;
122
- let transaction = options.transaction;
123
- if (!transaction) {
124
- newTransaction = true;
125
- transaction = await instance.sequelize.transaction();
126
- }
127
- const keys = Object.keys(values);
128
- try {
129
- for (const key of Object.keys(modelAssociations(instance))) {
130
- if (keys.includes(key)) {
131
- await updateAssociation(instance, key, values[key], {
132
- ...options,
133
- transaction
134
- });
135
- }
136
- }
137
- for (const belongsToMany of belongsToManyAssociations(instance)) {
138
- const throughModel = belongsToMany.through.model;
139
- const throughModelName = throughModel.name;
140
- if (values[throughModelName] && options.sourceModel) {
141
- const where = {
142
- [belongsToMany.foreignKey]: instance.get(belongsToMany.sourceKey),
143
- [belongsToMany.otherKey]: options.sourceModel.get(belongsToMany.targetKey)
144
- };
145
- await throughModel.update(values[throughModel.name], {
146
- where,
147
- context: options.context,
148
- transaction
149
- });
150
- }
151
- }
152
- if (newTransaction) {
153
- await transaction.commit();
154
- }
155
- } catch (error) {
156
- if (newTransaction) {
157
- await transaction.rollback();
158
- }
159
- throw error;
160
- }
161
- }
162
- __name(updateAssociations, "updateAssociations");
163
- function isReverseAssociationPair(a, b) {
164
- const typeSet = /* @__PURE__ */ new Set();
165
- typeSet.add(a.associationType);
166
- typeSet.add(b.associationType);
167
- if (typeSet.size == 1 && typeSet.has("BelongsToMany")) {
168
- return a.through.tableName === b.through.tableName && a.target.name === b.source.name && b.target.name === a.source.name && a.foreignKey === b.otherKey && a.sourceKey === b.targetKey && a.otherKey === b.foreignKey && a.targetKey === b.sourceKey;
169
- }
170
- if (typeSet.has("HasOne") && typeSet.has("BelongsTo") || typeSet.has("HasMany") && typeSet.has("BelongsTo")) {
171
- const sourceAssoc = a.associationType == "BelongsTo" ? b : a;
172
- const targetAssoc = sourceAssoc == a ? b : a;
173
- return sourceAssoc.source.name === targetAssoc.target.name && sourceAssoc.target.name === targetAssoc.source.name && sourceAssoc.foreignKey === targetAssoc.foreignKey && sourceAssoc.sourceKey === targetAssoc.targetKey;
174
- }
175
- return false;
176
- }
177
- __name(isReverseAssociationPair, "isReverseAssociationPair");
178
- async function updateAssociation(instance, key, value, options = {}) {
179
- const association = modelAssociationByKey(instance, key);
180
- if (!association) {
181
- return false;
182
- }
183
- if (options.associationContext && isReverseAssociationPair(association, options.associationContext)) {
184
- return false;
185
- }
186
- switch (association.associationType) {
187
- case "HasOne":
188
- case "BelongsTo":
189
- return updateSingleAssociation(instance, key, value, options);
190
- case "HasMany":
191
- case "BelongsToMany":
192
- return updateMultipleAssociation(instance, key, value, options);
193
- }
194
- }
195
- __name(updateAssociation, "updateAssociation");
196
- async function updateSingleAssociation(model, key, value, options = {}) {
197
- const association = modelAssociationByKey(model, key);
198
- if (!association) {
199
- return false;
200
- }
201
- if (!["undefined", "string", "number", "object"].includes(typeof value)) {
202
- return false;
203
- }
204
- if (Array.isArray(value)) {
205
- throw new Error(`The value of '${key}' cannot be in array format`);
206
- }
207
- const { recursive, context, updateAssociationValues = [], transaction } = options;
208
- const keys = getKeysByPrefix(updateAssociationValues, key);
209
- const setAccessor = association.accessors.set;
210
- const removeAssociation = /* @__PURE__ */ __name(async () => {
211
- await model[setAccessor](null, { transaction });
212
- model.setDataValue(key, null);
213
- return true;
214
- }, "removeAssociation");
215
- if (isUndefinedOrNull(value)) {
216
- return await removeAssociation();
217
- }
218
- if (association.associationType === "HasOne" && !model.get(association.sourceKeyAttribute)) {
219
- throw new Error(`The source key ${association.sourceKeyAttribute} is not set in ${model.constructor.name}`);
220
- }
221
- const checkBelongsToForeignKeyValue = /* @__PURE__ */ __name(() => {
222
- if (association.associationType === "BelongsTo" && !model.get(association.foreignKey)) {
223
- throw new Error(
224
- // @ts-ignore
225
- `The target key ${association.targetKey} is not set in ${association.target.name}`
226
- );
227
- }
228
- }, "checkBelongsToForeignKeyValue");
229
- if (isStringOrNumber(value)) {
230
- await model[setAccessor](value, { context, transaction });
231
- return true;
232
- }
233
- if (value instanceof import_model.Model) {
234
- await model[setAccessor](value, { context, transaction });
235
- model.setDataValue(key, value);
236
- return true;
237
- }
238
- const createAccessor = association.accessors.create;
239
- let dataKey;
240
- let M;
241
- if (association.associationType === "BelongsTo") {
242
- M = association.target;
243
- dataKey = association.targetKey;
244
- } else {
245
- M = association.target;
246
- dataKey = M.primaryKeyAttribute;
247
- }
248
- if (isStringOrNumber(value[dataKey])) {
249
- const instance2 = await M.findOne({
250
- where: {
251
- [dataKey]: value[dataKey]
252
- },
253
- transaction
254
- });
255
- if (instance2) {
256
- await model[setAccessor](instance2, { context, transaction });
257
- if (!recursive) {
258
- return;
259
- }
260
- if (updateAssociationValues.includes(key)) {
261
- await instance2.update(value, { ...options, transaction });
262
- }
263
- await updateAssociations(instance2, value, {
264
- ...options,
265
- transaction,
266
- associationContext: association,
267
- updateAssociationValues: keys
268
- });
269
- model.setDataValue(key, instance2);
270
- return true;
271
- }
272
- }
273
- const instance = await model[createAccessor](value, { context, transaction });
274
- await updateAssociations(instance, value, {
275
- ...options,
276
- transaction,
277
- associationContext: association,
278
- updateAssociationValues: keys
279
- });
280
- model.setDataValue(key, instance);
281
- if (association.targetKey) {
282
- model.setDataValue(association.foreignKey, instance[dataKey]);
283
- }
284
- checkBelongsToForeignKeyValue();
285
- }
286
- __name(updateSingleAssociation, "updateSingleAssociation");
287
- async function updateMultipleAssociation(model, key, value, options = {}) {
288
- var _a, _b;
289
- const association = modelAssociationByKey(model, key);
290
- if (!association) {
291
- return false;
292
- }
293
- if (!["undefined", "string", "number", "object"].includes(typeof value)) {
294
- return false;
295
- }
296
- const { recursive, context, updateAssociationValues = [], transaction } = options;
297
- const keys = getKeysByPrefix(updateAssociationValues, key);
298
- const setAccessor = association.accessors.set;
299
- const createAccessor = association.accessors.create;
300
- if (isUndefinedOrNull(value)) {
301
- await model[setAccessor](null, { transaction, context, individualHooks: true });
302
- model.setDataValue(key, null);
303
- return;
304
- }
305
- if (association.associationType === "HasMany" && !model.get(association.sourceKeyAttribute)) {
306
- throw new Error(`The source key ${association.sourceKeyAttribute} is not set in ${model.constructor.name}`);
307
- }
308
- if (isStringOrNumber(value)) {
309
- await model[setAccessor](value, { transaction, context, individualHooks: true });
310
- return;
311
- }
312
- value = import_lodash.default.castArray(value);
313
- const setItems = [];
314
- const objectItems = [];
315
- for (const item of value) {
316
- if (isUndefinedOrNull(item)) {
317
- continue;
318
- }
319
- if (isStringOrNumber(item)) {
320
- setItems.push(item);
321
- } else if (item instanceof import_model.Model) {
322
- setItems.push(item);
323
- } else if (item.sequelize) {
324
- setItems.push(item);
325
- } else if (typeof item === "object") {
326
- const targetKey2 = association.targetKey || "id";
327
- if (item[targetKey2]) {
328
- const attributes = {
329
- [targetKey2]: item[targetKey2]
330
- };
331
- const instance = association.target.build(attributes, { isNewRecord: false });
332
- setItems.push(instance);
333
- }
334
- objectItems.push(item);
335
- }
336
- }
337
- await model[setAccessor](setItems, { transaction, context, individualHooks: true });
338
- const newItems = [];
339
- const pk = association.target.primaryKeyAttribute;
340
- const tmpKey = (_a = association["options"]) == null ? void 0 : _a["targetKey"];
341
- let targetKey = pk;
342
- const db = model.constructor["database"];
343
- if (tmpKey !== pk) {
344
- const targetKeyFieldOptions = (_b = db.getFieldByPath(`${association.target.name}.${tmpKey}`)) == null ? void 0 : _b.options;
345
- if (targetKeyFieldOptions == null ? void 0 : targetKeyFieldOptions.unique) {
346
- targetKey = tmpKey;
347
- }
348
- }
349
- for (const item of objectItems) {
350
- const through = association.through ? association.through.model.name : null;
351
- const accessorOptions = {
352
- context,
353
- transaction
354
- };
355
- const throughValue = item[through];
356
- if (throughValue) {
357
- accessorOptions["through"] = throughValue;
358
- }
359
- if (pk !== targetKey && !isUndefinedOrNull(item[pk]) && isUndefinedOrNull(item[targetKey])) {
360
- throw new Error(`${targetKey} field value is empty`);
361
- }
362
- if (isUndefinedOrNull(item[targetKey])) {
363
- const instance = await model[createAccessor](item, accessorOptions);
364
- await updateAssociations(instance, item, {
365
- ...options,
366
- transaction,
367
- associationContext: association,
368
- updateAssociationValues: keys
369
- });
370
- newItems.push(instance);
371
- } else {
372
- const where = {
373
- [targetKey]: item[targetKey]
374
- };
375
- let instance = await association.target.findOne({
376
- where,
377
- transaction
378
- });
379
- if (!instance) {
380
- instance = await model[createAccessor](item, accessorOptions);
381
- await updateAssociations(instance, item, {
382
- ...options,
383
- transaction,
384
- associationContext: association,
385
- updateAssociationValues: keys
386
- });
387
- newItems.push(instance);
388
- continue;
389
- }
390
- const addAccessor = association.accessors.add;
391
- await model[addAccessor](instance, accessorOptions);
392
- if (!recursive) {
393
- continue;
394
- }
395
- if (updateAssociationValues.includes(key)) {
396
- await instance.update(item, { ...options, transaction });
397
- }
398
- await updateAssociations(instance, item, {
399
- ...options,
400
- transaction,
401
- associationContext: association,
402
- updateAssociationValues: keys
403
- });
404
- newItems.push(instance);
405
- }
406
- }
407
- for (const newItem of newItems) {
408
- const existIndexInSetItems = setItems.findIndex((setItem) => setItem[targetKey] === newItem[targetKey]);
409
- if (existIndexInSetItems !== -1) {
410
- setItems[existIndexInSetItems] = newItem;
411
- } else {
412
- setItems.push(newItem);
413
- }
414
- }
415
- model.setDataValue(key, setItems);
416
- }
417
- __name(updateMultipleAssociation, "updateMultipleAssociation");
418
- // Annotate the CommonJS export names for ESM import in node:
419
- 0 && (module.exports = {
420
- belongsToManyAssociations,
421
- modelAssociationByKey,
422
- modelAssociations,
423
- updateAssociation,
424
- updateAssociations,
425
- updateModelByValues,
426
- updateMultipleAssociation,
427
- updateSingleAssociation,
428
- updateThroughTableValue
429
- });
@@ -1,36 +0,0 @@
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
- import { ModelStatic } from 'sequelize';
10
- import { AssociationKeysToBeUpdate, BlackList, WhiteList } from './repository';
11
- type UpdateValueItem = string | number | UpdateValues;
12
- type UpdateValues = {
13
- [key: string]: UpdateValueItem | Array<UpdateValueItem>;
14
- };
15
- type UpdateAction = 'create' | 'update';
16
- export declare class UpdateGuard {
17
- model: ModelStatic<any>;
18
- action: UpdateAction;
19
- underscored: boolean;
20
- private associationKeysToBeUpdate;
21
- private blackList;
22
- private whiteList;
23
- static fromOptions(model: any, options: any): UpdateGuard;
24
- setAction(action: UpdateAction): void;
25
- setModel(model: ModelStatic<any>): void;
26
- setAssociationKeysToBeUpdate(associationKeysToBeUpdate: AssociationKeysToBeUpdate): void;
27
- setWhiteList(whiteList: WhiteList): void;
28
- setBlackList(blackList: BlackList): void;
29
- checkValues(values: any): void;
30
- /**
31
- * Sanitize values by whitelist blacklist
32
- * @param values
33
- */
34
- sanitize(values: UpdateValues): {};
35
- }
36
- export {};
@@ -1,212 +0,0 @@
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 __create = Object.create;
11
- var __defProp = Object.defineProperty;
12
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
13
- var __getOwnPropNames = Object.getOwnPropertyNames;
14
- var __getProtoOf = Object.getPrototypeOf;
15
- var __hasOwnProp = Object.prototype.hasOwnProperty;
16
- var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
17
- var __export = (target, all) => {
18
- for (var name in all)
19
- __defProp(target, name, { get: all[name], enumerable: true });
20
- };
21
- var __copyProps = (to, from, except, desc) => {
22
- if (from && typeof from === "object" || typeof from === "function") {
23
- for (let key of __getOwnPropNames(from))
24
- if (!__hasOwnProp.call(to, key) && key !== except)
25
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
26
- }
27
- return to;
28
- };
29
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
30
- // If the importer is in node compatibility mode or this is not an ESM
31
- // file that has been converted to a CommonJS file using a Babel-
32
- // compatible transform (i.e. "__esModule" has not been set), then set
33
- // "default" to the CommonJS "module.exports" for node compatibility.
34
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
35
- mod
36
- ));
37
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
38
- var update_guard_exports = {};
39
- __export(update_guard_exports, {
40
- UpdateGuard: () => UpdateGuard
41
- });
42
- module.exports = __toCommonJS(update_guard_exports);
43
- var import_lodash = __toESM(require("lodash"));
44
- var import_model = require("./model");
45
- const _UpdateGuard = class _UpdateGuard {
46
- model;
47
- action;
48
- underscored;
49
- associationKeysToBeUpdate;
50
- blackList;
51
- whiteList;
52
- static fromOptions(model, options) {
53
- const guard = new _UpdateGuard();
54
- guard.setModel(model);
55
- guard.setWhiteList(options.whitelist);
56
- guard.setBlackList(options.blacklist);
57
- guard.setAction(import_lodash.default.get(options, "action", "update"));
58
- guard.setAssociationKeysToBeUpdate(options.updateAssociationValues);
59
- if (options.underscored) {
60
- guard.underscored = options.underscored;
61
- }
62
- return guard;
63
- }
64
- setAction(action) {
65
- this.action = action;
66
- }
67
- setModel(model) {
68
- this.model = model;
69
- }
70
- setAssociationKeysToBeUpdate(associationKeysToBeUpdate) {
71
- if (this.action == "create") {
72
- this.associationKeysToBeUpdate = Object.keys(this.model.associations);
73
- } else {
74
- this.associationKeysToBeUpdate = associationKeysToBeUpdate;
75
- }
76
- }
77
- setWhiteList(whiteList) {
78
- this.whiteList = whiteList;
79
- }
80
- setBlackList(blackList) {
81
- this.blackList = blackList;
82
- }
83
- checkValues(values) {
84
- const dfs = /* @__PURE__ */ __name((values2, model) => {
85
- const associations = model.associations;
86
- const belongsToManyThroughNames = [];
87
- const associationValueKeys = Object.keys(associations).filter((key) => {
88
- return Object.keys(values2).includes(key);
89
- });
90
- const belongsToManyValueKeys = associationValueKeys.filter((key) => {
91
- return associations[key].associationType === "BelongsToMany";
92
- });
93
- const hasManyValueKeys = associationValueKeys.filter((key) => {
94
- return associations[key].associationType === "HasMany";
95
- });
96
- for (const belongsToManyKey of belongsToManyValueKeys) {
97
- const association = associations[belongsToManyKey];
98
- const through = association.through.model;
99
- belongsToManyThroughNames.push(through.name);
100
- }
101
- for (const hasManyKey of hasManyValueKeys) {
102
- const association = associations[hasManyKey];
103
- if (belongsToManyThroughNames.includes(association.target.name)) {
104
- throw new Error(
105
- `HasMany association ${hasManyKey} cannot be used with BelongsToMany association ${association.target.name} with same through model`
106
- );
107
- }
108
- }
109
- }, "dfs");
110
- dfs(values, this.model);
111
- }
112
- /**
113
- * Sanitize values by whitelist blacklist
114
- * @param values
115
- */
116
- sanitize(values) {
117
- if (values === null || values === void 0) {
118
- return values;
119
- }
120
- values = import_lodash.default.clone(values);
121
- if (!this.model) {
122
- throw new Error("please set model first");
123
- }
124
- this.checkValues(values);
125
- const associations = this.model.associations;
126
- const associationsValues = import_lodash.default.pick(values, Object.keys(associations));
127
- const listOfAssociation = /* @__PURE__ */ __name((list, association) => {
128
- if (list) {
129
- list = list.filter((whiteListKey) => whiteListKey.startsWith(`${association}.`)).map((whiteListKey) => whiteListKey.replace(`${association}.`, ""));
130
- if (list.length == 0) {
131
- return void 0;
132
- }
133
- return list;
134
- }
135
- return void 0;
136
- }, "listOfAssociation");
137
- Object.keys(associationsValues).forEach((association) => {
138
- let associationValues = associationsValues[association];
139
- const associationObj = associations[association];
140
- const filterAssociationToBeUpdate = /* @__PURE__ */ __name((value) => {
141
- var _a;
142
- if (value === null) {
143
- return value;
144
- }
145
- const associationKeysToBeUpdate = this.associationKeysToBeUpdate || [];
146
- if (associationKeysToBeUpdate.includes(association)) {
147
- return value;
148
- }
149
- const associationKeyName = ((_a = associationObj == null ? void 0 : associationObj["options"]) == null ? void 0 : _a.targetKey) ? associationObj["options"].targetKey : associationObj.target.primaryKeyAttribute;
150
- if (value[associationKeyName]) {
151
- return import_lodash.default.pick(value, [associationKeyName, ...Object.keys(associationObj.target.associations)]);
152
- }
153
- return value;
154
- }, "filterAssociationToBeUpdate");
155
- const sanitizeValue = /* @__PURE__ */ __name((value) => {
156
- const associationUpdateGuard = new _UpdateGuard();
157
- associationUpdateGuard.setModel(associations[association].target);
158
- ["whiteList", "blackList", "associationKeysToBeUpdate"].forEach((optionKey) => {
159
- associationUpdateGuard[`set${import_lodash.default.upperFirst(optionKey)}`](listOfAssociation(this[optionKey], association));
160
- });
161
- return associationUpdateGuard.sanitize(filterAssociationToBeUpdate(value));
162
- }, "sanitizeValue");
163
- if (Array.isArray(associationValues)) {
164
- associationValues = associationValues.map((value) => {
165
- if (value === void 0 || value === null || typeof value == "string" || typeof value == "number") {
166
- return value;
167
- } else {
168
- return sanitizeValue(value);
169
- }
170
- });
171
- } else if (typeof associationValues === "object" && associationValues !== null) {
172
- associationValues = sanitizeValue(associationValues);
173
- }
174
- values[association] = associationValues;
175
- if (associationObj.associationType === "BelongsTo") {
176
- if (typeof associationValues === "object" && associationValues !== null) {
177
- if (associationValues[associationObj.targetKey] != null) {
178
- values[associationObj.foreignKey] = associationValues[associationObj.targetKey];
179
- }
180
- } else {
181
- values[associationObj.foreignKey] = associationValues;
182
- }
183
- }
184
- });
185
- if (values instanceof import_model.Model) {
186
- return values;
187
- }
188
- let valuesKeys = Object.keys(values || {});
189
- if (this.whiteList) {
190
- valuesKeys = valuesKeys.filter((valueKey) => {
191
- return this.whiteList.findIndex((whiteKey) => {
192
- const keyPaths = whiteKey.split(".");
193
- return keyPaths[0] === valueKey;
194
- }) !== -1;
195
- });
196
- }
197
- if (this.blackList) {
198
- valuesKeys = valuesKeys.filter((valueKey) => !this.blackList.includes(valueKey));
199
- }
200
- const result = valuesKeys.reduce((obj, key) => {
201
- import_lodash.default.set(obj, key, values[key]);
202
- return obj;
203
- }, {});
204
- return result;
205
- }
206
- };
207
- __name(_UpdateGuard, "UpdateGuard");
208
- let UpdateGuard = _UpdateGuard;
209
- // Annotate the CommonJS export names for ESM import in node:
210
- 0 && (module.exports = {
211
- UpdateGuard
212
- });
package/lib/utils.d.ts DELETED
@@ -1,15 +0,0 @@
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
- import Database from './database';
10
- export declare function md5(value: string): string;
11
- export declare function checkIdentifier(value: string): void;
12
- export declare function getTableName(collectionName: string, options: any): any;
13
- export declare function snakeCase(name: string): any;
14
- export declare function patchSequelizeQueryInterface(db: Database): void;
15
- export declare function percent2float(value: string): number;