@mikro-orm/core 7.0.0-dev.22 → 7.0.0-dev.24

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 (70) hide show
  1. package/EntityManager.d.ts +12 -2
  2. package/EntityManager.js +40 -53
  3. package/README.md +1 -2
  4. package/connections/Connection.d.ts +4 -2
  5. package/connections/Connection.js +2 -2
  6. package/decorators/Entity.d.ts +15 -0
  7. package/decorators/Indexed.d.ts +2 -2
  8. package/decorators/ManyToMany.d.ts +2 -0
  9. package/decorators/ManyToOne.d.ts +2 -0
  10. package/decorators/OneToOne.d.ts +2 -0
  11. package/decorators/Transactional.d.ts +1 -0
  12. package/decorators/Transactional.js +3 -3
  13. package/drivers/IDatabaseDriver.d.ts +4 -0
  14. package/entity/ArrayCollection.d.ts +3 -1
  15. package/entity/ArrayCollection.js +7 -2
  16. package/entity/Collection.js +3 -2
  17. package/entity/EntityFactory.d.ts +6 -0
  18. package/entity/EntityFactory.js +17 -6
  19. package/entity/EntityHelper.js +5 -1
  20. package/entity/EntityLoader.js +27 -19
  21. package/entity/Reference.d.ts +5 -0
  22. package/entity/Reference.js +16 -0
  23. package/entity/WrappedEntity.js +1 -1
  24. package/entity/defineEntity.d.ts +537 -0
  25. package/entity/defineEntity.js +690 -0
  26. package/entity/index.d.ts +2 -0
  27. package/entity/index.js +2 -0
  28. package/entity/utils.d.ts +7 -0
  29. package/entity/utils.js +15 -3
  30. package/enums.d.ts +15 -2
  31. package/enums.js +13 -0
  32. package/errors.d.ts +6 -0
  33. package/errors.js +14 -0
  34. package/hydration/ObjectHydrator.js +1 -1
  35. package/index.d.ts +1 -1
  36. package/metadata/EntitySchema.js +10 -2
  37. package/metadata/MetadataDiscovery.d.ts +0 -1
  38. package/metadata/MetadataDiscovery.js +27 -18
  39. package/package.json +3 -3
  40. package/platforms/Platform.d.ts +3 -1
  41. package/serialization/SerializationContext.js +13 -10
  42. package/types/BooleanType.d.ts +1 -1
  43. package/types/DecimalType.js +1 -1
  44. package/types/DoubleType.js +1 -1
  45. package/typings.d.ts +32 -10
  46. package/typings.js +21 -4
  47. package/unit-of-work/ChangeSetComputer.js +3 -1
  48. package/unit-of-work/ChangeSetPersister.d.ts +4 -2
  49. package/unit-of-work/ChangeSetPersister.js +14 -10
  50. package/unit-of-work/UnitOfWork.d.ts +2 -1
  51. package/unit-of-work/UnitOfWork.js +36 -13
  52. package/utils/Configuration.d.ts +7 -1
  53. package/utils/Configuration.js +1 -0
  54. package/utils/ConfigurationLoader.js +2 -2
  55. package/utils/Cursor.js +3 -0
  56. package/utils/DataloaderUtils.d.ts +6 -1
  57. package/utils/DataloaderUtils.js +37 -20
  58. package/utils/EntityComparator.d.ts +6 -2
  59. package/utils/EntityComparator.js +29 -8
  60. package/utils/QueryHelper.d.ts +6 -0
  61. package/utils/QueryHelper.js +47 -4
  62. package/utils/RawQueryFragment.d.ts +34 -0
  63. package/utils/RawQueryFragment.js +35 -1
  64. package/utils/TransactionManager.d.ts +65 -0
  65. package/utils/TransactionManager.js +199 -0
  66. package/utils/Utils.d.ts +2 -2
  67. package/utils/Utils.js +31 -7
  68. package/utils/index.d.ts +1 -0
  69. package/utils/index.js +1 -0
  70. package/utils/upsert-utils.js +9 -1
