/
typings.ts
1151 lines (1009 loc) · 42.8 KB
/
typings.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import type { Transaction } from './connections';
import {
type Cascade,
type EventType,
type LoadStrategy,
type QueryOrderMap,
ReferenceKind,
} from './enums';
import {
type AssignOptions,
type Collection,
type EntityFactory,
EntityHelper,
type EntityIdentifier,
type EntityLoaderOptions,
type EntityRepository,
Reference,
type ScalarReference,
} from './entity';
import type { SerializationContext, SerializeOptions } from './serialization';
import type { EntitySchema, MetadataStorage } from './metadata';
import type { Type, types } from './types';
import type { Platform } from './platforms';
import type { Configuration } from './utils';
import { Utils } from './utils/Utils';
import { EntityComparator } from './utils/EntityComparator';
import type { EntityManager } from './EntityManager';
import type { EventSubscriber } from './events';
import type { FindOneOptions, FindOptions } from './drivers';
export type Constructor<T = unknown> = new (...args: any[]) => T;
export type Dictionary<T = any> = { [k: string]: T };
export type EntityKey<T = unknown> = string & keyof { [K in keyof T as CleanKeys<T, K>]?: unknown };
export type EntityValue<T> = T[EntityKey<T>];
export type FilterKey<T> = keyof FilterQuery<T>;
export type AsyncFunction<R = any, T = Dictionary> = (args: T) => Promise<T>;
export type Compute<T> = { [K in keyof T]: T[K] } & {};
type InternalKeys = 'EntityRepositoryType' | 'PrimaryKeyProp' | 'OptionalProps' | 'EagerProps' | 'HiddenProps' | '__selectedType' | '__loadedType';
export type CleanKeys<T, K extends keyof T> = T[K] extends Function ? never : (K extends symbol | InternalKeys ? never : K);
export type Cast<T, R> = T extends R ? T : R;
export type IsUnknown<T> = T extends unknown ? unknown extends T ? true : never : never;
export type IsAny<T> = 0 extends (1 & T) ? true : false;
export type IsNever<T, True = true, False = false> = [T] extends [never] ? True : False;
export type NoInfer<T> = [T][T extends any ? 0 : never];
export type MaybePromise<T> = T | Promise<T>;
export type DeepPartial<T> = T & {
[P in keyof T]?: T[P] extends (infer U)[]
? DeepPartial<U>[]
: T[P] extends Readonly<infer U>[]
? Readonly<DeepPartial<U>>[]
: DeepPartial<T[P]>
};
export const EntityRepositoryType = Symbol('EntityRepositoryType');
export const PrimaryKeyProp = Symbol('PrimaryKeyProp');
export const OptionalProps = Symbol('OptionalProps');
export const EagerProps = Symbol('EagerProps');
export const HiddenProps = Symbol('HiddenProps');
export const Config = Symbol('Config');
// eslint-disable-next-line @typescript-eslint/naming-convention
declare const __optional: unique symbol;
// eslint-disable-next-line @typescript-eslint/naming-convention
declare const __hidden: unique symbol;
// eslint-disable-next-line @typescript-eslint/naming-convention
declare const __config: unique symbol;
export type Opt<T = unknown> = T & { [__optional]?: 1 };
export type Hidden<T = unknown> = T & { [__hidden]?: 1 };
export type DefineConfig<T extends TypeConfig> = T & { [__config]?: 1 };
export type CleanTypeConfig<T> = Compute<Pick<T, Extract<keyof T, keyof TypeConfig>>>;
export interface TypeConfig {
forceObject?: boolean;
}
export type UnwrapPrimary<T> = T extends Scalar
? T
: T extends Reference<infer U>
? Primary<U>
: Primary<T>;
type PrimaryPropToType<T, Keys extends (keyof T)[]> = {
[Index in keyof Keys]: UnwrapPrimary<T[Keys[Index]]>;
};
type ReadonlyPrimary<T> = T extends any[] ? Readonly<T> : T;
export type Primary<T> =
IsAny<T> extends true
? any
: T extends { [PrimaryKeyProp]?: infer PK }
? (PK extends keyof T ? ReadonlyPrimary<UnwrapPrimary<T[PK]>> : (PK extends (keyof T)[] ? ReadonlyPrimary<PrimaryPropToType<T, PK>> : PK))
: T extends { _id?: infer PK }
? ReadonlyPrimary<PK> | string
: T extends { uuid?: infer PK }
? ReadonlyPrimary<PK>
: T extends { id?: infer PK }
? ReadonlyPrimary<PK>
: T;
export type PrimaryProperty<T> = T extends { [PrimaryKeyProp]?: infer PK }
? (PK extends keyof T ? PK : (PK extends any[] ? PK[number] : never)) : T extends { _id?: any }
? (T extends { id?: any } ? 'id' | '_id' : '_id') : T extends { uuid?: any }
? 'uuid' : T extends { id?: any }
? 'id' : never;
export type IPrimaryKeyValue = number | string | bigint | Date | { toHexString(): string };
export type IPrimaryKey<T extends IPrimaryKeyValue = IPrimaryKeyValue> = T;
export type Scalar = boolean | number | string | bigint | symbol | Date | RegExp | Uint8Array | { toHexString(): string };
export type ExpandScalar<T> = null | (T extends string
? T | RegExp
: T extends Date
? Date | string
: T extends bigint
? bigint | string | number
: T);
export type OperatorMap<T> = {
$and?: ExpandQuery<T>[];
$or?: ExpandQuery<T>[];
$eq?: ExpandScalar<T> | ExpandScalar<T>[];
$ne?: ExpandScalar<T>;
$in?: ExpandScalar<T>[];
$nin?: ExpandScalar<T>[];
$not?: ExpandQuery<T>;
$none?: ExpandQuery<T>;
$some?: ExpandQuery<T>;
$every?: ExpandQuery<T>;
$gt?: ExpandScalar<T>;
$gte?: ExpandScalar<T>;
$lt?: ExpandScalar<T>;
$lte?: ExpandScalar<T>;
$like?: string;
$re?: string;
$ilike?: string;
$fulltext?: string;
$overlap?: string[] | object;
$contains?: string[] | object;
$contained?: string[] | object;
$exists?: boolean;
};
export type FilterItemValue<T> = T | ExpandScalar<T> | Primary<T>;
export type FilterValue<T> = OperatorMap<FilterItemValue<T>> | FilterItemValue<T> | FilterItemValue<T>[] | null;
export type FilterObject<T> = { -readonly [K in EntityKey<T>]?: ExpandQuery<ExpandProperty<T[K]>> | FilterValue<ExpandProperty<T[K]>> | null };
export type ExpandObject<T> = T extends object
? T extends Scalar
? never
: FilterObject<T>
: never;
export type ExpandQuery<T> = T extends object
? T extends Scalar
? never
: FilterQuery<T>
: FilterValue<T>;
export type EntityProps<T> = { -readonly [K in EntityKey<T>]?: T[K] };
export type ObjectQuery<T> = OperatorMap<T> & ExpandObject<T>;
export type FilterQuery<T> =
| ObjectQuery<T>
| NonNullable<ExpandScalar<Primary<T>>>
| NonNullable<EntityProps<T> & OperatorMap<T>>
| FilterQuery<T>[];
export type QBFilterQuery<T = any> = ObjectQuery<T> | Dictionary;
export interface IWrappedEntity<Entity extends object> {
isInitialized(): boolean;
isTouched(): boolean;
populated(populated?: boolean): void;
populate<Hint extends string = never>(populate: AutoPath<Entity, Hint>[] | false, options?: EntityLoaderOptions<Entity>): Promise<Loaded<Entity, Hint>>;
init<
Hint extends string = never,
Fields extends string = '*',
Excludes extends string = never,
>(options?: FindOneOptions<Entity, Hint, Fields, Excludes>): Promise<Loaded<Entity, Hint, Fields, Excludes> | null>;
toReference(): Ref<Entity> & LoadedReference<Loaded<Entity, AddEager<Entity>>>;
toObject(): EntityDTO<Entity>;
toObject(ignoreFields: never[]): EntityDTO<Entity>;
toObject<Ignored extends EntityKey<Entity>>(ignoreFields: Ignored[]): Omit<EntityDTO<Entity>, Ignored>;
toJSON(...args: any[]): EntityDTO<Entity>;
toPOJO(): EntityDTO<Entity>;
serialize<Hint extends string = never, Exclude extends string = never>(options?: SerializeOptions<Entity, Hint, Exclude>): EntityDTO<Loaded<Entity, Hint>>;
assign<
Naked extends FromEntityType<Entity> = FromEntityType<Entity>,
Data extends EntityData<Naked> | Partial<EntityDTO<Naked>> = EntityData<Naked> | Partial<EntityDTO<Naked>>,
>(data: Data & IsSubset<EntityData<Naked>, Data>, options?: AssignOptions): MergeSelected<Entity, Naked, keyof Data & string>;
getSchema(): string | undefined;
setSchema(schema?: string): void;
}
export interface IWrappedEntityInternal<Entity extends object> extends IWrappedEntity<Entity> {
hasPrimaryKey(): boolean;
getPrimaryKey(convertCustomTypes?: boolean): Primary<Entity> | null;
getPrimaryKeys(convertCustomTypes?: boolean): Primary<Entity>[] | null;
setPrimaryKey(val: Primary<Entity>): void;
getSerializedPrimaryKey(): string & keyof Entity;
__meta: EntityMetadata<Entity>;
__data: Dictionary;
__em?: any; // we cannot have `EntityManager` here as that causes a cycle
__platform: Platform;
__config: Configuration;
__factory: EntityFactory; // internal factory instance that has its own global fork
__hydrator: IHydrator;
__initialized: boolean;
__touched: boolean;
__originalEntityData?: EntityData<Entity>;
__loadedProperties: Set<string>;
__identifier?: EntityIdentifier;
__managed: boolean;
__processing: boolean;
__schema?: string;
__populated: boolean;
__onLoadFired: boolean;
__reference?: Ref<Entity>;
__pk?: Primary<Entity>;
__primaryKeys: Primary<Entity>[];
__serializationContext: {
root?: SerializationContext<Entity>;
populate?: PopulateOptions<Entity>[];
fields?: string[];
exclude?: string[];
};
}
export type AnyEntity<T = any> = Partial<T>;
export type EntityClass<T> = Function & { prototype: T };
export type EntityClassGroup<T> = { entity: EntityClass<T>; schema: EntityMetadata<T> | EntitySchema<T> };
export type EntityName<T> = string | EntityClass<T> | EntitySchema<T, any> | { name: string };
// we need to restrict the type in the generic argument, otherwise inference don't work, so we use two types here
export type GetRepository<Entity extends { [k: PropertyKey]: any }, Fallback> = Entity[typeof EntityRepositoryType] extends EntityRepository<Entity> | undefined ? NonNullable<Entity[typeof EntityRepositoryType]> : Fallback;
export type EntityDataPropValue<T> = T | Primary<T>;
type ExpandEntityProp<T> = T extends Record<string, any>
? { [K in keyof T as CleanKeys<T, K>]?: EntityDataProp<ExpandProperty<T[K]>> | EntityDataPropValue<ExpandProperty<T[K]>> | null } | EntityDataPropValue<ExpandProperty<T>>
: T;
type ExpandRequiredEntityProp<T, I> = T extends Record<string, any>
? ExpandRequiredEntityPropObject<T, I> | EntityDataPropValue<ExpandProperty<T>>
: T;
type ExpandRequiredEntityPropObject<T, I = never> = {
[K in keyof T as RequiredKeys<T, K, I>]: RequiredEntityDataProp<ExpandProperty<T[K]>, T> | EntityDataPropValue<ExpandProperty<T[K]>>;
} & {
[K in keyof T as OptionalKeys<T, K, I>]?: RequiredEntityDataProp<ExpandProperty<T[K]>, T> | EntityDataPropValue<ExpandProperty<T[K]>> | null | undefined;
};
export type EntityDataProp<T> = T extends Date
? string | Date
: T extends Scalar
? T
: T extends Reference<infer U>
? EntityDataNested<U>
: T extends ScalarReference<infer U>
? EntityDataProp<U>
: T extends Collection<infer U, any>
? U | U[] | EntityDataNested<U> | EntityDataNested<U>[]
: T extends readonly (infer U)[]
? U | U[] | EntityDataNested<U> | EntityDataNested<U>[]
: EntityDataNested<T>;
export type RequiredEntityDataProp<T, O> = T extends Date
? string | Date
: T extends Scalar
? T
: T extends Reference<infer U>
? RequiredEntityDataNested<U, O>
: T extends ScalarReference<infer U>
? RequiredEntityDataProp<U, O>
: T extends Collection<infer U, any>
? U | U[] | RequiredEntityDataNested<U, O> | RequiredEntityDataNested<U, O>[]
: T extends readonly (infer U)[]
? U | U[] | RequiredEntityDataNested<U, O> | RequiredEntityDataNested<U, O>[]
: RequiredEntityDataNested<T, O>;
export type EntityDataNested<T> = T extends undefined
? never
: T extends any[]
? Readonly<T>
: EntityData<T> | ExpandEntityProp<T>;
type EntityDataItem<T> = T | EntityDataProp<T> | null;
export type RequiredEntityDataNested<T, O> = T extends any[]
? Readonly<T>
: RequiredEntityData<T, O> | ExpandRequiredEntityProp<T, O>;
type ExplicitlyOptionalProps<T> = (T extends { [OptionalProps]?: infer K } ? K : never) | ({ [K in keyof T]: T[K] extends Opt ? K : never }[keyof T] & {});
type NullableKeys<T> = { [K in keyof T]: null extends T[K] ? K : never }[keyof T];
type ProbablyOptionalProps<T> = ExplicitlyOptionalProps<T> | 'id' | '_id' | 'uuid' | Defined<NullableKeys<T>>;
type IsOptional<T, K extends keyof T, I> = T[K] extends Collection<any, any>
? true
: ExtractType<T[K]> extends I
? true
: K extends ProbablyOptionalProps<T>
? true
: false;
type RequiredKeys<T, K extends keyof T, I> = IsOptional<T, K, I> extends false ? CleanKeys<T, K> : never;
type OptionalKeys<T, K extends keyof T, I> = IsOptional<T, K, I> extends false ? never : CleanKeys<T, K>;
export type EntityData<T> = { [K in EntityKey<T>]?: EntityDataItem<T[K]> };
export type RequiredEntityData<T, I = never> = {
[K in keyof T as RequiredKeys<T, K, I>]: T[K] | RequiredEntityDataProp<T[K], T> | Primary<T[K]>
} & {
[K in keyof T as OptionalKeys<T, K, I>]?: T[K] | RequiredEntityDataProp<T[K], T> | Primary<T[K]> | null
};
export type EntityDictionary<T> = EntityData<T> & Record<any, any>;
type ExtractEagerProps<T> = T extends { [EagerProps]?: infer PK } ? PK : never;
type Relation<T> = {
[P in keyof T as T[P] extends unknown[] | Record<string | number | symbol, unknown> ? P : never]?: T[P]
};
/** Identity type that can be used to get around issues with cycles in bidirectional relations. It will disable reflect-metadata inference. */
export type Rel<T> = T;
/** Alias for `ScalarReference` (see {@apilink Ref}). */
export type ScalarRef<T> = ScalarReference<T>;
/** Alias for `Reference<T> & { id: number }` (see {@apilink Ref}). */
export type EntityRef<T extends object> = true extends IsUnknown<PrimaryProperty<T>>
? Reference<T>
: IsAny<T> extends true
? Reference<T>
: ({ [K in PrimaryProperty<T> & keyof T]: T[K] } & Reference<T>);
/**
* Ref type represents a `Reference` instance, and adds the primary keys to its prototype automatically, so you can do
* `ref.id` instead of `ref.unwrap().id`. It resolves to either `ScalarRef` or `EntityRef`, based on the type argument.
*/
export type Ref<T> = IsAny<T> extends true
? Reference<T & object>
: T extends Scalar
? ScalarReference<T>
: EntityRef<T & object>;
type ExtractHiddenProps<T> = (T extends { [HiddenProps]?: infer K } ? K : never) | ({ [K in keyof T]: T[K] extends Hidden ? K : never }[keyof T] & {});
type ExcludeHidden<T, K extends keyof T> = K extends ExtractHiddenProps<T> ? never : K;
type EntityDTONested<T, C extends TypeConfig> = T extends undefined | null ? T : EntityDTO<T, C>;
type ExtractConfig<T> = T extends { [Config]?: infer K } ? (K & TypeConfig) : TypeConfig;
type PreferExplicitConfig<E, I> = IsNever<E, I, E>;
type PrimaryOrObject<T, U, C extends TypeConfig> =
PreferExplicitConfig<C, ExtractConfig<T>>['forceObject'] extends true
? { [K in PrimaryProperty<U> & keyof U]: U[K] }
: Primary<U>;
export type EntityDTOProp<E, T, C extends TypeConfig = never> = T extends Scalar
? T
: T extends LoadedReference<infer U>
? EntityDTONested<U, C>
: T extends Reference<infer U>
? PrimaryOrObject<E, U, C>
: T extends ScalarReference<infer U>
? U
: T extends { getItems(check?: boolean): infer U }
? (U extends readonly (infer V)[] ? EntityDTONested<V, C>[] : EntityDTONested<U, C>)
: T extends { $: infer U }
? (U extends readonly (infer V)[] ? EntityDTONested<V, C>[] : EntityDTONested<U, C>)
: T extends readonly (infer U)[]
? (T extends readonly [infer U, ...infer V] ? T : U[])
: T extends Relation<T>
? EntityDTONested<T, C>
: T;
export type EntityDTO<T, C extends TypeConfig = never> = { [K in EntityKey<T> as ExcludeHidden<T, K>]: EntityDTOProp<T, T[K], C> };
export type CheckCallback<T> = (columns: Record<keyof T, string>) => string;
export type GeneratedColumnCallback<T> = (columns: Record<keyof T, string>) => string;
export interface CheckConstraint<T = any> {
name?: string;
property?: string;
expression: string | CheckCallback<T>;
}
export type AnyString = string & {};
export interface EntityProperty<Owner = any, Target = any> {
name: EntityKey<Owner>;
entity: () => EntityName<Owner>;
type: keyof typeof types | AnyString;
runtimeType: 'number' | 'string' | 'boolean' | 'bigint' | 'Buffer' | 'Date';
targetMeta?: EntityMetadata<Target>;
columnTypes: string[];
generated?: string | GeneratedColumnCallback<Owner>;
customType?: Type<any>;
customTypes: (Type<any> | undefined)[];
hasConvertToJSValueSQL: boolean;
hasConvertToDatabaseValueSQL: boolean;
autoincrement?: boolean;
returning?: boolean;
primary?: boolean;
serializedPrimaryKey: boolean;
lazy?: boolean;
array?: boolean;
length?: number;
precision?: number;
scale?: number;
kind: ReferenceKind;
ref?: boolean;
fieldNames: string[];
fieldNameRaw?: string;
default?: string | number | boolean | null;
defaultRaw?: string;
formula?: (alias: string) => string;
prefix?: string | boolean;
embedded?: [EntityKey<Owner>, EntityKey<Owner>];
embeddedPath?: string[];
embeddable: EntityClass<Owner>;
embeddedProps: Dictionary<EntityProperty>;
discriminatorColumn?: string; // only for poly embeddables currently
object?: boolean;
index?: boolean | string;
unique?: boolean | string;
nullable?: boolean;
inherited?: boolean;
unsigned?: boolean;
mapToPk?: boolean;
persist?: boolean;
hydrate?: boolean;
trackChanges?: boolean;
hidden?: boolean;
enum?: boolean;
items?: (number | string)[];
nativeEnumName?: string; // for postgres, by default it uses text column with check constraint
version?: boolean;
concurrencyCheck?: boolean;
eager?: boolean;
setter?: boolean;
getter?: boolean;
getterName?: keyof Owner;
cascade: Cascade[];
orphanRemoval?: boolean;
onCreate?: (entity: Owner) => any;
onUpdate?: (entity: Owner) => any;
deleteRule?: 'cascade' | 'no action' | 'set null' | 'set default' | AnyString;
updateRule?: 'cascade' | 'no action' | 'set null' | 'set default' | AnyString;
strategy?: LoadStrategy;
owner: boolean;
inversedBy: EntityKey<Target>;
mappedBy: EntityKey<Target>;
orderBy?: QueryOrderMap<Owner> | QueryOrderMap<Owner>[];
customOrder?: string[] | number[] | boolean[];
fixedOrder?: boolean;
fixedOrderColumn?: string;
pivotTable: string;
pivotEntity: string;
joinColumns: string[];
inverseJoinColumns: string[];
referencedColumnNames: string[];
referencedTableName: string;
referencedPKs: EntityKey<Owner>[];
serializer?: (value: any) => any;
serializedName?: string;
comment?: string;
/** mysql only */
extra?: string;
userDefined?: boolean;
optional?: boolean; // for ts-morph
ignoreSchemaChanges?: ('type' | 'extra')[];
}
export class EntityMetadata<T = any> {
private static counter = 0;
readonly _id = 1000 * EntityMetadata.counter++; // keep the id >= 1000 to allow computing cache keys by simple addition
readonly propertyOrder = new Map<string, number>();
constructor(meta: Partial<EntityMetadata> = {}) {
this.properties = {} as any;
this.props = [];
this.primaryKeys = [];
this.filters = {};
this.hooks = {};
this.indexes = [];
this.uniques = [];
this.checks = [];
this.referencingProperties = [];
this.concurrencyCheckKeys = new Set();
Object.assign(this, meta);
}
addProperty(prop: Partial<EntityProperty<T>>, sync = true) {
if (prop.pivotTable && !prop.pivotEntity) {
prop.pivotEntity = prop.pivotTable;
}
this.properties[prop.name!] = prop as EntityProperty<T>;
this.propertyOrder.set(prop.name!, this.props.length);
/* istanbul ignore next */
if (sync) {
this.sync();
}
}
removeProperty(name: string, sync = true) {
delete this.properties[name as EntityKey<T>];
this.propertyOrder.delete(name);
/* istanbul ignore next */
if (sync) {
this.sync();
}
}
getPrimaryProps(): EntityProperty<T>[] {
return this.primaryKeys.map(pk => this.properties[pk]);
}
get tableName(): string {
return this.collection;
}
set tableName(name: string) {
this.collection = name;
}
sync(initIndexes = false) {
this.root ??= this;
const props = Object.values<EntityProperty<T>>(this.properties).sort((a, b) => this.propertyOrder.get(a.name)! - this.propertyOrder.get(b.name)!);
this.props = [...props.filter(p => p.primary), ...props.filter(p => !p.primary)];
this.relations = this.props.filter(prop => prop.kind !== ReferenceKind.SCALAR && prop.kind !== ReferenceKind.EMBEDDED);
this.bidirectionalRelations = this.relations.filter(prop => prop.mappedBy || prop.inversedBy);
this.uniqueProps = this.props.filter(prop => prop.unique);
this.comparableProps = this.props.filter(prop => EntityComparator.isComparable(prop, this));
this.hydrateProps = this.props.filter(prop => {
// `prop.userDefined` is either `undefined` or `false`
const discriminator = this.root.discriminatorColumn === prop.name && prop.userDefined === false;
// even if we don't have a setter, do not ignore value from database!
const onlyGetter = prop.getter && !prop.setter;
return !prop.inherited && prop.hydrate !== false && !discriminator && !prop.embedded && !onlyGetter;
});
this.trackingProps = this.hydrateProps
.filter(prop => !prop.getter && !prop.setter)
.filter(prop => ![ReferenceKind.ONE_TO_MANY, ReferenceKind.MANY_TO_MANY].includes(prop.kind))
.filter(prop => !prop.serializedPrimaryKey);
this.selfReferencing = this.relations.some(prop => [this.className, this.root.className].includes(prop.type));
this.hasUniqueProps = this.uniques.length + this.uniqueProps.length > 0;
this.virtual = !!this.expression;
this.checks = Utils.removeDuplicates(this.checks);
this.indexes = Utils.removeDuplicates(this.indexes);
this.uniques = Utils.removeDuplicates(this.uniques);
for (const hook of Utils.keys(this.hooks)) {
this.hooks[hook] = Utils.removeDuplicates(this.hooks[hook] as any);
}
if (this.virtual) {
this.readonly = true;
}
if (initIndexes && this.name) {
this.props.forEach(prop => this.initIndexes(prop));
}
this.definedProperties = this.trackingProps.reduce((o, prop) => {
const isCollection = [ReferenceKind.ONE_TO_MANY, ReferenceKind.MANY_TO_MANY].includes(prop.kind);
const isReference = [ReferenceKind.ONE_TO_ONE, ReferenceKind.MANY_TO_ONE].includes(prop.kind) && (prop.inversedBy || prop.mappedBy) && !prop.mapToPk;
if (isReference) {
// eslint-disable-next-line @typescript-eslint/no-this-alias
const meta = this;
o[prop.name] = {
get() {
return this.__helper.__data[prop.name];
},
set(val: AnyEntity) {
const wrapped = this.__helper;
const hydrator = wrapped.hydrator as IHydrator;
const entity = Reference.unwrapReference(val ?? wrapped.__data[prop.name]);
const old = Reference.unwrapReference(wrapped.__data[prop.name]);
wrapped.__data[prop.name] = Reference.wrapReference(val, prop as EntityProperty);
// when propagation from inside hydration, we set the FK to the entity data immediately
if (val && hydrator.isRunning() && wrapped.__originalEntityData && prop.owner) {
wrapped.__originalEntityData[prop.name] = Utils.getPrimaryKeyValues(val, prop.targetMeta!.primaryKeys, true);
} else {
wrapped.__touched = !hydrator.isRunning();
}
EntityHelper.propagate(meta, entity, this, prop, Reference.unwrapReference(val), old);
},
enumerable: true,
configurable: true,
};
}
if (prop.inherited || prop.primary || isCollection || prop.persist === false || prop.trackChanges === false || isReference || prop.embedded) {
return o;
}
o[prop.name] = {
get() {
return this.__helper.__data[prop.name];
},
set(val: unknown) {
if (typeof val === 'object' && !!val && '__raw' in val) {
(val as Dictionary).use();
}
this.__helper.__data[prop.name] = val;
this.__helper.__touched = !this.__helper.hydrator.isRunning();
},
enumerable: true,
configurable: true,
};
return o;
}, { __gettersDefined: { value: true, enumerable: false } } as Dictionary);
}
private initIndexes(prop: EntityProperty<T>): void {
const simpleIndex = this.indexes.find(index => index.properties === prop.name && !index.options && !index.type && !index.expression);
const simpleUnique = this.uniques.find(index => index.properties === prop.name && !index.options);
const owner = prop.kind === ReferenceKind.MANY_TO_ONE;
if (!prop.index && simpleIndex) {
Utils.defaultValue(simpleIndex, 'name', true);
prop.index = simpleIndex.name;
this.indexes.splice(this.indexes.indexOf(simpleIndex), 1);
}
if (!prop.unique && simpleUnique) {
Utils.defaultValue(simpleUnique, 'name', true);
prop.unique = simpleUnique.name;
this.uniques.splice(this.uniques.indexOf(simpleUnique), 1);
}
if (prop.index && owner && prop.fieldNames.length > 1) {
this.indexes.push({ properties: prop.name });
prop.index = false;
}
/* istanbul ignore next */
if (owner && prop.fieldNames.length > 1 && prop.unique) {
this.uniques.push({ properties: prop.name });
prop.unique = false;
}
}
/** @internal */
clone() {
return this;
}
}
export interface SimpleColumnMeta {
name: string;
type: string;
}
export interface EntityMetadata<T = any> {
name?: string; // abstract classes do not have a name, but once discovery ends, we have only non-abstract classes stored
className: string;
tableName: string;
schema?: string;
pivotTable?: boolean;
virtual?: boolean;
// we need to use `em: any` here otherwise an expression would not be assignable with more narrow type like `SqlEntityManager`
// also return type is unknown as it can be either QB instance (which we cannot type here) or array of POJOs (e.g. for mongodb)
expression?: string | ((em: any, where: FilterQuery<T>, options: FindOptions<T, any, any, any>) => object | string);
discriminatorColumn?: EntityKey<T>;
discriminatorValue?: number | string;
discriminatorMap?: Dictionary<string>;
embeddable: boolean;
constructorParams: EntityKey<T>[];
forceConstructor: boolean;
toJsonParams: string[];
extends: string;
collection: string;
path: string;
primaryKeys: EntityKey<T>[];
simplePK: boolean; // whether the PK can be compared via `===`, e.g. simple scalar without a custom mapped type
compositePK: boolean;
versionProperty: EntityKey<T>;
concurrencyCheckKeys: Set<EntityKey<T>>;
serializedPrimaryKey: EntityKey<T>;
properties: { [K in EntityKey<T>]: EntityProperty<T> };
props: EntityProperty<T>[];
relations: EntityProperty<T>[];
bidirectionalRelations: EntityProperty<T>[];
referencingProperties: { meta: EntityMetadata<T>; prop: EntityProperty<T> }[];
comparableProps: EntityProperty<T>[]; // for EntityComparator
trackingProps: EntityProperty<T>[]; // for change-tracking and propagation
hydrateProps: EntityProperty<T>[]; // for Hydrator
uniqueProps: EntityProperty<T>[];
indexes: { properties: EntityKey<T> | EntityKey<T>[]; name?: string; type?: string; options?: Dictionary; expression?: string }[];
uniques: { properties: EntityKey<T> | EntityKey<T>[]; name?: string; options?: Dictionary; expression?: string }[];
checks: CheckConstraint<T>[];
repository: () => EntityClass<EntityRepository<any>>;
hooks: { [K in EventType]?: (keyof T | EventSubscriber<T>[EventType])[] };
prototype: T;
class: EntityClass<T>;
abstract: boolean;
useCache: boolean;
filters: Dictionary<FilterDef>;
comment?: string;
selfReferencing?: boolean;
hasUniqueProps?: boolean;
readonly?: boolean;
polymorphs?: EntityMetadata[];
root: EntityMetadata<T>;
definedProperties: Dictionary;
/** @internal can be used for computed numeric cache keys */
readonly _id: number;
}
export interface CreateSchemaOptions {
wrap?: boolean;
schema?: string;
}
export interface ClearDatabaseOptions {
schema?: string;
truncate?: boolean;
}
export interface EnsureDatabaseOptions extends CreateSchemaOptions, ClearDatabaseOptions {
clear?: boolean;
create?: boolean;
}
export interface DropSchemaOptions {
wrap?: boolean;
dropMigrationsTable?: boolean;
dropDb?: boolean;
schema?: string;
}
export interface UpdateSchemaOptions<DatabaseSchema = unknown> {
wrap?: boolean;
safe?: boolean;
dropDb?: boolean;
dropTables?: boolean;
schema?: string;
fromSchema?: DatabaseSchema;
}
export interface RefreshDatabaseOptions extends CreateSchemaOptions {
ensureIndexes?: boolean;
}
export interface ISchemaGenerator {
createSchema(options?: CreateSchemaOptions): Promise<void>;
ensureDatabase(): Promise<boolean>;
getCreateSchemaSQL(options?: CreateSchemaOptions): Promise<string>;
dropSchema(options?: DropSchemaOptions): Promise<void>;
getDropSchemaSQL(options?: Omit<DropSchemaOptions, 'dropDb'>): Promise<string>;
updateSchema(options?: UpdateSchemaOptions): Promise<void>;
getUpdateSchemaSQL(options?: UpdateSchemaOptions): Promise<string>;
getUpdateSchemaMigrationSQL(options?: UpdateSchemaOptions): Promise<{ up: string; down: string }>;
createDatabase(name: string): Promise<void>;
dropDatabase(name?: string): Promise<void>;
execute(sql: string, options?: { wrap?: boolean }): Promise<void>;
ensureIndexes(): Promise<void>;
refreshDatabase(options?: RefreshDatabaseOptions): Promise<void>;
clearDatabase(options?: ClearDatabaseOptions): Promise<void>;
}
export interface GenerateOptions {
path?: string;
save?: boolean;
schema?: string;
skipTables?: string[];
skipColumns?: Record<string, string[]>;
bidirectionalRelations?: boolean;
identifiedReferences?: boolean;
entitySchema?: boolean;
esmImport?: boolean;
scalarTypeInDecorator?: boolean;
scalarPropertiesForRelations?: 'always' | 'never' | 'smart';
fileName?: (className: string) => string;
onlyPurePivotTables?: boolean;
readOnlyPivotTables?: boolean;
}
export interface IEntityGenerator {
generate(options?: GenerateOptions): Promise<string[]>;
}
export type UmzugMigration = { name: string; path?: string };
export type MigrateOptions = { from?: string | number; to?: string | number; migrations?: string[]; transaction?: Transaction };
export type MigrationResult = { fileName: string; code: string; diff: MigrationDiff };
export type MigrationRow = { name: string; executed_at: Date };
/**
* @internal
*/
export interface IMigratorStorage {
executed(): Promise<string[]>;
logMigration(params: Dictionary): Promise<void>;
unlogMigration(params: Dictionary): Promise<void>;
getExecutedMigrations(): Promise<MigrationRow[]>;
ensureTable?(): Promise<void>;
setMasterMigration(trx: Transaction): void;
unsetMasterMigration(): void;
getMigrationName(name: string): string;
getTableName?(): { schemaName?: string; tableName: string };
}
export interface IMigrator {
/**
* Checks current schema for changes, generates new migration if there are any.
*/
createMigration(path?: string, blank?: boolean, initial?: boolean, name?: string): Promise<MigrationResult>;
/**
* Checks current schema for changes.
*/
checkMigrationNeeded(): Promise<boolean>;
/**
* Creates initial migration. This generates the schema based on metadata, and checks whether all the tables
* are already present. If yes, it will also automatically log the migration as executed.
* Initial migration can be created only if the schema is already aligned with the metadata, or when no schema
* is present - in such case regular migration would have the same effect.
*/
createInitialMigration(path?: string): Promise<MigrationResult>;
/**
* Returns list of already executed migrations.
*/
getExecutedMigrations(): Promise<MigrationRow[]>;
/**
* Returns list of pending (not yet executed) migrations found in the migration directory.
*/
getPendingMigrations(): Promise<UmzugMigration[]>;
/**
* Executes specified migrations. Without parameter it will migrate up to the latest version.
*/
up(options?: string | string[] | MigrateOptions): Promise<UmzugMigration[]>;
/**
* Executes down migrations to the given point. Without parameter it will migrate one version down.
*/
down(options?: string | string[] | MigrateOptions): Promise<UmzugMigration[]>;
/**
* @internal
*/
getStorage(): IMigratorStorage;
}
export interface MigrationDiff {
up: string[];
down: string[];
}
export interface IMigrationGenerator {
/**
* Generates the full contents of migration file. Uses `generateMigrationFile` to get the file contents.
*/
generate(diff: MigrationDiff, path?: string, name?: string): Promise<[string, string]>;
/**
* Creates single migration statement. By default adds `this.addSql(sql);` to the code.
*/
createStatement(sql: string, padLeft: number): string;
/**
* Returns the file contents of given migration.
*/
generateMigrationFile(className: string, diff: MigrationDiff): string;
}
export interface Migration {
up(): Promise<void>;
down(): Promise<void>;
}
export interface MigrationObject {
name: string;
class: Constructor<Migration>;
}
export type FilterDef = {
name: string;
cond: Dictionary | ((args: Dictionary, type: 'read' | 'update' | 'delete', em: any, options?: FindOptions<any, any, any, any> | FindOneOptions<any, any, any, any>) => Dictionary | Promise<Dictionary>);
default?: boolean;
entity?: string[];
args?: boolean;
};
export type Populate<T, P extends string = never> = readonly AutoPath<T, P, '*' | '$infer'>[] | false;
export type PopulateOptions<T> = {
field: EntityKey<T>;
strategy?: LoadStrategy;
all?: boolean;
filter?: boolean;
children?: PopulateOptions<T[keyof T]>[];
};
type Loadable<T extends object> = Collection<T, any> | Reference<T> | Ref<T> | readonly T[]; // we need to support raw arrays in embeddables too to allow population
type ExtractType<T> = T extends Loadable<infer U> ? U : T;
type ExtractStringKeys<T> = { [K in keyof T]: CleanKeys<T, K> }[keyof T] & {};
type StringKeys<T, E extends string = never> = T extends Collection<any, any>
? ExtractStringKeys<ExtractType<T>> | E
: T extends Reference<any>
? ExtractStringKeys<ExtractType<T>> | E
: T extends object
? ExtractStringKeys<ExtractType<T>> | E
: never;
type GetStringKey<T, K extends StringKeys<T, string>, E extends string> = K extends keyof T ? ExtractType<T[K]> : (K extends E ? keyof T : never);
// limit depth of the recursion to 5 (inspired by https://www.angularfix.com/2022/01/why-am-i-getting-instantiation-is.html)
type Prev = [never, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
// for pivot joining via populate hint, e.g. `tags:ref`
type CollectionKeys<T> = T extends object
? {
[K in keyof T]: T[K] extends Collection<any>
? IsAny<T[K]> extends true
? never
: K & string
: never
}[keyof T] & {}
: never;
export type AutoPath<O, P extends string | boolean, E extends string = never, D extends Prev[number] = 9> =
P extends boolean
? P
: [D] extends [never]
? any
: P extends any
? P extends string
? (P & `${string}.` extends never ? P : P & `${string}.`) extends infer Q
? Q extends `${infer A}.${infer B}`
? A extends StringKeys<O, E>
? `${A}.${AutoPath<Defined<GetStringKey<O, A, E>>, B, E, Prev[D]>}`
: never
: Q extends StringKeys<O, E>
? (Defined<GetStringKey<O, Q, E>> extends unknown ? Exclude<P, `${string}.`> : never) | (StringKeys<Defined<GetStringKey<O, Q, E>>, E> extends never ? never : `${Q}.`)
: StringKeys<O, E> | `${CollectionKeys<O>}:ref`
: never
: never
: never;
export type UnboxArray<T> = T extends any[] ? ArrayElement<T> : T;
export type ArrayElement<ArrayType extends unknown[]> = ArrayType extends (infer ElementType)[] ? ElementType : never;
export type ExpandProperty<T> = T extends Reference<infer U>
? NonNullable<U>
: T extends Collection<infer U, any>
? NonNullable<U>
: T extends (infer U)[]
? NonNullable<U>
: NonNullable<T>;
type LoadedLoadable<T, E extends object> =
T extends Collection<any, any>
? LoadedCollection<E>
: T extends Reference<any>
? LoadedReference<E>
: T extends ScalarReference<infer U>
? LoadedScalarReference<U>
: T extends Scalar | Scalar[]
? T
: E;
type IsTrue<T> = IsNever<T> extends true
? false
: T extends boolean
? T extends true
? true
: false
: false;
type StringLiteral<T> = T extends string ? string extends T ? never : T : never;
type Prefix<T, K> = K extends `${infer S}.${string}` ? S : (K extends '*' ? keyof T : K);
type IsPrefixedExclude<T, K extends keyof T, E extends string> =
K extends E
? never
: K;
export type IsPrefixed<T, K extends keyof T, L extends string, E extends string = never> =
IsNever<E> extends false
? IsPrefixedExclude<T, K, E>
: K extends symbol
? never
: IsTrue<L> extends true
? (T[K] & {} extends Loadable<any> ? K : never)
: IsNever<StringLiteral<L>> extends true
? never
: K extends Prefix<T, L>
? K
: K extends PrimaryProperty<T>
? K
: never;
// filter by prefix and map to suffix
type Suffix<Key, Hint extends string, All = true | '*'> = Hint extends `${infer Pref}.${infer Suf}`
? (Pref extends Key ? Suf : never)