@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
package/lib/repository.js DELETED
@@ -1,609 +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 repository_exports = {};
48
- __export(repository_exports, {
49
- Repository: () => Repository,
50
- Transactionable: () => import_sequelize2.Transactionable
51
- });
52
- module.exports = __toCommonJS(repository_exports);
53
- var import_flat = require("flat");
54
- var import_lodash = __toESM(require("lodash"));
55
- var import_sequelize = require("sequelize");
56
- var import_utils = require("@nocobase/utils");
57
- var import_must_have_filter_decorator = __toESM(require("./decorators/must-have-filter-decorator"));
58
- var import_target_collection_decorator = __toESM(require("./decorators/target-collection-decorator"));
59
- var import_transaction_decorator = require("./decorators/transaction-decorator");
60
- var import_eager_loading_tree = require("./eager-loading/eager-loading-tree");
61
- var import_array_field_repository = require("./field-repository/array-field-repository");
62
- var import_fields = require("./fields");
63
- var import_filter_parser = __toESM(require("./filter-parser"));
64
- var import_options_parser = require("./options-parser");
65
- var import_belongs_to_many_repository = require("./relation-repository/belongs-to-many-repository");
66
- var import_belongs_to_repository = require("./relation-repository/belongs-to-repository");
67
- var import_hasmany_repository = require("./relation-repository/hasmany-repository");
68
- var import_hasone_repository = require("./relation-repository/hasone-repository");
69
- var import_update_associations = require("./update-associations");
70
- var import_update_guard = require("./update-guard");
71
- var import_sequelize2 = require("sequelize");
72
- const debug = require("debug")("noco-database");
73
- const transaction = (0, import_transaction_decorator.transactionWrapperBuilder)(function() {
74
- return this.collection.model.sequelize.transaction();
75
- });
76
- const _RelationRepositoryBuilder = class _RelationRepositoryBuilder {
77
- collection;
78
- associationName;
79
- association;
80
- builderMap = {
81
- HasOne: import_hasone_repository.HasOneRepository,
82
- BelongsTo: import_belongs_to_repository.BelongsToRepository,
83
- BelongsToMany: import_belongs_to_many_repository.BelongsToManyRepository,
84
- HasMany: import_hasmany_repository.HasManyRepository,
85
- ArrayField: import_array_field_repository.ArrayFieldRepository
86
- };
87
- constructor(collection, associationName) {
88
- this.collection = collection;
89
- this.associationName = associationName;
90
- this.association = this.collection.model.associations[this.associationName];
91
- if (!this.association) {
92
- const field = collection.getField(associationName);
93
- if (field && field instanceof import_fields.ArrayField) {
94
- this.association = {
95
- associationType: "ArrayField"
96
- };
97
- }
98
- }
99
- }
100
- of(id) {
101
- if (!this.association) {
102
- return;
103
- }
104
- const klass = this.builder()[this.association.associationType];
105
- return new klass(this.collection, this.associationName, id);
106
- }
107
- builder() {
108
- return this.builderMap;
109
- }
110
- };
111
- __name(_RelationRepositoryBuilder, "RelationRepositoryBuilder");
112
- let RelationRepositoryBuilder = _RelationRepositoryBuilder;
113
- const _Repository = class _Repository {
114
- database;
115
- collection;
116
- model;
117
- constructor(collection) {
118
- this.database = collection.context.database;
119
- this.collection = collection;
120
- this.model = collection.model;
121
- }
122
- static valuesToFilter(values = {}, filterKeys) {
123
- const removeArrayIndexInKey = /* @__PURE__ */ __name((key) => {
124
- const chunks = key.split(".");
125
- return chunks.filter((chunk) => {
126
- return !chunk.match(/\d+/);
127
- }).join(".");
128
- }, "removeArrayIndexInKey");
129
- const filterAnd = [];
130
- const flattedValues = (0, import_flat.flatten)(values);
131
- const flattedValuesObject = {};
132
- for (const key in flattedValues) {
133
- const keyWithoutArrayIndex = removeArrayIndexInKey(key);
134
- if (flattedValuesObject[keyWithoutArrayIndex]) {
135
- if (!Array.isArray(flattedValuesObject[keyWithoutArrayIndex])) {
136
- flattedValuesObject[keyWithoutArrayIndex] = [flattedValuesObject[keyWithoutArrayIndex]];
137
- }
138
- flattedValuesObject[keyWithoutArrayIndex].push(flattedValues[key]);
139
- } else {
140
- flattedValuesObject[keyWithoutArrayIndex] = [flattedValues[key]];
141
- }
142
- }
143
- for (const filterKey of filterKeys) {
144
- const filterValue = flattedValuesObject[filterKey] ? flattedValuesObject[filterKey] : import_lodash.default.get(values, filterKey);
145
- if (filterValue) {
146
- filterAnd.push({
147
- [filterKey]: filterValue
148
- });
149
- } else {
150
- filterAnd.push({
151
- [filterKey]: null
152
- });
153
- }
154
- }
155
- return {
156
- $and: filterAnd
157
- };
158
- }
159
- /**
160
- * return count by filter
161
- */
162
- async count(countOptions) {
163
- var _a;
164
- let options = countOptions ? import_lodash.default.clone(countOptions) : {};
165
- const transaction2 = await this.getTransaction(options);
166
- if (countOptions == null ? void 0 : countOptions.filter) {
167
- options = {
168
- ...options,
169
- ...this.parseFilter(countOptions.filter, countOptions)
170
- };
171
- }
172
- if (countOptions == null ? void 0 : countOptions.filterByTk) {
173
- options["where"] = {
174
- [import_sequelize.Op.and]: [
175
- options["where"] || {},
176
- {
177
- [this.collection.filterTargetKey]: options.filterByTk
178
- }
179
- ]
180
- };
181
- }
182
- const queryOptions = {
183
- ...options,
184
- distinct: Boolean(this.collection.model.primaryKeyAttribute)
185
- };
186
- if (((_a = queryOptions.include) == null ? void 0 : _a.length) === 0) {
187
- delete queryOptions.include;
188
- }
189
- const count = await this.collection.model.count({
190
- ...queryOptions,
191
- transaction: transaction2
192
- });
193
- return count;
194
- }
195
- async aggregate(options) {
196
- var _a;
197
- const { method, field } = options;
198
- const queryOptions = this.buildQueryOptions({
199
- ...options,
200
- fields: []
201
- });
202
- (_a = options.optionsTransformer) == null ? void 0 : _a.call(options, queryOptions);
203
- delete queryOptions.order;
204
- const hasAssociationFilter = (() => {
205
- if (queryOptions.include && queryOptions.include.length > 0) {
206
- const filterInclude = queryOptions.include.filter((include) => {
207
- var _a2;
208
- return Object.keys(include.where || {}).length > 0 || ((_a2 = JSON.stringify(queryOptions == null ? void 0 : queryOptions.filter)) == null ? void 0 : _a2.includes(include.association));
209
- });
210
- return filterInclude.length > 0;
211
- }
212
- return false;
213
- })();
214
- if (hasAssociationFilter) {
215
- const primaryKeyField = this.model.primaryKeyAttribute;
216
- const queryInterface = this.database.sequelize.getQueryInterface();
217
- const findOptions = {
218
- ...queryOptions,
219
- raw: true,
220
- includeIgnoreAttributes: false,
221
- attributes: [
222
- [
223
- import_sequelize.Sequelize.literal(
224
- `DISTINCT ${queryInterface.quoteIdentifiers(`${this.collection.name}.${primaryKeyField}`)}`
225
- ),
226
- primaryKeyField
227
- ]
228
- ]
229
- };
230
- const ids = await this.model.findAll(findOptions);
231
- return await this.model.aggregate(field, method, {
232
- ...import_lodash.default.omit(queryOptions, ["where", "include"]),
233
- where: {
234
- [primaryKeyField]: ids.map((node) => node[primaryKeyField])
235
- }
236
- });
237
- }
238
- return await this.model.aggregate(field, method, queryOptions);
239
- }
240
- async chunk(options) {
241
- const { chunkSize, callback, limit: overallLimit } = options;
242
- const transaction2 = await this.getTransaction(options);
243
- let offset = 0;
244
- let totalProcessed = 0;
245
- while (true) {
246
- const currentLimit = overallLimit !== void 0 ? Math.min(chunkSize, overallLimit - totalProcessed) : chunkSize;
247
- const rows = await this.find({
248
- ...options,
249
- limit: currentLimit,
250
- offset,
251
- transaction: transaction2
252
- });
253
- if (rows.length === 0) {
254
- break;
255
- }
256
- await callback(rows, options);
257
- offset += currentLimit;
258
- totalProcessed += rows.length;
259
- if (overallLimit !== void 0 && totalProcessed >= overallLimit) {
260
- break;
261
- }
262
- }
263
- }
264
- /**
265
- * find
266
- * @param options
267
- */
268
- async find(options = {}) {
269
- if ((options == null ? void 0 : options.targetCollection) && (options == null ? void 0 : options.targetCollection) !== this.collection.name) {
270
- return await this.database.getCollection(options.targetCollection).repository.find(options);
271
- }
272
- const model = this.collection.model;
273
- const transaction2 = await this.getTransaction(options);
274
- const opts = {
275
- subQuery: false,
276
- ...this.buildQueryOptions(options)
277
- };
278
- let rows;
279
- if (opts.include && opts.include.length > 0) {
280
- const eagerLoadingTree = import_eager_loading_tree.EagerLoadingTree.buildFromSequelizeOptions({
281
- model,
282
- rootAttributes: opts.attributes,
283
- includeOption: opts.include,
284
- rootOrder: opts.order,
285
- rootQueryOptions: opts,
286
- db: this.database
287
- });
288
- await eagerLoadingTree.load(transaction2);
289
- rows = eagerLoadingTree.root.instances;
290
- } else {
291
- rows = await model.findAll({
292
- ...opts,
293
- transaction: transaction2
294
- });
295
- }
296
- await this.collection.db.emitAsync("afterRepositoryFind", {
297
- findOptions: options,
298
- dataCollection: this.collection,
299
- data: rows
300
- });
301
- return rows;
302
- }
303
- /**
304
- * find and count
305
- * @param options
306
- */
307
- async findAndCount(options) {
308
- options = {
309
- ...options,
310
- transaction: await this.getTransaction(options)
311
- };
312
- const count = await this.count(options);
313
- const results = count ? await this.find(options) : [];
314
- return [results, count];
315
- }
316
- /**
317
- * Find By Id
318
- *
319
- */
320
- findById(id) {
321
- return this.collection.model.findByPk(id);
322
- }
323
- /**
324
- * Find one record from database
325
- *
326
- * @param options
327
- */
328
- async findOne(options) {
329
- const transaction2 = await this.getTransaction(options);
330
- const rows = await this.find({ ...options, limit: 1, transaction: transaction2 });
331
- return rows.length == 1 ? rows[0] : null;
332
- }
333
- /**
334
- * Get the first record matching the attributes or create it.
335
- */
336
- async firstOrCreate(options) {
337
- const { filterKeys, values, transaction: transaction2, hooks } = options;
338
- const filter = _Repository.valuesToFilter(values, filterKeys);
339
- const instance = await this.findOne({ filter, transaction: transaction2 });
340
- if (instance) {
341
- return instance;
342
- }
343
- return this.create({ values, transaction: transaction2, hooks });
344
- }
345
- async updateOrCreate(options) {
346
- const { filterKeys, values, transaction: transaction2, hooks } = options;
347
- const filter = _Repository.valuesToFilter(values, filterKeys);
348
- const instance = await this.findOne({ filter, transaction: transaction2 });
349
- if (instance) {
350
- return await this.update({
351
- filterByTk: instance.get(this.collection.filterTargetKey || this.collection.model.primaryKeyAttribute),
352
- values,
353
- transaction: transaction2,
354
- hooks
355
- });
356
- }
357
- return this.create({ values, transaction: transaction2, hooks });
358
- }
359
- async create(options) {
360
- if (Array.isArray(options.values)) {
361
- return this.createMany({
362
- ...options,
363
- records: options.values
364
- });
365
- }
366
- const transaction2 = await this.getTransaction(options);
367
- const guard = import_update_guard.UpdateGuard.fromOptions(this.model, {
368
- ...options,
369
- action: "create",
370
- underscored: this.collection.options.underscored
371
- });
372
- const values = guard.sanitize(options.values || {});
373
- const instance = await this.model.create(values, {
374
- ...options,
375
- transaction: transaction2
376
- });
377
- if (!instance) {
378
- return;
379
- }
380
- await (0, import_update_associations.updateAssociations)(instance, values, {
381
- ...options,
382
- transaction: transaction2
383
- });
384
- if (options.hooks !== false) {
385
- await this.database.emitAsync(`${this.collection.name}.afterCreateWithAssociations`, instance, {
386
- ...options,
387
- transaction: transaction2
388
- });
389
- await this.database.emitAsync(`${this.collection.name}.afterSaveWithAssociations`, instance, {
390
- ...options,
391
- transaction: transaction2
392
- });
393
- instance.clearChangedWithAssociations();
394
- }
395
- return instance;
396
- }
397
- async createMany(options) {
398
- const transaction2 = await this.getTransaction(options);
399
- const { records } = options;
400
- const instances = [];
401
- for (const values of records) {
402
- const instance = await this.create({ ...options, values, transaction: transaction2 });
403
- instances.push(instance);
404
- }
405
- return instances;
406
- }
407
- async update(options) {
408
- if (Array.isArray(options.values)) {
409
- return this.updateMany({
410
- ...options,
411
- records: options.values
412
- });
413
- }
414
- const transaction2 = await this.getTransaction(options);
415
- const guard = import_update_guard.UpdateGuard.fromOptions(this.model, { ...options, underscored: this.collection.options.underscored });
416
- const values = guard.sanitize(options.values);
417
- if (options.individualHooks === false) {
418
- const { model: Model2 } = this.collection;
419
- const primaryKeyField = Model2.primaryKeyField || Model2.primaryKeyAttribute;
420
- const queryOptions2 = this.buildQueryOptions({
421
- ...options,
422
- fields: [primaryKeyField]
423
- });
424
- const rows = await this.find({
425
- ...queryOptions2,
426
- transaction: transaction2
427
- });
428
- const [result] = await Model2.update(values, {
429
- where: {
430
- [primaryKeyField]: rows.map((row) => row.get(primaryKeyField))
431
- },
432
- fields: options.fields,
433
- hooks: options.hooks,
434
- validate: options.validate,
435
- sideEffects: options.sideEffects,
436
- limit: options.limit,
437
- silent: options.silent,
438
- transaction: transaction2
439
- });
440
- return result;
441
- }
442
- const queryOptions = this.buildQueryOptions(options);
443
- const instances = await this.find({
444
- ...queryOptions,
445
- transaction: transaction2
446
- });
447
- for (const instance of instances) {
448
- await (0, import_update_associations.updateModelByValues)(instance, values, {
449
- ...options,
450
- sanitized: true,
451
- transaction: transaction2
452
- });
453
- }
454
- if (options.hooks !== false) {
455
- for (const instance of instances) {
456
- await this.database.emitAsync(`${this.collection.name}.afterUpdateWithAssociations`, instance, {
457
- ...options,
458
- transaction: transaction2
459
- });
460
- await this.database.emitAsync(`${this.collection.name}.afterSaveWithAssociations`, instance, {
461
- ...options,
462
- transaction: transaction2
463
- });
464
- instance.clearChangedWithAssociations();
465
- }
466
- }
467
- return instances;
468
- }
469
- async updateMany(options) {
470
- const transaction2 = await this.getTransaction(options);
471
- const { records } = options;
472
- const instances = [];
473
- for (const values of records) {
474
- const filterByTk = values[this.model.primaryKeyAttribute];
475
- if (!filterByTk) {
476
- throw new Error("filterByTk invalid");
477
- }
478
- const instance = await this.update({ values, filterByTk, transaction: transaction2 });
479
- instances.push(instance);
480
- }
481
- return instances;
482
- }
483
- async destroy(options) {
484
- const transaction2 = await this.getTransaction(options);
485
- const modelFilterKey = this.collection.filterTargetKey;
486
- options = options;
487
- if (options["individualHooks"] === void 0) {
488
- options["individualHooks"] = true;
489
- }
490
- const filterByTk = options.filterByTk && !import_lodash.default.isArray(options.filterByTk) ? [options.filterByTk] : options.filterByTk;
491
- if (this.collection.model.primaryKeyAttributes.length !== 1 && filterByTk && !import_lodash.default.get(this.collection.options, "filterTargetKey")) {
492
- if (this.collection.model.primaryKeyAttributes.length > 1) {
493
- throw new Error(`filterByTk is not supported for composite primary key`);
494
- } else {
495
- throw new Error(`filterByTk is not supported for collection that has no primary key`);
496
- }
497
- }
498
- if (filterByTk && !options.filter) {
499
- return await this.model.destroy({
500
- ...options,
501
- where: {
502
- [modelFilterKey]: {
503
- [import_sequelize.Op.in]: filterByTk
504
- }
505
- },
506
- transaction: transaction2
507
- });
508
- }
509
- if (options.filter && (0, import_utils.isValidFilter)(options.filter)) {
510
- if (this.collection.model.primaryKeyAttributes.length !== 1 && !import_lodash.default.get(this.collection.options, "filterTargetKey")) {
511
- const queryOptions = {
512
- ...this.buildQueryOptions(options)
513
- };
514
- return await this.model.destroy({
515
- ...queryOptions,
516
- transaction: transaction2
517
- });
518
- }
519
- let pks = (await this.find({
520
- filter: options.filter,
521
- transaction: transaction2
522
- })).map((instance) => instance.get(modelFilterKey));
523
- if (filterByTk) {
524
- pks = import_lodash.default.intersection(
525
- pks.map((i) => `${i}`),
526
- filterByTk.map((i) => `${i}`)
527
- );
528
- }
529
- return await this.destroy({
530
- ...import_lodash.default.omit(options, "filter"),
531
- filterByTk: pks,
532
- transaction: transaction2
533
- });
534
- }
535
- if (options.truncate) {
536
- return await this.model.destroy({
537
- ...options,
538
- truncate: true,
539
- transaction: transaction2
540
- });
541
- }
542
- }
543
- /**
544
- * @param association target association
545
- */
546
- relation(association) {
547
- return new RelationRepositoryBuilder(this.collection, association);
548
- }
549
- buildQueryOptions(options) {
550
- const parser = new import_options_parser.OptionsParser(options, {
551
- collection: this.collection
552
- });
553
- const params = parser.toSequelizeParams();
554
- debug("sequelize query params %o", params);
555
- if (options.where && params.where) {
556
- params.where = {
557
- [import_sequelize.Op.and]: [params.where, options.where]
558
- };
559
- }
560
- return { where: {}, ...options, ...params };
561
- }
562
- parseFilter(filter, options) {
563
- const parser = new import_filter_parser.default(filter, {
564
- collection: this.collection,
565
- app: {
566
- ctx: options == null ? void 0 : options.context
567
- }
568
- });
569
- return parser.toSequelizeParams();
570
- }
571
- async getTransaction(options, autoGen = false) {
572
- if (import_lodash.default.isPlainObject(options) && options.transaction) {
573
- return options.transaction;
574
- }
575
- if (autoGen) {
576
- return await this.model.sequelize.transaction();
577
- }
578
- return null;
579
- }
580
- };
581
- __name(_Repository, "Repository");
582
- __decorateClass([
583
- transaction()
584
- ], _Repository.prototype, "create", 1);
585
- __decorateClass([
586
- transaction()
587
- ], _Repository.prototype, "createMany", 1);
588
- __decorateClass([
589
- transaction(),
590
- (0, import_must_have_filter_decorator.default)(),
591
- import_target_collection_decorator.default
592
- ], _Repository.prototype, "update", 1);
593
- __decorateClass([
594
- transaction()
595
- ], _Repository.prototype, "updateMany", 1);
596
- __decorateClass([
597
- transaction((args, transaction2) => {
598
- return {
599
- filterByTk: args[0],
600
- transaction: transaction2
601
- };
602
- })
603
- ], _Repository.prototype, "destroy", 1);
604
- let Repository = _Repository;
605
- // Annotate the CommonJS export names for ESM import in node:
606
- 0 && (module.exports = {
607
- Repository,
608
- Transactionable
609
- });
@@ -1,13 +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 { Parser } from 'node-sql-parser';
10
- import type * as SQLParserTypes from 'node-sql-parser';
11
- declare const _default: Parser;
12
- export default _default;
13
- export type { SQLParserTypes };
@@ -1,33 +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 __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 sql_parser_exports = {};
28
- __export(sql_parser_exports, {
29
- default: () => sql_parser_default
30
- });
31
- module.exports = __toCommonJS(sql_parser_exports);
32
- var import_node_sql_parser = require("node-sql-parser");
33
- var sql_parser_default = new import_node_sql_parser.Parser();
@@ -1,41 +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 { Model } from './model';
10
- import { Model as SequelizeModel } from 'sequelize';
11
- export declare class SyncRunner {
12
- private model;
13
- private readonly collection;
14
- private readonly database;
15
- private tableDescMap;
16
- private uniqueAttributes;
17
- constructor(model: typeof Model);
18
- get tableName(): string | {
19
- tableName: string;
20
- schema: string;
21
- delimiter: string;
22
- };
23
- get sequelize(): import("sequelize").Sequelize;
24
- get queryInterface(): import("sequelize").QueryInterface;
25
- get rawAttributes(): {
26
- [attribute: string]: import("sequelize").ModelAttributeColumnOptions<SequelizeModel<any, any>>;
27
- };
28
- runSync(options: any): Promise<any>;
29
- handleUniqueFieldBeforeSync(beforeColumns: any, options: any): Promise<void>;
30
- handlePrimaryKeyBeforeSync(columns: any, options: any): Promise<void>;
31
- handlePrimaryKey(columns: any, options: any): Promise<void>;
32
- handleDefaultValues(columns: any, options: any): Promise<void>;
33
- handleUniqueIndex(options: any): Promise<void>;
34
- getColumns(options: any): Promise<import("sequelize").ColumnsDescription>;
35
- isParentColumn(columnName: string, options: any): Promise<boolean>;
36
- removeUnusedColumns(columns: any, options: any): Promise<void>;
37
- findAttributeByColumnName(columnName: string): any;
38
- performSync(options: any): Promise<any>;
39
- handleZeroColumnModel(options: any): Promise<void>;
40
- handleSchema(options: any): Promise<void>;
41
- }