@mikro-orm/core 7.0.0-dev.34 → 7.0.0-dev.35

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.
@@ -1,17 +1,23 @@
1
1
  import { types } from '../types/index.js';
2
2
  import { EntitySchema } from '../metadata/EntitySchema.js';
3
3
  /** @internal */
4
- export class PropertyOptionsBuilder {
4
+ export class UniversalPropertyOptionsBuilder {
5
5
  '~options';
6
6
  '~type';
7
7
  constructor(options) {
8
8
  this['~options'] = options;
9
9
  }
10
+ assignOptions(options) {
11
+ return new UniversalPropertyOptionsBuilder({ ...this['~options'], ...options });
12
+ }
13
+ $type() {
14
+ return this.assignOptions({});
15
+ }
10
16
  /**
11
17
  * Alias for `fieldName`.
12
18
  */
13
19
  name(name) {
14
- return new PropertyOptionsBuilder({ ...this['~options'], name });
20
+ return this.assignOptions({ name });
15
21
  }
16
22
  /**
17
23
  * Specify database column name for this property.
@@ -19,7 +25,7 @@ export class PropertyOptionsBuilder {
19
25
  * @see https://mikro-orm.io/docs/naming-strategy
20
26
  */
21
27
  fieldName(fieldName) {
22
- return new PropertyOptionsBuilder({ ...this['~options'], fieldName });
28
+ return this.assignOptions({ fieldName });
23
29
  }
24
30
  /**
25
31
  * Specify database column names for this property.
@@ -28,19 +34,19 @@ export class PropertyOptionsBuilder {
28
34
  * @see https://mikro-orm.io/docs/naming-strategy
29
35
  */
30
36
  fieldNames(...fieldNames) {
31
- return new PropertyOptionsBuilder({ ...this['~options'], fieldNames });
37
+ return this.assignOptions({ fieldNames });
32
38
  }
33
39
  /**
34
40
  * 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
41
  */
36
42
  columnType(columnType) {
37
- return new PropertyOptionsBuilder({ ...this['~options'], columnType });
43
+ return this.assignOptions({ columnType });
38
44
  }
39
45
  /**
40
46
  * 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
47
  */
42
48
  columnTypes(...columnTypes) {
43
- return new PropertyOptionsBuilder({ ...this['~options'], columnTypes });
49
+ return this.assignOptions({ columnTypes });
44
50
  }
45
51
  /**
46
52
  * Explicitly specify the runtime type.
@@ -49,72 +55,72 @@ export class PropertyOptionsBuilder {
49
55
  * @see https://mikro-orm.io/docs/custom-types
50
56
  */
51
57
  type(type) {
52
- return new PropertyOptionsBuilder({ ...this['~options'], type });
58
+ return this.assignOptions({ type });
53
59
  }
54
60
  /**
55
61
  * 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
62
  * 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
63
  */
58
64
  runtimeType(runtimeType) {
59
- return new PropertyOptionsBuilder({ ...this['~options'], runtimeType });
65
+ return this.assignOptions({ runtimeType });
60
66
  }
61
67
  /**
62
68
  * 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
69
  */
64
70
  length(length) {
65
- return new PropertyOptionsBuilder({ ...this['~options'], length });
71
+ return this.assignOptions({ length });
66
72
  }
67
73
  /**
68
74
  * Set precision of database column to represent the number of significant digits. (SQL only)
69
75
  */
70
76
  precision(precision) {
71
- return new PropertyOptionsBuilder({ ...this['~options'], precision });
77
+ return this.assignOptions({ precision });
72
78
  }
73
79
  /**
74
80
  * Set scale of database column to represents the number of digits after the decimal point. (SQL only)
75
81
  */
76
82
  scale(scale) {
77
- return new PropertyOptionsBuilder({ ...this['~options'], scale });
83
+ return this.assignOptions({ scale });
78
84
  }
79
85
  /**
80
86
  * Explicitly specify the auto increment of the primary key.
81
87
  */
82
88
  autoincrement(autoincrement = true) {
83
- return new PropertyOptionsBuilder({ ...this['~options'], autoincrement });
89
+ return this.assignOptions({ autoincrement });
84
90
  }
85
91
  /**
86
92
  * Add the property to the `returning` statement.
87
93
  */
88
94
  returning(returning = true) {
89
- return new PropertyOptionsBuilder({ ...this['~options'], returning });
95
+ return this.assignOptions({ returning });
90
96
  }
91
97
  /**
92
98
  * Automatically set the property value when entity gets created, executed during flush operation.
93
99
  * @param entity
94
100
  */
95
101
  onCreate(onCreate) {
96
- return new PropertyOptionsBuilder({ ...this['~options'], onCreate });
102
+ return this.assignOptions({ onCreate });
97
103
  }
98
104
  /**
99
105
  * Automatically update the property value every time entity gets updated, executed during flush operation.
100
106
  * @param entity
101
107
  */
102
108
  onUpdate(onUpdate) {
103
- return new PropertyOptionsBuilder({ ...this['~options'], onUpdate });
109
+ return this.assignOptions({ onUpdate });
104
110
  }
105
111
  /**
106
112
  * Specify default column value for {@link https://mikro-orm.io/docs/schema-generator Schema Generator}.
107
113
  * This is a runtime value, assignable to the entity property. (SQL only)
108
114
  */
109
115
  default(defaultValue) {
110
- return new PropertyOptionsBuilder({ ...this['~options'], default: defaultValue });
116
+ return this.assignOptions({ default: defaultValue });
111
117
  }
112
118
  /**
113
119
  * Specify SQL functions for {@link https://mikro-orm.io/docs/schema-generator Schema Generator}. (SQL only)
114
120
  * Since v4 you should use defaultRaw for SQL functions. e.g. now()
115
121
  */
116
122
  defaultRaw(defaultRaw) {
117
- return new PropertyOptionsBuilder({ ...this['~options'], defaultRaw });
123
+ return this.assignOptions({ defaultRaw });
118
124
  }
119
125
  /**
120
126
  * Set to map some SQL snippet for the entity.
@@ -122,43 +128,43 @@ export class PropertyOptionsBuilder {
122
128
  * @see https://mikro-orm.io/docs/defining-entities#formulas Formulas
123
129
  */
124
130
  formula(formula) {
125
- return new PropertyOptionsBuilder({ ...this['~options'], formula });
131
+ return this.assignOptions({ formula });
126
132
  }
127
133
  /**
128
134
  * For generated columns. This will be appended to the column type after the `generated always` clause.
129
135
  */
130
136
  generated(generated) {
131
- return new PropertyOptionsBuilder({ ...this['~options'], generated });
137
+ return this.assignOptions({ generated });
132
138
  }
133
139
  /**
134
140
  * Set column as nullable for {@link https://mikro-orm.io/docs/schema-generator Schema Generator}.
135
141
  */
136
142
  nullable(nullable = true) {
137
- return new PropertyOptionsBuilder({ ...this['~options'], nullable });
143
+ return this.assignOptions({ nullable });
138
144
  }
139
145
  /**
140
146
  * Set column as unsigned for {@link https://mikro-orm.io/docs/schema-generator Schema Generator}. (SQL only)
141
147
  */
142
148
  unsigned(unsigned = true) {
143
- return new PropertyOptionsBuilder({ ...this['~options'], unsigned });
149
+ return this.assignOptions({ unsigned });
144
150
  }
145
151
  /**
146
152
  * Set false to define {@link https://mikro-orm.io/docs/serializing#shadow-properties Shadow Property}.
147
153
  */
148
154
  persist(persist = true) {
149
- return new PropertyOptionsBuilder({ ...this['~options'], persist });
155
+ return this.assignOptions({ persist });
150
156
  }
151
157
  /**
152
158
  * Set false to disable hydration of this property. Useful for persisted getters.
153
159
  */
154
160
  hydrate(hydrate = true) {
155
- return new PropertyOptionsBuilder({ ...this['~options'], hydrate });
161
+ return this.assignOptions({ hydrate });
156
162
  }
157
163
  /**
158
164
  * 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
165
  */
160
166
  ref(ref = true) {
161
- return new PropertyOptionsBuilder({ ...this['~options'], ref });
167
+ return this.assignOptions({ ref });
162
168
  }
163
169
  /**
164
170
  * Set false to disable change tracking on a property level.
@@ -166,37 +172,37 @@ export class PropertyOptionsBuilder {
166
172
  * @see https://mikro-orm.io/docs/unit-of-work#change-tracking-and-performance-considerations
167
173
  */
168
174
  trackChanges(trackChanges = true) {
169
- return new PropertyOptionsBuilder({ ...this['~options'], trackChanges });
175
+ return this.assignOptions({ trackChanges });
170
176
  }
171
177
  /**
172
178
  * Set to true to omit the property when {@link https://mikro-orm.io/docs/serializing Serializing}.
173
179
  */
174
180
  hidden(hidden = true) {
175
- return new PropertyOptionsBuilder({ ...this['~options'], hidden });
181
+ return this.assignOptions({ hidden });
176
182
  }
177
183
  /**
178
184
  * Set to true to enable {@link https://mikro-orm.io/docs/transactions#optimistic-locking Optimistic Locking} via version field. (SQL only)
179
185
  */
180
186
  version(version = true) {
181
- return new PropertyOptionsBuilder({ ...this['~options'], version });
187
+ return this.assignOptions({ version });
182
188
  }
183
189
  /**
184
190
  * Set to true to enable {@link https://mikro-orm.io/docs/transactions#optimistic-locking Optimistic Locking} via concurrency fields.
185
191
  */
186
192
  concurrencyCheck(concurrencyCheck = true) {
187
- return new PropertyOptionsBuilder({ ...this['~options'], concurrencyCheck });
193
+ return this.assignOptions({ concurrencyCheck });
188
194
  }
189
195
  /**
190
196
  * Explicitly specify index on a property.
191
197
  */
192
198
  index(index = true) {
193
- return new PropertyOptionsBuilder({ ...this['~options'], index });
199
+ return this.assignOptions({ index });
194
200
  }
195
201
  /**
196
202
  * Set column as unique for {@link https://mikro-orm.io/docs/schema-generator Schema Generator}. (SQL only)
197
203
  */
198
204
  unique(unique = true) {
199
- return new PropertyOptionsBuilder({ ...this['~options'], unique });
205
+ return this.assignOptions({ unique });
200
206
  }
201
207
  /**
202
208
  * Specify column with check constraints. (Postgres driver only)
@@ -204,7 +210,7 @@ export class PropertyOptionsBuilder {
204
210
  * @see https://mikro-orm.io/docs/defining-entities#check-constraints
205
211
  */
206
212
  check(check) {
207
- return new PropertyOptionsBuilder({ ...this['~options'], check });
213
+ return this.assignOptions({ check });
208
214
  }
209
215
  /**
210
216
  * Set to omit the property from the select clause for lazy loading.
@@ -212,7 +218,7 @@ export class PropertyOptionsBuilder {
212
218
  * @see https://mikro-orm.io/docs/defining-entities#lazy-scalar-properties
213
219
  */
214
220
  lazy(lazy = true, ref = true) {
215
- return new PropertyOptionsBuilder({ ...this['~options'], lazy, ref });
221
+ return this.assignOptions({ lazy, ref });
216
222
  }
217
223
  /**
218
224
  * Set true to define entity's unique primary key identifier.
@@ -220,7 +226,7 @@ export class PropertyOptionsBuilder {
220
226
  * @see https://mikro-orm.io/docs/decorators#primarykey
221
227
  */
222
228
  primary(primary = true) {
223
- return new PropertyOptionsBuilder({ ...this['~options'], primary });
229
+ return this.assignOptions({ primary });
224
230
  }
225
231
  /**
226
232
  * Set true to define the properties as setter. (virtual)
@@ -234,7 +240,7 @@ export class PropertyOptionsBuilder {
234
240
  * ```
235
241
  */
236
242
  setter(setter = true) {
237
- return new PropertyOptionsBuilder({ ...this['~options'], setter });
243
+ return this.assignOptions({ setter });
238
244
  }
239
245
  /**
240
246
  * Set true to define the properties as getter. (virtual)
@@ -248,7 +254,7 @@ export class PropertyOptionsBuilder {
248
254
  * ```
249
255
  */
250
256
  getter(getter = true) {
251
- return new PropertyOptionsBuilder({ ...this['~options'], getter });
257
+ return this.assignOptions({ getter });
252
258
  }
253
259
  /**
254
260
  * When defining a property over a method (not a getter, a regular function), you can use this option to point
@@ -263,7 +269,7 @@ export class PropertyOptionsBuilder {
263
269
  * ```
264
270
  */
265
271
  getterName(getterName) {
266
- return new PropertyOptionsBuilder({ ...this['~options'], getterName });
272
+ return this.assignOptions({ getterName });
267
273
  }
268
274
  /**
269
275
  * Set to define serialized primary key for MongoDB. (virtual)
@@ -272,7 +278,7 @@ export class PropertyOptionsBuilder {
272
278
  * @see https://mikro-orm.io/docs/decorators#serializedprimarykey
273
279
  */
274
280
  serializedPrimaryKey(serializedPrimaryKey = true) {
275
- return new PropertyOptionsBuilder({ ...this['~options'], serializedPrimaryKey });
281
+ return this.assignOptions({ serializedPrimaryKey });
276
282
  }
277
283
  /**
278
284
  * Set to use serialize property. Allow to specify a callback that will be used when serializing a property.
@@ -280,36 +286,36 @@ export class PropertyOptionsBuilder {
280
286
  * @see https://mikro-orm.io/docs/serializing#property-serializers
281
287
  */
282
288
  serializer(serializer) {
283
- return new PropertyOptionsBuilder({ ...this['~options'], serializer });
289
+ return this.assignOptions({ serializer });
284
290
  }
285
291
  /**
286
292
  * Specify name of key for the serialized value.
287
293
  */
288
294
  serializedName(serializedName) {
289
- return new PropertyOptionsBuilder({ ...this['~options'], serializedName });
295
+ return this.assignOptions({ serializedName });
290
296
  }
291
297
  /**
292
298
  * Specify serialization groups for `serialize()` calls. If a property does not specify any group, it will be included,
293
299
  * otherwise only properties with a matching group are included.
294
300
  */
295
301
  groups(...groups) {
296
- return new PropertyOptionsBuilder({ ...this['~options'], groups });
302
+ return this.assignOptions({ groups });
297
303
  }
298
304
  /**
299
305
  * Specify a custom order based on the values. (SQL only)
300
306
  */
301
307
  customOrder(...customOrder) {
302
- return new PropertyOptionsBuilder({ ...this['~options'], customOrder });
308
+ return this.assignOptions({ customOrder });
303
309
  }
304
310
  /**
305
311
  * Specify comment of column for {@link https://mikro-orm.io/docs/schema-generator Schema Generator}. (SQL only)
306
312
  */
307
313
  comment(comment) {
308
- return new PropertyOptionsBuilder({ ...this['~options'], comment });
314
+ return this.assignOptions({ comment });
309
315
  }
310
316
  /** mysql only */
311
317
  extra(extra) {
312
- return new PropertyOptionsBuilder({ ...this['~options'], extra });
318
+ return this.assignOptions({ extra });
313
319
  }
314
320
  /**
315
321
  * Set to avoid a perpetual diff from the {@link https://mikro-orm.io/docs/schema-generator Schema Generator} when columns are generated.
@@ -317,342 +323,172 @@ export class PropertyOptionsBuilder {
317
323
  * @see https://mikro-orm.io/docs/defining-entities#sql-generated-columns
318
324
  */
319
325
  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;
326
+ return this.assignOptions({ ignoreSchemaChanges });
331
327
  }
332
328
  array(array = true) {
333
- return new EnumOptionsBuilder({ ...this['~options'], array });
329
+ return this.assignOptions({ array });
334
330
  }
335
331
  /** for postgres, by default it uses text column with check constraint */
336
332
  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;
333
+ return this.assignOptions({ nativeEnumName });
345
334
  }
