@tachybase/database 0.23.8

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 (226) hide show
  1. package/.turbo/turbo-build.log +10 -0
  2. package/LICENSE +201 -0
  3. package/lib/collection-factory.d.ts +17 -0
  4. package/lib/collection-factory.js +52 -0
  5. package/lib/collection-group-manager.d.ts +26 -0
  6. package/lib/collection-group-manager.js +57 -0
  7. package/lib/collection-importer.d.ts +7 -0
  8. package/lib/collection-importer.js +74 -0
  9. package/lib/collection.d.ts +138 -0
  10. package/lib/collection.js +686 -0
  11. package/lib/database-utils/index.d.ts +9 -0
  12. package/lib/database-utils/index.js +71 -0
  13. package/lib/database-utils/traverseJSON.d.ts +10 -0
  14. package/lib/database-utils/traverseJSON.js +126 -0
  15. package/lib/database.d.ts +201 -0
  16. package/lib/database.js +765 -0
  17. package/lib/decorators/must-have-filter-decorator.d.ts +2 -0
  18. package/lib/decorators/must-have-filter-decorator.js +38 -0
  19. package/lib/decorators/target-collection-decorator.d.ts +2 -0
  20. package/lib/decorators/target-collection-decorator.js +47 -0
  21. package/lib/decorators/transaction-decorator.d.ts +1 -0
  22. package/lib/decorators/transaction-decorator.js +88 -0
  23. package/lib/eager-loading/eager-loading-tree.d.ts +31 -0
  24. package/lib/eager-loading/eager-loading-tree.js +439 -0
  25. package/lib/errors/identifier-error.d.ts +3 -0
  26. package/lib/errors/identifier-error.js +35 -0
  27. package/lib/errors/zero-column-table-error.d.ts +2 -0
  28. package/lib/errors/zero-column-table-error.js +31 -0
  29. package/lib/features/references-map.d.ts +18 -0
  30. package/lib/features/references-map.js +109 -0
  31. package/lib/features/referential-integrity-check.d.ts +8 -0
  32. package/lib/features/referential-integrity-check.js +83 -0
  33. package/lib/field-repository/array-field-repository.d.ts +28 -0
  34. package/lib/field-repository/array-field-repository.js +197 -0
  35. package/lib/fields/array-field.d.ts +11 -0
  36. package/lib/fields/array-field.js +57 -0
  37. package/lib/fields/belongs-to-field.d.ts +17 -0
  38. package/lib/fields/belongs-to-field.js +154 -0
  39. package/lib/fields/belongs-to-many-field.d.ts +20 -0
  40. package/lib/fields/belongs-to-many-field.js +187 -0
  41. package/lib/fields/boolean-field.d.ts +8 -0
  42. package/lib/fields/boolean-field.js +36 -0
  43. package/lib/fields/context-field.d.ts +14 -0
  44. package/lib/fields/context-field.js +70 -0
  45. package/lib/fields/date-field.d.ts +13 -0
  46. package/lib/fields/date-field.js +64 -0
  47. package/lib/fields/field.d.ts +43 -0
  48. package/lib/fields/field.js +156 -0
  49. package/lib/fields/has-inverse-field.d.ts +4 -0
  50. package/lib/fields/has-inverse-field.js +15 -0
  51. package/lib/fields/has-many-field.d.ts +68 -0
  52. package/lib/fields/has-many-field.js +156 -0
  53. package/lib/fields/has-one-field.d.ts +68 -0
  54. package/lib/fields/has-one-field.js +150 -0
  55. package/lib/fields/index.d.ts +46 -0
  56. package/lib/fields/index.js +65 -0
  57. package/lib/fields/json-field.d.ts +14 -0
  58. package/lib/fields/json-field.js +54 -0
  59. package/lib/fields/nanoid-field.d.ts +13 -0
  60. package/lib/fields/nanoid-field.js +58 -0
  61. package/lib/fields/number-field.d.ts +41 -0
  62. package/lib/fields/number-field.js +85 -0
  63. package/lib/fields/password-field.d.ts +21 -0
  64. package/lib/fields/password-field.js +95 -0
  65. package/lib/fields/radio-field.d.ts +16 -0
  66. package/lib/fields/radio-field.js +66 -0
  67. package/lib/fields/relation-field.d.ts +21 -0
  68. package/lib/fields/relation-field.js +79 -0
  69. package/lib/fields/set-field.d.ts +10 -0
  70. package/lib/fields/set-field.js +46 -0
  71. package/lib/fields/sort-field.d.ts +16 -0
  72. package/lib/fields/sort-field.js +187 -0
  73. package/lib/fields/string-field.d.ts +8 -0
  74. package/lib/fields/string-field.js +36 -0
  75. package/lib/fields/text-field.d.ts +8 -0
  76. package/lib/fields/text-field.js +36 -0
  77. package/lib/fields/time-field.d.ts +8 -0
  78. package/lib/fields/time-field.js +36 -0
  79. package/lib/fields/uid-field.d.ts +13 -0
  80. package/lib/fields/uid-field.js +63 -0
  81. package/lib/fields/uuid-field.d.ts +9 -0
  82. package/lib/fields/uuid-field.js +45 -0
  83. package/lib/fields/virtual-field.d.ts +8 -0
  84. package/lib/fields/virtual-field.js +36 -0
  85. package/lib/filter-match.d.ts +1 -0
  86. package/lib/filter-match.js +66 -0
  87. package/lib/filter-parser.d.ts +21 -0
  88. package/lib/filter-parser.js +230 -0
  89. package/lib/helpers.d.ts +3 -0
  90. package/lib/helpers.js +167 -0
  91. package/lib/index.d.ts +30 -0
  92. package/lib/index.js +114 -0
  93. package/lib/inherited-collection.d.ts +14 -0
  94. package/lib/inherited-collection.js +138 -0
  95. package/lib/inherited-map.d.ts +21 -0
  96. package/lib/inherited-map.js +113 -0
  97. package/lib/inherited-sync-runner.d.ts +4 -0
  98. package/lib/inherited-sync-runner.js +175 -0
  99. package/lib/listeners/adjacency-list.d.ts +2 -0
  100. package/lib/listeners/adjacency-list.js +42 -0
  101. package/lib/listeners/append-child-collection-name-after-repository-find.d.ts +6 -0
  102. package/lib/listeners/append-child-collection-name-after-repository-find.js +66 -0
  103. package/lib/listeners/index.d.ts +2 -0
  104. package/lib/listeners/index.js +33 -0
  105. package/lib/magic-attribute-model.d.ts +8 -0
  106. package/lib/magic-attribute-model.js +203 -0
  107. package/lib/migration.d.ts +35 -0
  108. package/lib/migration.js +103 -0
  109. package/lib/mock-database.d.ts +25 -0
  110. package/lib/mock-database.js +126 -0
  111. package/lib/model-hook.d.ts +12 -0
  112. package/lib/model-hook.js +92 -0
  113. package/lib/model.d.ts +23 -0
  114. package/lib/model.js +152 -0
  115. package/lib/operators/array.d.ts +2 -0
  116. package/lib/operators/array.js +158 -0
  117. package/lib/operators/association.d.ts +2 -0
  118. package/lib/operators/association.js +35 -0
  119. package/lib/operators/boolean.d.ts +2 -0
  120. package/lib/operators/boolean.js +38 -0
  121. package/lib/operators/child-collection.d.ts +2 -0
  122. package/lib/operators/child-collection.js +67 -0
  123. package/lib/operators/date.d.ts +2 -0
  124. package/lib/operators/date.js +140 -0
  125. package/lib/operators/empty.d.ts +5 -0
  126. package/lib/operators/empty.js +89 -0
  127. package/lib/operators/eq.d.ts +2 -0
  128. package/lib/operators/eq.js +35 -0
  129. package/lib/operators/index.d.ts +5 -0
  130. package/lib/operators/index.js +56 -0
  131. package/lib/operators/jsonb.d.ts +2 -0
  132. package/lib/operators/jsonb.js +50 -0
  133. package/lib/operators/ne.d.ts +2 -0
  134. package/lib/operators/ne.js +40 -0
  135. package/lib/operators/notIn.d.ts +2 -0
  136. package/lib/operators/notIn.js +33 -0
  137. package/lib/operators/string.d.ts +2 -0
  138. package/lib/operators/string.js +109 -0
  139. package/lib/operators/utils.d.ts +4 -0
  140. package/lib/operators/utils.js +40 -0
  141. package/lib/options-parser.d.ts +37 -0
  142. package/lib/options-parser.js +332 -0
  143. package/lib/playground.d.ts +1 -0
  144. package/lib/playground.js +43 -0
  145. package/lib/query-interface/mysql-query-interface.d.ts +35 -0
  146. package/lib/query-interface/mysql-query-interface.js +122 -0
  147. package/lib/query-interface/postgres-query-interface.d.ts +32 -0
  148. package/lib/query-interface/postgres-query-interface.js +189 -0
  149. package/lib/query-interface/query-interface-builder.d.ts +2 -0
  150. package/lib/query-interface/query-interface-builder.js +46 -0
  151. package/lib/query-interface/query-interface.d.ts +42 -0
  152. package/lib/query-interface/query-interface.js +46 -0
  153. package/lib/query-interface/sqlite-query-interface.d.ts +35 -0
  154. package/lib/query-interface/sqlite-query-interface.js +131 -0
  155. package/lib/relation-repository/belongs-to-many-repository.d.ts +21 -0
  156. package/lib/relation-repository/belongs-to-many-repository.js +280 -0
  157. package/lib/relation-repository/belongs-to-repository.d.ts +6 -0
  158. package/lib/relation-repository/belongs-to-repository.js +39 -0
  159. package/lib/relation-repository/hasmany-repository.d.ts +10 -0
  160. package/lib/relation-repository/hasmany-repository.js +182 -0
  161. package/lib/relation-repository/hasone-repository.d.ts +6 -0
  162. package/lib/relation-repository/hasone-repository.js +39 -0
  163. package/lib/relation-repository/multiple-relation-repository.d.ts +20 -0
  164. package/lib/relation-repository/multiple-relation-repository.js +229 -0
  165. package/lib/relation-repository/relation-repository.d.ts +29 -0
  166. package/lib/relation-repository/relation-repository.js +202 -0
  167. package/lib/relation-repository/single-relation-repository.d.ts +25 -0
  168. package/lib/relation-repository/single-relation-repository.js +162 -0
  169. package/lib/relation-repository/types.d.ts +7 -0
  170. package/lib/relation-repository/types.js +15 -0
  171. package/lib/repositories/tree-repository/adjacency-list-repository.d.ts +18 -0
  172. package/lib/repositories/tree-repository/adjacency-list-repository.js +183 -0
  173. package/lib/repositories/view-repository.d.ts +3 -0
  174. package/lib/repositories/view-repository.js +32 -0
  175. package/lib/repository.d.ts +206 -0
  176. package/lib/repository.js +597 -0
  177. package/lib/sql-collection/index.d.ts +2 -0
  178. package/lib/sql-collection/index.js +23 -0
  179. package/lib/sql-collection/query-generator.d.ts +10 -0
  180. package/lib/sql-collection/query-generator.js +70 -0
  181. package/lib/sql-collection/sql-collection.d.ts +7 -0
  182. package/lib/sql-collection/sql-collection.js +69 -0
  183. package/lib/sql-collection/sql-model.d.ts +16 -0
  184. package/lib/sql-collection/sql-model.js +168 -0
  185. package/lib/sql-parser/index.d.ts +10 -0
  186. package/lib/sql-parser/index.js +10699 -0
  187. package/lib/sql-parser/postgres.d.ts +10 -0
  188. package/lib/sql-parser/postgres.js +31506 -0
  189. package/lib/sql-parser/sql.pegjs +1297 -0
  190. package/lib/sync-runner.d.ts +30 -0
  191. package/lib/sync-runner.js +277 -0
  192. package/lib/types.d.ts +49 -0
  193. package/lib/types.js +15 -0
  194. package/lib/update-associations.d.ts +60 -0
  195. package/lib/update-associations.js +405 -0
  196. package/lib/update-guard.d.ts +28 -0
  197. package/lib/update-guard.js +202 -0
  198. package/lib/utils.d.ts +7 -0
  199. package/lib/utils.js +127 -0
  200. package/lib/value-parsers/array-value-parser.d.ts +8 -0
  201. package/lib/value-parsers/array-value-parser.js +58 -0
  202. package/lib/value-parsers/base-value-parser.d.ts +12 -0
  203. package/lib/value-parsers/base-value-parser.js +63 -0
  204. package/lib/value-parsers/boolean-value-parser.d.ts +4 -0
  205. package/lib/value-parsers/boolean-value-parser.js +52 -0
  206. package/lib/value-parsers/date-value-parser.d.ts +5 -0
  207. package/lib/value-parsers/date-value-parser.js +88 -0
  208. package/lib/value-parsers/index.d.ts +12 -0
  209. package/lib/value-parsers/index.js +78 -0
  210. package/lib/value-parsers/json-value-parser.d.ts +4 -0
  211. package/lib/value-parsers/json-value-parser.js +47 -0
  212. package/lib/value-parsers/number-value-parser.d.ts +4 -0
  213. package/lib/value-parsers/number-value-parser.js +58 -0
  214. package/lib/value-parsers/string-value-parser.d.ts +8 -0
  215. package/lib/value-parsers/string-value-parser.js +64 -0
  216. package/lib/value-parsers/to-many-value-parser.d.ts +13 -0
  217. package/lib/value-parsers/to-many-value-parser.js +108 -0
  218. package/lib/value-parsers/to-one-value-parser.d.ts +4 -0
  219. package/lib/value-parsers/to-one-value-parser.js +48 -0
  220. package/lib/view/field-type-map.d.ts +97 -0
  221. package/lib/view/field-type-map.js +90 -0
  222. package/lib/view/view-inference.d.ts +32 -0
  223. package/lib/view/view-inference.js +146 -0
  224. package/lib/view-collection.d.ts +6 -0
  225. package/lib/view-collection.js +45 -0
  226. package/package.json +38 -0
