@nocobase/database 1.2.12-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
package/lib/collection.js DELETED
@@ -1,663 +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 __decorateClass = (decorators, target, key, kind) => {
39
- var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
40
- for (var i = decorators.length - 1, decorator; i >= 0; i--)
41
- if (decorator = decorators[i])
42
- result = (kind ? decorator(target, key, result) : decorator(result)) || result;
43
- if (kind && result)
44
- __defProp(target, key, result);
45
- return result;
46
- };
47
- var collection_exports = {};
48
- __export(collection_exports, {
49
- Collection: () => Collection
50
- });
51
- module.exports = __toCommonJS(collection_exports);
52
- var import_deepmerge = __toESM(require("deepmerge"));
53
- var import_events = require("events");
54
- var import_lodash = __toESM(require("lodash"));
55
- var import_sequelize = require("sequelize");
56
- var import_model = require("./model");
57
- var import_adjacency_list_repository = require("./repositories/tree-repository/adjacency-list-repository");
58
- var import_repository = require("./repository");
59
- var import_utils = require("./utils");
60
- var import_safe_json_stringify = __toESM(require("safe-json-stringify"));
61
- function EnsureAtomicity(target, propertyKey, descriptor) {
62
- const originalMethod = descriptor.value;
63
- descriptor.value = function(...args) {
64
- const model = this.model;
65
- const beforeAssociationKeys = Object.keys(model.associations);
66
- const beforeRawAttributes = Object.keys(model.rawAttributes);
67
- const fieldName = args[0];
68
- const beforeField = this.getField(fieldName);
69
- try {
70
- return originalMethod.apply(this, args);
71
- } catch (error) {
72
- const afterAssociationKeys = Object.keys(model.associations);
73
- const createdAssociationKeys = import_lodash.default.difference(afterAssociationKeys, beforeAssociationKeys);
74
- for (const key of createdAssociationKeys) {
75
- delete this.model.associations[key];
76
- }
77
- const afterRawAttributes = Object.keys(model.rawAttributes);
78
- const createdRawAttributes = import_lodash.default.difference(afterRawAttributes, beforeRawAttributes);
79
- for (const key of createdRawAttributes) {
80
- delete this.model.rawAttributes[key];
81
- }
82
- if (!beforeField) {
83
- this.removeField(fieldName);
84
- }
85
- throw error;
86
- }
87
- };
88
- return descriptor;
89
- }
90
- __name(EnsureAtomicity, "EnsureAtomicity");
91
- const _Collection = class _Collection extends import_events.EventEmitter {
92
- options;
93
- context;
94
- isThrough;
95
- fields = /* @__PURE__ */ new Map();
96
- model;
97
- repository;
98
- constructor(options, context) {
99
- super();
100
- this.context = context;
101
- this.options = options;
102
- this.checkOptions(options);
103
- this.bindFieldEventListener();
104
- this.modelInit();
105
- this.db.modelCollection.set(this.model, this);
106
- this.db.tableNameCollectionMap.set(this.getTableNameWithSchemaAsString(), this);
107
- if (!options.inherits) {
108
- this.setFields(options.fields);
109
- }
110
- this.setRepository(options.repository);
111
- this.setSortable(options.sortable);
112
- }
113
- get filterTargetKey() {
114
- var _a;
115
- const targetKey = (_a = this.options) == null ? void 0 : _a.filterTargetKey;
116
- if (targetKey && this.model.getAttributes()[targetKey]) {
117
- return targetKey;
118
- }
119
- if (this.model.primaryKeyAttributes.length > 1) {
120
- return null;
121
- }
122
- return this.model.primaryKeyAttribute;
123
- }
124
- get name() {
125
- return this.options.name;
126
- }
127
- get origin() {
128
- return this.options.origin || "core";
129
- }
130
- get titleField() {
131
- return this.options.titleField || this.model.primaryKeyAttribute;
132
- }
133
- get db() {
134
- return this.context.database;
135
- }
136
- get treeParentField() {
137
- for (const [_2, field] of this.fields) {
138
- if (field.options.treeParent) {
139
- return field;
140
- }
141
- }
142
- }
143
- get treeChildrenField() {
144
- for (const [_2, field] of this.fields) {
145
- if (field.options.treeChildren) {
146
- return field;
147
- }
148
- }
149
- }
150
- tableName() {
151
- const { name, tableName } = this.options;
152
- const tName = tableName || name;
153
- return this.options.underscored ? (0, import_utils.snakeCase)(tName) : tName;
154
- }
155
- /**
156
- * @internal
157
- */
158
- modelInit() {
159
- if (this.model) {
160
- return;
161
- }
162
- const { name, model, autoGenId = true } = this.options;
163
- let M = import_model.Model;
164
- if (this.context.database.sequelize.isDefined(name)) {
165
- const m = this.context.database.sequelize.model(name);
166
- if (m.isThrough) {
167
- this.model = m;
168
- this.model.database = this.context.database;
169
- this.model.collection = this;
170
- return;
171
- }
172
- }
173
- if (typeof model === "string") {
174
- M = this.context.database.models.get(model) || import_model.Model;
175
- } else if (model) {
176
- M = model;
177
- }
178
- this.model = class extends M {
179
- };
180
- this.model.init(null, this.sequelizeModelOptions());
181
- this.model.options.modelName = this.options.name;
182
- if (!autoGenId) {
183
- this.model.removeAttribute("id");
184
- }
185
- this.model.database = this.context.database;
186
- this.model.collection = this;
187
- }
188
- setRepository(repository) {
189
- let repo = import_repository.Repository;
190
- if (typeof repository === "string") {
191
- repo = this.context.database.repositories.get(repository) || import_repository.Repository;
192
- }
193
- if (this.options.tree == "adjacency-list" || this.options.tree == "adjacencyList") {
194
- repo = import_adjacency_list_repository.AdjacencyListRepository;
195
- }
196
- this.repository = new repo(this);
197
- }
198
- forEachField(callback) {
199
- return [...this.fields.values()].forEach(callback);
200
- }
201
- findField(callback) {
202
- return [...this.fields.values()].find(callback);
203
- }
204
- hasField(name) {
205
- return this.fields.has(name);
206
- }
207
- getField(name) {
208
- return this.fields.get(name);
209
- }
210
- getFields() {
211
- return [...this.fields.values()];
212
- }
213
- addField(name, options) {
214
- return this.setField(name, options);
215
- }
216
- checkFieldType(name, options) {
217
- if (!this.options.underscored) {
218
- return;
219
- }
220
- const fieldName = options.field || (0, import_utils.snakeCase)(name);
221
- const field = this.findField((f) => {
222
- if (f.name === name) {
223
- return false;
224
- }
225
- if (f.field) {
226
- return f.field === fieldName;
227
- }
228
- return (0, import_utils.snakeCase)(f.name) === fieldName;
229
- });
230
- if (!field) {
231
- return;
232
- }
233
- if (options.type !== field.type) {
234
- throw new Error(`fields with same column must be of the same type ${JSON.stringify(options)}`);
235
- }
236
- }
237
- /**
238
- * @internal
239
- */
240
- correctOptions(options) {
241
- if (options.primaryKey && options.autoIncrement) {
242
- delete options.defaultValue;
243
- }
244
- }
245
- setField(name, options) {
246
- (0, import_utils.checkIdentifier)(name);
247
- this.checkFieldType(name, options);
248
- const { database } = this.context;
249
- database.logger.trace(`beforeSetField: ${(0, import_safe_json_stringify.default)(options)}`, {
250
- databaseInstanceId: database.instanceId,
251
- collectionName: this.name,
252
- fieldName: name
253
- });
254
- if (options.source) {
255
- const [sourceCollectionName, sourceFieldName] = options.source.split(".");
256
- const sourceCollection = this.db.collections.get(sourceCollectionName);
257
- if (!sourceCollection) {
258
- this.db.logger.warn(
259
- `source collection "${sourceCollectionName}" not found for field "${name}" at collection "${this.name}"`
260
- );
261
- return null;
262
- } else {
263
- const sourceField = sourceCollection.fields.get(sourceFieldName);
264
- if (!sourceField) {
265
- this.db.logger.warn(
266
- `Source field "${sourceFieldName}" not found for field "${name}" at collection "${this.name}". Source collection: "${sourceCollectionName}"`
267
- );
268
- return null;
269
- } else {
270
- options = { ...import_lodash.default.omit(sourceField.options, ["name", "primaryKey"]), ...options };
271
- }
272
- }
273
- }
274
- this.correctOptions(options);
275
- this.emit("field.beforeAdd", name, options, { collection: this });
276
- const field = database.buildField(
277
- { name, ...options },
278
- {
279
- ...this.context,
280
- collection: this
281
- }
282
- );
283
- const oldField = this.fields.get(name);
284
- if (oldField && oldField.options.inherit && field.typeToString() != oldField.typeToString()) {
285
- throw new Error(
286
- `Field type conflict: cannot set "${name}" on "${this.name}" to ${options.type}, parent "${name}" type is ${oldField.options.type}`
287
- );
288
- }
289
- this.removeField(name);
290
- this.fields.set(name, field);
291
- this.emit("field.afterAdd", field);
292
- this.db.emit("field.afterAdd", {
293
- collection: this,
294
- field
295
- });
296
- if (this.isParent()) {
297
- for (const child of this.context.database.inheritanceMap.getChildren(this.name, {
298
- deep: false
299
- })) {
300
- const childCollection = this.db.getCollection(child);
301
- const existField = childCollection.getField(name);
302
- if (!existField || existField.options.inherit) {
303
- childCollection.setField(name, {
304
- ...options,
305
- inherit: true
306
- });
307
- }
308
- }
309
- }
310
- return field;
311
- }
312
- setFields(fields, resetFields = true) {
313
- if (!Array.isArray(fields)) {
314
- return;
315
- }
316
- if (resetFields) {
317
- this.resetFields();
318
- }
319
- for (const { name, ...options } of fields) {
320
- this.addField(name, options);
321
- }
322
- }
323
- resetFields() {
324
- const fieldNames = this.fields.keys();
325
- for (const fieldName of fieldNames) {
326
- this.removeField(fieldName);
327
- }
328
- }
329
- remove() {
330
- return this.context.database.removeCollection(this.name);
331
- }
332
- async removeFieldFromDb(name, options) {
333
- const field = this.getField(name);
334
- if (!field) {
335
- return;
336
- }
337
- const attribute = this.model.rawAttributes[name];
338
- if (!attribute) {
339
- field.remove();
340
- return;
341
- }
342
- if (this.isInherited() && this.parentFields().has(name)) {
343
- return;
344
- }
345
- if (this.model._virtualAttributes.has(this.name)) {
346
- field.remove();
347
- return;
348
- }
349
- if (this.model.options.timestamps !== false) {
350
- let timestampsFields = ["createdAt", "updatedAt", "deletedAt"];
351
- if (this.db.options.underscored) {
352
- timestampsFields = timestampsFields.map((fieldName) => (0, import_utils.snakeCase)(fieldName));
353
- }
354
- if (timestampsFields.includes(field.columnName())) {
355
- this.fields.delete(name);
356
- return;
357
- }
358
- }
359
- const sortable = this.options.sortable;
360
- if (sortable) {
361
- let sortField;
362
- if (sortable === true) {
363
- sortField = "sort";
364
- } else if (typeof sortable === "string") {
365
- sortField = sortable;
366
- } else if (sortable.name) {
367
- sortField = sortable.name || "sort";
368
- }
369
- if (field.name === sortField) {
370
- return;
371
- }
372
- }
373
- if (this.isView()) {
374
- field.remove();
375
- return;
376
- }
377
- const columnReferencesCount = import_lodash.default.filter(this.model.rawAttributes, (attr) => attr.field == field.columnName()).length;
378
- if (await field.existsInDb({
379
- transaction: options == null ? void 0 : options.transaction
380
- }) && columnReferencesCount == 1) {
381
- const columns = await this.model.sequelize.getQueryInterface().describeTable(this.getTableNameWithSchema(), options);
382
- if (Object.keys(columns).length == 1) {
383
- await this.removeFromDb({
384
- ...options,
385
- cascade: true,
386
- dropCollection: false
387
- });
388
- } else {
389
- const queryInterface = this.db.sequelize.getQueryInterface();
390
- await queryInterface.removeColumn(this.getTableNameWithSchema(), field.columnName(), options);
391
- }
392
- }
393
- field.remove();
394
- }
395
- async removeFromDb(options) {
396
- if (!this.isView() && await this.existsInDb({
397
- transaction: options == null ? void 0 : options.transaction
398
- })) {
399
- const queryInterface = this.db.sequelize.getQueryInterface();
400
- await queryInterface.dropTable(this.getTableNameWithSchema(), options);
401
- }
402
- if ((options == null ? void 0 : options.dropCollection) !== false) {
403
- return this.remove();
404
- }
405
- }
406
- async existsInDb(options) {
407
- return this.db.queryInterface.collectionTableExists(this, options);
408
- }
409
- removeField(name) {
410
- if (!this.fields.has(name)) {
411
- return;
412
- }
413
- const field = this.fields.get(name);
414
- const bool = this.fields.delete(name);
415
- if (bool) {
416
- if (this.isParent()) {
417
- for (const child of this.db.inheritanceMap.getChildren(this.name, {
418
- deep: false
419
- })) {
420
- const childCollection = this.db.getCollection(child);
421
- const existField = childCollection.getField(name);
422
- if (existField && existField.options.inherit) {
423
- childCollection.removeField(name);
424
- }
425
- }
426
- }
427
- this.emit("field.afterRemove", field);
428
- }
429
- return field;
430
- }
431
- updateOptions(options, mergeOptions) {
432
- let newOptions = import_lodash.default.cloneDeep(options);
433
- newOptions = (0, import_deepmerge.default)(this.options, newOptions, mergeOptions);
434
- this.context.database.emit("beforeUpdateCollection", this, newOptions);
435
- this.options = newOptions;
436
- this.setFields(options.fields, false);
437
- if (options.repository) {
438
- this.setRepository(options.repository);
439
- }
440
- this.context.database.emit("afterUpdateCollection", this);
441
- return this;
442
- }
443
- setSortable(sortable) {
444
- if (!sortable) {
445
- return;
446
- }
447
- if (sortable === true) {
448
- this.setField("sort", {
449
- type: "sort",
450
- hidden: true
451
- });
452
- }
453
- if (typeof sortable === "string") {
454
- this.setField(sortable, {
455
- type: "sort",
456
- hidden: true
457
- });
458
- } else if (typeof sortable === "object") {
459
- const { name, ...opts } = sortable;
460
- this.setField(name || "sort", { type: "sort", hidden: true, ...opts });
461
- }
462
- }
463
- updateField(name, options) {
464
- if (!this.hasField(name)) {
465
- throw new Error(`field ${name} not exists`);
466
- }
467
- if (options.name && options.name !== name) {
468
- this.removeField(name);
469
- }
470
- this.setField(options.name || name, options);
471
- }
472
- addIndex(index) {
473
- if (!index) {
474
- return;
475
- }
476
- const indexes = this.model.options.indexes || [];
477
- let indexName = [];
478
- let indexItem;
479
- if (typeof index === "string") {
480
- indexItem = {
481
- fields: [index]
482
- };
483
- indexName = [index];
484
- } else if (Array.isArray(index)) {
485
- indexItem = {
486
- fields: index
487
- };
488
- indexName = index;
489
- } else if (index == null ? void 0 : index.fields) {
490
- indexItem = index;
491
- indexName = index.fields;
492
- }
493
- if (import_lodash.default.isEqual(this.model.primaryKeyAttributes, indexName)) {
494
- return;
495
- }
496
- const name = this.model.primaryKeyAttributes.join(",");
497
- if (name.startsWith(`${indexName.join(",")},`)) {
498
- return;
499
- }
500
- for (const item of indexes) {
501
- if (import_lodash.default.isEqual(item.fields, indexName)) {
502
- return;
503
- }
504
- const name2 = item.fields.join(",");
505
- if (name2.startsWith(`${indexName.join(",")},`)) {
506
- return;
507
- }
508
- }
509
- if (!indexItem) {
510
- return;
511
- }
512
- indexes.push(indexItem);
513
- const tableName = this.model.getTableName();
514
- this.model._indexes = this.model.options.indexes.map((index2) => import_sequelize.Utils.nameIndex(this.model._conformIndex(index2), tableName)).map((item) => {
515
- if (item.name && item.name.length > 63) {
516
- item.name = "i_" + (0, import_utils.md5)(item.name);
517
- }
518
- return item;
519
- });
520
- this.refreshIndexes();
521
- }
522
- removeIndex(fields) {
523
- if (!fields) {
524
- return;
525
- }
526
- const indexes = this.model._indexes;
527
- this.model._indexes = indexes.filter((item) => {
528
- return !import_lodash.default.isEqual(item.fields, fields);
529
- });
530
- this.refreshIndexes();
531
- }
532
- /**
533
- * @internal
534
- */
535
- refreshIndexes() {
536
- const indexes = this.model._indexes;
537
- this.model._indexes = import_lodash.default.uniqBy(
538
- indexes.filter((item) => {
539
- return item.fields.every((field) => this.model.rawAttributes[field]);
540
- }).map((item) => {
541
- item.fields = item.fields.map((field) => this.model.rawAttributes[field].field);
542
- return item;
543
- }),
544
- "name"
545
- );
546
- }
547
- async sync(syncOptions) {
548
- const modelNames = /* @__PURE__ */ new Set([this.model.name]);
549
- const { associations } = this.model;
550
- for (const associationKey in associations) {
551
- const association = associations[associationKey];
552
- modelNames.add(association.target.name);
553
- if (association.through) {
554
- modelNames.add(association.through.model.name);
555
- }
556
- }
557
- const models = [];
558
- this.context.database.sequelize.modelManager.forEachModel((model) => {
559
- if (modelNames.has(model.name)) {
560
- models.push(model);
561
- }
562
- });
563
- for (const model of models) {
564
- await model.sync(
565
- syncOptions || {
566
- force: false,
567
- alter: {
568
- drop: false
569
- }
570
- }
571
- );
572
- }
573
- }
574
- isInherited() {
575
- return false;
576
- }
577
- isParent() {
578
- return this.context.database.inheritanceMap.isParentNode(this.name);
579
- }
580
- getTableNameWithSchema() {
581
- const tableName = this.model.tableName;
582
- if (this.collectionSchema() && this.db.inDialect("postgres")) {
583
- return this.db.utils.addSchema(tableName, this.collectionSchema());
584
- }
585
- return tableName;
586
- }
587
- tableNameAsString(options) {
588
- const tableNameWithSchema = this.getTableNameWithSchema();
589
- if (import_lodash.default.isString(tableNameWithSchema)) {
590
- return tableNameWithSchema;
591
- }
592
- const schema = tableNameWithSchema.schema;
593
- const tableName = tableNameWithSchema.tableName;
594
- if ((options == null ? void 0 : options.ignorePublicSchema) && schema === "public") {
595
- return tableName;
596
- }
597
- return `${schema}.${tableName}`;
598
- }
599
- getTableNameWithSchemaAsString() {
600
- const tableName = this.model.tableName;
601
- if (this.collectionSchema() && this.db.inDialect("postgres")) {
602
- return `${this.collectionSchema()}.${tableName}`;
603
- }
604
- return tableName;
605
- }
606
- quotedTableName() {
607
- return this.db.utils.quoteTable(this.getTableNameWithSchema());
608
- }
609
- collectionSchema() {
610
- if (this.options.schema) {
611
- return this.options.schema;
612
- }
613
- if (this.db.options.schema) {
614
- return this.db.options.schema;
615
- }
616
- if (this.db.inDialect("postgres")) {
617
- return "public";
618
- }
619
- return void 0;
620
- }
621
- isView() {
622
- return false;
623
- }
624
- sequelizeModelOptions() {
625
- const { name } = this.options;
626
- return {
627
- ...import_lodash.default.omit(this.options, ["name", "fields", "model", "targetKey"]),
628
- modelName: name,
629
- sequelize: this.context.database.sequelize,
630
- tableName: this.tableName()
631
- };
632
- }
633
- bindFieldEventListener() {
634
- this.on("field.afterAdd", (field) => {
635
- field.bind();
636
- });
637
- this.on("field.afterRemove", (field) => {
638
- field.unbind();
639
- this.db.emit("field.afterRemove", field);
640
- });
641
- }
642
- checkOptions(options) {
643
- (0, import_utils.checkIdentifier)(options.name);
644
- this.checkTableName();
645
- }
646
- checkTableName() {
647
- const tableName = this.tableName();
648
- for (const [k, collection] of this.db.collections) {
649
- if (collection.name != this.options.name && tableName === collection.tableName() && collection.collectionSchema() === this.collectionSchema()) {
650
- throw new Error(`collection ${collection.name} and ${this.name} have same tableName "${tableName}"`);
651
- }
652
- }
653
- }
654
- };
655
- __name(_Collection, "Collection");
656
- __decorateClass([
657
- EnsureAtomicity
658
- ], _Collection.prototype, "setField", 1);
659
- let Collection = _Collection;
660
- // Annotate the CommonJS export names for ESM import in node:
661
- 0 && (module.exports = {
662
- Collection
663
- });
@@ -1,16 +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 default class DatabaseUtils {
11
- db: Database;
12
- constructor(db: Database);
13
- addSchema(tableName: any, schema?: any): any;
14
- quoteTable(tableName: any): any;
15
- schema(): string;
16
- }