346
335
  prefix(prefix) {
347
- return new EmbeddedOptionsBuilder({ ...this['~options'], prefix });
336
+ return this.assignOptions({ prefix });
348
337
  }
349
338
  prefixMode(prefixMode) {
350
- return new EmbeddedOptionsBuilder({ ...this['~options'], prefixMode });
339
+ return this.assignOptions({ prefixMode });
351
340
  }
352
341
  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;
342
+ return this.assignOptions({ object });
364
343
  }
365
344
  /** Set what actions on owning entity should be cascaded to the relationship. Defaults to [Cascade.PERSIST, Cascade.MERGE] (see {@doclink cascading}). */
366
345
  cascade(...cascade) {
367
- return new ReferenceOptionsBuilder({ ...this['~options'], cascade });
346
+ return this.assignOptions({ cascade });
368
347
  }
369
348
  /** Always load the relationship. Discouraged for use with to-many relations for performance reasons. */
370
349
  eager(eager = true) {
371
- return new ReferenceOptionsBuilder({ ...this['~options'], eager });
350
+ return this.assignOptions({ eager });
372
351
  }
373
352
  /** Override the default loading strategy for this property. This option has precedence over the global `loadStrategy`, but can be overridden by `FindOptions.strategy`. */
374
353
  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;
354
+ return this.assignOptions({ strategy });
399
355
  }
