-
Notifications
You must be signed in to change notification settings - Fork 283
/
Copy pathjoin-builder.ts
199 lines (179 loc) · 5.44 KB
/
join-builder.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
import { Expression } from '../expression/expression.js'
import { JoinNode } from '../operation-node/join-node.js'
import { OperationNodeSource } from '../operation-node/operation-node-source.js'
import { RawNode } from '../operation-node/raw-node.js'
import {
ComparisonOperatorExpression,
OperandValueExpressionOrList,
parseOn,
parseReferentialFilter,
} from '../parser/binary-operation-parser.js'
import { ReferenceExpression } from '../parser/reference-parser.js'
import {
ExistsExpression,
parseExists,
parseNotExists,
} from '../parser/unary-operation-parser.js'
import { freeze } from '../util/object-utils.js'
import { preventAwait } from '../util/prevent-await.js'
export class JoinBuilder<DB, TB extends keyof DB>
implements OperationNodeSource
{
readonly #props: JoinBuilderProps
constructor(props: JoinBuilderProps) {
this.#props = freeze(props)
}
/**
* Just like {@link WhereInterface.where} but adds an item to the join's
* `on` clause instead.
*
* See {@link WhereInterface.where} for documentation and examples.
*/
on<RE extends ReferenceExpression<DB, TB>>(
lhs: RE,
op: ComparisonOperatorExpression,
rhs: OperandValueExpressionOrList<DB, TB, RE>
): JoinBuilder<DB, TB>
on(
grouper: (qb: JoinBuilder<DB, TB>) => JoinBuilder<DB, TB>
): JoinBuilder<DB, TB>
on(expression: Expression<any>): JoinBuilder<DB, TB>
on(...args: any[]): JoinBuilder<DB, TB> {
return new JoinBuilder({
...this.#props,
joinNode: JoinNode.cloneWithOn(this.#props.joinNode, parseOn(args)),
})
}
/**
* Just like {@link WhereInterface.orWhere} but adds an item to the join's
* `on` clause instead.
*
* See {@link WhereInterface.orWhere} for documentation and examples.
*/
orOn<RE extends ReferenceExpression<DB, TB>>(
lhs: RE,
op: ComparisonOperatorExpression,
rhs: OperandValueExpressionOrList<DB, TB, RE>
): JoinBuilder<DB, TB>
orOn(
grouper: (qb: JoinBuilder<DB, TB>) => JoinBuilder<DB, TB>
): JoinBuilder<DB, TB>
orOn(expression: Expression<any>): JoinBuilder<DB, TB>
orOn(...args: any[]): JoinBuilder<DB, TB> {
return new JoinBuilder({
...this.#props,
joinNode: JoinNode.cloneWithOrOn(this.#props.joinNode, parseOn(args)),
})
}
/**
* Just like {@link WhereInterface.whereRef} but adds an item to the join's
* `on` clause instead.
*
* See {@link WhereInterface.whereRef} for documentation and examples.
*/
onRef(
lhs: ReferenceExpression<DB, TB>,
op: ComparisonOperatorExpression,
rhs: ReferenceExpression<DB, TB>
): JoinBuilder<DB, TB> {
return new JoinBuilder({
...this.#props,
joinNode: JoinNode.cloneWithOn(
this.#props.joinNode,
parseReferentialFilter(lhs, op, rhs)
),
})
}
/**
* Just like {@link WhereInterface.orWhereRef} but adds an item to the join's
* `on` clause instead.
*
* See {@link WhereInterface.orWhereRef} for documentation and examples.
*/
orOnRef(
lhs: ReferenceExpression<DB, TB>,
op: ComparisonOperatorExpression,
rhs: ReferenceExpression<DB, TB>
): JoinBuilder<DB, TB> {
return new JoinBuilder({
...this.#props,
joinNode: JoinNode.cloneWithOrOn(
this.#props.joinNode,
parseReferentialFilter(lhs, op, rhs)
),
})
}
/**
* Just like {@link WhereInterface.whereExists} but adds an item to the join's
* `on` clause instead.
*
* See {@link WhereInterface.whereExists} for documentation and examples.
*/
onExists(arg: ExistsExpression<DB, TB>): JoinBuilder<DB, TB> {
return new JoinBuilder({
...this.#props,
joinNode: JoinNode.cloneWithOn(this.#props.joinNode, parseExists(arg)),
})
}
/**
* Just like {@link WhereInterface.whereNotExists} but adds an item to the join's
* `on` clause instead.
*
* See {@link WhereInterface.whereNotExists} for documentation and examples.
*/
onNotExists(arg: ExistsExpression<DB, TB>): JoinBuilder<DB, TB> {
return new JoinBuilder({
...this.#props,
joinNode: JoinNode.cloneWithOn(this.#props.joinNode, parseNotExists(arg)),
})
}
/**
* Just like {@link WhereInterface.orWhereExists} but adds an item to the join's
* `on` clause instead.
*
* See {@link WhereInterface.orWhereExists} for documentation and examples.
*/
orOnExists(arg: ExistsExpression<DB, TB>): JoinBuilder<DB, TB> {
return new JoinBuilder({
...this.#props,
joinNode: JoinNode.cloneWithOrOn(this.#props.joinNode, parseExists(arg)),
})
}
/**
* Just like {@link WhereInterface.orWhereNotExists} but adds an item to the join's
* `on` clause instead.
*
* See {@link WhereInterface.orWhereNotExists} for documentation and examples.
*/
orOnNotExists(arg: ExistsExpression<DB, TB>): JoinBuilder<DB, TB> {
return new JoinBuilder({
...this.#props,
joinNode: JoinNode.cloneWithOrOn(
this.#props.joinNode,
parseNotExists(arg)
),
})
}
/**
* Adds `on true`.
*/
onTrue(): JoinBuilder<DB, TB> {
return new JoinBuilder({
...this.#props,
joinNode: JoinNode.cloneWithOn(
this.#props.joinNode,
RawNode.createWithSql('true')
),
})
}
toOperationNode(): JoinNode {
return this.#props.joinNode
}
}
preventAwait(
JoinBuilder,
"don't await JoinBuilder instances. They are never executed directly and are always just a part of a query."
)
export interface JoinBuilderProps {
readonly joinNode: JoinNode
}