bigal 12.1.5 → 13.0.0-beta2

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 (213) hide show
  1. package/.devcontainer/devcontainer.json +16 -0
  2. package/.husky/pre-commit +4 -0
  3. package/.prettierrc.cjs +34 -0
  4. package/CHANGELOG.md +5 -0
  5. package/dist/index.cjs +2626 -0
  6. package/dist/index.d.cts +892 -0
  7. package/dist/index.d.mts +892 -0
  8. package/dist/index.d.ts +892 -0
  9. package/dist/index.mjs +2605 -0
  10. package/eslint.config.mjs +25 -0
  11. package/package.json +44 -35
  12. package/tsconfig.build.json +7 -0
  13. package/Entity.d.ts +0 -16
  14. package/Entity.js +0 -13
  15. package/Entity.js.map +0 -1
  16. package/IReadonlyRepository.d.ts +0 -36
  17. package/IReadonlyRepository.js +0 -3
  18. package/IReadonlyRepository.js.map +0 -1
  19. package/IRepository.d.ts +0 -99
  20. package/IRepository.js +0 -3
  21. package/IRepository.js.map +0 -1
  22. package/ReadonlyRepository.d.ts +0 -69
  23. package/ReadonlyRepository.js +0 -687
  24. package/ReadonlyRepository.js.map +0 -1
  25. package/Repository.d.ts +0 -69
  26. package/Repository.js +0 -171
  27. package/Repository.js.map +0 -1
  28. package/SqlHelper.d.ts +0 -144
  29. package/SqlHelper.js +0 -1081
  30. package/SqlHelper.js.map +0 -1
  31. package/decorators/ColumnBaseOptions.d.ts +0 -10
  32. package/decorators/ColumnBaseOptions.js +0 -3
  33. package/decorators/ColumnBaseOptions.js.map +0 -1
  34. package/decorators/ColumnCollectionOptions.d.ts +0 -15
  35. package/decorators/ColumnCollectionOptions.js +0 -3
  36. package/decorators/ColumnCollectionOptions.js.map +0 -1
  37. package/decorators/ColumnModelOptions.d.ts +0 -7
  38. package/decorators/ColumnModelOptions.js +0 -3
  39. package/decorators/ColumnModelOptions.js.map +0 -1
  40. package/decorators/ColumnTypeOptions.d.ts +0 -21
  41. package/decorators/ColumnTypeOptions.js +0 -3
  42. package/decorators/ColumnTypeOptions.js.map +0 -1
  43. package/decorators/TableOptions.d.ts +0 -14
  44. package/decorators/TableOptions.js +0 -3
  45. package/decorators/TableOptions.js.map +0 -1
  46. package/decorators/column.d.ts +0 -9
  47. package/decorators/column.js +0 -93
  48. package/decorators/column.js.map +0 -1
  49. package/decorators/createDateColumn.d.ts +0 -6
  50. package/decorators/createDateColumn.js +0 -50
  51. package/decorators/createDateColumn.js.map +0 -1
  52. package/decorators/index.d.ts +0 -6
  53. package/decorators/index.js +0 -23
  54. package/decorators/index.js.map +0 -1
  55. package/decorators/primaryColumn.d.ts +0 -8
  56. package/decorators/primaryColumn.js +0 -66
  57. package/decorators/primaryColumn.js.map +0 -1
  58. package/decorators/table.d.ts +0 -5
  59. package/decorators/table.js +0 -40
  60. package/decorators/table.js.map +0 -1
  61. package/decorators/updateDateColumn.d.ts +0 -6
  62. package/decorators/updateDateColumn.js +0 -51
  63. package/decorators/updateDateColumn.js.map +0 -1
  64. package/decorators/versionColumn.d.ts +0 -6
  65. package/decorators/versionColumn.js +0 -51
  66. package/decorators/versionColumn.js.map +0 -1
  67. package/errors/QueryError.d.ts +0 -8
  68. package/errors/QueryError.js +0 -13
  69. package/errors/QueryError.js.map +0 -1
  70. package/errors/index.d.ts +0 -1
  71. package/errors/index.js +0 -18
  72. package/errors/index.js.map +0 -1
  73. package/index.d.ts +0 -35
  74. package/index.js +0 -220
  75. package/index.js.map +0 -1
  76. package/metadata/ColumnBaseMetadata.d.ts +0 -93
  77. package/metadata/ColumnBaseMetadata.js +0 -19
  78. package/metadata/ColumnBaseMetadata.js.map +0 -1
  79. package/metadata/ColumnCollectionMetadata.d.ts +0 -36
  80. package/metadata/ColumnCollectionMetadata.js +0 -63
  81. package/metadata/ColumnCollectionMetadata.js.map +0 -1
  82. package/metadata/ColumnMetadata.d.ts +0 -4
  83. package/metadata/ColumnMetadata.js +0 -3
  84. package/metadata/ColumnMetadata.js.map +0 -1
  85. package/metadata/ColumnModelMetadata.d.ts +0 -18
  86. package/metadata/ColumnModelMetadata.js +0 -43
  87. package/metadata/ColumnModelMetadata.js.map +0 -1
  88. package/metadata/ColumnModifierMetadata.d.ts +0 -46
  89. package/metadata/ColumnModifierMetadata.js +0 -3
  90. package/metadata/ColumnModifierMetadata.js.map +0 -1
  91. package/metadata/ColumnTypeMetadata.d.ts +0 -43
  92. package/metadata/ColumnTypeMetadata.js +0 -26
  93. package/metadata/ColumnTypeMetadata.js.map +0 -1
  94. package/metadata/MetadataStorage.d.ts +0 -13
  95. package/metadata/MetadataStorage.js +0 -19
  96. package/metadata/MetadataStorage.js.map +0 -1
  97. package/metadata/ModelMetadata.d.ts +0 -36
  98. package/metadata/ModelMetadata.js +0 -81
  99. package/metadata/ModelMetadata.js.map +0 -1
  100. package/metadata/index.d.ts +0 -10
  101. package/metadata/index.js +0 -33
  102. package/metadata/index.js.map +0 -1
  103. package/query/Comparer.d.ts +0 -1
  104. package/query/Comparer.js +0 -3
  105. package/query/Comparer.js.map +0 -1
  106. package/query/CountArgs.d.ts +0 -7
  107. package/query/CountArgs.js +0 -3
  108. package/query/CountArgs.js.map +0 -1
  109. package/query/CountResult.d.ts +0 -5
  110. package/query/CountResult.js +0 -3
  111. package/query/CountResult.js.map +0 -1
  112. package/query/CreateOptions.d.ts +0 -9
  113. package/query/CreateOptions.js +0 -3
  114. package/query/CreateOptions.js.map +0 -1
  115. package/query/CreateUpdateOptions.d.ts +0 -4
  116. package/query/CreateUpdateOptions.js +0 -3
  117. package/query/CreateUpdateOptions.js.map +0 -1
  118. package/query/DeleteOptions.d.ts +0 -12
  119. package/query/DeleteOptions.js +0 -3
  120. package/query/DeleteOptions.js.map +0 -1
  121. package/query/DestroyResult.d.ts +0 -5
  122. package/query/DestroyResult.js +0 -3
  123. package/query/DestroyResult.js.map +0 -1
  124. package/query/DoNotReturnRecords.d.ts +0 -3
  125. package/query/DoNotReturnRecords.js +0 -3
  126. package/query/DoNotReturnRecords.js.map +0 -1
  127. package/query/FindArgs.d.ts +0 -6
  128. package/query/FindArgs.js +0 -3
  129. package/query/FindArgs.js.map +0 -1
  130. package/query/FindOneArgs.d.ts +0 -11
  131. package/query/FindOneArgs.js +0 -3
  132. package/query/FindOneArgs.js.map +0 -1
  133. package/query/FindOneResult.d.ts +0 -12
  134. package/query/FindOneResult.js +0 -3
  135. package/query/FindOneResult.js.map +0 -1
  136. package/query/FindResult.d.ts +0 -15
  137. package/query/FindResult.js +0 -3
  138. package/query/FindResult.js.map +0 -1
  139. package/query/OnConflictOptions.d.ts +0 -25
  140. package/query/OnConflictOptions.js +0 -3
  141. package/query/OnConflictOptions.js.map +0 -1
  142. package/query/PaginateOptions.d.ts +0 -4
  143. package/query/PaginateOptions.js +0 -3
  144. package/query/PaginateOptions.js.map +0 -1
  145. package/query/PopulateArgs.d.ts +0 -13
  146. package/query/PopulateArgs.js +0 -3
  147. package/query/PopulateArgs.js.map +0 -1
  148. package/query/ReturnSelect.d.ts +0 -5
  149. package/query/ReturnSelect.js +0 -3
  150. package/query/ReturnSelect.js.map +0 -1
  151. package/query/Sort.d.ts +0 -15
  152. package/query/Sort.js +0 -3
  153. package/query/Sort.js.map +0 -1
  154. package/query/WhereQuery.d.ts +0 -21
  155. package/query/WhereQuery.js +0 -3
  156. package/query/WhereQuery.js.map +0 -1
  157. package/query/index.d.ts +0 -15
  158. package/query/index.js +0 -32
  159. package/query/index.js.map +0 -1
  160. package/types/ClassLike.d.ts +0 -8
  161. package/types/ClassLike.js +0 -3
  162. package/types/ClassLike.js.map +0 -1
  163. package/types/CreateUpdateParams.d.ts +0 -9
  164. package/types/CreateUpdateParams.js +0 -3
  165. package/types/CreateUpdateParams.js.map +0 -1
  166. package/types/EntityPrimitiveOrId.d.ts +0 -2
  167. package/types/EntityPrimitiveOrId.js +0 -3
  168. package/types/EntityPrimitiveOrId.js.map +0 -1
  169. package/types/ExcludeEntityCollections.d.ts +0 -5
  170. package/types/ExcludeEntityCollections.js +0 -3
  171. package/types/ExcludeEntityCollections.js.map +0 -1
  172. package/types/ExcludeFunctions.d.ts +0 -4
  173. package/types/ExcludeFunctions.js +0 -3
  174. package/types/ExcludeFunctions.js.map +0 -1
  175. package/types/GetValueType.d.ts +0 -1
  176. package/types/GetValueType.js +0 -3
  177. package/types/GetValueType.js.map +0 -1
  178. package/types/IncludeFunctions.d.ts +0 -4
  179. package/types/IncludeFunctions.js +0 -3
  180. package/types/IncludeFunctions.js.map +0 -1
  181. package/types/IsValueOfType.d.ts +0 -1
  182. package/types/IsValueOfType.js +0 -3
  183. package/types/IsValueOfType.js.map +0 -1
  184. package/types/OmitEntityCollections.d.ts +0 -7
  185. package/types/OmitEntityCollections.js +0 -3
  186. package/types/OmitEntityCollections.js.map +0 -1
  187. package/types/OmitFunctions.d.ts +0 -7
  188. package/types/OmitFunctions.js +0 -3
  189. package/types/OmitFunctions.js.map +0 -1
  190. package/types/PickAsType.d.ts +0 -3
  191. package/types/PickAsType.js +0 -3
  192. package/types/PickAsType.js.map +0 -1
  193. package/types/PickByValueType.d.ts +0 -5
  194. package/types/PickByValueType.js +0 -3
  195. package/types/PickByValueType.js.map +0 -1
  196. package/types/PickFunctions.d.ts +0 -4
  197. package/types/PickFunctions.js +0 -3
  198. package/types/PickFunctions.js.map +0 -1
  199. package/types/Populated.d.ts +0 -9
  200. package/types/Populated.js +0 -3
  201. package/types/Populated.js.map +0 -1
  202. package/types/QueryResult.d.ts +0 -9
  203. package/types/QueryResult.js +0 -3
  204. package/types/QueryResult.js.map +0 -1
  205. package/types/QueryResultOptionalPopulated.d.ts +0 -9
  206. package/types/QueryResultOptionalPopulated.js +0 -3
  207. package/types/QueryResultOptionalPopulated.js.map +0 -1
  208. package/types/QueryResultPopulated.d.ts +0 -9
  209. package/types/QueryResultPopulated.js +0 -3
  210. package/types/QueryResultPopulated.js.map +0 -1
  211. package/types/index.d.ts +0 -17
  212. package/types/index.js +0 -34
  213. package/types/index.js.map +0 -1