400
356
  /** 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
357
  owner(owner = true) {
402
- return new ManyToManyOptionsBuilder({ ...this['~options'], owner });
358
+ return this.assignOptions({ owner });
403
359
  }
404
360
  /** Point to the inverse side property name. */
405
361
  inversedBy(inversedBy) {
406
- return new ManyToManyOptionsBuilder({ ...this['~options'], inversedBy });
362
+ return this.assignOptions({ inversedBy });
407
363
  }
408
364
  /** Point to the owning side property name. */
409
365
  mappedBy(mappedBy) {
410
- return new ManyToManyOptionsBuilder({ ...this['~options'], mappedBy });
366
+ return this.assignOptions({ mappedBy });
411
367
  }
412
368
  /** Condition for {@doclink collections#declarative-partial-loading | Declarative partial loading}. */
413
369
  where(...where) {
414
- return new ManyToManyOptionsBuilder({ ...this['~options'], where: where });
370
+ return this.assignOptions({ where });
415
371
  }
416
372
  /** Set default ordering. */
417
373
  orderBy(...orderBy) {
418
- return new ManyToManyOptionsBuilder({ ...this['~options'], orderBy });
374
+ return this.assignOptions({ orderBy });
419
375
  }
420
376
  /** Force stable insertion order of items in the collection (see {@doclink collections | Collections}). */
