@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,335 +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 __name = (target, value) => __defProp(target, "name", { value, configurable: true });
15
- var __export = (target, all) => {
16
- for (var name in all)
17
- __defProp(target, name, { get: all[name], enumerable: true });
18
- };
19
- var __copyProps = (to, from, except, desc) => {
20
- if (from && typeof from === "object" || typeof from === "function") {
21
- for (let key of __getOwnPropNames(from))
22
- if (!__hasOwnProp.call(to, key) && key !== except)
23
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
24
- }
25
- return to;
26
- };
27
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
- var sync_runner_exports = {};
29
- __export(sync_runner_exports, {
30
- SyncRunner: () => SyncRunner
31
- });
32
- module.exports = __toCommonJS(sync_runner_exports);
33
- var import_inherited_sync_runner = require("./inherited-sync-runner");
34
- var import_sequelize = require("sequelize");
35
- var import_zero_column_table_error = require("./errors/zero-column-table-error");
36
- var import_utils = require("@nocobase/utils");
37
- const _SyncRunner = class _SyncRunner {
38
- constructor(model) {
39
- this.model = model;
40
- this.collection = model.collection;
41
- this.database = model.database;
42
- }
43
- collection;
44
- database;
45
- tableDescMap = {};
46
- uniqueAttributes = [];
47
- get tableName() {
48
- return this.model.getTableName();
49
- }
50
- get sequelize() {
51
- return this.model.sequelize;
52
- }
53
- get queryInterface() {
54
- return this.sequelize.getQueryInterface();
55
- }
56
- get rawAttributes() {
57
- return this.model.rawAttributes;
58
- }
59
- async runSync(options) {
60
- var _a;
61
- if (this.collection.isView()) {
62
- return;
63
- }
64
- if (this.collection.options.sync === false) {
65
- return;
66
- }
67
- const collectionSyncOptions = (_a = this.database.collectionFactory.collectionTypes.get(this.collection.constructor)) == null ? void 0 : _a.onSync;
68
- if (collectionSyncOptions) {
69
- await collectionSyncOptions(this.model, options);
70
- return;
71
- }
72
- await this.handleSchema(options);
73
- try {
74
- await this.handleZeroColumnModel(options);
75
- } catch (e) {
76
- if (e instanceof import_zero_column_table_error.ZeroColumnTableError) {
77
- return;
78
- }
79
- throw e;
80
- }
81
- try {
82
- const beforeColumns = await this.queryInterface.describeTable(this.tableName, options);
83
- await this.handlePrimaryKeyBeforeSync(beforeColumns, options);
84
- await this.handleUniqueFieldBeforeSync(beforeColumns, options);
85
- } catch (e) {
86
- if (!e.message.includes("No description found")) {
87
- throw e;
88
- }
89
- }
90
- const syncResult = await this.performSync(options);
91
- const columns = await this.queryInterface.describeTable(this.tableName, options);
92
- await this.handlePrimaryKey(columns, options);
93
- await this.handleDefaultValues(columns, options);
94
- await this.handleUniqueIndex(options);
95
- return syncResult;
96
- }
97
- async handleUniqueFieldBeforeSync(beforeColumns, options) {
98
- if (!this.database.inDialect("sqlite")) {
99
- return;
100
- }
101
- const newAttributes = Object.keys(this.rawAttributes).filter((key) => {
102
- return !Object.keys(beforeColumns).includes(this.rawAttributes[key].field) && this.rawAttributes[key].unique;
103
- });
104
- this.uniqueAttributes = newAttributes;
105
- for (const newAttribute of newAttributes) {
106
- this.rawAttributes[newAttribute].unique = false;
107
- }
108
- }
109
- async handlePrimaryKeyBeforeSync(columns, options) {
110
- const columnsBePrimaryKey = Object.keys(columns).filter((key) => {
111
- return columns[key].primaryKey == true;
112
- }).sort();
113
- const columnsWillBePrimaryKey = Object.keys(this.rawAttributes).filter((key) => {
114
- return this.rawAttributes[key].primaryKey == true;
115
- }).map((key) => {
116
- return this.rawAttributes[key].field;
117
- }).sort();
118
- if (columnsBePrimaryKey.length == 1 && !columnsWillBePrimaryKey.includes(columnsBePrimaryKey[0])) {
119
- if (this.database.inDialect("mariadb", "mysql")) {
120
- await this.sequelize.query(`ALTER TABLE ${this.collection.quotedTableName()} DROP PRIMARY KEY;`, options);
121
- }
122
- }
123
- }
124
- async handlePrimaryKey(columns, options) {
125
- try {
126
- const columnsBePrimaryKey = Object.keys(columns).filter((key) => {
127
- return columns[key].primaryKey == true;
128
- }).sort();
129
- const columnsWillBePrimaryKey = Object.keys(this.rawAttributes).filter((key) => {
130
- return this.rawAttributes[key].primaryKey == true;
131
- }).map((key) => {
132
- return this.rawAttributes[key].field;
133
- }).sort();
134
- if (columnsWillBePrimaryKey.length == 0) {
135
- return;
136
- }
137
- if (columnsWillBePrimaryKey.length == 1 && JSON.stringify(columnsBePrimaryKey) != JSON.stringify(columnsWillBePrimaryKey)) {
138
- if (this.database.inDialect("mariadb", "mysql")) {
139
- await this.sequelize.query(
140
- `ALTER TABLE ${this.collection.quotedTableName()} ADD PRIMARY KEY (${columnsWillBePrimaryKey[0]});`,
141
- options
142
- );
143
- } else {
144
- await this.queryInterface.addConstraint(this.tableName, {
145
- type: "primary key",
146
- fields: columnsWillBePrimaryKey,
147
- name: `${this.collection.tableName()}_${columnsWillBePrimaryKey.join("_")}_pk`,
148
- transaction: options == null ? void 0 : options.transaction
149
- });
150
- }
151
- }
152
- } catch (e) {
153
- if (e.message.includes("No description found")) {
154
- return;
155
- }
156
- throw e;
157
- }
158
- }
159
- async handleDefaultValues(columns, options) {
160
- const isJSONColumn = /* @__PURE__ */ __name((column) => {
161
- return ["JSON", "JSONB"].includes(column.type);
162
- }, "isJSONColumn");
163
- for (const columnName in columns) {
164
- const column = columns[columnName];
165
- const isPrimaryKey = /* @__PURE__ */ __name(() => {
166
- const attribute = this.findAttributeByColumnName(columnName);
167
- return attribute && attribute.primaryKey || column.primaryKey;
168
- }, "isPrimaryKey");
169
- if (isPrimaryKey())
170
- continue;
171
- if (await this.isParentColumn(columnName, options))
172
- continue;
173
- const currentAttribute = this.findAttributeByColumnName(columnName);
174
- if (!currentAttribute)
175
- continue;
176
- const attributeDefaultValue = (0, import_utils.isPlainObject)(currentAttribute.defaultValue) && isJSONColumn(column) ? JSON.stringify(currentAttribute.defaultValue) : currentAttribute.defaultValue;
177
- const columnDefaultValue = columns[columnName].defaultValue;
178
- if (columnDefaultValue === null && attributeDefaultValue === void 0)
179
- continue;
180
- if (columnDefaultValue === "NULL" && attributeDefaultValue === null)
181
- continue;
182
- if (columnDefaultValue != attributeDefaultValue) {
183
- const changeAttribute = {
184
- ...currentAttribute,
185
- defaultValue: attributeDefaultValue
186
- };
187
- if (this.database.inDialect("postgres")) {
188
- const query = this.queryInterface.queryGenerator.attributesToSQL(
189
- {
190
- // @ts-ignore
191
- [columnName]: this.queryInterface.normalizeAttribute(changeAttribute)
192
- },
193
- {
194
- context: "changeColumn",
195
- table: this.tableName
196
- }
197
- );
198
- const sql = this.queryInterface.queryGenerator.changeColumnQuery(this.tableName, query);
199
- const regex = /;ALTER TABLE "[^"]+"(\."[^"]+")? ALTER COLUMN "[^"]+" TYPE [^;]+;?$/;
200
- await this.sequelize.query(sql.replace(regex, ""), options);
201
- } else {
202
- await this.queryInterface.changeColumn(this.tableName, columnName, changeAttribute, options);
203
- }
204
- }
205
- }
206
- }
207
- async handleUniqueIndex(options) {
208
- for (const uniqueAttribute of this.uniqueAttributes) {
209
- this.rawAttributes[uniqueAttribute].unique = true;
210
- }
211
- const existsIndexes = await this.queryInterface.showIndex(this.collection.getTableNameWithSchema(), options);
212
- const existsUniqueIndexes = existsIndexes.filter((index) => index.unique);
213
- const uniqueAttributes = Object.keys(this.rawAttributes).filter((key) => {
214
- return this.rawAttributes[key].unique == true;
215
- });
216
- for (const existUniqueIndex of existsUniqueIndexes) {
217
- const isSingleField = existUniqueIndex.fields.length == 1;
218
- if (!isSingleField)
219
- continue;
220
- const columnName = existUniqueIndex.fields[0].attribute;
221
- const currentAttribute = this.findAttributeByColumnName(columnName);
222
- if (!currentAttribute || !currentAttribute.unique && !currentAttribute.primaryKey) {
223
- if (this.database.inDialect("postgres")) {
224
- const constraints = await this.queryInterface.showConstraint(this.tableName, existUniqueIndex.name, options);
225
- if (constraints.some((c) => c.constraintName === existUniqueIndex.name)) {
226
- await this.queryInterface.removeConstraint(this.tableName, existUniqueIndex.name, options);
227
- }
228
- }
229
- if (this.database.inDialect("sqlite")) {
230
- const changeAttribute = {
231
- ...currentAttribute,
232
- unique: false
233
- };
234
- await this.queryInterface.changeColumn(this.tableName, columnName, changeAttribute, options);
235
- } else {
236
- await this.queryInterface.removeIndex(this.tableName, existUniqueIndex.name, options);
237
- }
238
- }
239
- }
240
- for (const uniqueAttribute of uniqueAttributes) {
241
- const indexExists = existsUniqueIndexes.find((index) => {
242
- return index.fields.length == 1 && index.fields[0].attribute == this.rawAttributes[uniqueAttribute].field;
243
- });
244
- if (!indexExists) {
245
- await this.queryInterface.addIndex(this.tableName, [this.rawAttributes[uniqueAttribute].field], {
246
- unique: true,
247
- transaction: options == null ? void 0 : options.transaction,
248
- name: `${this.collection.tableName()}_${this.rawAttributes[uniqueAttribute].field}_uk`
249
- });
250
- }
251
- }
252
- }
253
- async getColumns(options) {
254
- return await this.queryInterface.describeTable(this.tableName, options);
255
- }
256
- async isParentColumn(columnName, options) {
257
- if (this.collection.isInherited()) {
258
- const parentCollections = this.collection.getFlatParents();
259
- for (const parentCollection of parentCollections) {
260
- let parentColumns = this.tableDescMap[parentCollection.name];
261
- if (!parentColumns) {
262
- parentColumns = await this.queryInterface.describeTable(parentCollection.getTableNameWithSchema(), options);
263
- this.tableDescMap[parentCollection.name] = parentColumns;
264
- }
265
- if (parentColumns[columnName]) {
266
- return true;
267
- }
268
- }
269
- }
270
- return false;
271
- }
272
- async removeUnusedColumns(columns, options) {
273
- for (const columnName in columns) {
274
- const currentAttribute = this.findAttributeByColumnName(columnName);
275
- if (!currentAttribute) {
276
- let shouldDelete = true;
277
- if (await this.isParentColumn(columnName, options)) {
278
- shouldDelete = false;
279
- }
280
- if (shouldDelete) {
281
- await this.queryInterface.removeColumn(this.model.getTableName(), columnName, options);
282
- continue;
283
- }
284
- }
285
- }
286
- }
287
- findAttributeByColumnName(columnName) {
288
- return Object.values(this.rawAttributes).find((attribute) => {
289
- return attribute.field == columnName;
290
- });
291
- }
292
- async performSync(options) {
293
- return this.collection.isInherited() ? await import_inherited_sync_runner.InheritedSyncRunner.syncInheritModel(this.model, options) : await import_sequelize.Model.sync.call(this.model, options);
294
- }
295
- async handleZeroColumnModel(options) {
296
- if (Object.keys(this.model.tableAttributes).length === 0) {
297
- if (this.database.inDialect("sqlite", "mysql", "mariadb", "postgres")) {
298
- throw new import_zero_column_table_error.ZeroColumnTableError(
299
- `Zero-column tables aren't supported in ${this.database.sequelize.getDialect()}`
300
- );
301
- }
302
- const queryInterface = this.queryInterface;
303
- if (!queryInterface.patched) {
304
- const oldDescribeTable = queryInterface.describeTable;
305
- queryInterface.describeTable = async function(...args) {
306
- try {
307
- return await oldDescribeTable.call(this, ...args);
308
- } catch (err) {
309
- if (err.message.includes("No description found for")) {
310
- return [];
311
- } else {
312
- throw err;
313
- }
314
- }
315
- };
316
- queryInterface.patched = true;
317
- }
318
- }
319
- }
320
- async handleSchema(options) {
321
- const _schema = this.model._schema;
322
- if (_schema && _schema != "public") {
323
- await this.sequelize.query(`CREATE SCHEMA IF NOT EXISTS "${_schema}";`, {
324
- raw: true,
325
- transaction: options == null ? void 0 : options.transaction
326
- });
327
- }
328
- }
329
- };
330
- __name(_SyncRunner, "SyncRunner");
331
- let SyncRunner = _SyncRunner;
332
- // Annotate the CommonJS export names for ESM import in node:
333
- 0 && (module.exports = {
334
- SyncRunner
335
- });
package/lib/types.d.ts DELETED
@@ -1,57 +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 type { Model } from './model';
10
- import type { CreateOptions, DestroyOptions, SaveOptions, SyncOptions, UpdateOptions } from 'sequelize/types';
11
- import { Collection, CollectionOptions } from './collection';
12
- import { HookReturn } from 'sequelize/types/hooks';
13
- import { ValidationOptions } from 'sequelize/types/instance-validator';
14
- export type CollectionNameType = string;
15
- export type ModelSyncEventType = 'beforeSync' | 'afterSync';
16
- export type ModelValidateEventType = 'beforeValidate' | 'afterValidate';
17
- export type ModelCreateEventType = 'beforeCreate' | 'afterCreate';
18
- export type ModelUpdateEventType = 'beforeUpdate' | 'afterUpdate';
19
- export type ModelSaveEventType = 'beforeSave' | 'afterSave';
20
- export type ModelDestroyEventType = 'beforeDestroy' | 'afterDestroy';
21
- export type ModelCreateWithAssociationsEventType = 'afterCreateWithAssociations';
22
- export type ModelUpdateWithAssociationsEventType = 'afterUpdateWithAssociations';
23
- export type ModelSaveWithAssociationsEventType = 'afterSaveWithAssociations';
24
- export type ModelBulkCreateEvnetType = 'beforeBulkCreate' | 'afterBulkCreate';
25
- export type ModelBulkUpdateEvnetType = 'beforeBulkUpdate' | 'afterBulkUpdate';
26
- export type ModelBulkDestroyEvnetType = 'beforeBulkDestroy' | 'afterBulkDestroy';
27
- export type ModelValidateEventTypes = ModelValidateEventType | `${CollectionNameType}.${ModelValidateEventType}`;
28
- export type ModelCreateEventTypes = ModelCreateEventType | `${CollectionNameType}.${ModelCreateEventType}`;
29
- export type ModelUpdateEventTypes = ModelUpdateEventType | `${CollectionNameType}.${ModelUpdateEventType}`;
30
- export type ModelSaveEventTypes = ModelSaveEventType | `${CollectionNameType}.${ModelSaveEventType}`;
31
- export type ModelDestroyEventTypes = ModelDestroyEventType | `${CollectionNameType}.${ModelDestroyEventType}`;
32
- export type ModelCreateWithAssociationsEventTypes = ModelCreateWithAssociationsEventType | `${CollectionNameType}.${ModelCreateWithAssociationsEventType}`;
33
- export type ModelUpdateWithAssociationsEventTypes = ModelUpdateWithAssociationsEventType | `${CollectionNameType}.${ModelUpdateWithAssociationsEventType}`;
34
- export type ModelSaveWithAssociationsEventTypes = ModelSaveWithAssociationsEventType | `${CollectionNameType}.${ModelSaveWithAssociationsEventType}`;
35
- export type ModelBulkCreateEvnetTypes = ModelBulkCreateEvnetType | `${CollectionNameType}.${ModelBulkCreateEvnetType}`;
36
- export type ModelBulkUpdateEvnetTypes = ModelBulkUpdateEvnetType | `${CollectionNameType}.${ModelBulkUpdateEvnetType}`;
37
- export type ModelBulkDestroyEvnetTypes = ModelBulkDestroyEvnetType | `${CollectionNameType}.${ModelBulkDestroyEvnetType}`;
38
- export type ModelEventTypes = ModelSyncEventType | ModelValidateEventTypes | ModelCreateEventTypes | ModelUpdateEventTypes | ModelSaveEventTypes | ModelDestroyEventTypes | ModelCreateWithAssociationsEventTypes | ModelUpdateWithAssociationsEventTypes | ModelSaveWithAssociationsEventTypes | ModelBulkCreateEvnetTypes | ModelBulkUpdateEvnetTypes | ModelBulkDestroyEvnetTypes;
39
- export type DatabaseBeforeDefineCollectionEventType = 'beforeDefineCollection';
40
- export type DatabaseAfterDefineCollectionEventType = 'afterDefineCollection';
41
- export type DatabaseBeforeRemoveCollectionEventType = 'beforeRemoveCollection';
42
- export type DatabaseAfterRemoveCollectionEventType = 'afterRemoveCollection';
43
- export type DatabaseEventTypes = DatabaseBeforeDefineCollectionEventType | DatabaseAfterDefineCollectionEventType | DatabaseBeforeRemoveCollectionEventType | DatabaseAfterRemoveCollectionEventType;
44
- export type EventType = ModelEventTypes | DatabaseEventTypes | string;
45
- export type { HookReturn };
46
- export type SyncListener = (options?: SyncOptions) => HookReturn;
47
- export type ValidateListener = (model: Model, options?: ValidationOptions) => HookReturn;
48
- export type CreateListener = (model: Model, options?: CreateOptions) => HookReturn;
49
- export type UpdateListener = (model: Model, options?: UpdateOptions) => HookReturn;
50
- export type SaveListener = (model: Model, options?: SaveOptions) => HookReturn;
51
- export type DestroyListener = (model: Model, options?: DestroyOptions) => HookReturn;
52
- export type CreateWithAssociationsListener = (model: Model, options?: CreateOptions) => HookReturn;
53
- export type UpdateWithAssociationsListener = (model: Model, options?: UpdateOptions) => HookReturn;
54
- export type SaveWithAssociationsListener = (model: Model, options?: SaveOptions) => HookReturn;
55
- export type BeforeDefineCollectionListener = (options: CollectionOptions) => void;
56
- export type AfterDefineCollectionListener = (collection: Collection) => void;
57
- export type RemoveCollectionListener = (collection: Collection) => void;
package/lib/types.js DELETED
@@ -1,24 +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 __copyProps = (to, from, except, desc) => {
15
- if (from && typeof from === "object" || typeof from === "function") {
16
- for (let key of __getOwnPropNames(from))
17
- if (!__hasOwnProp.call(to, key) && key !== except)
18
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
19
- }
20
- return to;
21
- };
22
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
23
- var types_exports = {};
24
- module.exports = __toCommonJS(types_exports);
@@ -1,68 +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 { Association, BelongsToMany, Hookable, Transactionable } from 'sequelize';
10
- import { Model } from './model';
11
- export declare function modelAssociations(instance: Model): {
12
- [key: string]: Association<import("sequelize").Model<any, any>, import("sequelize").Model<any, any>>;
13
- };
14
- export declare function belongsToManyAssociations(instance: Model): Array<BelongsToMany>;
15
- export declare function modelAssociationByKey(instance: Model, key: string): Association;
16
- type UpdateValue = {
17
- [key: string]: any;
18
- };
19
- interface UpdateOptions extends Transactionable {
20
- filter?: any;
21
- filterByTk?: number | string;
22
- whitelist?: string[];
23
- blacklist?: string[];
24
- updateAssociationValues?: string[];
25
- sanitized?: boolean;
26
- sourceModel?: Model;
27
- }
28
- interface UpdateAssociationOptions extends Transactionable, Hookable {
29
- updateAssociationValues?: string[];
30
- sourceModel?: Model;
31
- context?: any;
32
- associationContext?: any;
33
- recursive?: boolean;
34
- }
35
- export declare function updateModelByValues(instance: Model, values: UpdateValue, options?: UpdateOptions): Promise<void>;
36
- export declare function updateThroughTableValue(instance: Model, throughName: string, throughValues: any, source: Model, transaction?: any): Promise<any>;
37
- /**
38
- * update association of instance by values
39
- * @param instance
40
- * @param values
41
- * @param options
42
- */
43
- export declare function updateAssociations(instance: Model, values: any, options?: UpdateAssociationOptions): Promise<void>;
44
- /**
45
- * update model association by key
46
- * @param instance
47
- * @param key
48
- * @param value
49
- * @param options
50
- */
51
- export declare function updateAssociation(instance: Model, key: string, value: any, options?: UpdateAssociationOptions): Promise<boolean>;
52
- /**
53
- * update belongsTo and HasOne
54
- * @param model
55
- * @param key
56
- * @param value
57
- * @param options
58
- */
59
- export declare function updateSingleAssociation(model: Model, key: string, value: any, options?: UpdateAssociationOptions): Promise<boolean>;
60
- /**
61
- * update multiple association of model by value
62
- * @param model
63
- * @param key
64
- * @param value
65
- * @param options
66
- */
67
- export declare function updateMultipleAssociation(model: Model, key: string, value: any, options?: UpdateAssociationOptions): Promise<boolean>;
68
- export {};