-
-
Notifications
You must be signed in to change notification settings - Fork 495
/
IDatabaseDriver.ts
227 lines (180 loc) · 8.4 KB
/
IDatabaseDriver.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
import type {
ConnectionType, EntityData, EntityMetadata, EntityProperty, FilterQuery, Primary, Dictionary, QBFilterQuery,
IPrimaryKey, PopulateOptions, EntityDictionary, AutoPath, ObjectQuery, FilterObject,
} from '../typings';
import type { Connection, QueryResult, Transaction } from '../connections';
import type { FlushMode, LockMode, QueryOrderMap, QueryFlag, LoadStrategy, PopulateHint } from '../enums';
import type { Platform } from '../platforms';
import type { MetadataStorage } from '../metadata';
import type { Collection } from '../entity/Collection';
import type { EntityManager } from '../EntityManager';
import type { DriverException } from '../exceptions';
import type { Configuration } from '../utils/Configuration';
import type { LoggerContext } from '../logging';
export const EntityManagerType = Symbol('EntityManagerType');
export interface IDatabaseDriver<C extends Connection = Connection> {
[EntityManagerType]: EntityManager<this>;
readonly config: Configuration;
init(): Promise<void>;
createEntityManager<D extends IDatabaseDriver = IDatabaseDriver>(useContext?: boolean): D[typeof EntityManagerType];
connect(): Promise<C>;
close(force?: boolean): Promise<void>;
reconnect(): Promise<C>;
getConnection(type?: ConnectionType): C;
/**
* Finds selection of entities
*/
find<T extends object, P extends string = never, F extends string = '*'>(entityName: string, where: FilterQuery<T>, options?: FindOptions<T, P, F>): Promise<EntityData<T>[]>;
/**
* Finds single entity (table row, document)
*/
findOne<T extends object, P extends string = never, F extends string = '*'>(entityName: string, where: FilterQuery<T>, options?: FindOneOptions<T, P, F>): Promise<EntityData<T> | null>;
findVirtual<T extends object>(entityName: string, where: FilterQuery<T>, options: FindOptions<T, any, any>): Promise<EntityData<T>[]>;
nativeInsert<T extends object>(entityName: string, data: EntityDictionary<T>, options?: NativeInsertUpdateOptions<T>): Promise<QueryResult<T>>;
nativeInsertMany<T extends object>(entityName: string, data: EntityDictionary<T>[], options?: NativeInsertUpdateManyOptions<T>): Promise<QueryResult<T>>;
nativeUpdate<T extends object>(entityName: string, where: FilterQuery<T>, data: EntityDictionary<T>, options?: NativeInsertUpdateOptions<T>): Promise<QueryResult<T>>;
nativeUpdateMany<T extends object>(entityName: string, where: FilterQuery<T>[], data: EntityDictionary<T>[], options?: NativeInsertUpdateManyOptions<T>): Promise<QueryResult<T>>;
nativeDelete<T extends object>(entityName: string, where: FilterQuery<T>, options?: NativeDeleteOptions<T>): Promise<QueryResult<T>>;
syncCollection<T extends object, O extends object>(collection: Collection<T, O>, options?: DriverMethodOptions): Promise<void>;
count<T extends object, P extends string = never>(entityName: string, where: FilterQuery<T>, options?: CountOptions<T, P>): Promise<number>;
aggregate(entityName: string, pipeline: any[]): Promise<any[]>;
mapResult<T extends object>(result: EntityDictionary<T>, meta: EntityMetadata<T>, populate?: PopulateOptions<T>[]): EntityData<T> | null;
/**
* When driver uses pivot tables for M:N, this method will load identifiers for given collections from them
*/
loadFromPivotTable<T extends object, O extends object>(prop: EntityProperty, owners: Primary<O>[][], where?: FilterQuery<T>, orderBy?: OrderDefinition<T>, ctx?: Transaction, options?: FindOptions<T, any, any>): Promise<Dictionary<T[]>>;
getPlatform(): Platform;
setMetadata(metadata: MetadataStorage): void;
getMetadata(): MetadataStorage;
/**
* Returns name of the underlying database dependencies (e.g. `mongodb` or `mysql2`)
* for SQL drivers it also returns `knex` in the array as connectors are not used directly there
*/
getDependencies(): string[];
lockPessimistic<T extends object>(entity: T, options: LockOptions): Promise<void>;
/**
* Converts native db errors to standardized driver exceptions
*/
convertException(exception: Error): DriverException;
/**
* @internal
*/
getSchemaName(meta?: EntityMetadata, options?: { schema?: string }): string | undefined;
}
export type EntityField<T, P extends string = never> = keyof T | '*' | AutoPath<T, P, '*'>;
export type OrderDefinition<T> = (QueryOrderMap<T> & { 0?: never }) | QueryOrderMap<T>[];
export interface FindOptions<T, P extends string = never, F extends string = never> {
where?: FilterQuery<T>;
populate?: readonly AutoPath<T, P>[] | boolean;
populateWhere?: ObjectQuery<T> | PopulateHint;
fields?: readonly AutoPath<T, F, '*'>[];
orderBy?: OrderDefinition<T>;
cache?: boolean | number | [string, number];
limit?: number;
offset?: number;
/** Fetch items `before` this cursor. */
before?: string | { startCursor: string | null } | FilterObject<T>;
/** Fetch items `after` this cursor. */
after?: string | { endCursor: string | null } | FilterObject<T>;
/** Fetch `first` N items. */
first?: number;
/** Fetch `last` N items. */
last?: number;
/** Fetch one more item than `first`/`last`, enabled automatically in `em.findByCursor` to check if there is a next page. */
overfetch?: boolean;
refresh?: boolean;
convertCustomTypes?: boolean;
disableIdentityMap?: boolean;
schema?: string;
flags?: QueryFlag[];
/** sql only */
groupBy?: string | string[];
having?: QBFilterQuery<T>;
/** sql only */
strategy?: LoadStrategy | 'select-in' | 'joined';
flushMode?: FlushMode | 'commit' | 'auto' | 'always';
filters?: Dictionary<boolean | Dictionary> | string[] | boolean;
/** sql only */
lockMode?: Exclude<LockMode, LockMode.OPTIMISTIC>;
/** sql only */
lockTableAliases?: string[];
ctx?: Transaction;
connectionType?: ConnectionType;
/** sql only */
indexHint?: string;
/** sql only */
comments?: string | string[];
/** sql only */
hintComments?: string | string[];
loggerContext?: LoggerContext;
}
export interface FindByCursorOptions<T extends object, P extends string = never, F extends string = never> extends Omit<FindOptions<T, P, F>, 'limit' | 'offset'> {
}
export interface FindOneOptions<T extends object, P extends string = never, F extends string = never> extends Omit<FindOptions<T, P, F>, 'limit' | 'lockMode'> {
lockMode?: LockMode;
lockVersion?: number | Date;
}
export interface FindOneOrFailOptions<T extends object, P extends string = never, F extends string = never> extends FindOneOptions<T, P, F> {
failHandler?: (entityName: string, where: Dictionary | IPrimaryKey | any) => Error;
strict?: boolean;
}
export interface NativeInsertUpdateOptions<T> {
convertCustomTypes?: boolean;
ctx?: Transaction;
schema?: string;
/** `nativeUpdate()` only option */
upsert?: boolean;
}
export interface NativeInsertUpdateManyOptions<T> extends NativeInsertUpdateOptions<T> {
processCollections?: boolean;
}
export interface UpsertOptions<Entity> extends Omit<NativeInsertUpdateOptions<Entity>, 'upsert'> {
onConflictFields?: (keyof Entity)[];
onConflictAction?: 'ignore' | 'merge';
onConflictMergeFields?: (keyof Entity)[];
onConflictExcludeFields?: (keyof Entity)[];
}
export interface UpsertManyOptions<Entity> extends UpsertOptions<Entity> {
batchSize?: number;
}
export interface CountOptions<T extends object, P extends string = never> {
filters?: Dictionary<boolean | Dictionary> | string[] | boolean;
schema?: string;
groupBy?: string | readonly string[];
having?: QBFilterQuery<T>;
cache?: boolean | number | [string, number];
populate?: readonly AutoPath<T, P>[] | boolean;
ctx?: Transaction;
connectionType?: ConnectionType;
/** sql only */
indexHint?: string;
/** sql only */
comments?: string | string[];
/** sql only */
hintComments?: string | string[];
}
export interface UpdateOptions<T> {
filters?: Dictionary<boolean | Dictionary> | string[] | boolean;
schema?: string;
ctx?: Transaction;
}
export interface DeleteOptions<T> extends DriverMethodOptions {
filters?: Dictionary<boolean | Dictionary> | string[] | boolean;
}
export interface NativeDeleteOptions<T> extends DriverMethodOptions {
filters?: Dictionary<boolean | Dictionary> | string[] | boolean;
}
export interface LockOptions extends DriverMethodOptions {
lockMode?: LockMode;
lockVersion?: number | Date;
lockTableAliases?: string[];
}
export interface DriverMethodOptions {
ctx?: Transaction;
schema?: string;
}
export interface GetReferenceOptions {
wrapped?: boolean;
convertCustomTypes?: boolean;
schema?: string;
}