-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
/
FindOptionsUtils.ts
411 lines (358 loc) · 15.5 KB
/
FindOptionsUtils.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
import { FindManyOptions } from "./FindManyOptions"
import { FindOneOptions } from "./FindOneOptions"
import { SelectQueryBuilder } from "../query-builder/SelectQueryBuilder"
import { FindRelationsNotFoundError } from "../error/FindRelationsNotFoundError"
import { EntityMetadata } from "../metadata/EntityMetadata"
import { DriverUtils } from "../driver/DriverUtils"
import { FindTreeOptions } from "./FindTreeOptions"
/**
* Utilities to work with FindOptions.
*/
export class FindOptionsUtils {
// -------------------------------------------------------------------------
// Public Static Methods
// -------------------------------------------------------------------------
/**
* Checks if given object is really instance of FindOneOptions interface.
*/
static isFindOneOptions<Entity = any>(
obj: any,
): obj is FindOneOptions<Entity> {
const possibleOptions: FindOneOptions<Entity> = obj
return (
possibleOptions &&
(Array.isArray(possibleOptions.select) ||
Array.isArray(possibleOptions.relations) ||
typeof possibleOptions.select === "object" ||
typeof possibleOptions.relations === "object" ||
typeof possibleOptions.where === "object" ||
// typeof possibleOptions.where === "string" ||
typeof possibleOptions.join === "object" ||
typeof possibleOptions.order === "object" ||
typeof possibleOptions.cache === "object" ||
typeof possibleOptions.cache === "boolean" ||
typeof possibleOptions.cache === "number" ||
typeof possibleOptions.comment === "string" ||
typeof possibleOptions.lock === "object" ||
typeof possibleOptions.loadRelationIds === "object" ||
typeof possibleOptions.loadRelationIds === "boolean" ||
typeof possibleOptions.loadEagerRelations === "boolean" ||
typeof possibleOptions.withDeleted === "boolean" ||
typeof possibleOptions.relationLoadStrategy === "string" ||
typeof possibleOptions.transaction === "boolean")
)
}
/**
* Checks if given object is really instance of FindManyOptions interface.
*/
static isFindManyOptions<Entity = any>(
obj: any,
): obj is FindManyOptions<Entity> {
const possibleOptions: FindManyOptions<Entity> = obj
return (
possibleOptions &&
(this.isFindOneOptions(possibleOptions) ||
typeof (possibleOptions as FindManyOptions<any>).skip ===
"number" ||
typeof (possibleOptions as FindManyOptions<any>).take ===
"number" ||
typeof (possibleOptions as FindManyOptions<any>).skip ===
"string" ||
typeof (possibleOptions as FindManyOptions<any>).take ===
"string")
)
}
/**
* Checks if given object is really instance of FindOptions interface.
*/
static extractFindManyOptionsAlias(object: any): string | undefined {
if (this.isFindManyOptions(object) && object.join)
return object.join.alias
return undefined
}
/**
* Applies give find many options to the given query builder.
static applyFindManyOptionsOrConditionsToQueryBuilder<T>(qb: SelectQueryBuilder<T>, options: FindManyOptions<T>|Partial<T>|undefined): SelectQueryBuilder<T> {
if (this.isFindManyOptions(options))
return this.applyOptionsToQueryBuilder(qb, options);
if (options)
return qb.where(options);
return qb;
}*/
/**
* Applies give find options to the given query builder.
static applyOptionsToQueryBuilder<T>(qb: SelectQueryBuilder<T>, options: FindOneOptions<T>|FindManyOptions<T>|undefined): SelectQueryBuilder<T> {
// if options are not set then simply return query builder. This is made for simplicity of usage.
if (!options || (!this.isFindOneOptions(options) && !this.isFindManyOptions(options)))
return qb;
if (options.transaction === true) {
qb.expressionMap.useTransaction = true;
}
if (!qb.expressionMap.mainAlias || !qb.expressionMap.mainAlias.hasMetadata)
return qb;
const metadata = qb.expressionMap.mainAlias!.metadata;
// apply all options from FindOptions
if (options.comment) {
qb.comment(options.comment);
}
if (options.withDeleted) {
qb.withDeleted();
}
if (options.select) {
qb.select([]);
options.select.forEach(select => {
if (!metadata.hasColumnWithPropertyPath(`${select}`))
throw new TypeORMError(`${select} column was not found in the ${metadata.name} entity.`);
const columns = metadata.findColumnsWithPropertyPath(`${select}`);
for (const column of columns) {
qb.addSelect(qb.alias + "." + column.propertyPath);
}
});
}
if (options.relations) {
// Copy because `applyRelationsRecursively` modifies it
const allRelations = [...options.relations];
this.applyRelationsRecursively(qb, allRelations, qb.expressionMap.mainAlias!.name, qb.expressionMap.mainAlias!.metadata, "");
// recursive removes found relations from allRelations array
// if there are relations left in this array it means those relations were not found in the entity structure
// so, we give an exception about not found relations
if (allRelations.length > 0)
throw new FindRelationsNotFoundError(allRelations);
}
if (options.join) {
if (options.join.leftJoin)
Object.keys(options.join.leftJoin).forEach(key => {
qb.leftJoin(options.join!.leftJoin![key], key);
});
if (options.join.innerJoin)
Object.keys(options.join.innerJoin).forEach(key => {
qb.innerJoin(options.join!.innerJoin![key], key);
});
if (options.join.leftJoinAndSelect)
Object.keys(options.join.leftJoinAndSelect).forEach(key => {
qb.leftJoinAndSelect(options.join!.leftJoinAndSelect![key], key);
});
if (options.join.innerJoinAndSelect)
Object.keys(options.join.innerJoinAndSelect).forEach(key => {
qb.innerJoinAndSelect(options.join!.innerJoinAndSelect![key], key);
});
}
if (options.cache) {
if (options.cache instanceof Object) {
const cache = options.cache as { id: any, milliseconds: number };
qb.cache(cache.id, cache.milliseconds);
} else {
qb.cache(options.cache);
}
}
if (options.lock) {
if (options.lock.mode === "optimistic") {
qb.setLock(options.lock.mode, options.lock.version);
} else if (
options.lock.mode === "pessimistic_read" ||
options.lock.mode === "pessimistic_write" ||
options.lock.mode === "dirty_read" ||
options.lock.mode === "pessimistic_partial_write" ||
options.lock.mode === "pessimistic_write_or_fail" ||
options.lock.mode === "for_no_key_update" ||
options.lock.mode === "for_key_share"
) {
const tableNames = options.lock.tables ? options.lock.tables.map((table) => {
const tableAlias = qb.expressionMap.aliases.find((alias) => {
return alias.metadata.tableNameWithoutPrefix === table;
});
if (!tableAlias) {
throw new TypeORMError(`"${table}" is not part of this query`);
}
return qb.escape(tableAlias.name);
}) : undefined;
qb.setLock(options.lock.mode, undefined, tableNames);
}
}
if (options.loadRelationIds === true) {
qb.loadAllRelationIds();
} else if (options.loadRelationIds instanceof Object) {
qb.loadAllRelationIds(options.loadRelationIds as any);
}
if (options.where)
qb.where(options.where);
if ((options as FindManyOptions<T>).skip)
qb.skip((options as FindManyOptions<T>).skip!);
if ((options as FindManyOptions<T>).take)
qb.take((options as FindManyOptions<T>).take!);
if (options.order)
Object.keys(options.order).forEach(key => {
const order = ((options as FindOneOptions<T>).order as any)[key as any];
if (!metadata.findColumnWithPropertyPath(key))
throw new Error(`${key} column was not found in the ${metadata.name} entity.`);
switch (order) {
case 1:
qb.addOrderBy(qb.alias + "." + key, "ASC");
break;
case -1:
qb.addOrderBy(qb.alias + "." + key, "DESC");
break;
case "ASC":
qb.addOrderBy(qb.alias + "." + key, "ASC");
break;
case "DESC":
qb.addOrderBy(qb.alias + "." + key, "DESC");
break;
}
});
return qb;
}*/
static applyOptionsToTreeQueryBuilder<T>(
qb: SelectQueryBuilder<T>,
options?: FindTreeOptions,
): SelectQueryBuilder<T> {
if (options?.relations) {
// Copy because `applyRelationsRecursively` modifies it
const allRelations = [...options.relations]
FindOptionsUtils.applyRelationsRecursively(
qb,
allRelations,
qb.expressionMap.mainAlias!.name,
qb.expressionMap.mainAlias!.metadata,
"",
)
// recursive removes found relations from allRelations array
// if there are relations left in this array it means those relations were not found in the entity structure
// so, we give an exception about not found relations
if (allRelations.length > 0)
throw new FindRelationsNotFoundError(allRelations)
}
return qb
}
// -------------------------------------------------------------------------
// Protected Static Methods
// -------------------------------------------------------------------------
/**
* Adds joins for all relations and sub-relations of the given relations provided in the find options.
*/
public static applyRelationsRecursively(
qb: SelectQueryBuilder<any>,
allRelations: string[],
alias: string,
metadata: EntityMetadata,
prefix: string,
): void {
// find all relations that match given prefix
let matchedBaseRelations: string[] = []
if (prefix) {
const regexp = new RegExp("^" + prefix.replace(".", "\\.") + "\\.")
matchedBaseRelations = allRelations
.filter((relation) => relation.match(regexp))
.map((relation) => relation.replace(regexp, ""))
.filter((relation) =>
metadata.findRelationWithPropertyPath(relation),
)
} else {
matchedBaseRelations = allRelations.filter((relation) =>
metadata.findRelationWithPropertyPath(relation),
)
}
// go through all matched relations and add join for them
matchedBaseRelations.forEach((relation) => {
// generate a relation alias
let relationAlias: string = DriverUtils.buildAlias(
qb.connection.driver,
{ joiner: "__" },
alias,
relation,
)
// add a join for the found relation
const selection = alias + "." + relation
qb.leftJoinAndSelect(selection, relationAlias)
// remove added relations from the allRelations array, this is needed to find all not found relations at the end
allRelations.splice(
allRelations.indexOf(
prefix ? prefix + "." + relation : relation,
),
1,
)
// try to find sub-relations
const join = qb.expressionMap.joinAttributes.find(
(join) => join.entityOrProperty === selection,
)
this.applyRelationsRecursively(
qb,
allRelations,
join!.alias.name,
join!.metadata!,
prefix ? prefix + "." + relation : relation,
)
// join the eager relations of the found relation
const relMetadata = metadata.relations.find(
(metadata) => metadata.propertyName === relation,
)
if (relMetadata) {
this.joinEagerRelations(
qb,
relationAlias,
relMetadata.inverseEntityMetadata,
)
}
})
}
public static joinEagerRelations(
qb: SelectQueryBuilder<any>,
alias: string,
metadata: EntityMetadata,
) {
metadata.eagerRelations.forEach((relation) => {
// generate a relation alias
let relationAlias = DriverUtils.buildAlias(
qb.connection.driver,
qb.connection.namingStrategy.eagerJoinRelationAlias(
alias,
relation.propertyPath,
),
)
// add a join for the relation
// Checking whether the relation wasn't joined yet.
let addJoin = true
for (const join of qb.expressionMap.joinAttributes) {
if (
join.condition !== undefined ||
join.mapToProperty !== undefined ||
join.isMappingMany !== undefined ||
join.direction !== "LEFT" ||
join.entityOrProperty !==
`${alias}.${relation.propertyPath}`
) {
continue
}
addJoin = false
relationAlias = join.alias.name
break
}
if (addJoin) {
qb.leftJoin(alias + "." + relation.propertyPath, relationAlias)
}
// Checking whether the relation wasn't selected yet.
// This check shall be after the join check to detect relationAlias.
let addSelect = true
for (const select of qb.expressionMap.selects) {
if (
select.aliasName !== undefined ||
select.virtual !== undefined ||
select.selection !== relationAlias
) {
continue
}
addSelect = false
break
}
if (addSelect) {
qb.addSelect(relationAlias)
}
// (recursive) join the eager relations
this.joinEagerRelations(
qb,
relationAlias,
relation.inverseEntityMetadata,
)
})
}
}