-
-
Notifications
You must be signed in to change notification settings - Fork 496
/
ScalarCriteriaNode.ts
44 lines (33 loc) · 1.35 KB
/
ScalarCriteriaNode.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
import { MetadataStorage, ReferenceType } from '@mikro-orm/core';
import { QueryBuilder, CriteriaNode } from './internal';
export class ScalarCriteriaNode extends CriteriaNode {
static create(metadata: MetadataStorage, entityName: string, payload: any, parent?: CriteriaNode, key?: string): ScalarCriteriaNode {
const node = new ScalarCriteriaNode(metadata, entityName, parent, key);
node.payload = payload;
return node;
}
process(qb: QueryBuilder, alias?: string): any {
if (this.shouldJoin()) {
const nestedAlias = qb.getAliasForJoinPath(this.getPath()) || qb.getNextAlias();
const field = `${alias}.${this.prop!.name}`;
if (this.prop!.reference === ReferenceType.MANY_TO_MANY) {
qb.join(field, nestedAlias, undefined, 'pivotJoin', this.getPath());
} else {
qb.join(field, nestedAlias, undefined, 'leftJoin', this.getPath());
}
if (this.prop!.reference === ReferenceType.ONE_TO_ONE) {
qb.addSelect(field);
}
}
return this.payload;
}
shouldJoin(): boolean {
if (!this.parent || !this.prop || [ReferenceType.SCALAR, ReferenceType.ONE_TO_MANY].includes(this.prop.reference)) {
return false;
}
if (this.prop.reference === ReferenceType.ONE_TO_ONE) {
return !this.prop.owner;
}
return this.prop.reference === ReferenceType.MANY_TO_MANY;
}
}