@@ -0,0 +1,30 @@
1
+ import { Model as SequelizeModel } from 'sequelize';
2
+ import { Model } from './model';
3
+ export declare class SyncRunner {
4
+ private model;
5
+ private readonly collection;
6
+ private readonly database;
7
+ private tableDescMap;
8
+ constructor(model: typeof Model);
9
+ get tableName(): string | {
10
+ tableName: string;
11
+ schema: string;
12
+ delimiter: string;
13
+ };
14
+ get sequelize(): import("sequelize").Sequelize;
15
+ get queryInterface(): import("sequelize").QueryInterface;
16
+ get rawAttributes(): {
17
+ [attribute: string]: import("sequelize").ModelAttributeColumnOptions<SequelizeModel<any, any>>;
18
+ };
19
+ runSync(options: any): Promise<any>;
20
+ handlePrimaryKey(columns: any, options: any): Promise<void>;
21
+ handleDefaultValues(columns: any, options: any): Promise<void>;
22
+ handleUniqueIndex(options: any): Promise<void>;
23
+ getColumns(options: any): Promise<import("sequelize").ColumnsDescription>;
24
+ isParentColumn(columnName: string, options: any): Promise<boolean>;
25
+ removeUnusedColumns(columns: any, options: any): Promise<void>;
26
+ findAttributeByColumnName(columnName: string): any;
27
+ performSync(options: any): Promise<any>;
28
+ handleZeroColumnModel(options: any): Promise<void>;
29
+ handleSchema(options: any): Promise<void>;
30
+ }
@@ -0,0 +1,277 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var sync_runner_exports = {};
20
+ __export(sync_runner_exports, {
21
+ SyncRunner: () => SyncRunner
22
+ });
23
+ module.exports = __toCommonJS(sync_runner_exports);
24
+ var import_utils = require("@tachybase/utils");
25
+ var import_sequelize = require("sequelize");
26
+ var import_zero_column_table_error = require("./errors/zero-column-table-error");
27
+ var import_inherited_sync_runner = require("./inherited-sync-runner");
28
+ const _SyncRunner = class _SyncRunner {
29
+ constructor(model) {
30
+ this.model = model;
31
+ this.collection = model.collection;
32
+ this.database = model.database;
33
+ }
34
+ collection;
35
+ database;
36
+ tableDescMap = {};
37
+ get tableName() {
38
+ return this.model.getTableName();
39
+ }
40
+ get sequelize() {
41
+ return this.model.sequelize;
42
+ }
43
+ get queryInterface() {
44
+ return this.sequelize.getQueryInterface();
45
+ }
46
+ get rawAttributes() {
47
+ return this.model.rawAttributes;
48
+ }
49
+ async runSync(options) {
50
+ var _a;
51
+ if (this.collection.isView()) {
52
+ return;
53
+ }
54
+ if (this.collection.options.sync === false) {
55
+ return;
56
+ }
57
+ const collectionSyncOptions = (_a = this.database.collectionFactory.collectionTypes.get(
58
+ // @ts-ignore
59
+ this.collection.constructor
60
+ )) == null ? void 0 : _a.onSync;
61
+ if (collectionSyncOptions) {
62
+ await collectionSyncOptions(this.model, options);
63
+ return;
64
+ }
65
+ await this.handleSchema(options);
66
+ try {
67
+ await this.handleZeroColumnModel(options);
68
+ } catch (e) {
69
+ if (e instanceof import_zero_column_table_error.ZeroColumnTableError) {
70
+ return;
71
+ }
72
+ throw e;
73
+ }
74
+ const syncResult = await this.performSync(options);
75
+ const columns = await this.queryInterface.describeTable(this.tableName, options);
76
+ await this.handlePrimaryKey(columns, options);
77
+ await this.handleDefaultValues(columns, options);
78
+ await this.handleUniqueIndex(options);
79
+ return syncResult;
80
+ }
81
+ async handlePrimaryKey(columns, options) {
82
+ if (!this.database.inDialect("postgres")) {
83
+ return;
84
+ }
85
+ try {
86
+ const columnsBePrimaryKey = Object.keys(columns).filter((key) => {
87
+ return columns[key].primaryKey == true;
88
+ }).sort();
89
+ const columnsWillBePrimaryKey = Object.keys(this.rawAttributes).filter((key) => {
90
+ return this.rawAttributes[key].primaryKey == true;
91
+ }).map((key) => {
92
+ return this.rawAttributes[key].field;
93
+ }).sort();
94
+ if (columnsWillBePrimaryKey.length == 0) {
95
+ return;
96
+ }
97
+ if (JSON.stringify(columnsBePrimaryKey) != JSON.stringify(columnsWillBePrimaryKey)) {
98
+ await this.queryInterface.addConstraint(this.tableName, {
99
+ type: "primary key",
100
+ fields: columnsWillBePrimaryKey,
101
+ name: `${this.collection.tableName()}_${columnsWillBePrimaryKey.join("_")}_pk`,
102
+ transaction: options == null ? void 0 : options.transaction
103
+ });
104
+ }
105
+ } catch (e) {
106
+ if (e.message.includes("No description found")) {
107
+ return;
108
+ }
109
+ }
110
+ }
111
+ async handleDefaultValues(columns, options) {
112
+ const isJSONColumn = /* @__PURE__ */ __name((column) => {
113
+ return ["JSON", "JSONB"].includes(column.type);
114
+ }, "isJSONColumn");
115
+ for (const columnName in columns) {
116
+ const column = columns[columnName];
117
+ const isPrimaryKey = /* @__PURE__ */ __name(() => {
118
+ const attribute = this.findAttributeByColumnName(columnName);
119
+ return attribute && attribute.primaryKey || column.primaryKey;
120
+ }, "isPrimaryKey");
121
+ if (isPrimaryKey()) continue;
122
+ if (await this.isParentColumn(columnName, options)) continue;
123
+ const currentAttribute = this.findAttributeByColumnName(columnName);
124
+ if (!currentAttribute) continue;
125
+ const attributeDefaultValue = (0, import_utils.isPlainObject)(currentAttribute.defaultValue) && isJSONColumn(column) ? JSON.stringify(currentAttribute.defaultValue) : currentAttribute.defaultValue;
126
+ const columnDefaultValue = columns[columnName].defaultValue;
127
+ if (columnDefaultValue === null && attributeDefaultValue === void 0) continue;
128
+ if (columnDefaultValue === "NULL" && attributeDefaultValue === null) continue;
129
+ if (columnDefaultValue != attributeDefaultValue) {
130
+ const changeAttribute = {
131
+ ...currentAttribute,
132
+ defaultValue: attributeDefaultValue
133
+ };
134
+ if (this.database.inDialect("postgres")) {
135
+ const query = this.queryInterface.queryGenerator.attributesToSQL(
136
+ {
137
+ // @ts-ignore
138
+ [columnName]: this.queryInterface.normalizeAttribute(changeAttribute)
139
+ },
140
+ {
141
+ context: "changeColumn",
142
+ table: this.tableName
143
+ }
144
+ );
145
+ const sql = this.queryInterface.queryGenerator.changeColumnQuery(this.tableName, query);
146
+ const regex = /;ALTER TABLE "[^"]+"(\."[^"]+")? ALTER COLUMN "[^"]+" TYPE [^;]+;?$/;
147
+ await this.sequelize.query(sql.replace(regex, ""), options);
148
+ } else {
149
+ await this.queryInterface.changeColumn(this.tableName, columnName, changeAttribute, options);
150
+ }
151
+ }
152
+ }
153
+ }
154
+ async handleUniqueIndex(options) {
155
+ const existsIndexes = await this.queryInterface.showIndex(this.collection.getTableNameWithSchema(), options);
156
+ const existsUniqueIndexes = existsIndexes.filter((index) => index.unique);
157
+ const uniqueAttributes = Object.keys(this.rawAttributes).filter((key) => {
158
+ return this.rawAttributes[key].unique == true;
159
+ });
160
+ for (const existUniqueIndex of existsUniqueIndexes) {
161
+ const isSingleField = existUniqueIndex.fields.length == 1;
162
+ if (!isSingleField) continue;
163
+ const columnName = existUniqueIndex.fields[0].attribute;
164
+ const currentAttribute = this.findAttributeByColumnName(columnName);
165
+ if (!currentAttribute || !currentAttribute.unique && !currentAttribute.primaryKey) {
166
+ if (this.database.inDialect("postgres")) {
167
+ const constraints = await this.queryInterface.showConstraint(this.tableName, existUniqueIndex.name, options);
168
+ if (constraints.some((c) => c.constraintName === existUniqueIndex.name)) {
169
+ await this.queryInterface.removeConstraint(this.tableName, existUniqueIndex.name, options);
170
+ }
171
+ }
172
+ if (this.database.inDialect("sqlite")) {
173
+ const changeAttribute = {
174
+ ...currentAttribute,
175
+ unique: false
176
+ };
177
+ await this.queryInterface.changeColumn(this.tableName, columnName, changeAttribute, options);
178
+ } else {
179
+ await this.queryInterface.removeIndex(this.tableName, existUniqueIndex.name, options);
180
+ }
181
+ }
182
+ }
183
+ for (const uniqueAttribute of uniqueAttributes) {
184
+ const indexExists = existsUniqueIndexes.find((index) => {
185
+ return index.fields.length == 1 && index.fields[0].attribute == this.rawAttributes[uniqueAttribute].field;
186
+ });
187
+ if (!indexExists) {
188
+ await this.queryInterface.addIndex(this.tableName, [this.rawAttributes[uniqueAttribute].field], {
189
+ unique: true,
190
+ transaction: options == null ? void 0 : options.transaction
191
+ });
192
+ }
193
+ }
194
+ }
195
+ async getColumns(options) {
196
+ return await this.queryInterface.describeTable(this.tableName, options);
197
+ }
198
+ async isParentColumn(columnName, options) {
199
+ if (this.collection.isInherited()) {
200
+ const parentCollections = this.collection.getFlatParents();
201
+ for (const parentCollection of parentCollections) {
202
+ let parentColumns = this.tableDescMap[parentCollection.name];
203
+ if (!parentColumns) {
204
+ parentColumns = await this.queryInterface.describeTable(parentCollection.getTableNameWithSchema(), options);
205
+ this.tableDescMap[parentCollection.name] = parentColumns;
206
+ }
207
+ if (parentColumns[columnName]) {
208
+ return true;
209
+ }
210
+ }
211
+ }
212
+ return false;
213
+ }
214
+ async removeUnusedColumns(columns, options) {
215
+ for (const columnName in columns) {
216
+ const currentAttribute = this.findAttributeByColumnName(columnName);
217
+ if (!currentAttribute) {
218
+ let shouldDelete = true;
219
+ if (await this.isParentColumn(columnName, options)) {
220
+ shouldDelete = false;
221
+ }
222
+ if (shouldDelete) {
223
+ await this.queryInterface.removeColumn(this.model.getTableName(), columnName, options);
224
+ continue;
225
+ }
226
+ }
227
+ }
228
+ }
229
+ findAttributeByColumnName(columnName) {
230
+ return Object.values(this.rawAttributes).find((attribute) => {
231
+ return attribute.field == columnName;
232
+ });
233
+ }
234
+ async performSync(options) {
235
+ return this.collection.isInherited() ? await import_inherited_sync_runner.InheritedSyncRunner.syncInheritModel(this.model, options) : await import_sequelize.Model.sync.call(this.model, options);
236
+ }
237
+ async handleZeroColumnModel(options) {
238
+ if (Object.keys(this.model.tableAttributes).length === 0) {
239
+ if (this.database.inDialect("sqlite", "mysql", "mariadb", "postgres")) {
240
+ throw new import_zero_column_table_error.ZeroColumnTableError(
241
+ `Zero-column tables aren't supported in ${this.database.sequelize.getDialect()}`
242
+ );
243
+ }
244
+ const queryInterface = this.queryInterface;
245
+ if (!queryInterface.patched) {
246
+ const oldDescribeTable = queryInterface.describeTable;
247
+ queryInterface.describeTable = async function(...args) {
248
+ try {
249
+ return await oldDescribeTable.call(this, ...args);
250
+ } catch (err) {
251
+ if (err.message.includes("No description found for")) {
252
+ return [];
253
+ } else {
254
+ throw err;
255
+ }
256
+ }
257
+ };
258
+ queryInterface.patched = true;
259
+ }
260
+ }
261
+ }
262
+ async handleSchema(options) {
263
+ const _schema = this.model._schema;
264
+ if (_schema && _schema != "public") {
265
+ await this.sequelize.query(`CREATE SCHEMA IF NOT EXISTS "${_schema}";`, {
266
+ raw: true,
267
+ transaction: options == null ? void 0 : options.transaction
268
+ });
269
+ }
270
+ }
271
+ };
272
+ __name(_SyncRunner, "SyncRunner");
273
+ let SyncRunner = _SyncRunner;
274
+ // Annotate the CommonJS export names for ESM import in node:
275
+ 0 && (module.exports = {
276
+ SyncRunner
277
+ });
package/lib/types.d.ts ADDED
@@ -0,0 +1,49 @@
1
+ import type { CreateOptions, DestroyOptions, SaveOptions, SyncOptions, UpdateOptions } from 'sequelize';
2
+ import { HookReturn } from 'sequelize/types/hooks';
3
+ import { ValidationOptions } from 'sequelize/types/instance-validator';
4
+ import { Collection, CollectionOptions } from './collection';
5
+ import type { Model } from './model';
6
+ export type CollectionNameType = string;
7
+ export type ModelSyncEventType = 'beforeSync' | 'afterSync';
8
+ export type ModelValidateEventType = 'beforeValidate' | 'afterValidate';
9
+ export type ModelCreateEventType = 'beforeCreate' | 'afterCreate';
10
+ export type ModelUpdateEventType = 'beforeUpdate' | 'afterUpdate';
11
+ export type ModelSaveEventType = 'beforeSave' | 'afterSave';
12
+ export type ModelDestroyEventType = 'beforeDestroy' | 'afterDestroy';
13
+ export type ModelCreateWithAssociationsEventType = 'afterCreateWithAssociations';
14
+ export type ModelUpdateWithAssociationsEventType = 'afterUpdateWithAssociations';
15
+ export type ModelSaveWithAssociationsEventType = 'afterSaveWithAssociations';
16
+ export type ModelBulkCreateEvnetType = 'beforeBulkCreate' | 'afterBulkCreate';
17
+ export type ModelBulkUpdateEvnetType = 'beforeBulkUpdate' | 'afterBulkUpdate';
18
+ export type ModelBulkDestroyEvnetType = 'beforeBulkDestroy' | 'afterBulkDestroy';
19
+ export type ModelValidateEventTypes = ModelValidateEventType | `${CollectionNameType}.${ModelValidateEventType}`;
20
+ export type ModelCreateEventTypes = ModelCreateEventType | `${CollectionNameType}.${ModelCreateEventType}`;
21
+ export type ModelUpdateEventTypes = ModelUpdateEventType | `${CollectionNameType}.${ModelUpdateEventType}`;
22
+ export type ModelSaveEventTypes = ModelSaveEventType | `${CollectionNameType}.${ModelSaveEventType}`;
23
+ export type ModelDestroyEventTypes = ModelDestroyEventType | `${CollectionNameType}.${ModelDestroyEventType}`;
24
+ export type ModelCreateWithAssociationsEventTypes = ModelCreateWithAssociationsEventType | `${CollectionNameType}.${ModelCreateWithAssociationsEventType}`;
25
+ export type ModelUpdateWithAssociationsEventTypes = ModelUpdateWithAssociationsEventType | `${CollectionNameType}.${ModelUpdateWithAssociationsEventType}`;
26
+ export type ModelSaveWithAssociationsEventTypes = ModelSaveWithAssociationsEventType | `${CollectionNameType}.${ModelSaveWithAssociationsEventType}`;
27
+ export type ModelBulkCreateEvnetTypes = ModelBulkCreateEvnetType | `${CollectionNameType}.${ModelBulkCreateEvnetType}`;
28
+ export type ModelBulkUpdateEvnetTypes = ModelBulkUpdateEvnetType | `${CollectionNameType}.${ModelBulkUpdateEvnetType}`;
29
+ export type ModelBulkDestroyEvnetTypes = ModelBulkDestroyEvnetType | `${CollectionNameType}.${ModelBulkDestroyEvnetType}`;
30
+ export type ModelEventTypes = ModelSyncEventType | ModelValidateEventTypes | ModelCreateEventTypes | ModelUpdateEventTypes | ModelSaveEventTypes | ModelDestroyEventTypes | ModelCreateWithAssociationsEventTypes | ModelUpdateWithAssociationsEventTypes | ModelSaveWithAssociationsEventTypes | ModelBulkCreateEvnetTypes | ModelBulkUpdateEvnetTypes | ModelBulkDestroyEvnetTypes;
31
+ export type DatabaseBeforeDefineCollectionEventType = 'beforeDefineCollection';
32
+ export type DatabaseAfterDefineCollectionEventType = 'afterDefineCollection';
33
+ export type DatabaseBeforeRemoveCollectionEventType = 'beforeRemoveCollection';
34
+ export type DatabaseAfterRemoveCollectionEventType = 'afterRemoveCollection';
35
+ export type DatabaseEventTypes = DatabaseBeforeDefineCollectionEventType | DatabaseAfterDefineCollectionEventType | DatabaseBeforeRemoveCollectionEventType | DatabaseAfterRemoveCollectionEventType;
36
+ export type EventType = ModelEventTypes | DatabaseEventTypes | string;
37
+ export type { HookReturn };
38
+ export type SyncListener = (options?: SyncOptions) => HookReturn;
39
+ export type ValidateListener = (model: Model, options?: ValidationOptions) => HookReturn;
40
+ export type CreateListener = (model: Model, options?: CreateOptions) => HookReturn;
41
+ export type UpdateListener = (model: Model, options?: UpdateOptions) => HookReturn;
42
+ export type SaveListener = (model: Model, options?: SaveOptions) => HookReturn;
43
+ export type DestroyListener = (model: Model, options?: DestroyOptions) => HookReturn;
44
+ export type CreateWithAssociationsListener = (model: Model, options?: CreateOptions) => HookReturn;
45
+ export type UpdateWithAssociationsListener = (model: Model, options?: UpdateOptions) => HookReturn;
46
+ export type SaveWithAssociationsListener = (model: Model, options?: SaveOptions) => HookReturn;
47
+ export type BeforeDefineCollectionListener = (options: CollectionOptions) => void;
48
+ export type AfterDefineCollectionListener = (collection: Collection) => void;
49
+ export type RemoveCollectionListener = (collection: Collection) => void;
package/lib/types.js ADDED
@@ -0,0 +1,15 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __copyProps = (to, from, except, desc) => {
6
+ if (from && typeof from === "object" || typeof from === "function") {
7
+ for (let key of __getOwnPropNames(from))
8
+ if (!__hasOwnProp.call(to, key) && key !== except)
9
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
10
+ }
11
+ return to;
12
+ };
13
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
14
+ var types_exports = {};
15
+ module.exports = __toCommonJS(types_exports);
@@ -0,0 +1,60 @@
1
+ import { Association, BelongsToMany, Hookable, Transactionable } from 'sequelize';
2
+ import { Model } from './model';
3
+ export declare function modelAssociations(instance: Model): {
4
+ [key: string]: Association<import("sequelize").Model<any, any>, import("sequelize").Model<any, any>>;
5
+ };
6
+ export declare function belongsToManyAssociations(instance: Model): Array<BelongsToMany>;
7
+ export declare function modelAssociationByKey(instance: Model, key: string): Association;
8
+ type UpdateValue = {
9
+ [key: string]: any;
10
+ };
11
+ interface UpdateOptions extends Transactionable {
12
+ filter?: any;
13
+ filterByTk?: number | string;
14
+ whitelist?: string[];
15
+ blacklist?: string[];
16
+ updateAssociationValues?: string[];
17
+ sanitized?: boolean;
18
+ sourceModel?: Model;
19
+ }
20
+ interface UpdateAssociationOptions extends Transactionable, Hookable {
21
+ updateAssociationValues?: string[];
22
+ sourceModel?: Model;
23
+ context?: any;
24
+ associationContext?: any;
25
+ recursive?: boolean;
26
+ }
27
+ export declare function updateModelByValues(instance: Model, values: UpdateValue, options?: UpdateOptions): Promise<void>;
28
+ export declare function updateThroughTableValue(instance: Model, throughName: string, throughValues: any, source: Model, transaction?: any): Promise<any>;
29
+ /**
30
+ * update association of instance by values
31
+ * @param instance
32
+ * @param values
33
+ * @param options
34
+ */
35
+ export declare function updateAssociations(instance: Model, values: any, options?: UpdateAssociationOptions): Promise<void>;
36
+ /**
37
+ * update model association by key
38
+ * @param instance
39
+ * @param key
40
+ * @param value
41
+ * @param options
42
+ */
43
+ export declare function updateAssociation(instance: Model, key: string, value: any, options?: UpdateAssociationOptions): Promise<boolean>;
44
+ /**
45
+ * update belongsTo and HasOne
46
+ * @param model
47
+ * @param key
48
+ * @param value
49
+ * @param options
50
+ */
51
+ export declare function updateSingleAssociation(model: Model, key: string, value: any, options?: UpdateAssociationOptions): Promise<boolean>;
52
+ /**
53
+ * update multiple association of model by value
54
+ * @param model
55
+ * @param key
56
+ * @param value
57
+ * @param options
58
+ */
59
+ export declare function updateMultipleAssociation(model: Model, key: string, value: any, options?: UpdateAssociationOptions): Promise<boolean>;
60
+ export {};