-
-
Notifications
You must be signed in to change notification settings - Fork 495
/
typings.ts
212 lines (196 loc) · 8.75 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
import { QueryOrder } from './enums';
import { AssignOptions, Cascade, Collection, EntityRepository, EntityValidator, IdentifiedReference, Reference, ReferenceType, LoadStrategy } from './entity';
import { EntityManager } from './EntityManager';
import { LockMode } from './unit-of-work';
import { Platform } from './platforms';
import { EntitySchema, MetadataStorage } from './metadata';
import { Type } from './types';
export type Constructor<T> = new (...args: any[]) => T;
export type Dictionary<T = any> = { [k: string]: T };
export type NonFunctionPropertyNames<T> = { [K in keyof T]: T[K] extends Function ? never : K }[keyof T];
export type PartialEntityProperty<T, P extends keyof T> = null | (T extends Date | RegExp ? T : T[P] | (true extends IsEntity<T[P]> ? PartialEntity<T[P]> | Primary<T[P]> : never));
export type PartialEntity<T> = T extends Reference<infer U> ? { [P in keyof U]?: PartialEntityProperty<U, P> } : { [P in keyof T]?: PartialEntityProperty<T, P> };
export type DeepPartialEntity<T> = {
[P in keyof T]?: null | (T[P] extends (infer U)[]
? DeepPartialEntity<U>[]
: T[P] extends readonly (infer U)[]
? readonly DeepPartialEntity<U>[]
: T extends Date | RegExp
? T
: DeepPartialEntity<T[P]> | PartialEntity<T[P]> | Primary<T[P]> | OperatorMap<T[P]> | StringProp<T[P]>)
};
export const PrimaryKeyType = Symbol('PrimaryKeyType');
export type Primary<T> = T extends { [PrimaryKeyType]: infer PK }
? PK : T extends { _id: infer PK }
? PK | string : T extends { uuid: infer PK }
? PK : T extends { id: infer PK }
? PK : never;
export type PrimaryMap<T extends AnyEntity<T>> = Record<keyof T, Primary<T>>;
export type IPrimaryKeyValue = number | string | bigint | { toHexString(): string };
export type IPrimaryKey<T extends IPrimaryKeyValue = IPrimaryKeyValue> = T;
export type IsScalar<T> = T extends number | string | bigint | Date | RegExp ? true : never;
export type IsEntity<T> = T extends Reference<T> | { [PrimaryKeyType]: any } | { _id: any } | { uuid: string } | { id: number | string | bigint } ? true : never;
export type OneOrArray<T> = T | T[];
export type OperatorMap<T> = {
$and?: Query<T>[];
$or?: Query<T>[];
$eq?: Query<T>;
$ne?: Query<T>;
$in?: Query<T>[];
$nin?: Query<T>[];
$not?: Query<T>;
$gt?: Query<T>;
$gte?: Query<T>;
$lt?: Query<T>;
$lte?: Query<T>;
$like?: string;
$re?: string;
};
export type StringProp<T> = T extends string ? string | RegExp : never;
export type EntityOrPrimary<T> = true extends IsScalar<T> ? never : DeepPartialEntity<ReferencedEntity<T>> | PartialEntity<ReferencedEntity<T>> | Primary<ReferencedEntity<T>> | ReferencedEntity<T>;
export type CollectionItem<T> = T extends Collection<infer K> ? EntityOrPrimary<K> : never;
export type ReferencedEntity<T> = T extends Reference<infer K> ? K : T;
export type FilterValue<T> = T | OperatorMap<T> | StringProp<T> | OneOrArray<CollectionItem<T> | EntityOrPrimary<T>> | null;
export type Query<T> = true extends IsEntity<T>
? { [K in keyof T]?: Query<ReferencedEntity<T[K]>> | FilterValue<ReferencedEntity<T[K]>> | null } | FilterValue<ReferencedEntity<T>>
: T extends Collection<infer K>
? { [KK in keyof K]?: Query<K[KK]> | FilterValue<K[KK]> | null } | FilterValue<K>
: FilterValue<T>;
export type FilterQuery<T> = Query<T> | { [PrimaryKeyType]?: any };
export type QBFilterQuery<T = any> = FilterQuery<T> & Dictionary;
export interface IWrappedEntity<T, PK extends keyof T> {
isInitialized(): boolean;
populated(populated?: boolean): void;
init(populated?: boolean, lockMode?: LockMode): Promise<T>;
toReference(): IdentifiedReference<T, PK>;
toObject(ignoreFields?: string[]): Dictionary;
toJSON(...args: any[]): Dictionary;
assign(data: any, options?: AssignOptions | boolean): T;
}
export interface IWrappedEntityInternal<T, PK extends keyof T> extends IWrappedEntity<T, PK> {
__uuid: string;
__meta: EntityMetadata<T>;
__internal: { platform: Platform; metadata: MetadataStorage; validator: EntityValidator };
__data: Dictionary;
__em?: EntityManager;
__initialized?: boolean;
__populated: boolean;
__lazyInitialized: boolean;
__primaryKey: PrimaryMap<T>;
__primaryKeys: Primary<T>[];
__serializedPrimaryKey: string & keyof T;
}
export type AnyEntity<T = any, PK extends keyof T = keyof T> = { [K in PK]?: T[K] } & { [PrimaryKeyType]?: T[PK] };
export type EntityClass<T extends AnyEntity<T>> = Function & { prototype: T };
export type EntityClassGroup<T extends AnyEntity<T>> = { entity: EntityClass<T>; schema: EntityMetadata<T> | EntitySchema<T> };
export type EntityName<T extends AnyEntity<T>> = string | EntityClass<T>;
export type EntityData<T extends AnyEntity<T>> = { [K in keyof T]?: T[K] | Primary<T[K]> | CollectionItem<T[K]>[] } & Dictionary;
export interface EntityProperty<T extends AnyEntity<T> = any> {
name: string & keyof T;
entity: () => EntityName<T>;
type: string;
columnTypes: string[];
customType: Type<any>;
primary: boolean;
serializedPrimaryKey: boolean;
discriminator?: boolean;
length?: any;
reference: ReferenceType;
wrappedReference?: boolean;
fieldNames: string[];
default?: string | number | boolean | null;
defaultRaw?: string;
formula?: (alias: string) => string;
prefix?: string | boolean;
embedded?: [string, string];
embeddable: Constructor<T>;
embeddedProps: Dictionary<EntityProperty>;
index?: boolean | string;
unique?: boolean | string;
nullable?: boolean;
inherited?: boolean;
unsigned: boolean;
persist?: boolean;
hidden?: boolean;
enum?: boolean;
items?: (number | string)[];
version?: boolean;
eager?: boolean;
setter?: boolean;
getter?: boolean;
getterName?: keyof T;
cascade: Cascade[];
orphanRemoval?: boolean;
onCreate?: (entity: T) => any;
onUpdate?: (entity: T) => any;
onDelete?: 'cascade' | 'no action' | 'set null' | 'set default' | string;
onUpdateIntegrity?: 'cascade' | 'no action' | 'set null' | 'set default' | string;
strategy?: LoadStrategy;
owner: boolean;
inversedBy: string;
mappedBy: string;
orderBy?: { [field: string]: QueryOrder };
fixedOrder?: boolean;
fixedOrderColumn?: string;
pivotTable: string;
joinColumns: string[];
inverseJoinColumns: string[];
referencedColumnNames: string[];
referencedTableName: string;
}
export type HookType = 'onInit' | 'beforeCreate' | 'afterCreate' | 'beforeUpdate' | 'afterUpdate' | 'beforeDelete' | 'afterDelete';
export interface EntityMetadata<T extends AnyEntity<T> = any> {
name: string;
className: string;
tableName: string;
pivotTable: boolean;
discriminatorColumn?: string;
discriminatorValue?: string;
discriminatorMap?: Dictionary<string>;
embeddable: boolean;
constructorParams: string[];
toJsonParams: string[];
extends: string;
collection: string;
path: string;
primaryKeys: (keyof T & string)[];
compositePK: boolean;
versionProperty: keyof T & string;
serializedPrimaryKey: keyof T & string;
properties: { [K in keyof T & string]: EntityProperty<T> };
indexes: { properties: string | string[]; name?: string; type?: string; options?: Dictionary }[];
uniques: { properties: string | string[]; name?: string; options?: Dictionary }[];
customRepository: () => Constructor<EntityRepository<T>>;
hooks: Partial<Record<HookType, (string & keyof T)[]>>;
prototype: T;
class: Constructor<T>;
abstract: boolean;
useCache: boolean;
}
export interface ISchemaGenerator {
generate(): Promise<string>;
createSchema(wrap?: boolean): Promise<void>;
ensureDatabase(): Promise<void>;
getCreateSchemaSQL(wrap?: boolean): Promise<string>;
dropSchema(wrap?: boolean, dropMigrationsTable?: boolean, dropDb?: boolean): Promise<void>;
getDropSchemaSQL(wrap?: boolean, dropMigrationsTable?: boolean): Promise<string>;
updateSchema(wrap?: boolean, safe?: boolean, dropDb?: boolean, dropTables?: boolean): Promise<void>;
getUpdateSchemaSQL(wrap?: boolean, safe?: boolean, dropDb?: boolean, dropTables?: boolean): Promise<string>;
createDatabase(name: string): Promise<void>;
dropDatabase(name: string): Promise<void>;
execute(sql: string): Promise<void>;
}
export interface IEntityGenerator {
generate(options?: { baseDir?: string; save?: boolean }): Promise<string[]>;
}
type UmzugMigration = { path?: string; file: string };
type MigrateOptions = { from?: string | number; to?: string | number; migrations?: string[] };
type MigrationResult = { fileName: string; code: string; diff: string[] };
type MigrationRow = { name: string; executed_at: Date };
export interface IMigrator {
createMigration(path?: string, blank?: boolean): Promise<MigrationResult>;
getExecutedMigrations(): Promise<MigrationRow[]>;
getPendingMigrations(): Promise<UmzugMigration[]>;
up(options?: string | string[] | MigrateOptions): Promise<UmzugMigration[]>;
down(options?: string | string[] | MigrateOptions): Promise<UmzugMigration[]>;
}