421
377
  fixedOrder(fixedOrder = true) {
422
- return new ManyToManyOptionsBuilder({ ...this['~options'], fixedOrder });
378
+ return this.assignOptions({ fixedOrder });
423
379
  }
424
380
  /** Override default order column name (`id`) for fixed ordering. */
425
381
  fixedOrderColumn(fixedOrderColumn) {
426
- return new ManyToManyOptionsBuilder({ ...this['~options'], fixedOrderColumn });
382
+ return this.assignOptions({ fixedOrderColumn });
427
383
  }
428
384
  /** Override default name for pivot table (see {@doclink naming-strategy | Naming Strategy}). */
429
385
  pivotTable(pivotTable) {
430
- return new ManyToManyOptionsBuilder({ ...this['~options'], pivotTable });
386
+ return this.assignOptions({ pivotTable });
431
387
  }
432
388
  /** Set pivot entity for this relation (see {@doclink collections#custom-pivot-table-entity | Custom pivot table entity}). */
433
389
  pivotEntity(pivotEntity) {
434
- return new ManyToManyOptionsBuilder({ ...this['~options'], pivotEntity });
390
+ return this.assignOptions({ pivotEntity });
435
391
  }
436
392
  /** 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
393
  joinColumn(joinColumn) {
438
- return new ManyToManyOptionsBuilder({ ...this['~options'], joinColumn });
394
+ return this.assignOptions({ joinColumn });
439
395
  }
440
396
  /** 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
397
  joinColumns(...joinColumns) {
442
- return new ManyToManyOptionsBuilder({ ...this['~options'], joinColumns });
398
+ return this.assignOptions({ joinColumns });
443
399
  }
444
400
  /** 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
401
  inverseJoinColumn(inverseJoinColumn) {
446
- return new ManyToManyOptionsBuilder({ ...this['~options'], inverseJoinColumn });
402
+ return this.assignOptions({ inverseJoinColumn });
447
403
  }
448
404
  /** 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
405
  inverseJoinColumns(...inverseJoinColumns) {
450
- return new ManyToManyOptionsBuilder({ ...this['~options'], inverseJoinColumns });
406
+ return this.assignOptions({ inverseJoinColumns });
451
407
  }
452
408
  /** 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
409
  referenceColumnName(referenceColumnName) {
454
- return new ManyToManyOptionsBuilder({ ...this['~options'], referenceColumnName });
410
+ return this.assignOptions({ referenceColumnName });
455
411
  }
456
412
  /** 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
413
  referencedColumnNames(...referencedColumnNames) {
458
- return new ManyToManyOptionsBuilder({ ...this['~options'], referencedColumnNames });
414
+ return this.assignOptions({ referencedColumnNames });
459
415
  }
460
416
  /** What to do when the target entity gets deleted. */