package/dist/index.cjs ADDED
@@ -0,0 +1,2626 @@
1
+ 'use strict';
2
+
3
+ const _ = require('lodash');
4
+
5
+ function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
6
+
7
+ const ___default = /*#__PURE__*/_interopDefaultCompat(_);
8
+
9
+ var __defProp$7 = Object.defineProperty;
10
+ var __defNormalProp$7 = (obj, key, value) => key in obj ? __defProp$7(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
11
+ var __publicField$7 = (obj, key, value) => {
12
+ __defNormalProp$7(obj, typeof key !== "symbol" ? key + "" : key, value);
13
+ return value;
14
+ };
15
+ class MetadataStorage {
16
+ constructor() {
17
+ __publicField$7(this, "models", []);
18
+ // All columns for all models. This data only represents @column specifics, not additional column modifiers
19
+ __publicField$7(this, "columns", []);
20
+ // This represents additional column behavior separate from the main @column decorator. For example, @primaryColumn, @versionColumn, etc
21
+ // This behavior will be merged over defaults from @column()
22
+ __publicField$7(this, "columnModifiers", []);
23
+ }
24
+ }
25
+
26
+ var __defProp$6 = Object.defineProperty;
27
+ var __defNormalProp$6 = (obj, key, value) => key in obj ? __defProp$6(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
28
+ var __publicField$6 = (obj, key, value) => {
29
+ __defNormalProp$6(obj, typeof key !== "symbol" ? key + "" : key, value);
30
+ return value;
31
+ };
32
+ class ColumnBaseMetadata {
33
+ constructor({
34
+ target,
35
+ name,
36
+ propertyName,
37
+ required = false,
38
+ insert = true,
39
+ update = true,
40
+ primary = false,
41
+ createDate = false,
42
+ updateDate = false,
43
+ version = false
44
+ }) {
45
+ /**
46
+ * Name of class with @table decorator
47
+ */
48
+ __publicField$6(this, "target");
49
+ /**
50
+ * Column name in the database
51
+ */
52
+ __publicField$6(this, "name");
53
+ /**
54
+ * Class property to which the column is applied
55
+ */
56
+ __publicField$6(this, "propertyName");
57
+ /**
58
+ * Indicates if a value is required for creates.
59
+ */
60
+ __publicField$6(this, "required");
61
+ /**
62
+ * Indicates if column is inserted by default. Default is true
63
+ */
64
+ __publicField$6(this, "insert");
65
+ /**
66
+ * Indicates if column value is updated by "save" operation. Default is true
67
+ */
68
+ __publicField$6(this, "update");
69
+ /**
70
+ * Indicates if this column is a primary key.
71
+ * Same can be achieved when @primaryColumn decorator is used
72
+ */
73
+ __publicField$6(this, "primary");
74
+ /**
75
+ * Value will be equal to `new Date()` when the row is inserted into the table
76
+ * Same can be achieved when @createDateColumn decorator is used
77
+ */
78
+ __publicField$6(this, "createDate");
79
+ /**
80
+ * Value will be equal to `new Date()` when the row is updated
81
+ * Same can be achieved when @updateDateColumn decorator is used
82
+ */
83
+ __publicField$6(this, "updateDate");
84
+ /**
85
+ * Value will be set to 1 when the row is inserted. Value will be incremented by one when the row is updated
86
+ * Same can be achieved when @versionColumn decorator is used
87
+ */
88
+ __publicField$6(this, "version");
89
+ this.target = target;
90
+ this.name = name;
91
+ this.propertyName = propertyName;
92
+ this.required = required;
93
+ this.insert = insert;
94
+ this.update = update;
95
+ this.primary = primary;
96
+ this.createDate = createDate;
97
+ this.updateDate = updateDate;
98
+ this.version = version;
99
+ }
100
+ }
101
+
102
+ var __defProp$5 = Object.defineProperty;
103
+ var __defNormalProp$5 = (obj, key, value) => key in obj ? __defProp$5(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
104
+ var __publicField$5 = (obj, key, value) => {
105
+ __defNormalProp$5(obj, typeof key !== "symbol" ? key + "" : key, value);
106
+ return value;
107
+ };
108
+ class ColumnCollectionMetadata extends ColumnBaseMetadata {
109
+ constructor({
110
+ target,
111
+ //
112
+ name,
113
+ propertyName,
114
+ required,
115
+ insert,
116
+ update,
117
+ primary,
118
+ createDate,
119
+ updateDate,
120
+ version,
121
+ collection,
122
+ via,
123
+ through
124
+ }) {
125
+ super({
126
+ target,
127
+ name,
128
+ propertyName,
129
+ required,
130
+ insert,
131
+ update,
132
+ primary,
133
+ createDate,
134
+ updateDate,
135
+ version
136
+ });
137
+ __publicField$5(this, "_collectionString");
138
+ __publicField$5(this, "_collectionFn");
139
+ __publicField$5(this, "_throughString");
140
+ __publicField$5(this, "_throughFn");
141
+ /**
142
+ * Property name of the on the collection item type
143
+ */
144
+ __publicField$5(this, "via");
145
+ this.via = via;
146
+ if (typeof collection === "string") {
147
+ this._collectionString = collection;
148
+ } else {
149
+ this._collectionFn = collection;
150
+ }
151
+ if (typeof through === "string") {
152
+ this._throughString = through;
153
+ } else if (through) {
154
+ this._throughFn = through;
155
+ }
156
+ }
157
+ /**
158
+ * Type of the items in the collection
159
+ */
160
+ get collection() {
161
+ if (this._collectionString) {
162
+ return this._collectionString;
163
+ }
164
+ if (!this._collectionFn) {
165
+ throw new Error(`Unable to determine collection type for ${this.target}#${this.propertyName}`);
166
+ }
167
+ this._collectionString = this._collectionFn();
168
+ return this._collectionString;
169
+ }
170
+ /**
171
+ * Name of the junction table for multi-multi associations
172
+ */
173
+ get through() {
174
+ if (this._throughString) {
175
+ return this._throughString;
176
+ }
177
+ if (this._throughFn) {
178
+ this._throughString = this._throughFn();
179
+ return this._throughString;
180
+ }
181
+ return void 0;
182
+ }
183
+ }
184
+
185
+ var __defProp$4 = Object.defineProperty;
186
+ var __defNormalProp$4 = (obj, key, value) => key in obj ? __defProp$4(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
187
+ var __publicField$4 = (obj, key, value) => {
188
+ __defNormalProp$4(obj, typeof key !== "symbol" ? key + "" : key, value);
189
+ return value;
190
+ };
191
+ class ColumnModelMetadata extends ColumnBaseMetadata {
192
+ constructor({
193
+ target,
194
+ //
195
+ name,
196
+ propertyName,
197
+ required,
198
+ insert,
199
+ update,
200
+ primary,
201
+ createDate,
202
+ updateDate,
203
+ version,
204
+ model
205
+ }) {
206
+ super({
207
+ target,
208
+ name,
209
+ propertyName,
210
+ required,
211
+ insert,
212
+ update,
213
+ primary,
214
+ createDate,
215
+ updateDate,
216
+ version
217
+ });
218
+ __publicField$4(this, "_modelString");
219
+ __publicField$4(this, "_modelFn");
220
+ if (typeof model === "string") {
221
+ this._modelString = model;
222
+ } else {
223
+ this._modelFn = model;
224
+ }
225
+ }
226
+ /**
227
+ * Name of the model represented by this column id
228
+ */
229
+ get model() {
230
+ if (this._modelString) {
231
+ return this._modelString;
232
+ }
233
+ if (!this._modelFn) {
234
+ throw new Error(`Unable to determine model type for ${this.target}#${this.propertyName}`);
235
+ }
236
+ this._modelString = this._modelFn();
237
+ return this._modelString;
238
+ }
239
+ }
240
+
241
+ var __defProp$3 = Object.defineProperty;
242
+ var __defNormalProp$3 = (obj, key, value) => key in obj ? __defProp$3(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
243
+ var __publicField$3 = (obj, key, value) => {
244
+ __defNormalProp$3(obj, typeof key !== "symbol" ? key + "" : key, value);
245
+ return value;
246
+ };
247
+ class ColumnTypeMetadata extends ColumnBaseMetadata {
248
+ constructor(options) {
249
+ super({
250
+ target: options.target,
251
+ name: options.name,
252
+ propertyName: options.propertyName,
253
+ required: options.required,
254
+ insert: options.insert,
255
+ update: options.update,
256
+ primary: options.primary,
257
+ createDate: options.createDate,
258
+ updateDate: options.updateDate,
259
+ version: options.version
260
+ });
261
+ /**
262
+ * Type of the column
263
+ */
264
+ __publicField$3(this, "type");
265
+ /**
266
+ * Default database value
267
+ */
268
+ __publicField$3(this, "defaultsTo");
269
+ /**
270
+ * Array of possible enumerated values
271
+ */
272
+ __publicField$3(this, "enum");
273
+ /**
274
+ * If set, enforces a maximum length check on the column
275
+ *
276
+ * Applies to types: string | string[]
277
+ */
278
+ __publicField$3(this, "maxLength");
279
+ this.type = options.type;
280
+ this.defaultsTo = options.defaultsTo;
281
+ this.enum = options.enum;
282
+ this.maxLength = options.maxLength;
283
+ }
284
+ }
285
+
286
+ var __defProp$2 = Object.defineProperty;
287
+ var __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
288
+ var __publicField$2 = (obj, key, value) => {
289
+ __defNormalProp$2(obj, typeof key !== "symbol" ? key + "" : key, value);
290
+ return value;
291
+ };
292
+ class ModelMetadata {
293
+ constructor({
294
+ name,
295
+ //
296
+ type,
297
+ connection,
298
+ tableName,
299
+ readonly = false
300
+ }) {
301
+ __publicField$2(this, "_columns", []);
302
+ __publicField$2(this, "_primaryKeyColumn");
303
+ __publicField$2(this, "_createDateColumns", []);
304
+ __publicField$2(this, "_updateDateColumns", []);
305
+ __publicField$2(this, "_versionDateColumns", []);
306
+ __publicField$2(this, "name");
307
+ __publicField$2(this, "type");
308
+ __publicField$2(this, "connection");
309
+ __publicField$2(this, "tableName");
310
+ __publicField$2(this, "readonly");
311
+ __publicField$2(this, "columnsByColumnName", {});
312
+ __publicField$2(this, "columnsByPropertyName", {});
313
+ this.name = name;
314
+ this.type = type;
315
+ this.connection = connection;
316
+ this.tableName = tableName ?? ___default.snakeCase(name);
317
+ this.readonly = readonly;
318
+ }
319
+ set columns(columns) {
320
+ this._columns = columns;
321
+ this.columnsByColumnName = {};
322
+ this.columnsByPropertyName = {};
323
+ for (const column of columns) {
324
+ this.columnsByColumnName[column.name] = column;
325
+ this.columnsByPropertyName[column.propertyName] = column;
326
+ if (column.primary) {
327
+ this._primaryKeyColumn = column;
328
+ }
329
+ if (column.createDate) {
330
+ this._createDateColumns.push(column);
331
+ }
332
+ if (column.updateDate) {
333
+ this._updateDateColumns.push(column);
334
+ }
335
+ if (column.version) {
336
+ this._versionDateColumns.push(column);
337
+ }
338
+ }
339
+ }
340
+ get columns() {
341
+ return this._columns;
342
+ }
343
+ get primaryKeyColumn() {
344
+ return this._primaryKeyColumn;
345
+ }
346
+ get createDateColumns() {
347
+ return this._createDateColumns;
348
+ }
349
+ get updateDateColumns() {
350
+ return this._updateDateColumns;
351
+ }
352
+ get versionColumns() {
353
+ return this._versionDateColumns;
354
+ }
355
+ }
356
+
357
+ function getMetadataStorage() {
358
+ if (!global.bigAlMetadataArgsStorage) {
359
+ global.bigAlMetadataArgsStorage = new MetadataStorage();
360
+ }
361
+ return global.bigAlMetadataArgsStorage;
362
+ }
363
+
364
+ var __defProp$1 = Object.defineProperty;
365
+ var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
366
+ var __publicField$1 = (obj, key, value) => {
367
+ __defNormalProp$1(obj, typeof key !== "symbol" ? key + "" : key, value);
368
+ return value;
369
+ };
370
+ class QueryError extends Error {
371
+ constructor(message, model, where) {
372
+ super(message);
373
+ __publicField$1(this, "model");
374
+ __publicField$1(this, "where");
375
+ this.name = "QueryError";
376
+ this.model = model;
377
+ this.where = where;
378
+ Object.setPrototypeOf(this, QueryError.prototype);
379
+ }
380
+ }
381
+
382
+ function getSelectQueryAndParams({
383
+ repositoriesByModelNameLowered,
384
+ model,
385
+ select,
386
+ where,
387
+ sorts,
388
+ skip,
389
+ limit
390
+ }) {
391
+ let query = "SELECT ";
392
+ query += getColumnsToSelect({
393
+ model,
394
+ select
395
+ });
396
+ query += ` FROM "${model.tableName}"`;
397
+ const { whereStatement, params } = buildWhereStatement({
398
+ repositoriesByModelNameLowered,
399
+ model,
400
+ where
401
+ });
402
+ if (whereStatement) {
403
+ query += ` ${whereStatement}`;
404
+ }
405
+ const orderStatement = buildOrderStatement({
406
+ model,
407
+ sorts
408
+ });
409
+ if (orderStatement) {
410
+ query += ` ${orderStatement}`;
411
+ }
412
+ if (limit) {
413
+ if (___default.isString(limit)) {
414
+ limit = Number(limit);
415
+ }
416
+ if (!___default.isFinite(limit)) {
417
+ throw new QueryError("Limit should be a number", model, where);
418
+ }
419
+ query += ` LIMIT ${limit}`;
420
+ }
421
+ if (skip) {
422
+ if (___default.isString(skip)) {
423
+ skip = Number(skip);
424
+ }
425
+ if (!___default.isFinite(skip)) {
426
+ throw new QueryError("Skip should be a number", model, where);
427
+ }
428
+ query += ` OFFSET ${skip}`;
429
+ }
430
+ if (process.env.DEBUG_BIGAL?.toLowerCase() === "true") {
431
+ console.log(`BigAl: ${query}`);
432
+ }
433
+ return {
434
+ query,
435
+ params
436
+ };
437
+ }
438
+ function getCountQueryAndParams({
439
+ repositoriesByModelNameLowered,
440
+ model,
441
+ where
442
+ }) {
443
+ let query = `SELECT count(*) AS "count" FROM "${model.tableName}"`;
444
+ const { whereStatement, params } = buildWhereStatement({
445
+ repositoriesByModelNameLowered,
446
+ model,
447
+ where
448
+ });
449
+ if (whereStatement) {
450
+ query += ` ${whereStatement}`;
451
+ }
452
+ return {
453
+ query,
454
+ params
455
+ };
456
+ }
457
+ function getInsertQueryAndParams({
458
+ repositoriesByModelNameLowered,
459
+ model,
460
+ values,
461
+ returnRecords = true,
462
+ returnSelect,
463
+ onConflict
464
+ }) {
465
+ const entitiesToInsert = ___default.isArray(values) ? values : [values];
466
+ const conflictTargetColumns = [];
467
+ const columnsToInsert = [];
468
+ const columnsToMerge = [];
469
+ let hasEmptyMergeColumns = false;
470
+ for (const column of model.columns) {
471
+ const collectionColumn = column;
472
+ if (!collectionColumn.collection) {
473
+ const { defaultsTo } = column;
474
+ let defaultValue;
475
+ if (___default.isFunction(defaultsTo)) {
476
+ defaultValue = defaultsTo();
477
+ } else if (!___default.isUndefined(defaultsTo)) {
478
+ defaultValue = defaultsTo;
479
+ } else if (column.createDate) {
480
+ defaultValue = /* @__PURE__ */ new Date();
481
+ } else if (column.updateDate) {
482
+ defaultValue = /* @__PURE__ */ new Date();
483
+ } else if (column.version) {
484
+ defaultValue = 1;
485
+ }
486
+ const hasDefaultValue = !___default.isUndefined(defaultValue);
487
+ let includePropertyName = false;
488
+ for (const entity of entitiesToInsert) {
489
+ if (hasDefaultValue && ___default.isUndefined(entity[column.propertyName])) {
490
+ entity[column.propertyName] = defaultValue;
491
+ }
492
+ if (___default.isUndefined(entity[column.propertyName])) {
493
+ if (column.required) {
494
+ throw new QueryError(`Create statement for "${model.name}" is missing value for required field: ${column.propertyName}`, model);
495
+ }
496
+ } else {
497
+ includePropertyName = true;
498
+ const { maxLength, type } = column;
499
+ if (maxLength && ["string", "string[]"].includes(type)) {
500
+ const entityValues = entity[column.propertyName] ?? "";
501
+ const normalizedValues = Array.isArray(entityValues) ? entityValues : [entityValues];
502
+ for (const normalizedValue of normalizedValues) {
503
+ if (normalizedValue.length > maxLength) {
504
+ throw new QueryError(`Create statement for "${model.name}" contains a value that exceeds maxLength on field: ${column.propertyName}`, model);
505
+ }
506
+ }
507
+ }
508
+ }
509
+ }
510
+ if (includePropertyName) {
511
+ columnsToInsert.push(column);
512
+ }
513
+ if (onConflict) {
514
+ if (Array.isArray(onConflict.targets) && onConflict.targets.includes(column.propertyName) || !Array.isArray(onConflict.targets) && onConflict.targets.columns.includes(column.propertyName)) {
515
+ conflictTargetColumns.push(column);
516
+ }
517
+ if (onConflict.action === "merge") {
518
+ const mergeColumns = Array.isArray(onConflict.merge) ? onConflict.merge : onConflict.merge?.columns;
519
+ if (mergeColumns) {
520
+ hasEmptyMergeColumns = !mergeColumns.length;
521
+ if (mergeColumns.includes(column.propertyName)) {
522
+ columnsToMerge.push(column);
523
+ }
524
+ } else if (!column.createDate && !column.primary) {
525
+ columnsToMerge.push(column);
526
+ }
527
+ }
528
+ }
529
+ }
530
+ }
531
+ const valueCollections = entitiesToInsert.map(() => []);
532
+ const params = [];
533
+ let query = `INSERT INTO "${model.tableName}" (`;
534
+ for (const [columnIndex, column] of columnsToInsert.entries()) {
535
+ if (columnIndex > 0) {
536
+ query += ",";
537
+ }
538
+ query += `"${column.name}"`;
539
+ for (const [entityIndex, entity] of entitiesToInsert.entries()) {
540
+ let value;
541
+ const entityValue = entity[column.propertyName];
542
+ if (___default.isNil(entityValue)) {
543
+ value = "NULL";
544
+ } else {
545
+ const isJsonArray = column.type === "json" && ___default.isArray(entityValue);
546
+ const relatedModelName = column.model;
547
+ if (relatedModelName && ___default.isObject(entityValue)) {
548
+ const relatedModelRepository = repositoriesByModelNameLowered[relatedModelName.toLowerCase()];
549
+ if (!relatedModelRepository) {
550
+ throw new QueryError(`Unable to find model schema (${relatedModelName}) specified as model type for "${column.propertyName}" on "${model.name}"`, model);
551
+ }
552
+ const relatedModelPrimaryKey = relatedModelRepository.model.primaryKeyColumn;
553
+ if (!relatedModelPrimaryKey) {
554
+ throw new QueryError(`Unable to find primary key column for ${relatedModelName} when inserting ${model.name}.${column.propertyName} value.`, model);
555
+ }
556
+ const primaryKeyValue = entityValue[relatedModelPrimaryKey.propertyName];
557
+ if (___default.isNil(primaryKeyValue)) {
558
+ throw new QueryError(`Undefined primary key value for hydrated object value for "${column.propertyName}" on "${model.name}"`, model);
559
+ }
560
+ params.push(primaryKeyValue);
561
+ } else if (isJsonArray) {
562
+ params.push(JSON.stringify(entityValue));
563
+ } else {
564
+ params.push(entityValue);
565
+ }
566
+ value = `$${params.length}`;
567
+ if (isJsonArray) {
568
+ value += "::jsonb";
569
+ }
570
+ }
571
+ const valuesForEntityIndex = valueCollections[entityIndex];
572
+ if (!valuesForEntityIndex) {
573
+ throw new QueryError("Error trying to get insert values for entity index", model);
574
+ }
575
+ valuesForEntityIndex.push(value);
576
+ }
577
+ }
578
+ query += ") VALUES ";
579
+ for (const [index, valueCollection] of valueCollections.entries()) {
580
+ if (index > 0) {
581
+ query += ",";
582
+ }
583
+ query += `(${valueCollection.join(",")})`;
584
+ }
585
+ if (onConflict) {
586
+ query += " ON CONFLICT (";
587
+ for (const [index, targetColumn] of conflictTargetColumns.entries()) {
588
+ if (index > 0) {
589
+ query += ",";
590
+ }
591
+ query += `"${targetColumn.name}"`;
592
+ }
593
+ query += ") ";
594
+ if (!Array.isArray(onConflict.targets)) {
595
+ const { whereStatement } = buildWhereStatement({
596
+ repositoriesByModelNameLowered,
597
+ model,
598
+ where: onConflict.targets.where,
599
+ params
600
+ });
601
+ if (whereStatement) {
602
+ query += `${whereStatement} `;
603
+ }
604
+ }
605
+ if (onConflict.action === "ignore" || hasEmptyMergeColumns) {
606
+ query += "DO NOTHING";
607
+ } else {
608
+ query += "DO UPDATE SET ";
609
+ for (const [index, column] of columnsToMerge.entries()) {
610
+ if (index > 0) {
611
+ query += ",";
612
+ }
613
+ if (column.version) {
614
+ query += `"${column.name}"="${column.name}"+1`;
615
+ } else {
616
+ query += `"${column.name}"=EXCLUDED."${column.name}"`;
617
+ }
618
+ }
619
+ if (!Array.isArray(onConflict.merge) && onConflict.merge?.where) {
620
+ const { whereStatement } = buildWhereStatement({
621
+ repositoriesByModelNameLowered,
622
+ model,
623
+ where: onConflict.merge.where,
624
+ params
625
+ });
626
+ if (whereStatement) {
627
+ query += ` ${whereStatement}`;
628
+ }
629
+ }
630
+ }
631
+ }
632
+ if (returnRecords) {
633
+ query += " RETURNING ";
634
+ query += getColumnsToSelect({
635
+ model,
636
+ select: returnSelect
637
+ });
638
+ }
639
+ return {
640
+ query,
641
+ params
642
+ };
643
+ }
644
+ function getUpdateQueryAndParams({
645
+ repositoriesByModelNameLowered,
646
+ model,
647
+ where,
648
+ values = {},
649
+ returnRecords = true,
650
+ returnSelect
651
+ }) {
652
+ for (const column of model.updateDateColumns) {
653
+ if (___default.isUndefined(values[column.propertyName])) {
654
+ values[column.propertyName] = /* @__PURE__ */ new Date();
655
+ }
656
+ }
657
+ const params = [];
658
+ let query = `UPDATE "${model.tableName}" SET `;
659
+ let isFirstProperty = true;
660
+ for (const [propertyName, value] of Object.entries(values)) {
661
+ const column = model.columnsByPropertyName[propertyName];
662
+ if (column && !column.collection) {
663
+ if (!isFirstProperty) {
664
+ query += ",";
665
+ }
666
+ query += `"${column.name}"=`;
667
+ if (___default.isNil(value)) {
668
+ query += "NULL";
669
+ } else {
670
+ const isJsonArray = column.type === "json" && ___default.isArray(value);
671
+ const relatedModelName = column.model;
672
+ const { maxLength, type } = column;
673
+ if (maxLength && ["string", "string[]"].includes(type)) {
674
+ const normalizedValues = Array.isArray(value) ? value : [value];
675
+ for (const normalizedValue of normalizedValues) {
676
+ if (normalizedValue?.length > maxLength) {
677
+ throw new QueryError(`Update statement for "${model.name}" contains a value that exceeds maxLength on field: ${column.propertyName}`, model);
678
+ }
679
+ }
680
+ }
681
+ if (relatedModelName && ___default.isObject(value)) {
682
+ const relatedModelRepository = repositoriesByModelNameLowered[relatedModelName.toLowerCase()];
683
+ if (!relatedModelRepository) {
684
+ throw new QueryError(`Unable to find model schema (${relatedModelName}) specified as model type for "${propertyName}" on "${model.name}"`, model);
685
+ }
686
+ const relatedModelPrimaryKey = relatedModelRepository.model.primaryKeyColumn;
687
+ if (!relatedModelPrimaryKey) {
688
+ throw new QueryError(`Unable to find primary key column for ${relatedModelName} when inserting ${model.name}.${column.propertyName} value.`, model);
689
+ }
690
+ const primaryKeyValue = value[relatedModelPrimaryKey.propertyName];
691
+ if (___default.isNil(primaryKeyValue)) {
692
+ throw new QueryError(`Undefined primary key value for hydrated object value for "${column.propertyName}" on "${model.name}"`, model);
693
+ }
694
+ params.push(primaryKeyValue);
695
+ } else if (isJsonArray) {
696
+ params.push(JSON.stringify(value));
697
+ } else {
698
+ params.push(value);
699
+ }
700
+ query += `$${params.length}`;
701
+ if (isJsonArray) {
702
+ query += "::jsonb";
703
+ }
704
+ }
705
+ isFirstProperty = false;
706
+ }
707
+ }
708
+ for (const column of model.versionColumns) {
709
+ if (!___default.isUndefined(values[column.propertyName])) {
710
+ if (!isFirstProperty) {
711
+ query += ",";
712
+ }
713
+ query += `"${column.name}"="${column.name}"+1`;
714
+ isFirstProperty = false;
715
+ }
716
+ }
717
+ const { whereStatement } = buildWhereStatement({
718
+ repositoriesByModelNameLowered,
719
+ model,
720
+ where,
721
+ params
722
+ });
723
+ if (whereStatement) {
724
+ query += ` ${whereStatement}`;
725
+ }
726
+ if (returnRecords) {
727
+ query += " RETURNING ";
728
+ query += getColumnsToSelect({
729
+ model,
730
+ select: returnSelect
731
+ });
732
+ }
733
+ return {
734
+ query,
735
+ params
736
+ };
737
+ }
738
+ function getDeleteQueryAndParams({
739
+ repositoriesByModelNameLowered,
740
+ model,
741
+ where,
742
+ returnRecords = true,
743
+ returnSelect
744
+ }) {
745
+ let query = `DELETE FROM "${model.tableName}"`;
746
+ const { whereStatement, params } = buildWhereStatement({
747
+ repositoriesByModelNameLowered,
748
+ model,
749
+ where
750
+ });
751
+ if (whereStatement) {
752
+ query += ` ${whereStatement}`;
753
+ }
754
+ if (returnRecords) {
755
+ query += " RETURNING ";
756
+ query += getColumnsToSelect({
757
+ model,
758
+ select: returnSelect
759
+ });
760
+ }
761
+ return {
762
+ query,
763
+ params
764
+ };
765
+ }
766
+ function getColumnsToSelect({
767
+ model,
768
+ select
769
+ }) {
770
+ let selectColumns;
771
+ if (select) {
772
+ const { primaryKeyColumn } = model;
773
+ selectColumns = new Set(select);
774
+ if (primaryKeyColumn) {
775
+ selectColumns.add(primaryKeyColumn.propertyName);
776
+ }
777
+ } else {
778
+ selectColumns = /* @__PURE__ */ new Set();
779
+ for (const column of model.columns) {
780
+ if (!column.collection) {
781
+ selectColumns.add(column.propertyName);
782
+ }
783
+ }
784
+ }
785
+ let query = "";
786
+ for (const [index, propertyName] of Array.from(selectColumns).entries()) {
787
+ const column = model.columnsByPropertyName[propertyName];
788
+ if (!column) {
789
+ throw new QueryError(`Unable to find column for property: ${propertyName} on ${model.tableName}`, model);
790
+ }
791
+ if (index > 0) {
792
+ query += ",";
793
+ }
794
+ if (column.name === propertyName) {
795
+ query += `"${propertyName}"`;
796
+ } else {
797
+ query += `"${column.name}" AS "${propertyName}"`;
798
+ }
799
+ }
800
+ return query;
801
+ }
802
+ function buildWhereStatement({
803
+ repositoriesByModelNameLowered,
804
+ model,
805
+ where,
806
+ params = []
807
+ }) {
808
+ let whereStatement;
809
+ if (___default.isObject(where) && Object.keys(where).length) {
810
+ whereStatement = buildWhere({
811
+ repositoriesByModelNameLowered,
812
+ model,
813
+ comparer: "and",
814
+ value: where,
815
+ params
816
+ });
817
+ if (!whereStatement) {
818
+ throw new QueryError(`WHERE statement is unexpectedly empty.`, model, where);
819
+ }
820
+ }
821
+ if (whereStatement) {
822
+ whereStatement = `WHERE ${whereStatement}`;
823
+ }
824
+ return {
825
+ whereStatement,
826
+ params
827
+ };
828
+ }
829
+ function buildOrderStatement({ model, sorts }) {
830
+ if (___default.isNil(sorts) || !___default.some(sorts)) {
831
+ return "";
832
+ }
833
+ let orderStatement = "ORDER BY ";
834
+ for (const [index, orderProperty] of sorts.entries()) {
835
+ if (index > 0) {
836
+ orderStatement += ",";
837
+ }
838
+ const { propertyName, descending } = orderProperty;
839
+ const column = model.columnsByPropertyName[propertyName];
840
+ if (!column) {
841
+ throw new QueryError(`Property (${propertyName}) not found in model (${model.name}).`, model);
842
+ }
843
+ orderStatement += `"${column.name}"`;
844
+ if (descending) {
845
+ orderStatement += " DESC";
846
+ }
847
+ }
848
+ return orderStatement;
849
+ }
850
+ function buildWhere({
851
+ repositoriesByModelNameLowered,
852
+ model,
853
+ propertyName,
854
+ comparer,
855
+ isNegated = false,
856
+ value,
857
+ params = []
858
+ }) {
859
+ switch (comparer ?? propertyName) {
860
+ case "!":
861
+ case "not":
862
+ return buildWhere({
863
+ repositoriesByModelNameLowered,
864
+ model,
865
+ propertyName,
866
+ isNegated: true,
867
+ value,
868
+ params
869
+ });
870
+ case "or":
871
+ return buildOrOperatorStatement({
872
+ repositoriesByModelNameLowered,
873
+ model,
874
+ isNegated,
875
+ value,
876
+ params
877
+ });
878
+ case "contains":
879
+ if (___default.isArray(value)) {
880
+ const values = value.map((val) => {
881
+ if (!___default.isString(val)) {
882
+ throw new QueryError(`Expected all array values to be strings for "contains" constraint. Property (${propertyName ?? ""}) in model (${model.name}).`, model);
883
+ }
884
+ return `%${val}%`;
885
+ });
886
+ return buildWhere({
887
+ repositoriesByModelNameLowered,
888
+ model,
889
+ propertyName,
890
+ comparer: "like",
891
+ isNegated,
892
+ value: values,
893
+ params
894
+ });
895
+ }
896
+ if (___default.isString(value)) {
897
+ return buildWhere({
898
+ repositoriesByModelNameLowered,
899
+ model,
900
+ propertyName,
901
+ comparer: "like",
902
+ isNegated,
903
+ value: `%${value}%`,
904
+ params
905
+ });
906
+ }
907
+ throw new QueryError(`Expected value to be a string for "contains" constraint. Property (${propertyName ?? ""}) in model (${model.name}).`, model);
908
+ case "startsWith":
909
+ if (___default.isArray(value)) {
910
+ const values = value.map((val) => {
911
+ if (!___default.isString(val)) {
912
+ throw new QueryError(`Expected all array values to be strings for "startsWith" constraint. Property (${propertyName ?? ""}) in model (${model.name}).`, model);
913
+ }
914
+ return `${val}%`;
915
+ });
916
+ return buildWhere({
917
+ repositoriesByModelNameLowered,
918
+ model,
919
+ propertyName,
920
+ comparer: "like",
921
+ isNegated,
922
+ value: values,
923
+ params
924
+ });
925
+ }
926
+ if (___default.isString(value)) {
927
+ return buildWhere({
928
+ repositoriesByModelNameLowered,
929
+ model,
930
+ propertyName,
931
+ comparer: "like",
932
+ isNegated,
933
+ value: `${value}%`,
934
+ params
935
+ });
936
+ }
937
+ throw new QueryError(`Expected value to be a string for "startsWith" constraint. Property (${propertyName ?? ""}) in model (${model.name}).`, model);
938
+ case "endsWith":
939
+ if (___default.isArray(value)) {
940
+ const values = value.map((val) => {
941
+ if (!___default.isString(val)) {
942
+ throw new QueryError(`Expected all array values to be strings for "endsWith" constraint. Property (${propertyName ?? ""}) in model (${model.name}).`, model);
943
+ }
944
+ return `%${val}`;
945
+ });
946
+ return buildWhere({
947
+ repositoriesByModelNameLowered,
948
+ model,
949
+ propertyName,
950
+ comparer: "like",
951
+ isNegated,
952
+ value: values,
953
+ params
954
+ });
955
+ }
956
+ if (___default.isString(value)) {
957
+ return buildWhere({
958
+ repositoriesByModelNameLowered,
959
+ model,
960
+ propertyName,
961
+ comparer: "like",
962
+ isNegated,
963
+ value: `%${value}`,
964
+ params
965
+ });
966
+ }
967
+ throw new QueryError(`Expected value to be a string for "endsWith" constraint. Property (${propertyName ?? ""}) in model (${model.name}).`, model);
968
+ case "like":
969
+ return buildLikeOperatorStatement({
970
+ model,
971
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
972
+ propertyName,
973
+ isNegated,
974
+ value,
975
+ params
976
+ });
977
+ default: {
978
+ if (___default.isUndefined(value)) {
979
+ throw new QueryError(`Attempting to query with an undefined value. ${propertyName ?? ""} on ${model.name}`, model);
980
+ }
981
+ if (propertyName) {
982
+ const column = model.columnsByPropertyName[propertyName];
983
+ if (column && ___default.isObject(value)) {
984
+ if (column.primary) {
985
+ const primaryKeyValue = value[column.propertyName];
986
+ if (!___default.isNil(primaryKeyValue)) {
987
+ return buildWhere({
988
+ repositoriesByModelNameLowered,
989
+ model,
990
+ propertyName,
991
+ comparer,
992
+ isNegated,
993
+ value: primaryKeyValue,
994
+ params
995
+ });
996
+ }
997
+ } else if (column.model) {
998
+ const relatedModelRepository = repositoriesByModelNameLowered[column.model.toLowerCase()];
999
+ if (!relatedModelRepository) {
1000
+ throw new QueryError(`Unable to find model schema (${column.model}) specified in where clause for "${column.propertyName}"`, model);
1001
+ }
1002
+ const relatedModelPrimaryKey = relatedModelRepository.model.primaryKeyColumn;
1003
+ if (!relatedModelPrimaryKey) {
1004
+ throw new QueryError(`Unable to find primary key column for ${column.model} specified in where clause for ${model.name}.${column.propertyName}`, model);
1005
+ }
1006
+ const primaryKeyValue = value[relatedModelPrimaryKey.propertyName];
1007
+ if (!___default.isNil(primaryKeyValue)) {
1008
+ return buildWhere({
1009
+ repositoriesByModelNameLowered,
1010
+ model,
1011
+ propertyName,
1012
+ comparer,
1013
+ isNegated,
1014
+ value: primaryKeyValue,
1015
+ params
1016
+ });
1017
+ }
1018
+ }
1019
+ }
1020
+ }
1021
+ if (___default.isArray(value)) {
1022
+ if (!value.length) {
1023
+ const columnTypeFromPropertyName = propertyName ? model.columnsByPropertyName[propertyName] : null;
1024
+ const columnTypeFromComparer = comparer ? model.columnsByPropertyName[comparer] : null;
1025
+ const arrayColumn = columnTypeFromPropertyName ?? columnTypeFromComparer;
1026
+ if (arrayColumn) {
1027
+ const arrayColumnType = arrayColumn.type ? arrayColumn.type.toLowerCase() : "";
1028
+ if (arrayColumnType === "array" || arrayColumnType === "string[]" || arrayColumnType === "integer[]" || arrayColumnType === "float[]" || arrayColumnType === "boolean[]") {
1029
+ return `"${arrayColumn.name}"${isNegated ? "<>" : "="}'{}'`;
1030
+ }
1031
+ }
1032
+ if (isNegated) {
1033
+ return "1=1";
1034
+ }
1035
+ return "1<>1";
1036
+ }
1037
+ const orConstraints = [];
1038
+ const valueWithoutNull = [];
1039
+ for (const item of value) {
1040
+ if (___default.isNull(item)) {
1041
+ orConstraints.push(
1042
+ buildWhere({
1043
+ repositoriesByModelNameLowered,
1044
+ model,
1045
+ propertyName,
1046
+ isNegated,
1047
+ value: null,
1048
+ params
1049
+ })
1050
+ );
1051
+ } else if (item === "") {
1052
+ orConstraints.push(
1053
+ buildWhere({
1054
+ repositoriesByModelNameLowered,
1055
+ model,
1056
+ propertyName,
1057
+ isNegated,
1058
+ value: "",
1059
+ params
1060
+ })
1061
+ );
1062
+ } else {
1063
+ valueWithoutNull.push(item);
1064
+ }
1065
+ }
1066
+ if (valueWithoutNull.length === 1) {
1067
+ orConstraints.push(
1068
+ buildWhere({
1069
+ repositoriesByModelNameLowered,
1070
+ model,
1071
+ propertyName,
1072
+ isNegated,
1073
+ value: valueWithoutNull[0],
1074
+ params
1075
+ })
1076
+ );
1077
+ } else if (valueWithoutNull.length) {
1078
+ const columnTypeFromPropertyName = propertyName ? model.columnsByPropertyName[propertyName] : null;
1079
+ const columnTypeFromComparer = comparer ? model.columnsByPropertyName[comparer] : null;
1080
+ const columnType = columnTypeFromPropertyName ?? columnTypeFromComparer;
1081
+ if (columnType) {
1082
+ let columnTypeLowered = columnType.type ? columnType.type.toLowerCase() : "";
1083
+ if (columnTypeLowered === "array" || columnTypeLowered === "string[]" || columnTypeLowered === "integer[]" || columnTypeLowered === "float[]" || columnTypeLowered === "boolean[]") {
1084
+ for (const val of valueWithoutNull) {
1085
+ orConstraints.push(
1086
+ buildWhere({
1087
+ repositoriesByModelNameLowered,
1088
+ model,
1089
+ propertyName,
1090
+ isNegated,
1091
+ value: val,
1092
+ params
1093
+ })
1094
+ );
1095
+ }
1096
+ } else {
1097
+ if (!columnTypeLowered) {
1098
+ const columnAsModelType = columnType;
1099
+ if (columnAsModelType.model) {
1100
+ const relatedModelRepository = repositoriesByModelNameLowered[columnAsModelType.model.toLowerCase()];
1101
+ if (!relatedModelRepository) {
1102
+ throw new QueryError(`Unable to find model schema (${columnAsModelType.model}) specified in where clause for "${columnAsModelType.propertyName}"`, model);
1103
+ }
1104
+ const relatedModelPrimaryKey = relatedModelRepository.model.primaryKeyColumn;
1105
+ if (!relatedModelPrimaryKey) {
1106
+ throw new QueryError(`Unable to find primary key column for ${columnAsModelType.model} specified in where clause for ${model.name}.${columnAsModelType.propertyName}`, model);
1107
+ }
1108
+ columnTypeLowered = relatedModelPrimaryKey.type ? relatedModelPrimaryKey.type.toLowerCase() : "";
1109
+ }
1110
+ }
1111
+ let castType;
1112
+ switch (columnTypeLowered) {
1113
+ case "int":
1114
+ case "integer":
1115
+ case "integer[]":
1116
+ castType = "::INTEGER[]";
1117
+ break;
1118
+ case "float":
1119
+ case "float[]":
1120
+ castType = "::NUMERIC[]";
1121
+ break;
1122
+ case "boolean":
1123
+ case "boolean[]":
1124
+ castType = "::BOOLEAN[]";
1125
+ break;
1126
+ default:
1127
+ castType = "::TEXT[]";
1128
+ break;
1129
+ }
1130
+ params.push(valueWithoutNull);
1131
+ orConstraints.push(`"${columnType.name}"${isNegated ? "<>ALL" : "=ANY"}($${params.length}${castType})`);
1132
+ }
1133
+ }
1134
+ }
1135
+ if (orConstraints.length === 1) {
1136
+ return orConstraints[0] ?? "";
1137
+ }
1138
+ if (isNegated) {
1139
+ return orConstraints.join(" AND ");
1140
+ }
1141
+ if (orConstraints.length) {
1142
+ return `(${orConstraints.join(" OR ")})`;
1143
+ }
1144
+ return "";
1145
+ }
1146
+ if (___default.isObject(value) && !___default.isDate(value)) {
1147
+ const andValues = [];
1148
+ for (const [key, where] of Object.entries(value)) {
1149
+ let subQueryComparer;
1150
+ if (isComparer(key)) {
1151
+ subQueryComparer = key;
1152
+ } else {
1153
+ propertyName = key;
1154
+ }
1155
+ andValues.push(
1156
+ buildWhere({
1157
+ repositoriesByModelNameLowered,
1158
+ model,
1159
+ propertyName,
1160
+ comparer: subQueryComparer,
1161
+ isNegated,
1162
+ value: where,
1163
+ params
1164
+ })
1165
+ );
1166
+ }
1167
+ return andValues.join(" AND ");
1168
+ }
1169
+ return buildComparisonOperatorStatement({
1170
+ model,
1171
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1172
+ propertyName,
1173
+ comparer,
1174
+ isNegated,
1175
+ value,
1176
+ params
1177
+ });
1178
+ }
1179
+ }
1180
+ }
1181
+ function buildOrOperatorStatement({
1182
+ repositoriesByModelNameLowered,
1183
+ model,
1184
+ isNegated,
1185
+ value,
1186
+ params = []
1187
+ }) {
1188
+ const orClauses = [];
1189
+ for (const constraint of value) {
1190
+ const orClause = buildWhere({
1191
+ repositoriesByModelNameLowered,
1192
+ model,
1193
+ isNegated,
1194
+ value: constraint,
1195
+ params
1196
+ });
1197
+ if (orClause) {
1198
+ orClauses.push(`(${orClause})`);
1199
+ }
1200
+ }
1201
+ if (orClauses.length === 1) {
1202
+ return orClauses[0] ?? "";
1203
+ }
1204
+ if (isNegated) {
1205
+ return orClauses.join(" AND ");
1206
+ }
1207
+ if (orClauses.length) {
1208
+ return `(${orClauses.join(" OR ")})`;
1209
+ }
1210
+ return "";
1211
+ }
1212
+ function buildLikeOperatorStatement({ model, propertyName, isNegated, value, params }) {
1213
+ if (___default.isArray(value)) {
1214
+ if (!value.length) {
1215
+ if (isNegated) {
1216
+ return "1=1";
1217
+ }
1218
+ return "1<>1";
1219
+ }
1220
+ if (value.length > 1) {
1221
+ const orConstraints = [];
1222
+ for (const item of value) {
1223
+ if (___default.isNull(item)) {
1224
+ orConstraints.push(
1225
+ buildLikeOperatorStatement({
1226
+ model,
1227
+ propertyName,
1228
+ isNegated,
1229
+ value: null,
1230
+ params
1231
+ })
1232
+ );
1233
+ } else if (item === "") {
1234
+ orConstraints.push(
1235
+ buildLikeOperatorStatement({
1236
+ model,
1237
+ propertyName,
1238
+ isNegated,
1239
+ value: "",
1240
+ params
1241
+ })
1242
+ );
1243
+ } else {
1244
+ orConstraints.push(
1245
+ buildLikeOperatorStatement({
1246
+ model,
1247
+ propertyName,
1248
+ isNegated,
1249
+ value: item,
1250
+ params
1251
+ })
1252
+ );
1253
+ }
1254
+ }
1255
+ if (orConstraints.length === 1) {
1256
+ return orConstraints[0] ?? "";
1257
+ }
1258
+ if (isNegated) {
1259
+ return orConstraints.join(" AND ");
1260
+ }
1261
+ if (orConstraints.length) {
1262
+ return `(${orConstraints.join(" OR ")})`;
1263
+ }
1264
+ return "";
1265
+ }
1266
+ value = value[0];
1267
+ }
1268
+ const column = model.columnsByPropertyName[propertyName];
1269
+ if (!column) {
1270
+ throw new QueryError(`Unable to find property ${propertyName} on model ${model.name}`, model);
1271
+ }
1272
+ if (___default.isNull(value)) {
1273
+ return `"${column.name}" ${isNegated ? "IS NOT" : "IS"} NULL`;
1274
+ }
1275
+ if (___default.isString(value)) {
1276
+ if (value) {
1277
+ params.push(value);
1278
+ const columnType = column.type?.toLowerCase();
1279
+ if (columnType === "array" || columnType === "string[]") {
1280
+ return `${isNegated ? "NOT " : ""}EXISTS(SELECT 1 FROM (SELECT unnest("${column.name}") AS "unnested_${column.name}") __unnested WHERE "unnested_${column.name}" ILIKE $${params.length})`;
1281
+ }
1282
+ return `"${column.name}"${isNegated ? " NOT" : ""} ILIKE $${params.length}`;
1283
+ }
1284
+ return `"${column.name}" ${isNegated ? "!=" : "="} ''`;
1285
+ }
1286
+ throw new QueryError(`Expected value to be a string for "like" constraint. Property (${propertyName}) in model (${model.name}).`, model);
1287
+ }
1288
+ function buildComparisonOperatorStatement({ model, propertyName, comparer, isNegated, value, params = [] }) {
1289
+ const column = model.columnsByPropertyName[propertyName];
1290
+ if (!column) {
1291
+ throw new QueryError(`Unable to find property ${propertyName} on model ${model.name}`, model);
1292
+ }
1293
+ if (___default.isNull(value)) {
1294
+ return `"${column.name}" ${isNegated ? "IS NOT" : "IS"} NULL`;
1295
+ }
1296
+ params.push(value);
1297
+ const columnType = column.type ?? "unknown";
1298
+ const supportsLessThanGreaterThan = columnType !== "array" && columnType !== "json";
1299
+ switch (comparer) {
1300
+ case "<":
1301
+ if (!supportsLessThanGreaterThan) {
1302
+ throw new QueryError(`< operator is not supported for ${columnType} type. ${propertyName || ""} on ${model.name}`, model);
1303
+ }
1304
+ return `"${column.name}"${isNegated ? ">=" : "<"}$${params.length}`;
1305
+ case "<=":
1306
+ if (!supportsLessThanGreaterThan) {
1307
+ throw new QueryError(`<= operator is not supported for ${columnType} type. ${propertyName || ""} on ${model.name}`, model);
1308
+ }
1309
+ return `"${column.name}"${isNegated ? ">" : "<="}$${params.length}`;
1310
+ case ">":
1311
+ if (!supportsLessThanGreaterThan) {
1312
+ throw new QueryError(`> operator is not supported for ${columnType} type. ${propertyName || ""} on ${model.name}`, model);
1313
+ }
1314
+ return `"${column.name}"${isNegated ? "<=" : ">"}$${params.length}`;
1315
+ case ">=":
1316
+ if (!supportsLessThanGreaterThan) {
1317
+ throw new QueryError(`>= operator is not supported for ${columnType} type. ${propertyName || ""} on ${model.name}`, model);
1318
+ }
1319
+ return `"${column.name}"${isNegated ? "<" : ">="}$${params.length}`;
1320
+ default:
1321
+ if (columnType === "array" || columnType.endsWith("[]")) {
1322
+ return `$${params.length}${isNegated ? "<>ALL(" : "=ANY("}"${column.name}")`;
1323
+ }
1324
+ return `"${column.name}"${isNegated ? "<>" : "="}$${params.length}`;
1325
+ }
1326
+ }
1327
+ function isComparer(value) {
1328
+ switch (value) {
1329
+ case "!":
1330
+ case "not":
1331
+ case "or":
1332
+ case "and":
1333
+ case "contains":
1334
+ case "startsWith":
1335
+ case "endsWith":
1336
+ case "like":
1337
+ case "<":
1338
+ case "<=":
1339
+ case ">":
1340
+ case ">=":
1341
+ return true;
1342
+ default:
1343
+ return false;
1344
+ }
1345
+ }
1346
+
1347
+ var __defProp = Object.defineProperty;
1348
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
1349
+ var __publicField = (obj, key, value) => {
1350
+ __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
1351
+ return value;
1352
+ };
1353
+ class ReadonlyRepository {
1354
+ constructor({ modelMetadata, type, pool, readonlyPool, repositoriesByModelNameLowered }) {
1355
+ __publicField(this, "_modelMetadata");
1356
+ __publicField(this, "_type");
1357
+ __publicField(this, "_pool");
1358
+ __publicField(this, "_readonlyPool");
1359
+ __publicField(this, "_repositoriesByModelNameLowered");
1360
+ __publicField(this, "_floatProperties", []);
1361
+ __publicField(this, "_intProperties", []);
1362
+ this._modelMetadata = modelMetadata;
1363
+ this._type = type;
1364
+ this._pool = pool;
1365
+ this._readonlyPool = readonlyPool ?? pool;
1366
+ this._repositoriesByModelNameLowered = repositoriesByModelNameLowered;
1367
+ for (const column of modelMetadata.columns) {
1368
+ if (column.type === "float") {
1369
+ this._floatProperties.push(column.propertyName);
1370
+ } else if (column.type === "integer") {
1371
+ this._intProperties.push(column.propertyName);
1372
+ }
1373
+ }
1374
+ }
1375
+ get model() {
1376
+ return this._modelMetadata;
1377
+ }
1378
+ /**
1379
+ * Gets a single object
1380
+ * @param {object} [args] - Arguments
1381
+ * @param {string[]} [args.select] - Array of model property names to return from the query.
1382
+ * @param {object} [args.where] - Object representing the where query
1383
+ * @param {string|object} [args.sort] - Property name(s) to sort by
1384
+ */
1385
+ findOne(args = {}) {
1386
+ const { stack } = new Error(`${this.model.name}.findOne()`);
1387
+ let select;
1388
+ let where = {};
1389
+ let sort = null;
1390
+ let poolOverride;
1391
+ for (const [name, value] of Object.entries(args)) {
1392
+ let isWhereCriteria = false;
1393
+ switch (name) {
1394
+ case "select":
1395
+ if (value) {
1396
+ select = new Set(value);
1397
+ }
1398
+ break;
1399
+ case "where":
1400
+ where = value;
1401
+ break;
1402
+ case "sort":
1403
+ sort = value;
1404
+ break;
1405
+ case "pool":
1406
+ poolOverride = value;
1407
+ break;
1408
+ default:
1409
+ select = void 0;
1410
+ where = args;
1411
+ sort = null;
1412
+ isWhereCriteria = true;
1413
+ break;
1414
+ }
1415
+ if (isWhereCriteria) {
1416
+ break;
1417
+ }
1418
+ }
1419
+ const populates = [];
1420
+ const manuallySetFields = [];
1421
+ const sorts = sort ? this._convertSortsToOrderBy(sort) : [];
1422
+ const modelInstance = this;
1423
+ return {
1424
+ /**
1425
+ * Filters the query
1426
+ * @param {object} value - Object representing the where query
1427
+ */
1428
+ where(value) {
1429
+ where = value;
1430
+ return this;
1431
+ },
1432
+ /**
1433
+ * Populates/hydrates relations
1434
+ * @param {string} propertyName - Name of property to join
1435
+ * @param {object} [options] - Populate options
1436
+ * @param {object} [options.where] - Object representing the where query
1437
+ * @param {string[]} [options.select] - Array of model property names to return from the query.
1438
+ * @param {string|object} [options.sort] - Property name(s) to sort by
1439
+ * @param {string|number} [options.skip] - Number of records to skip
1440
+ * @param {string|number} [options.limit] - Number of results to return
1441
+ */
1442
+ populate(propertyName, options) {
1443
+ if (select && !select.has(propertyName)) {
1444
+ for (const column of modelInstance.model.columns) {
1445
+ if (column.model && column.propertyName === propertyName) {
1446
+ select.add(column.propertyName);
1447
+ }
1448
+ }
1449
+ }
1450
+ populates.push({
1451
+ propertyName,
1452
+ where: options?.where,
1453
+ select: options?.select,
1454
+ sort: options?.sort,
1455
+ skip: options?.skip,
1456
+ limit: options?.limit,
1457
+ pool: options?.pool ?? poolOverride
1458
+ });
1459
+ return this;
1460
+ },
1461
+ /**
1462
+ * Sorts the query
1463
+ * @param {string|object} [value]
1464
+ */
1465
+ sort(value) {
1466
+ if (value) {
1467
+ sorts.push(...modelInstance._convertSortsToOrderBy(value));
1468
+ }
1469
+ return this;
1470
+ },
1471
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
1472
+ UNSAFE_withOriginalFieldType(_propertyName) {
1473
+ return this;
1474
+ },
1475
+ UNSAFE_withFieldValue(propertyName, value) {
1476
+ manuallySetFields.push({
1477
+ propertyName,
1478
+ value
1479
+ });
1480
+ return this;
1481
+ },
1482
+ async then(resolve, reject) {
1483
+ try {
1484
+ if (___default.isString(where)) {
1485
+ return await reject(new Error("The query cannot be a string, it must be an object"));
1486
+ }
1487
+ const { query, params } = getSelectQueryAndParams({
1488
+ repositoriesByModelNameLowered: modelInstance._repositoriesByModelNameLowered,
1489
+ model: modelInstance.model,
1490
+ select: select ? Array.from(select) : void 0,
1491
+ where,
1492
+ sorts,
1493
+ limit: 1,
1494
+ skip: 0
1495
+ });
1496
+ const pool = poolOverride ?? modelInstance._readonlyPool;
1497
+ const results = await pool.query(query, params);
1498
+ const firstResult = ___default.first(results.rows);
1499
+ if (firstResult) {
1500
+ const result = modelInstance._buildInstance(firstResult);
1501
+ if (populates.length) {
1502
+ await modelInstance.populateFields([result], populates);
1503
+ }
1504
+ for (const manuallySetField of manuallySetFields) {
1505
+ result[manuallySetField.propertyName] = manuallySetField.value;
1506
+ }
1507
+ return await resolve(result);
1508
+ }
1509
+ return await resolve(null);
1510
+ } catch (ex) {
1511
+ const typedException = ex;
1512
+ if (typedException.stack) {
1513
+ typedException.stack += `
1514
+ ${stack ?? ""}`;
1515
+ } else {
1516
+ typedException.stack = stack;
1517
+ }
1518
+ return reject(typedException);
1519
+ }
1520
+ }
1521
+ };
1522
+ }
1523
+ /**
1524
+ * Gets a collection of objects
1525
+ * @param {object} [args] - Arguments
1526
+ * @param {string[]} [args.select] - Array of model property names to return from the query.
1527
+ * @param {object} [args.where] - Object representing the where query
1528
+ * @param {string|object} [args.sort] - Property name(s) to sort by
1529
+ * @param {string|number} [args.skip] - Number of records to skip
1530
+ * @param {string|number} [args.limit] - Number of results to return
1531
+ */
1532
+ find(args = {}) {
1533
+ const { stack } = new Error(`${this.model.name}.find()`);
1534
+ let select;
1535
+ let where = {};
1536
+ let sort = null;
1537
+ let skip = null;
1538
+ let limit = null;
1539
+ let poolOverride;
1540
+ for (const [name, value] of Object.entries(args)) {
1541
+ let isWhereCriteria = false;
1542
+ switch (name) {
1543
+ case "select":
1544
+ if (value) {
1545
+ select = new Set(value);
1546
+ }
1547
+ break;
1548
+ case "where":
1549
+ where = value;
1550
+ break;
1551
+ case "sort":
1552
+ sort = value;
1553
+ break;
1554
+ case "skip":
1555
+ skip = value;
1556
+ break;
1557
+ case "limit":
1558
+ limit = value;
1559
+ break;
1560
+ case "pool":
1561
+ poolOverride = value;
1562
+ break;
1563
+ default:
1564
+ select = void 0;
1565
+ where = args;
1566
+ sort = null;
1567
+ skip = null;
1568
+ limit = null;
1569
+ isWhereCriteria = true;
1570
+ break;
1571
+ }
1572
+ if (isWhereCriteria) {
1573
+ break;
1574
+ }
1575
+ }
1576
+ const populates = [];
1577
+ const sorts = sort ? this._convertSortsToOrderBy(sort) : [];
1578
+ const modelInstance = this;
1579
+ return {
1580
+ /**
1581
+ * Filters the query
1582
+ * @param {object} value - Object representing the where query
1583
+ */
1584
+ where(value) {
1585
+ where = value;
1586
+ return this;
1587
+ },
1588
+ /**
1589
+ * Populates/hydrates relations
1590
+ * @param {string} propertyName - Name of property to join
1591
+ * @param {object} [options] - Populate options
1592
+ * @param {object} [options.where] - Object representing the where query
1593
+ * @param {string[]} [options.select] - Array of model property names to return from the query.
1594
+ * @param {string|object} [options.sort] - Property name(s) to sort by
1595
+ * @param {string|number} [options.skip] - Number of records to skip
1596
+ * @param {string|number} [options.limit] - Number of results to return
1597
+ */
1598
+ populate(propertyName, options) {
1599
+ if (select && !select.has(propertyName)) {
1600
+ for (const column of modelInstance.model.columns) {
1601
+ if (column.model && column.propertyName === propertyName) {
1602
+ select.add(column.propertyName);
1603
+ }
1604
+ }
1605
+ }
1606
+ populates.push({
1607
+ propertyName,
1608
+ where: options?.where,
1609
+ select: options?.select,
1610
+ sort: options?.sort,
1611
+ skip: options?.skip,
1612
+ limit: options?.limit,
1613
+ pool: options?.pool ?? poolOverride
1614
+ });
1615
+ return this;
1616
+ },
1617
+ /**
1618
+ * Sorts the query
1619
+ * @param {string|string[]|object} [value]
1620
+ */
1621
+ sort(value) {
1622
+ if (value) {
1623
+ sorts.push(...modelInstance._convertSortsToOrderBy(value));
1624
+ }
1625
+ return this;
1626
+ },
1627
+ /**
1628
+ * Limits results returned by the query
1629
+ * @param {number} value
1630
+ */
1631
+ limit(value) {
1632
+ limit = value;
1633
+ return this;
1634
+ },
1635
+ /**
1636
+ * Skips records returned by the query
1637
+ * @param {number} value
1638
+ */
1639
+ skip(value) {
1640
+ skip = value;
1641
+ return this;
1642
+ },
1643
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
1644
+ UNSAFE_withOriginalFieldType(_propertyName) {
1645
+ return this;
1646
+ },
1647
+ /**
1648
+ * Pages records returned by the query
1649
+ * @param {number} [page=1] - Page to return - Starts at 1
1650
+ * @param {number} [limit=10] - Number of records to return
1651
+ */
1652
+ paginate({ page = 1, limit: paginateLimit = 10 }) {
1653
+ const safePage = Math.max(page, 1);
1654
+ return this.skip(safePage * paginateLimit - paginateLimit).limit(paginateLimit);
1655
+ },
1656
+ async then(resolve, reject) {
1657
+ try {
1658
+ if (___default.isString(where)) {
1659
+ return await reject(new Error("The query cannot be a string, it must be an object"));
1660
+ }
1661
+ const { query, params } = getSelectQueryAndParams({
1662
+ repositoriesByModelNameLowered: modelInstance._repositoriesByModelNameLowered,
1663
+ model: modelInstance.model,
1664
+ select: select ? Array.from(select) : void 0,
1665
+ where,
1666
+ sorts,
1667
+ skip: skip ?? 0,
1668
+ limit: limit ?? 0
1669
+ });
1670
+ const pool = poolOverride ?? modelInstance._readonlyPool;
1671
+ const results = await pool.query(query, params);
1672
+ const entities = modelInstance._buildInstances(results.rows);
1673
+ if (populates.length) {
1674
+ await modelInstance.populateFields(entities, populates);
1675
+ }
1676
+ return await resolve(entities);
1677
+ } catch (ex) {
1678
+ const typedException = ex;
1679
+ if (typedException.stack) {
1680
+ typedException.stack += `
1681
+ ${stack ?? ""}`;
1682
+ } else {
1683
+ typedException.stack = stack;
1684
+ }
1685
+ return reject(typedException);
1686
+ }
1687
+ }
1688
+ };
1689
+ }
1690
+ /**
1691
+ * Gets a count of rows matching the where query
1692
+ * @param {object} [args] - Arguments
1693
+ * @param {object} [args.where] - Object representing the where query
1694
+ * @param {object} [args.pool] - Override the db pool to use for the query
1695
+ * @returns {number} Number of records matching the where criteria
1696
+ */
1697
+ count(args = {}) {
1698
+ const { stack } = new Error(`${this.model.name}.count()`);
1699
+ let where = {};
1700
+ let poolOverride;
1701
+ for (const [name, value] of Object.entries(args)) {
1702
+ let isWhereCriteria = false;
1703
+ switch (name) {
1704
+ case "where":
1705
+ where = value;
1706
+ break;
1707
+ case "pool":
1708
+ poolOverride = value;
1709
+ break;
1710
+ default:
1711
+ where = args;
1712
+ isWhereCriteria = true;
1713
+ break;
1714
+ }
1715
+ if (isWhereCriteria) {
1716
+ break;
1717
+ }
1718
+ }
1719
+ const modelInstance = this;
1720
+ return {
1721
+ /**
1722
+ * Filters the query
1723
+ * @param {object} value - Object representing the where query
1724
+ */
1725
+ where(value) {
1726
+ where = value;
1727
+ return this;
1728
+ },
1729
+ async then(resolve, reject) {
1730
+ try {
1731
+ const { query, params } = getCountQueryAndParams({
1732
+ repositoriesByModelNameLowered: modelInstance._repositoriesByModelNameLowered,
1733
+ model: modelInstance.model,
1734
+ where
1735
+ });
1736
+ const pool = poolOverride ?? modelInstance._readonlyPool;
1737
+ const result = await pool.query(query, params);
1738
+ const firstResult = ___default.first(result.rows);
1739
+ const originalValue = firstResult ? firstResult.count : 0;
1740
+ return await resolve(Number(originalValue));
1741
+ } catch (ex) {
1742
+ const typedException = ex;
1743
+ if (typedException.stack) {
1744
+ typedException.stack += `
1745
+ ${stack ?? ""}`;
1746
+ } else {
1747
+ typedException.stack = stack;
1748
+ }
1749
+ return reject(typedException);
1750
+ }
1751
+ }
1752
+ };
1753
+ }
1754
+ _buildInstance(row) {
1755
+ if (___default.isNil(row)) {
1756
+ return row;
1757
+ }
1758
+ const instance = new this._type();
1759
+ Object.assign(instance, row);
1760
+ for (const name of this._floatProperties) {
1761
+ const originalValue = row[name];
1762
+ if (!___default.isNil(originalValue) && typeof originalValue === "string") {
1763
+ try {
1764
+ const value = Number(originalValue);
1765
+ if (___default.isFinite(value) && value.toString() === originalValue) {
1766
+ instance[name] = value;
1767
+ }
1768
+ } catch {
1769
+ }
1770
+ }
1771
+ }
1772
+ for (const name of this._intProperties) {
1773
+ const originalValue = row[name];
1774
+ if (!___default.isNil(originalValue) && typeof originalValue === "string") {
1775
+ try {
1776
+ const value = Number(originalValue);
1777
+ if (___default.isFinite(value) && value.toString() === originalValue) {
1778
+ const valueAsInt = ___default.toInteger(value);
1779
+ if (Number.isSafeInteger(valueAsInt)) {
1780
+ instance[name] = valueAsInt;
1781
+ }
1782
+ }
1783
+ } catch {
1784
+ }
1785
+ }
1786
+ }
1787
+ return instance;
1788
+ }
1789
+ _buildInstances(rows) {
1790
+ if (___default.isNil(rows)) {
1791
+ return [];
1792
+ }
1793
+ return rows.map((row) => this._buildInstance(row));
1794
+ }
1795
+ // eslint-disable-next-line class-methods-use-this
1796
+ _convertSortsToOrderBy(sorts) {
1797
+ const result = [];
1798
+ if (sorts) {
1799
+ if (Array.isArray(sorts)) {
1800
+ for (const sort of sorts) {
1801
+ const parts = sort.trim().split(" ");
1802
+ const propertyName = parts.shift();
1803
+ result.push({
1804
+ propertyName,
1805
+ descending: /desc/i.test(parts.join(""))
1806
+ });
1807
+ }
1808
+ } else if (___default.isString(sorts)) {
1809
+ for (const sort of sorts.split(",")) {
1810
+ const parts = sort.trim().split(" ");
1811
+ const propertyName = parts.shift();
1812
+ result.push({
1813
+ propertyName,
1814
+ descending: /desc/i.test(parts.join(""))
1815
+ });
1816
+ }
1817
+ } else if (___default.isObject(sorts)) {
1818
+ for (const [propertyName, orderValue] of Object.entries(sorts)) {
1819
+ let descending = false;
1820
+ const order = orderValue;
1821
+ if (order && (order === -1 || /desc/i.test(`${order}`))) {
1822
+ descending = true;
1823
+ }
1824
+ result.push({
1825
+ propertyName,
1826
+ descending
1827
+ });
1828
+ }
1829
+ }
1830
+ }
1831
+ return result;
1832
+ }
1833
+ // NOTE: This will mutate `entities`
1834
+ async populateFields(entities, populates) {
1835
+ if (!entities.length) {
1836
+ return;
1837
+ }
1838
+ const populateQueries = [];
1839
+ for (const populate of populates) {
1840
+ const column = this.model.columnsByPropertyName[populate.propertyName];
1841
+ if (!column) {
1842
+ throw new Error(`Unable to find ${populate.propertyName} on ${this.model.name} model for populating.`);
1843
+ }
1844
+ const modelColumn = column;
1845
+ const collectionColumn = column;
1846
+ if (modelColumn.model) {
1847
+ populateQueries.push(this.populateSingleAssociation(entities, populate, modelColumn));
1848
+ } else if (collectionColumn.collection) {
1849
+ const populateRepository = this._repositoriesByModelNameLowered[collectionColumn.collection.toLowerCase()];
1850
+ if (!populateRepository) {
1851
+ throw new Error(`Unable to find populate repository for collection by name ${collectionColumn.collection}. From ${column.target}#${populate.propertyName}`);
1852
+ }
1853
+ const { primaryKeyColumn } = this.model;
1854
+ if (!primaryKeyColumn) {
1855
+ throw new Error(`Unable to populate ${column.target}#${column.propertyName}. There is no primary key defined in ${this.model.name}`);
1856
+ }
1857
+ const entityIds = /* @__PURE__ */ new Set();
1858
+ for (const entity of entities) {
1859
+ const id = entity[primaryKeyColumn.propertyName];
1860
+ if (___default.isNil(id)) {
1861
+ throw new Error(`Primary key (${primaryKeyColumn.propertyName}) has no value for entity ${column.target}.`);
1862
+ }
1863
+ entityIds.add(id);
1864
+ }
1865
+ if (collectionColumn.through) {
1866
+ const populateModelPrimaryKeyColumn = populateRepository.model.primaryKeyColumn;
1867
+ if (!populateModelPrimaryKeyColumn) {
1868
+ throw new Error(
1869
+ `Unable to populate ${collectionColumn.collection} objects from ${column.target}#${column.propertyName}. There is no primary key defined in ${collectionColumn.collection}`
1870
+ );
1871
+ }
1872
+ populateQueries.push(
1873
+ this.populateManyManyCollection(
1874
+ entities,
1875
+ primaryKeyColumn.propertyName,
1876
+ Array.from(entityIds),
1877
+ populateModelPrimaryKeyColumn.propertyName,
1878
+ populate,
1879
+ collectionColumn,
1880
+ populateRepository
1881
+ )
1882
+ );
1883
+ } else {
1884
+ populateQueries.push(this.populateOneManyCollection(entities, primaryKeyColumn.propertyName, Array.from(entityIds), populate, collectionColumn, populateRepository));
1885
+ }
1886
+ }
1887
+ }
1888
+ if (populateQueries.length) {
1889
+ await Promise.all(populateQueries);
1890
+ }
1891
+ }
1892
+ async populateSingleAssociation(entities, populate, column) {
1893
+ const populateRepository = this._repositoriesByModelNameLowered[column.model.toLowerCase()];
1894
+ if (!populateRepository) {
1895
+ throw new Error(`Unable to find populate repository by entity name: ${column.model}. From ${column.target}#${column.propertyName}`);
1896
+ }
1897
+ if (!populateRepository.model.primaryKeyColumn) {
1898
+ throw new Error(`Unable to populate ${column.model} from ${column.target}#${column.propertyName}. There is no primary key defined in ${column.model}`);
1899
+ }
1900
+ const propertyName = populate.propertyName;
1901
+ const populateIds = /* @__PURE__ */ new Set();
1902
+ for (const entity of entities) {
1903
+ const populateId = entity[propertyName];
1904
+ if (populateId) {
1905
+ populateIds.add(populateId);
1906
+ }
1907
+ }
1908
+ const populateWhere = {
1909
+ [populateRepository.model.primaryKeyColumn.propertyName]: Array.from(populateIds),
1910
+ ...populate.where
1911
+ };
1912
+ const populateResults = await populateRepository.find({
1913
+ select: populate.select,
1914
+ where: populateWhere,
1915
+ sort: populate.sort,
1916
+ pool: populate.pool
1917
+ });
1918
+ const populateResultsById = ___default.keyBy(populateResults, populateRepository.model.primaryKeyColumn.propertyName);
1919
+ for (const entity of entities) {
1920
+ entity[propertyName] = populateResultsById[entity[propertyName]];
1921
+ }
1922
+ }
1923
+ async populateOneManyCollection(entities, primaryKeyPropertyName, entityIds, populate, column, populateRepository) {
1924
+ if (entities.length > 1 && populate.select && !populate.select.includes(column.via)) {
1925
+ throw new Error(`Unable to populate "${populate.propertyName}" on ${this.model.name}. "${column.via}" is not included in select array.`);
1926
+ }
1927
+ const populateWhere = {
1928
+ [column.via]: entityIds,
1929
+ ...populate.where
1930
+ };
1931
+ const populateResults = await populateRepository.find({
1932
+ select: populate.select,
1933
+ where: populateWhere,
1934
+ sort: populate.sort,
1935
+ skip: populate.skip,
1936
+ limit: populate.limit,
1937
+ pool: populate.pool
1938
+ });
1939
+ if (entities.length === 1) {
1940
+ for (const entity of entities) {
1941
+ entity[populate.propertyName] = populateResults;
1942
+ }
1943
+ } else {
1944
+ const populateResultsByEntityId = ___default.groupBy(populateResults, column.via);
1945
+ for (const entity of entities) {
1946
+ const id = entity[primaryKeyPropertyName];
1947
+ entity[populate.propertyName] = populateResultsByEntityId[id] || [];
1948
+ }
1949
+ }
1950
+ }
1951
+ async populateManyManyCollection(entities, primaryKeyPropertyName, entityIds, populateModelPrimaryKeyPropertyName, populate, column, populateRepository) {
1952
+ if (!column.through) {
1953
+ throw new Error(`Unable to populate multi-map collection: Missing "through" value. From ${column.target}#${populate.propertyName}`);
1954
+ }
1955
+ const throughRepository = this._repositoriesByModelNameLowered[column.through.toLowerCase()];
1956
+ if (!throughRepository) {
1957
+ throw new Error(`Unable to find repository for multi-map collection: ${column.through}. From ${column.target}#${populate.propertyName}`);
1958
+ }
1959
+ let relatedModelColumn;
1960
+ for (const populateModelColumn of populateRepository.model.columns) {
1961
+ const { through } = populateModelColumn;
1962
+ if (through && through.toLowerCase() === column.through.toLowerCase()) {
1963
+ relatedModelColumn = populateModelColumn;
1964
+ break;
1965
+ }
1966
+ }
1967
+ if (!relatedModelColumn) {
1968
+ throw new Error(`Unable to find property on related model for multi-map collection: ${column.through}. From ${column.target}#${populate.propertyName}`);
1969
+ }
1970
+ const mapRecords = await throughRepository.find({
1971
+ select: [column.via, relatedModelColumn.via],
1972
+ where: {
1973
+ [column.via]: entityIds
1974
+ },
1975
+ pool: populate.pool
1976
+ });
1977
+ const populateIds = /* @__PURE__ */ new Set();
1978
+ const populateIdsByEntityId = {};
1979
+ for (const mapRecord of mapRecords) {
1980
+ const entityId = mapRecord[column.via];
1981
+ const populatedId = mapRecord[relatedModelColumn.via];
1982
+ populateIds.add(populatedId);
1983
+ const entityPopulateIds = populateIdsByEntityId[entityId] ?? [];
1984
+ entityPopulateIds.push(populatedId);
1985
+ populateIdsByEntityId[entityId] = entityPopulateIds;
1986
+ }
1987
+ const populateWhere = ___default.merge(
1988
+ {
1989
+ [populateModelPrimaryKeyPropertyName]: Array.from(populateIds)
1990
+ },
1991
+ populate.where
1992
+ );
1993
+ const populateResults = await populateRepository.find({
1994
+ select: populate.select,
1995
+ where: populateWhere,
1996
+ sort: populate.sort,
1997
+ skip: populate.skip,
1998
+ limit: populate.limit,
1999
+ pool: populate.pool
2000
+ });
2001
+ const populateResultsById = ___default.keyBy(populateResults, populateModelPrimaryKeyPropertyName);
2002
+ for (const entity of entities) {
2003
+ const populatedItems = [];
2004
+ const entityId = entity[primaryKeyPropertyName];
2005
+ const populateIdsForEntity = populateIdsByEntityId[entityId] ?? [];
2006
+ for (const id of populateIdsForEntity) {
2007
+ const populatedItem = populateResultsById[id];
2008
+ if (populatedItem) {
2009
+ populatedItems.push(populatedItem);
2010
+ }
2011
+ }
2012
+ entity[populate.propertyName] = populatedItems;
2013
+ }
2014
+ }
2015
+ }
2016
+
2017
+ class Repository extends ReadonlyRepository {
2018
+ /**
2019
+ * Creates an object using the specified values
2020
+ * @param {object|object[]} values - Values to insert as a new object. If an array is specified, multiple rows will be inserted
2021
+ * @param {object} [options]
2022
+ * @param {boolean} [options.returnRecords=true] - Determines if inserted records should be returned
2023
+ * @param {string[]} [options.returnSelect] - Array of model property names to return from the query.
2024
+ * @param {object} [options.onConflict] - Options to handle conflicts due to a unique constraint or exclusion constraint error during insert
2025
+ * @returns {object|object[]|void} Return value from the db
2026
+ */
2027
+ async create(values, options) {
2028
+ if (this.model.readonly) {
2029
+ throw new Error(`${this.model.name} is readonly.`);
2030
+ }
2031
+ if (___default.isArray(values) && !values.length) {
2032
+ return [];
2033
+ }
2034
+ if (this._type.beforeCreate) {
2035
+ if (Array.isArray(values)) {
2036
+ values = await Promise.all(values.map((value) => this._type.beforeCreate ? this._type.beforeCreate(value) : Promise.resolve()));
2037
+ } else {
2038
+ values = await this._type.beforeCreate(values);
2039
+ }
2040
+ }
2041
+ let returnRecords = true;
2042
+ let returnSelect;
2043
+ if (options) {
2044
+ if (options.returnRecords === false) {
2045
+ returnRecords = false;
2046
+ } else if (options.returnSelect) {
2047
+ returnSelect = options.returnSelect;
2048
+ }
2049
+ }
2050
+ const { query, params } = getInsertQueryAndParams({
2051
+ repositoriesByModelNameLowered: this._repositoriesByModelNameLowered,
2052
+ model: this.model,
2053
+ values,
2054
+ returnRecords,
2055
+ returnSelect,
2056
+ onConflict: options?.onConflict
2057
+ });
2058
+ const results = await this._pool.query(query, params);
2059
+ if (returnRecords) {
2060
+ if (___default.isArray(values)) {
2061
+ return this._buildInstances(results.rows);
2062
+ }
2063
+ const firstResult = ___default.first(results.rows);
2064
+ if (firstResult) {
2065
+ return this._buildInstance(firstResult);
2066
+ }
2067
+ throw new Error("Unknown error getting created rows back from the database");
2068
+ }
2069
+ return void 0;
2070
+ }
2071
+ /**
2072
+ * Updates object(s) matching the where query, with the specified values
2073
+ * @param {object} where - Object representing the where query
2074
+ * @param {object} values - Values to update
2075
+ * @param {object} [options]
2076
+ * @param {boolean} [options.returnRecords=true] - Determines if inserted records should be returned
2077
+ * @param {string[]} [options.returnSelect] - Array of model property names to return from the query.
2078
+ * @returns {object[]|void} Return values from the db or `true` if returnRecords=false
2079
+ */
2080
+ async update(where, values, options) {
2081
+ if (this.model.readonly) {
2082
+ throw new Error(`${this.model.name} is readonly.`);
2083
+ }
2084
+ if (___default.isString(where)) {
2085
+ throw new Error("The query cannot be a string, it must be an object");
2086
+ }
2087
+ if (this._type.beforeUpdate) {
2088
+ values = await this._type.beforeUpdate(values);
2089
+ }
2090
+ let returnRecords = true;
2091
+ let returnSelect;
2092
+ if (options) {
2093
+ if (options.returnRecords === false) {
2094
+ returnRecords = false;
2095
+ } else if (options.returnSelect) {
2096
+ returnSelect = options.returnSelect;
2097
+ }
2098
+ }
2099
+ const { query, params } = getUpdateQueryAndParams({
2100
+ repositoriesByModelNameLowered: this._repositoriesByModelNameLowered,
2101
+ model: this.model,
2102
+ where,
2103
+ values,
2104
+ returnRecords,
2105
+ returnSelect
2106
+ });
2107
+ const results = await this._pool.query(query, params);
2108
+ if (returnRecords) {
2109
+ return this._buildInstances(results.rows);
2110
+ }
2111
+ return void 0;
2112
+ }
2113
+ /**
2114
+ * Destroys object(s) matching the where query
2115
+ * @param {object} where - Object representing the where query
2116
+ * @param {object} [options]
2117
+ * @param {boolean} [options.returnRecords=false] - Determines if inserted records should be returned
2118
+ * @param {string[]} [options.returnSelect] - Array of model property names to return from the query.
2119
+ * @returns {object[]|void} `void` or records affected if returnRecords=true
2120
+ */
2121
+ destroy(where = {}, options) {
2122
+ if (this.model.readonly) {
2123
+ throw new Error(`${this.model.name} is readonly.`);
2124
+ }
2125
+ const { stack } = new Error(`${this.model.name}.destroy()`);
2126
+ const modelInstance = this;
2127
+ const returnSelect = options?.returnSelect;
2128
+ const returnRecords = options?.returnRecords ?? !!returnSelect;
2129
+ return {
2130
+ /**
2131
+ * Filters the query
2132
+ * @param {object} value - Object representing the where query
2133
+ */
2134
+ where(value) {
2135
+ where = value;
2136
+ return this;
2137
+ },
2138
+ async then(resolve, reject) {
2139
+ if (___default.isString(where)) {
2140
+ return reject(new Error("The query cannot be a string, it must be an object"));
2141
+ }
2142
+ try {
2143
+ const { query, params } = getDeleteQueryAndParams({
2144
+ repositoriesByModelNameLowered: modelInstance._repositoriesByModelNameLowered,
2145
+ model: modelInstance.model,
2146
+ where,
2147
+ returnRecords,
2148
+ returnSelect
2149
+ });
2150
+ const result = await modelInstance._pool.query(query, params);
2151
+ if (returnRecords) {
2152
+ return await resolve(modelInstance._buildInstances(result.rows));
2153
+ }
2154
+ return await resolve();
2155
+ } catch (ex) {
2156
+ const typedException = ex;
2157
+ if (typedException.stack) {
2158
+ typedException.stack = `${typedException.stack}
2159
+
2160
+ ${stack}`;
2161
+ } else {
2162
+ typedException.stack = stack;
2163
+ }
2164
+ return reject(typedException);
2165
+ }
2166
+ }
2167
+ };
2168
+ }
2169
+ }
2170
+
2171
+ function column(dbColumnNameOrOptions, options) {
2172
+ return function columnDecorator(object, propertyName) {
2173
+ if (!dbColumnNameOrOptions) {
2174
+ dbColumnNameOrOptions = ___default.snakeCase(propertyName);
2175
+ }
2176
+ let dbColumnName;
2177
+ if (typeof dbColumnNameOrOptions === "string") {
2178
+ dbColumnName = dbColumnNameOrOptions;
2179
+ } else {
2180
+ options = dbColumnNameOrOptions;
2181
+ }
2182
+ if (!options) {
2183
+ options = {};
2184
+ }
2185
+ if (!dbColumnName) {
2186
+ dbColumnName = options.name ?? ___default.snakeCase(propertyName);
2187
+ }
2188
+ const metadataStorage = getMetadataStorage();
2189
+ const columnCollectionOptions = options;
2190
+ if (columnCollectionOptions.collection || columnCollectionOptions.via) {
2191
+ if (!columnCollectionOptions.collection) {
2192
+ throw new Error("Unable to determine collection value. Please try specifying values as a strings to avoid circular dependency issues.");
2193
+ }
2194
+ metadataStorage.columns.push(
2195
+ new ColumnCollectionMetadata({
2196
+ target: object.constructor.name,
2197
+ name: dbColumnName,
2198
+ propertyName,
2199
+ required: columnCollectionOptions.required,
2200
+ collection: columnCollectionOptions.collection,
2201
+ through: columnCollectionOptions.through,
2202
+ via: columnCollectionOptions.via
2203
+ })
2204
+ );
2205
+ return;
2206
+ }
2207
+ const columnModelOptions = options;
2208
+ if (columnModelOptions.model) {
2209
+ metadataStorage.columns.push(
2210
+ new ColumnModelMetadata({
2211
+ target: object.constructor.name,
2212
+ name: dbColumnName,
2213
+ propertyName,
2214
+ required: columnModelOptions.required,
2215
+ model: columnModelOptions.model
2216
+ })
2217
+ );
2218
+ return;
2219
+ }
2220
+ const columnTypeOptions = options;
2221
+ metadataStorage.columns.push(
2222
+ new ColumnTypeMetadata({
2223
+ target: object.constructor.name,
2224
+ name: dbColumnName,
2225
+ propertyName,
2226
+ required: columnTypeOptions.required,
2227
+ type: columnTypeOptions.type,
2228
+ defaultsTo: columnTypeOptions.defaultsTo,
2229
+ enum: columnTypeOptions.enum,
2230
+ maxLength: columnTypeOptions.maxLength
2231
+ })
2232
+ );
2233
+ };
2234
+ }
2235
+
2236
+ function createDateColumn(dbColumnNameOrOptions, options) {
2237
+ return function createDateColumnDecorator(object, propertyName) {
2238
+ const metadataStorage = getMetadataStorage();
2239
+ let dbColumnName;
2240
+ if (typeof dbColumnNameOrOptions === "string") {
2241
+ dbColumnName = dbColumnNameOrOptions;
2242
+ } else {
2243
+ options = dbColumnNameOrOptions;
2244
+ }
2245
+ if (dbColumnNameOrOptions) {
2246
+ if (!options) {
2247
+ options = {};
2248
+ }
2249
+ if (!dbColumnName) {
2250
+ dbColumnName = options.name ?? ___default.snakeCase(propertyName);
2251
+ }
2252
+ metadataStorage.columns.push(
2253
+ new ColumnTypeMetadata({
2254
+ target: object.constructor.name,
2255
+ name: dbColumnName,
2256
+ propertyName,
2257
+ createDate: true,
2258
+ required: options.required,
2259
+ type: options.type
2260
+ })
2261
+ );
2262
+ } else {
2263
+ metadataStorage.columnModifiers.push({
2264
+ target: object.constructor.name,
2265
+ name: dbColumnName ?? ___default.snakeCase(propertyName),
2266
+ propertyName,
2267
+ createDate: true,
2268
+ required: options ? options.required : void 0,
2269
+ type: options ? options.type : "datetime"
2270
+ });
2271
+ }
2272
+ };
2273
+ }
2274
+
2275
+ function primaryColumn(dbColumnNameOrOptions, options) {
2276
+ return function primaryColumnDecorator(object, propertyName) {
2277
+ let dbColumnName;
2278
+ if (typeof dbColumnNameOrOptions === "string") {
2279
+ dbColumnName = dbColumnNameOrOptions;
2280
+ } else {
2281
+ options = dbColumnNameOrOptions;
2282
+ }
2283
+ if (dbColumnNameOrOptions) {
2284
+ if (!options) {
2285
+ options = {};
2286
+ }
2287
+ if (!dbColumnName) {
2288
+ dbColumnName = options.name ?? ___default.snakeCase(propertyName);
2289
+ }
2290
+ const { type } = options;
2291
+ const { model } = options;
2292
+ const metadataStorage = getMetadataStorage();
2293
+ if (model) {
2294
+ metadataStorage.columns.push(
2295
+ new ColumnModelMetadata({
2296
+ target: object.constructor.name,
2297
+ name: dbColumnName,
2298
+ propertyName,
2299
+ primary: true,
2300
+ required: options.required,
2301
+ model
2302
+ })
2303
+ );
2304
+ } else {
2305
+ metadataStorage.columns.push(
2306
+ new ColumnTypeMetadata({
2307
+ target: object.constructor.name,
2308
+ name: dbColumnName,
2309
+ propertyName,
2310
+ primary: true,
2311
+ required: options.required,
2312
+ type
2313
+ })
2314
+ );
2315
+ }
2316
+ } else {
2317
+ const metadataStorage = getMetadataStorage();
2318
+ metadataStorage.columnModifiers.push({
2319
+ target: object.constructor.name,
2320
+ name: dbColumnName ?? ___default.snakeCase(propertyName),
2321
+ propertyName,
2322
+ primary: true,
2323
+ required: options ? options.required : void 0,
2324
+ type: options ? options.type : void 0,
2325
+ model: options ? options.model : void 0
2326
+ });
2327
+ }
2328
+ };
2329
+ }
2330
+
2331
+ function table(dbNameOrTableOptions, options) {
2332
+ return function tableDecorator(classObject) {
2333
+ const className = classObject.name;
2334
+ let dbTableName;
2335
+ if (typeof dbNameOrTableOptions === "string") {
2336
+ dbTableName = dbNameOrTableOptions;
2337
+ } else {
2338
+ options = dbNameOrTableOptions;
2339
+ }
2340
+ if (!options) {
2341
+ options = {};
2342
+ }
2343
+ if (!options.name) {
2344
+ options.name = dbTableName ?? ___default.snakeCase(className);
2345
+ }
2346
+ const metadataStorage = getMetadataStorage();
2347
+ const modelMetadata = new ModelMetadata({
2348
+ name: className,
2349
+ type: classObject,
2350
+ tableName: options.name,
2351
+ readonly: options.readonly ?? false,
2352
+ connection: options.connection
2353
+ });
2354
+ metadataStorage.models.push(modelMetadata);
2355
+ };
2356
+ }
2357
+
2358
+ function updateDateColumn(dbColumnNameOrOptions, options) {
2359
+ return function updateDateColumnDecorator(object, propertyName) {
2360
+ let dbColumnName;
2361
+ if (typeof dbColumnNameOrOptions === "string") {
2362
+ dbColumnName = dbColumnNameOrOptions;
2363
+ } else {
2364
+ options = dbColumnNameOrOptions;
2365
+ }
2366
+ if (dbColumnNameOrOptions) {
2367
+ if (!options) {
2368
+ options = {};
2369
+ }
2370
+ if (!dbColumnName) {
2371
+ dbColumnName = options.name ?? ___default.snakeCase(propertyName);
2372
+ }
2373
+ const metadataStorage = getMetadataStorage();
2374
+ metadataStorage.columns.push(
2375
+ new ColumnTypeMetadata({
2376
+ target: object.constructor.name,
2377
+ name: dbColumnName,
2378
+ propertyName,
2379
+ updateDate: true,
2380
+ required: options.required,
2381
+ type: options.type
2382
+ })
2383
+ );
2384
+ } else {
2385
+ const metadataStorage = getMetadataStorage();
2386
+ metadataStorage.columnModifiers.push({
2387
+ target: object.constructor.name,
2388
+ name: dbColumnName ?? ___default.snakeCase(propertyName),
2389
+ propertyName,
2390
+ updateDate: true,
2391
+ required: options ? options.required : void 0,
2392
+ type: options ? options.type : "datetime"
2393
+ });
2394
+ }
2395
+ };
2396
+ }
2397
+
2398
+ function versionColumn(dbColumnNameOrOptions, options) {
2399
+ return function versionColumnDecorator(object, propertyName) {
2400
+ let dbColumnName;
2401
+ if (typeof dbColumnNameOrOptions === "string") {
2402
+ dbColumnName = dbColumnNameOrOptions;
2403
+ } else {
2404
+ options = dbColumnNameOrOptions;
2405
+ }
2406
+ if (dbColumnNameOrOptions) {
2407
+ if (!options) {
2408
+ options = {};
2409
+ }
2410
+ if (!dbColumnName) {
2411
+ dbColumnName = options.name ?? ___default.snakeCase(propertyName);
2412
+ }
2413
+ const metadataStorage = getMetadataStorage();
2414
+ metadataStorage.columns.push(
2415
+ new ColumnTypeMetadata({
2416
+ target: object.constructor.name,
2417
+ name: dbColumnName,
2418
+ propertyName,
2419
+ version: true,
2420
+ required: options.required,
2421
+ type: options.type
2422
+ })
2423
+ );
2424
+ } else {
2425
+ const metadataStorage = getMetadataStorage();
2426
+ metadataStorage.columnModifiers.push({
2427
+ target: object.constructor.name,
2428
+ name: dbColumnName ?? ___default.snakeCase(propertyName),
2429
+ propertyName,
2430
+ version: true,
2431
+ required: options ? options.required : void 0,
2432
+ type: options ? options.type : void 0
2433
+ });
2434
+ }
2435
+ };
2436
+ }
2437
+
2438
+ class Entity {
2439
+ static beforeCreate(values) {
2440
+ return values;
2441
+ }
2442
+ static beforeUpdate(values) {
2443
+ return values;
2444
+ }
2445
+ }
2446
+
2447
+ function getInheritanceTree(model) {
2448
+ const tree = [model];
2449
+ function getRecursivePrototypesOf(parentEntity) {
2450
+ const proto = Object.getPrototypeOf(parentEntity);
2451
+ if (proto && proto.name && proto.name !== "Function") {
2452
+ tree.unshift(proto);
2453
+ getRecursivePrototypesOf(proto);
2454
+ }
2455
+ }
2456
+ getRecursivePrototypesOf(model);
2457
+ return tree;
2458
+ }
2459
+ function initialize({ models, pool, readonlyPool = pool, connections = {}, expose }) {
2460
+ if (!models.length) {
2461
+ throw new Error("Models need to be specified to read all model information from decorators");
2462
+ }
2463
+ const inheritanceTreesByModelName = {};
2464
+ const modelNames = [];
2465
+ for (const model of models) {
2466
+ inheritanceTreesByModelName[model.name] = getInheritanceTree(model);
2467
+ modelNames.push(model.name);
2468
+ }
2469
+ const metadataStorage = getMetadataStorage();
2470
+ const modelMetadataByModelName = {};
2471
+ for (const model of metadataStorage.models) {
2472
+ modelMetadataByModelName[model.name] = model;
2473
+ }
2474
+ const columnsByPropertyNameForModel = {};
2475
+ for (const column of metadataStorage.columns) {
2476
+ const columns = columnsByPropertyNameForModel[column.target] ?? {};
2477
+ columns[column.propertyName] = column;
2478
+ columnsByPropertyNameForModel[column.target] = columns;
2479
+ }
2480
+ const columnModifiersByPropertyNameForModel = {};
2481
+ for (const columnModifier of metadataStorage.columnModifiers) {
2482
+ const columnModifiersForModel = columnModifiersByPropertyNameForModel[columnModifier.target] ?? {};
2483
+ const columnModifiersForProperty = columnModifiersForModel[columnModifier.propertyName] ?? [];
2484
+ columnModifiersForProperty.push(columnModifier);
2485
+ columnModifiersForModel[columnModifier.propertyName] = columnModifiersForProperty;
2486
+ columnModifiersByPropertyNameForModel[columnModifier.target] = columnModifiersForModel;
2487
+ }
2488
+ for (const model of models) {
2489
+ let modelMetadata;
2490
+ let inheritedColumnsByPropertyName = {};
2491
+ const inheritedColumnModifiersByPropertyName = /* @__PURE__ */ new Map();
2492
+ for (const inheritedClass of inheritanceTreesByModelName[model.name] ?? []) {
2493
+ modelMetadata = modelMetadataByModelName[inheritedClass.name] ?? modelMetadata;
2494
+ const columnsByPropertyName = columnsByPropertyNameForModel[inheritedClass.name] ?? {};
2495
+ inheritedColumnsByPropertyName = {
2496
+ ...inheritedColumnsByPropertyName,
2497
+ ...columnsByPropertyName
2498
+ };
2499
+ for (const [propertyName] of Object.entries(columnsByPropertyName)) {
2500
+ inheritedColumnModifiersByPropertyName.delete(propertyName);
2501
+ }
2502
+ const columnModifiersByPropertyName = columnModifiersByPropertyNameForModel[inheritedClass.name] ?? {};
2503
+ for (const [propertyName, columnModifiers] of Object.entries(columnModifiersByPropertyName)) {
2504
+ inheritedColumnModifiersByPropertyName.set(propertyName, [...inheritedColumnModifiersByPropertyName.get(propertyName) ?? [], ...columnModifiers ?? []]);
2505
+ }
2506
+ }
2507
+ if (!modelMetadata) {
2508
+ throw new Error(`Unable to find @table() on ${model.name}`);
2509
+ }
2510
+ modelMetadataByModelName[model.name] = new ModelMetadata({
2511
+ ...modelMetadata,
2512
+ name: model.name,
2513
+ type: model
2514
+ });
2515
+ columnsByPropertyNameForModel[model.name] = inheritedColumnsByPropertyName;
2516
+ columnModifiersByPropertyNameForModel[model.name] = Object.fromEntries(inheritedColumnModifiersByPropertyName);
2517
+ }
2518
+ for (const [modelName, columnModifiersByPropertyName] of Object.entries(columnModifiersByPropertyNameForModel)) {
2519
+ const columnsByPropertyName = columnsByPropertyNameForModel[modelName] ?? {};
2520
+ for (const [propertyName, columnModifiers] of Object.entries(columnModifiersByPropertyName)) {
2521
+ const column = columnsByPropertyName[propertyName];
2522
+ if (column) {
2523
+ for (const columnModifier of columnModifiers) {
2524
+ Object.assign(column, ___default.omit(columnModifier, ["target", "name", "propertyName", "type", "model"]));
2525
+ }
2526
+ } else {
2527
+ let columnDetails = {
2528
+ target: modelName,
2529
+ propertyName
2530
+ };
2531
+ for (const columnModifier of columnModifiers) {
2532
+ columnDetails = {
2533
+ ...columnDetails,
2534
+ ...columnModifier
2535
+ };
2536
+ }
2537
+ if (!columnDetails.name) {
2538
+ throw new Error(`Missing column name for ${modelName}#${propertyName}`);
2539
+ }
2540
+ if (!columnDetails.type && !columnDetails.model) {
2541
+ throw new Error(`Missing column type for ${modelName}#${propertyName}`);
2542
+ }
2543
+ if (columnDetails.model) {
2544
+ columnsByPropertyName[propertyName] = new ColumnModelMetadata({
2545
+ ...columnDetails,
2546
+ name: columnDetails.name,
2547
+ model: columnDetails.model
2548
+ });
2549
+ } else if (columnDetails.type) {
2550
+ columnsByPropertyName[propertyName] = new ColumnTypeMetadata({
2551
+ ...columnDetails,
2552
+ name: columnDetails.name,
2553
+ type: columnDetails.type
2554
+ });
2555
+ }
2556
+ }
2557
+ }
2558
+ columnsByPropertyNameForModel[modelName] = columnsByPropertyName;
2559
+ }
2560
+ const repositoriesByModelNameLowered = {};
2561
+ const repositoriesByModelName = {};
2562
+ for (const modelName of modelNames) {
2563
+ const model = modelMetadataByModelName[modelName];
2564
+ if (!model) {
2565
+ throw new Error(`Unable to find @table() on ${modelName}`);
2566
+ }
2567
+ const columnsByPropertyName = columnsByPropertyNameForModel[modelName];
2568
+ if (!columnsByPropertyName) {
2569
+ throw new Error(`Did not find any columns decorated with @column. Model: ${modelName}`);
2570
+ }
2571
+ model.columns = Object.values(columnsByPropertyName);
2572
+ let modelPool = pool;
2573
+ let modelReadonlyPool = readonlyPool;
2574
+ if (model.connection) {
2575
+ const modelConnection = connections[model.connection];
2576
+ if (!modelConnection) {
2577
+ throw new Error(`Unable to find connection (${model.connection}) for entity: ${model.name}`);
2578
+ }
2579
+ modelPool = modelConnection.pool || pool;
2580
+ modelReadonlyPool = modelConnection.readonlyPool ?? modelPool;
2581
+ }
2582
+ let repository;
2583
+ if (model.readonly) {
2584
+ repository = new ReadonlyRepository({
2585
+ modelMetadata: model,
2586
+ type: model.type,
2587
+ repositoriesByModelNameLowered,
2588
+ pool: modelPool,
2589
+ readonlyPool: modelReadonlyPool
2590
+ });
2591
+ repositoriesByModelNameLowered[model.name.toLowerCase()] = repository;
2592
+ repositoriesByModelName[model.name] = repository;
2593
+ } else {
2594
+ repository = new Repository({
2595
+ modelMetadata: model,
2596
+ type: model.type,
2597
+ repositoriesByModelNameLowered,
2598
+ pool: modelPool,
2599
+ readonlyPool: modelReadonlyPool
2600
+ });
2601
+ repositoriesByModelNameLowered[model.name.toLowerCase()] = repository;
2602
+ repositoriesByModelName[model.name] = repository;
2603
+ }
2604
+ if (expose) {
2605
+ expose(repository, model);
2606
+ }
2607
+ }
2608
+ return repositoriesByModelName;
2609
+ }
2610
+
2611
+ exports.ColumnBaseMetadata = ColumnBaseMetadata;
2612
+ exports.ColumnCollectionMetadata = ColumnCollectionMetadata;
2613
+ exports.ColumnModelMetadata = ColumnModelMetadata;
2614
+ exports.ColumnTypeMetadata = ColumnTypeMetadata;
2615
+ exports.Entity = Entity;
2616
+ exports.ModelMetadata = ModelMetadata;
2617
+ exports.ReadonlyRepository = ReadonlyRepository;
2618
+ exports.Repository = Repository;
2619
+ exports.column = column;
2620
+ exports.createDateColumn = createDateColumn;
2621
+ exports.getMetadataStorage = getMetadataStorage;
2622
+ exports.initialize = initialize;
2623
+ exports.primaryColumn = primaryColumn;
2624
+ exports.table = table;
2625
+ exports.updateDateColumn = updateDateColumn;
2626
+ exports.versionColumn = versionColumn;