@@ -0,0 +1,690 @@
1
+ import { types } from '../types/index.js';
2
+ import { EntitySchema } from '../metadata/EntitySchema.js';
3
+ /** @internal */
4
+ export class PropertyOptionsBuilder {
5
+ '~options';
6
+ '~type';
7
+ constructor(options) {
8
+ this['~options'] = options;
9
+ }
10
+ /**
11
+ * Alias for `fieldName`.
12
+ */
13
+ name(name) {
14
+ return new PropertyOptionsBuilder({ ...this['~options'], name });
15
+ }
16
+ /**
17
+ * Specify database column name for this property.
18
+ *
19
+ * @see https://mikro-orm.io/docs/naming-strategy
20
+ */
21
+ fieldName(fieldName) {
22
+ return new PropertyOptionsBuilder({ ...this['~options'], fieldName });
23
+ }
24
+ /**
25
+ * Specify database column names for this property.
26
+ * Same as `fieldName` but for composite FKs.
27
+ *
28
+ * @see https://mikro-orm.io/docs/naming-strategy
29
+ */
30
+ fieldNames(...fieldNames) {
31
+ return new PropertyOptionsBuilder({ ...this['~options'], fieldNames });
32
+ }
33
+ /**
34
+ * Specify an exact database column type for {@link https://mikro-orm.io/docs/schema-generator Schema Generator}. This option is only for simple properties represented by a single column. (SQL only)
35
+ */
36
+ columnType(columnType) {
37
+ return new PropertyOptionsBuilder({ ...this['~options'], columnType });
38
+ }
39
+ /**
40
+ * Specify an exact database column type for {@link https://mikro-orm.io/docs/schema-generator Schema Generator}. This option is suitable for composite keys, where one property is represented by multiple columns. (SQL only)
41
+ */
42
+ columnTypes(...columnTypes) {
43
+ return new PropertyOptionsBuilder({ ...this['~options'], columnTypes });
44
+ }
45
+ /**
46
+ * Explicitly specify the runtime type.
47
+ *
48
+ * @see https://mikro-orm.io/docs/metadata-providers
49
+ * @see https://mikro-orm.io/docs/custom-types
50
+ */
51
+ type(type) {
52
+ return new PropertyOptionsBuilder({ ...this['~options'], type });
53
+ }
54
+ /**
55
+ * Runtime type of the property. This is the JS type that your property is mapped to, e.g. `string` or `number`, and is normally inferred automatically via `reflect-metadata`.
56
+ * In some cases, the inference won't work, and you might need to specify the `runtimeType` explicitly - the most common one is when you use a union type with null like `foo: number | null`.
57
+ */
58
+ runtimeType(runtimeType) {
59
+ return new PropertyOptionsBuilder({ ...this['~options'], runtimeType });
60
+ }
61
+ /**
62
+ * Set length of database column, used for datetime/timestamp/varchar column types for {@link https://mikro-orm.io/docs/schema-generator Schema Generator}. (SQL only)
63
+ */
64
+ length(length) {
65
+ return new PropertyOptionsBuilder({ ...this['~options'], length });
66
+ }
67
+ /**
68
+ * Set precision of database column to represent the number of significant digits. (SQL only)
69
+ */
70
+ precision(precision) {
71
+ return new PropertyOptionsBuilder({ ...this['~options'], precision });
72
+ }
73
+ /**
74
+ * Set scale of database column to represents the number of digits after the decimal point. (SQL only)
75
+ */
76
+ scale(scale) {
77
+ return new PropertyOptionsBuilder({ ...this['~options'], scale });
78
+ }
79
+ /**
80
+ * Explicitly specify the auto increment of the primary key.
81
+ */
82
+ autoincrement(autoincrement = true) {
83
+ return new PropertyOptionsBuilder({ ...this['~options'], autoincrement });
84
+ }
85
+ /**
86
+ * Add the property to the `returning` statement.
87
+ */
88
+ returning(returning = true) {
89
+ return new PropertyOptionsBuilder({ ...this['~options'], returning });
90
+ }
91
+ /**
92
+ * Automatically set the property value when entity gets created, executed during flush operation.
93
+ * @param entity
94
+ */
95
+ onCreate(onCreate) {
96
+ return new PropertyOptionsBuilder({ ...this['~options'], onCreate });
97
+ }
98
+ /**
99
+ * Automatically update the property value every time entity gets updated, executed during flush operation.
100
+ * @param entity
101
+ */
102
+ onUpdate(onUpdate) {
103
+ return new PropertyOptionsBuilder({ ...this['~options'], onUpdate });
104
+ }
105
+ /**
106
+ * Specify default column value for {@link https://mikro-orm.io/docs/schema-generator Schema Generator}.
107
+ * This is a runtime value, assignable to the entity property. (SQL only)
108
+ */
109
+ default(defaultValue) {
110
+ return new PropertyOptionsBuilder({ ...this['~options'], default: defaultValue });
111
+ }
112
+ /**
113
+ * Specify SQL functions for {@link https://mikro-orm.io/docs/schema-generator Schema Generator}. (SQL only)
114
+ * Since v4 you should use defaultRaw for SQL functions. e.g. now()
115
+ */
116
+ defaultRaw(defaultRaw) {
117
+ return new PropertyOptionsBuilder({ ...this['~options'], defaultRaw });
118
+ }
119
+ /**
120
+ * Set to map some SQL snippet for the entity.
121
+ *
122
+ * @see https://mikro-orm.io/docs/defining-entities#formulas Formulas
123
+ */
124
+ formula(formula) {
125
+ return new PropertyOptionsBuilder({ ...this['~options'], formula });
126
+ }
127
+ /**
128
+ * For generated columns. This will be appended to the column type after the `generated always` clause.
129
+ */
130
+ generated(generated) {
131
+ return new PropertyOptionsBuilder({ ...this['~options'], generated });
132
+ }
133
+ /**
134
+ * Set column as nullable for {@link https://mikro-orm.io/docs/schema-generator Schema Generator}.
135
+ */
136
+ nullable(nullable = true) {
137
+ return new PropertyOptionsBuilder({ ...this['~options'], nullable });
138
+ }
139
+ /**
140
+ * Set column as unsigned for {@link https://mikro-orm.io/docs/schema-generator Schema Generator}. (SQL only)
141
+ */
142
+ unsigned(unsigned = true) {
143
+ return new PropertyOptionsBuilder({ ...this['~options'], unsigned });
144
+ }
145
+ /**
146
+ * Set false to define {@link https://mikro-orm.io/docs/serializing#shadow-properties Shadow Property}.
147
+ */
148
+ persist(persist = true) {
149
+ return new PropertyOptionsBuilder({ ...this['~options'], persist });
150
+ }
151
+ /**
152
+ * Set false to disable hydration of this property. Useful for persisted getters.
153
+ */
154
+ hydrate(hydrate = true) {
155
+ return new PropertyOptionsBuilder({ ...this['~options'], hydrate });
156
+ }
157
+ /**
158
+ * Enable `ScalarReference` wrapper for lazy values. Use this in combination with `lazy: true` to have a type-safe accessor object in place of the value.
159
+ */
160
+ ref(ref = true) {
161
+ return new PropertyOptionsBuilder({ ...this['~options'], ref });
162
+ }
163
+ /**
164
+ * Set false to disable change tracking on a property level.
165
+ *
166
+ * @see https://mikro-orm.io/docs/unit-of-work#change-tracking-and-performance-considerations
167
+ */
168
+ trackChanges(trackChanges = true) {
169
+ return new PropertyOptionsBuilder({ ...this['~options'], trackChanges });
170
+ }
171
+ /**
172
+ * Set to true to omit the property when {@link https://mikro-orm.io/docs/serializing Serializing}.
173
+ */
174
+ hidden(hidden = true) {
175
+ return new PropertyOptionsBuilder({ ...this['~options'], hidden });
176
+ }
177
+ /**
178
+ * Set to true to enable {@link https://mikro-orm.io/docs/transactions#optimistic-locking Optimistic Locking} via version field. (SQL only)
179
+ */
180
+ version(version = true) {
181
+ return new PropertyOptionsBuilder({ ...this['~options'], version });
182
+ }
183
+ /**
184
+ * Set to true to enable {@link https://mikro-orm.io/docs/transactions#optimistic-locking Optimistic Locking} via concurrency fields.
185
+ */
186
+ concurrencyCheck(concurrencyCheck = true) {
187
+ return new PropertyOptionsBuilder({ ...this['~options'], concurrencyCheck });
188
+ }
189
+ /**
190
+ * Explicitly specify index on a property.
191
+ */
192
+ index(index = true) {
193
+ return new PropertyOptionsBuilder({ ...this['~options'], index });
194
+ }
195
+ /**
196
+ * Set column as unique for {@link https://mikro-orm.io/docs/schema-generator Schema Generator}. (SQL only)
197
+ */
198
+ unique(unique = true) {
199
+ return new PropertyOptionsBuilder({ ...this['~options'], unique });
200
+ }
201
+ /**
202
+ * Specify column with check constraints. (Postgres driver only)
203
+ *
204
+ * @see https://mikro-orm.io/docs/defining-entities#check-constraints
205
+ */
206
+ check(check) {
207
+ return new PropertyOptionsBuilder({ ...this['~options'], check });
208
+ }
209
+ /**
210
+ * Set to omit the property from the select clause for lazy loading.
211
+ *
212
+ * @see https://mikro-orm.io/docs/defining-entities#lazy-scalar-properties
213
+ */
214
+ lazy(lazy = true, ref = true) {
215
+ return new PropertyOptionsBuilder({ ...this['~options'], lazy, ref });
216
+ }
217
+ /**
218
+ * Set true to define entity's unique primary key identifier.
219
+ *
220
+ * @see https://mikro-orm.io/docs/decorators#primarykey
221
+ */
222
+ primary(primary = true) {
223
+ return new PropertyOptionsBuilder({ ...this['~options'], primary });
224
+ }
225
+ /**
226
+ * Set true to define the properties as setter. (virtual)
227
+ *
228
+ * @example
229
+ * ```
230
+ * @Property({ setter: true })
231
+ * set address(value: string) {
232
+ * this._address = value.toLocaleLowerCase();
233
+ * }
234
+ * ```
235
+ */
236
+ setter(setter = true) {
237
+ return new PropertyOptionsBuilder({ ...this['~options'], setter });
238
+ }
239
+ /**
240
+ * Set true to define the properties as getter. (virtual)
241
+ *
242
+ * @example
243
+ * ```
244
+ * @Property({ getter: true })
245
+ * get fullName() {
246
+ * return this.firstName + this.lastName;
247
+ * }
248
+ * ```
249
+ */
250
+ getter(getter = true) {
251
+ return new PropertyOptionsBuilder({ ...this['~options'], getter });
252
+ }
253
+ /**
254
+ * When defining a property over a method (not a getter, a regular function), you can use this option to point
255
+ * to the method name.
256
+ *
257
+ * @example
258
+ * ```
259
+ * @Property({ getter: true })
260
+ * getFullName() {
261
+ * return this.firstName + this.lastName;
262
+ * }
263
+ * ```
264
+ */
265
+ getterName(getterName) {
266
+ return new PropertyOptionsBuilder({ ...this['~options'], getterName });
267
+ }
268
+ /**
269
+ * Set to define serialized primary key for MongoDB. (virtual)
270
+ * Alias for `@SerializedPrimaryKey()` decorator.
271
+ *
272
+ * @see https://mikro-orm.io/docs/decorators#serializedprimarykey
273
+ */
274
+ serializedPrimaryKey(serializedPrimaryKey = true) {
275
+ return new PropertyOptionsBuilder({ ...this['~options'], serializedPrimaryKey });
276
+ }
277
+ /**
278
+ * Set to use serialize property. Allow to specify a callback that will be used when serializing a property.
279
+ *
280
+ * @see https://mikro-orm.io/docs/serializing#property-serializers
281
+ */
282
+ serializer(serializer) {
283
+ return new PropertyOptionsBuilder({ ...this['~options'], serializer });
284
+ }
285
+ /**
286
+ * Specify name of key for the serialized value.
287
+ */
288
+ serializedName(serializedName) {
289
+ return new PropertyOptionsBuilder({ ...this['~options'], serializedName });
290
+ }
291
+ /**
292
+ * Specify serialization groups for `serialize()` calls. If a property does not specify any group, it will be included,
293
+ * otherwise only properties with a matching group are included.
294
+ */
295
+ groups(...groups) {
296
+ return new PropertyOptionsBuilder({ ...this['~options'], groups });
297
+ }
298
+ /**
299
+ * Specify a custom order based on the values. (SQL only)
300
+ */
301
+ customOrder(...customOrder) {
302
+ return new PropertyOptionsBuilder({ ...this['~options'], customOrder });
303
+ }
304
+ /**
305
+ * Specify comment of column for {@link https://mikro-orm.io/docs/schema-generator Schema Generator}. (SQL only)
306
+ */
307
+ comment(comment) {
308
+ return new PropertyOptionsBuilder({ ...this['~options'], comment });
309
+ }
310
+ /** mysql only */
311
+ extra(extra) {
312
+ return new PropertyOptionsBuilder({ ...this['~options'], extra });
313
+ }
314
+ /**
315
+ * Set to avoid a perpetual diff from the {@link https://mikro-orm.io/docs/schema-generator Schema Generator} when columns are generated.
316
+ *
317
+ * @see https://mikro-orm.io/docs/defining-entities#sql-generated-columns
318
+ */
319
+ ignoreSchemaChanges(...ignoreSchemaChanges) {
320
+ return new PropertyOptionsBuilder({ ...this['~options'], ignoreSchemaChanges });
321
+ }
322
+ $type() {
323
+ return new PropertyOptionsBuilder({ ...this['~options'] });
324
+ }
325
+ }
326
+ /** @internal */
327
+ export class EnumOptionsBuilder extends PropertyOptionsBuilder {
328
+ constructor(options) {
329
+ super(options);
330
+ this['~options'] = options;
331
+ }
332
+ array(array = true) {
333
+ return new EnumOptionsBuilder({ ...this['~options'], array });
334
+ }
335
+ /** for postgres, by default it uses text column with check constraint */
336
+ nativeEnumName(nativeEnumName) {
337
+ return new EnumOptionsBuilder({ ...this['~options'], nativeEnumName });
338
+ }
339
+ }
340
+ /** @internal */
341
+ export class EmbeddedOptionsBuilder extends PropertyOptionsBuilder {
342
+ constructor(options) {
343
+ super(options);
344
+ this['~options'] = options;
345
+ }
346
+ prefix(prefix) {
347
+ return new EmbeddedOptionsBuilder({ ...this['~options'], prefix });
348
+ }
349
+ prefixMode(prefixMode) {
350
+ return new EmbeddedOptionsBuilder({ ...this['~options'], prefixMode });
351
+ }
352
+ object(object = true) {
353
+ return new EmbeddedOptionsBuilder({ ...this['~options'], object });
354
+ }
355
+ array(array = true) {
356
+ return new EmbeddedOptionsBuilder({ ...this['~options'], array });
357
+ }
358
+ }
359
+ /** @internal */
360
+ export class ReferenceOptionsBuilder extends PropertyOptionsBuilder {
361
+ constructor(options) {
362
+ super(options);
363
+ this['~options'] = options;
364
+ }
365
+ /** Set what actions on owning entity should be cascaded to the relationship. Defaults to [Cascade.PERSIST, Cascade.MERGE] (see {@doclink cascading}). */
366
+ cascade(...cascade) {
367
+ return new ReferenceOptionsBuilder({ ...this['~options'], cascade });
368
+ }
369
+ /** Always load the relationship. Discouraged for use with to-many relations for performance reasons. */
370
+ eager(eager = true) {
371
+ return new ReferenceOptionsBuilder({ ...this['~options'], eager });
372
+ }
373
+ /** Override the default loading strategy for this property. This option has precedence over the global `loadStrategy`, but can be overridden by `FindOptions.strategy`. */
374
+ strategy(strategy) {
375
+ return new ReferenceOptionsBuilder({ ...this['~options'], strategy });
376
+ }
377
+ /**
378
+ * @internal
379
+ * re-declare to override type inference
380
+ */
381
+ /* v8 ignore next 3 */
382
+ ref(ref = true) {
383
+ return new ReferenceOptionsBuilder({ ...this['~options'], ref });
384
+ }
385
+ /**
386
+ * @internal
387
+ * re-declare to override type inference
388
+ */
389
+ /* v8 ignore next 3 */
390
+ primary(primary = true) {
391
+ return new ReferenceOptionsBuilder({ ...this['~options'], primary });
392
+ }
393
+ }
394
+ /** @internal */
395
+ export class ManyToManyOptionsBuilder extends ReferenceOptionsBuilder {
396
+ constructor(options) {
397
+ super(options);
398
+ this['~options'] = options;
399
+ }
400
+ /** Set this side as owning. Owning side is where the foreign key is defined. This option is not required if you use `inversedBy` or `mappedBy` to distinguish owning and inverse side. */
401
+ owner(owner = true) {
402
+ return new ManyToManyOptionsBuilder({ ...this['~options'], owner });
403
+ }
404
+ /** Point to the inverse side property name. */
405
+ inversedBy(inversedBy) {
406
+ return new ManyToManyOptionsBuilder({ ...this['~options'], inversedBy });
407
+ }
408
+ /** Point to the owning side property name. */
409
+ mappedBy(mappedBy) {
410
+ return new ManyToManyOptionsBuilder({ ...this['~options'], mappedBy });
411
+ }
412
+ /** Condition for {@doclink collections#declarative-partial-loading | Declarative partial loading}. */
413
+ where(...where) {
414
+ return new ManyToManyOptionsBuilder({ ...this['~options'], where: where });
415
+ }
416
+ /** Set default ordering. */
417
+ orderBy(...orderBy) {
418
+ return new ManyToManyOptionsBuilder({ ...this['~options'], orderBy });
419
+ }
420
+ /** Force stable insertion order of items in the collection (see {@doclink collections | Collections}). */
421
+ fixedOrder(fixedOrder = true) {
422
+ return new ManyToManyOptionsBuilder({ ...this['~options'], fixedOrder });
423
+ }
424
+ /** Override default order column name (`id`) for fixed ordering. */
425
+ fixedOrderColumn(fixedOrderColumn) {
426
+ return new ManyToManyOptionsBuilder({ ...this['~options'], fixedOrderColumn });
427
+ }
428
+ /** Override default name for pivot table (see {@doclink naming-strategy | Naming Strategy}). */
429
+ pivotTable(pivotTable) {
430
+ return new ManyToManyOptionsBuilder({ ...this['~options'], pivotTable });
431
+ }
432
+ /** Set pivot entity for this relation (see {@doclink collections#custom-pivot-table-entity | Custom pivot table entity}). */
433
+ pivotEntity(pivotEntity) {
434
+ return new ManyToManyOptionsBuilder({ ...this['~options'], pivotEntity });
435
+ }
436
+ /** Override the default database column name on the owning side (see {@doclink naming-strategy | Naming Strategy}). This option is only for simple properties represented by a single column. */
437
+ joinColumn(joinColumn) {
438
+ return new ManyToManyOptionsBuilder({ ...this['~options'], joinColumn });
439
+ }
440
+ /** Override the default database column name on the owning side (see {@doclink naming-strategy | Naming Strategy}). This option is suitable for composite keys, where one property is represented by multiple columns. */
441
+ joinColumns(...joinColumns) {
442
+ return new ManyToManyOptionsBuilder({ ...this['~options'], joinColumns });
443
+ }
444
+ /** Override the default database column name on the inverse side (see {@doclink naming-strategy | Naming Strategy}). This option is only for simple properties represented by a single column. */
445
+ inverseJoinColumn(inverseJoinColumn) {
446
+ return new ManyToManyOptionsBuilder({ ...this['~options'], inverseJoinColumn });
447
+ }
448
+ /** Override the default database column name on the inverse side (see {@doclink naming-strategy | Naming Strategy}). This option is suitable for composite keys, where one property is represented by multiple columns. */
449
+ inverseJoinColumns(...inverseJoinColumns) {
450
+ return new ManyToManyOptionsBuilder({ ...this['~options'], inverseJoinColumns });
451
+ }
452
+ /** Override the default database column name on the target entity (see {@doclink naming-strategy | Naming Strategy}). This option is only for simple properties represented by a single column. */
453
+ referenceColumnName(referenceColumnName) {
454
+ return new ManyToManyOptionsBuilder({ ...this['~options'], referenceColumnName });
455
+ }
456
+ /** Override the default database column name on the target entity (see {@doclink naming-strategy | Naming Strategy}). This option is suitable for composite keys, where one property is represented by multiple columns. */
457
+ referencedColumnNames(...referencedColumnNames) {
458
+ return new ManyToManyOptionsBuilder({ ...this['~options'], referencedColumnNames });
459
+ }
460
+ /** What to do when the target entity gets deleted. */
461
+ deleteRule(deleteRule) {
462
+ return new ManyToManyOptionsBuilder({ ...this['~options'], deleteRule });
463
+ }
464
+ /** What to do when the reference to the target entity gets updated. */
465
+ updateRule(updateRule) {
466
+ return new ManyToManyOptionsBuilder({ ...this['~options'], updateRule });
467
+ }
468
+ }
469
+ /** @internal */
470
+ export class ManyToOneOptionsBuilder extends ReferenceOptionsBuilder {
471
+ constructor(options) {
472
+ super(options);
473
+ this['~options'] = options;
474
+ }
475
+ /** Point to the inverse side property name. */
476
+ inversedBy(inversedBy) {
477
+ return new ManyToOneOptionsBuilder({ ...this['~options'], inversedBy });
478
+ }
479
+ /** Wrap the entity in {@apilink Reference} wrapper. */
480
+ ref(ref = true) {
481
+ return new ManyToOneOptionsBuilder({ ...this['~options'], ref });
482
+ }
483
+ /** Use this relation as a primary key. */
484
+ primary(primary = true) {
485
+ return new ManyToOneOptionsBuilder({ ...this['~options'], primary });
486
+ }
487
+ /** Map this relation to the primary key value instead of an entity. */
488
+ mapToPk(mapToPk = true) {
489
+ return new ManyToOneOptionsBuilder({ ...this['~options'], mapToPk });
490
+ }
491
+ /** Override the default database column name on the owning side (see {@doclink naming-strategy | Naming Strategy}). This option is only for simple properties represented by a single column. */
492
+ joinColumn(joinColumn) {
493
+ return new ManyToOneOptionsBuilder({ ...this['~options'], joinColumn });
494
+ }
495
+ /** Override the default database column name on the owning side (see {@doclink naming-strategy | Naming Strategy}). This option is suitable for composite keys, where one property is represented by multiple columns. */
496
+ joinColumns(...joinColumns) {
497
+ return new ManyToOneOptionsBuilder({ ...this['~options'], joinColumns });
498
+ }
499
+ /** When a part of a composite column is shared in other properties, use this option to specify what columns are considered as owned by this property. This is useful when your composite property is nullable, but parts of it are not. */
500
+ ownColumns(...ownColumns) {
501
+ return new ManyToOneOptionsBuilder({ ...this['~options'], ownColumns });
502
+ }
503
+ /** Override the default database column name on the target entity (see {@doclink naming-strategy | Naming Strategy}). This option is only for simple properties represented by a single column. */
504
+ referenceColumnName(referenceColumnName) {
505
+ return new ManyToOneOptionsBuilder({ ...this['~options'], referenceColumnName });
506
+ }
507
+ /** Override the default database column name on the target entity (see {@doclink naming-strategy | Naming Strategy}). This option is suitable for composite keys, where one property is represented by multiple columns. */
508
+ referencedColumnNames(...referencedColumnNames) {
509
+ return new ManyToOneOptionsBuilder({ ...this['~options'], referencedColumnNames });
510
+ }
511
+ /** What to do when the target entity gets deleted. */
512
+ deleteRule(deleteRule) {
513
+ return new ManyToOneOptionsBuilder({ ...this['~options'], deleteRule });
514
+ }
515
+ /** What to do when the reference to the target entity gets updated. */
516
+ updateRule(updateRule) {
517
+ return new ManyToOneOptionsBuilder({ ...this['~options'], updateRule });
518
+ }
519
+ /** Set the constraint type. Immediate constraints are checked for each statement, while deferred ones are only checked at the end of the transaction. Only for postgres unique constraints. */
520
+ deferMode(deferMode) {
521
+ return new ManyToOneOptionsBuilder({ ...this['~options'], deferMode });
522
+ }
523
+ }
524
+ /** @internal */
525
+ export class OneToManyOptionsBuilder extends ReferenceOptionsBuilder {
526
+ constructor(options) {
527
+ super(options);
528
+ this['~options'] = options;
529
+ }
530
+ /** Remove the entity when it gets disconnected from the relationship (see {@doclink cascading | Cascading}). */
531
+ orphanRemoval(orphanRemoval = true) {
532
+ return new OneToManyOptionsBuilder({ ...this['~options'], orphanRemoval });
533
+ }
534
+ /** Set default ordering. */
535
+ orderBy(orderBy) {
536
+ return new OneToManyOptionsBuilder({ ...this['~options'], orderBy });
537
+ }
538
+ /** Condition for {@doclink collections#declarative-partial-loading | Declarative partial loading}. */
539
+ where(where) {
540
+ return new OneToManyOptionsBuilder({ ...this['~options'], where });
541
+ }
542
+ /** Override the default database column name on the owning side (see {@doclink naming-strategy | Naming Strategy}). This option is only for simple properties represented by a single column. */
543
+ joinColumn(joinColumn) {
544
+ return new OneToManyOptionsBuilder({ ...this['~options'], joinColumn });
545
+ }
546
+ /** Override the default database column name on the owning side (see {@doclink naming-strategy | Naming Strategy}). This option is suitable for composite keys, where one property is represented by multiple columns. */
547
+ joinColumns(...joinColumns) {
548
+ return new OneToManyOptionsBuilder({ ...this['~options'], joinColumns });
549
+ }
550
+ /** Override the default database column name on the inverse side (see {@doclink naming-strategy | Naming Strategy}). This option is only for simple properties represented by a single column. */
551
+ inverseJoinColumn(inverseJoinColumn) {
552
+ return new OneToManyOptionsBuilder({ ...this['~options'], inverseJoinColumn });
553
+ }
554
+ /** Override the default database column name on the inverse side (see {@doclink naming-strategy | Naming Strategy}). This option is suitable for composite keys, where one property is represented by multiple columns. */
555
+ inverseJoinColumns(...inverseJoinColumns) {
556
+ return new OneToManyOptionsBuilder({ ...this['~options'], inverseJoinColumns });
557
+ }
558
+ /** Override the default database column name on the target entity (see {@doclink naming-strategy | Naming Strategy}). This option is only for simple properties represented by a single column. */
559
+ referenceColumnName(referenceColumnName) {
560
+ return new OneToManyOptionsBuilder({ ...this['~options'], referenceColumnName });
561
+ }
562
+ /** Override the default database column name on the target entity (see {@doclink naming-strategy | Naming Strategy}). This option is suitable for composite keys, where one property is represented by multiple columns. */
563
+ referencedColumnNames(...referencedColumnNames) {
564
+ return new OneToManyOptionsBuilder({ ...this['~options'], referencedColumnNames });
565
+ }
566
+ }
567
+ /** @internal */
568
+ export class OneToManyOptionsBuilderOnlyMappedBy {
569
+ constructor(options) {
570
+ this['~options'] = options;
571
+ }
572
+ /** Point to the owning side property name. */
573
+ mappedBy(mappedBy) {
574
+ return new OneToManyOptionsBuilder({ ...this['~options'], mappedBy });
575
+ }
576
+ }
577
+ /** @internal */
578
+ export class OneToOneOptionsBuilder extends ReferenceOptionsBuilder {
579
+ constructor(options) {
580
+ super(options);
581
+ this['~options'] = options;
582
+ }
583
+ /** Set this side as owning. Owning side is where the foreign key is defined. This option is not required if you use `inversedBy` or `mappedBy` to distinguish owning and inverse side. */
584
+ owner(owner = true) {
585
+ return new OneToOneOptionsBuilder({ ...this['~options'], owner });
586
+ }
587
+ /** Point to the inverse side property name. */
588
+ inversedBy(inversedBy) {
589
+ return new OneToOneOptionsBuilder({ ...this['~options'], inversedBy });
590
+ }
591
+ /** Wrap the entity in {@apilink Reference} wrapper. */
592
+ ref(ref = true) {
593
+ return new OneToOneOptionsBuilder({ ...this['~options'], ref });
594
+ }
595
+ /** Use this relation as a primary key. */
596
+ primary(primary = true) {
597
+ return new OneToOneOptionsBuilder({ ...this['~options'], primary });
598
+ }
599
+ /** Map this relation to the primary key value instead of an entity. */
600
+ mapToPk(mapToPk = true) {
601
+ return new OneToOneOptionsBuilder({ ...this['~options'], mapToPk });
602
+ }
603
+ /** When a part of a composite column is shared in other properties, use this option to specify what columns are considered as owned by this property. This is useful when your composite property is nullable, but parts of it are not. */
604
+ ownColumns(...ownColumns) {
605
+ return new OneToOneOptionsBuilder({ ...this['~options'], ownColumns });
606
+ }
607
+ /** What to do when the target entity gets deleted. */
608
+ deleteRule(deleteRule) {
609
+ return new OneToOneOptionsBuilder({ ...this['~options'], deleteRule });
610
+ }
611
+ /** What to do when the reference to the target entity gets updated. */
612
+ updateRule(updateRule) {
613
+ return new OneToOneOptionsBuilder({ ...this['~options'], updateRule });
614
+ }
615
+ /** Set the constraint type. Immediate constraints are checked for each statement, while deferred ones are only checked at the end of the transaction. Only for postgres unique constraints. */
616
+ deferMode(deferMode) {
617
+ return new OneToOneOptionsBuilder({ ...this['~options'], deferMode });
618
+ }
619
+ }
620
+ function createPropertyBuilders(options) {
621
+ return Object.fromEntries(Object.entries(options).map(([key, value]) => [key, () => new PropertyOptionsBuilder({ type: value })]));
622
+ }
623
+ const propertyBuilders = {
624
+ ...createPropertyBuilders(types),
625
+ json: () => new PropertyOptionsBuilder({ type: types.json }),
626
+ formula: (formula) => new PropertyOptionsBuilder({ formula }),
627
+ type: (type) => new PropertyOptionsBuilder({ type }),
628
+ enum: (items) => new EnumOptionsBuilder({
629
+ enum: true,
630
+ items,
631
+ }),
632
+ embedded: (target) => new EmbeddedOptionsBuilder({
633
+ entity: () => target,
634
+ kind: 'embedded',
635
+ }),
636
+ manyToMany: (target) => new ManyToManyOptionsBuilder({
637
+ entity: () => target,
638
+ kind: 'm:n',
639
+ }),
640
+ manyToOne: (target) => new ManyToOneOptionsBuilder({
641
+ entity: () => target,
642
+ kind: 'm:1',
643
+ ref: true,
644
+ }),
645
+ oneToMany: (target) => new OneToManyOptionsBuilderOnlyMappedBy({
646
+ entity: () => target,
647
+ kind: '1:m',
648
+ }),
649
+ oneToOne: (target) => new OneToOneOptionsBuilder({
650
+ entity: () => target,
651
+ kind: '1:1',
652
+ ref: true,
653
+ }),
654
+ };
655
+ function getBuilderOptions(builder) {
656
+ return '~options' in builder ? builder['~options'] : builder;
657
+ }
658
+ export function defineEntity(meta) {
659
+ const { properties: propertiesOrGetter, ...options } = meta;
660
+ const propertyOptions = typeof propertiesOrGetter === 'function' ? propertiesOrGetter(propertyBuilders) : propertiesOrGetter;
661
+ const properties = {};
662
+ for (const [key, builder] of Object.entries(propertyOptions)) {
663
+ const values = new Map();
664
+ if (typeof builder === 'function') {
665
+ Object.defineProperty(properties, key, {
666
+ get: () => {
667
+ let value = values.get(key);
668
+ if (value === undefined) {
669
+ value = getBuilderOptions(builder());
670
+ values.set(key, value);
671
+ }
672
+ return value;
673
+ },
674
+ set: (value) => {
675
+ values.set(key, value);
676
+ },
677
+ enumerable: true,
678
+ });
679
+ }
680
+ else {
681
+ Object.defineProperty(properties, key, {
682
+ value: getBuilderOptions(builder),
683
+ writable: true,
684
+ enumerable: true,
685
+ });
686
+ }
687
+ }
688
+ return new EntitySchema({ properties, ...options });
689
+ }
690
+ defineEntity.properties = propertyBuilders;