461
417
  deleteRule(deleteRule) {
462
- return new ManyToManyOptionsBuilder({ ...this['~options'], deleteRule });
418
+ return this.assignOptions({ deleteRule });
463
419
  }
464
420
  /** What to do when the reference to the target entity gets updated. */
465
421
  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 });
422
+ return this.assignOptions({ updateRule });
486
423
  }
487
424
  /** Map this relation to the primary key value instead of an entity. */
488
425
  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 });
426
+ return this.assignOptions({ mapToPk });
494
427
  }
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 });
428
+ /** 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. */
429
+ deferMode(deferMode) {
430
+ return this.assignOptions({ deferMode });
498
431
  }
499
432
  /** 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
433
  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 });
434
+ return this.assignOptions({ ownColumns });
510
435
  }
511
- /** What to do when the target entity gets deleted. */
512
- deleteRule(deleteRule) {
513
- return new ManyToOneOptionsBuilder({ ...this['~options'], deleteRule });
436
+ /** Enable/disable foreign key constraint creation on this relation */
437
+ createForeignKeyConstraint(createForeignKeyConstraint = true) {
438
+ return this.assignOptions({ createForeignKeyConstraint });
514
439
  }
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;
440
+ /** Set a custom foreign key constraint name, overriding NamingStrategy.indexName(). */
441
+ foreignKeyName(foreignKeyName) {
442
+ return this.assignOptions({ foreignKeyName });
529
443
  }
530
444
  /** Remove the entity when it gets disconnected from the relationship (see {@doclink cascading | Cascading}). */
531
445
  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 });
446
+ return this.assignOptions({ orphanRemoval });
565
447
  }
566
448
  }
567
449
  /** @internal */
568
- export class OneToManyOptionsBuilderOnlyMappedBy {
569
- constructor(options) {
570
- this['~options'] = options;
571
- }
450
+ export class OneToManyOptionsBuilderOnlyMappedBy extends UniversalPropertyOptionsBuilder {
572
451
  /** Point to the owning side property name. */
573
452
  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 });
453
+ return new UniversalPropertyOptionsBuilder({ ...this['~options'], mappedBy });
618
454
  }
619
455
  }
620
456
  function createPropertyBuilders(options) {
621
- return Object.fromEntries(Object.entries(options).map(([key, value]) => [key, () => new PropertyOptionsBuilder({ type: value })]));
457
+ return Object.fromEntries(Object.entries(options).map(([key, value]) => [key, () => new UniversalPropertyOptionsBuilder({ type: value })]));
622
458
  }
623
459
  const propertyBuilders = {
624
460
  ...createPropertyBuilders(types),
625
- bigint: (mode) => new PropertyOptionsBuilder({ type: new types.bigint(mode) }),
626
- array: (toJsValue = i => i, toDbValue = i => i) => new PropertyOptionsBuilder({ type: new types.array(toJsValue, toDbValue) }),
627
- decimal: (mode) => new PropertyOptionsBuilder({ type: new types.decimal(mode) }),
628
- json: () => new PropertyOptionsBuilder({ type: types.json }),
629
- formula: (formula) => new PropertyOptionsBuilder({ formula }),
630
- type: (type) => new PropertyOptionsBuilder({ type }),
631
- enum: (items) => new EnumOptionsBuilder({
461
+ bigint: (mode) => new UniversalPropertyOptionsBuilder({ type: new types.bigint(mode) }),
462
+ array: (toJsValue = i => i, toDbValue = i => i) => new UniversalPropertyOptionsBuilder({ type: new types.array(toJsValue, toDbValue) }),
463
+ decimal: (mode) => new UniversalPropertyOptionsBuilder({ type: new types.decimal(mode) }),
464
+ json: () => new UniversalPropertyOptionsBuilder({ type: types.json }),
465
+ formula: (formula) => new UniversalPropertyOptionsBuilder({ formula }),
466
+ datetime: (length) => new UniversalPropertyOptionsBuilder({ type: types.datetime, length }),
467
+ time: (length) => new UniversalPropertyOptionsBuilder({ type: types.time, length }),
468
+ type: (type) => new UniversalPropertyOptionsBuilder({ type }),
469
+ enum: (items) => new UniversalPropertyOptionsBuilder({
632
470
  enum: true,
633
471
  items,
634
472
  }),
635
- embedded: (target) => new EmbeddedOptionsBuilder({
473
+ embedded: (target) => new UniversalPropertyOptionsBuilder({
636
474
  entity: () => target,
637
475
  kind: 'embedded',
638
476
  }),
639
- manyToMany: (target) => new ManyToManyOptionsBuilder({
477
+ manyToMany: (target) => new UniversalPropertyOptionsBuilder({
640
478
  entity: () => target,
641
479
  kind: 'm:n',
642
480
  }),
643
- manyToOne: (target) => new ManyToOneOptionsBuilder({
481
+ manyToOne: (target) => new UniversalPropertyOptionsBuilder({
644
482
  entity: () => target,
645
483
  kind: 'm:1',
646
- ref: true,
647
484
  }),
648
485
  oneToMany: (target) => new OneToManyOptionsBuilderOnlyMappedBy({
649
486
  entity: () => target,
650
487
  kind: '1:m',
651
488
  }),
652
- oneToOne: (target) => new OneToOneOptionsBuilder({
489
+ oneToOne: (target) => new UniversalPropertyOptionsBuilder({
653
490
  entity: () => target,
654
491
  kind: '1:1',
655
- ref: true,
656
492
  }),
657
493
  };
658
494
  function getBuilderOptions(builder) {
@@ -662,8 +498,8 @@ export function defineEntity(meta) {
662
498
  const { properties: propertiesOrGetter, ...options } = meta;
663
499
  const propertyOptions = typeof propertiesOrGetter === 'function' ? propertiesOrGetter(propertyBuilders) : propertiesOrGetter;
664
500
  const properties = {};
501
+ const values = new Map();
665
502
  for (const [key, builder] of Object.entries(propertyOptions)) {
666
- const values = new Map();
667
503
  if (typeof builder === 'function') {
668
504
  Object.defineProperty(properties, key, {
669
505
  get: () => {
@@ -691,3 +527,4 @@ export function defineEntity(meta) {
691
527
  return new EntitySchema({ properties, ...options });
692
528
  }
693
529
  defineEntity.properties = propertyBuilders;
530
+ export { propertyBuilders as p };