-
Notifications
You must be signed in to change notification settings - Fork 280
/
query-node.ts
90 lines (81 loc) · 2.23 KB
/
query-node.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
import { InsertQueryNode } from './insert-query-node.js'
import { SelectQueryNode } from './select-query-node.js'
import { UpdateQueryNode } from './update-query-node.js'
import { DeleteQueryNode } from './delete-query-node.js'
import { WhereNode } from './where-node.js'
import { freeze } from '../util/object-utils.js'
import { JoinNode } from './join-node.js'
import { SelectionNode } from './selection-node.js'
import { ReturningNode } from './returning-node.js'
import { OperationNode } from './operation-node.js'
export type QueryNode =
| SelectQueryNode
| DeleteQueryNode
| InsertQueryNode
| UpdateQueryNode
export type MutatingQueryNode =
| DeleteQueryNode
| InsertQueryNode
| UpdateQueryNode
export type FilterableQueryNode =
| SelectQueryNode
| DeleteQueryNode
| UpdateQueryNode
/**
* @internal
*/
export const QueryNode = freeze({
is(node: OperationNode): node is QueryNode {
return (
DeleteQueryNode.is(node) ||
InsertQueryNode.is(node) ||
UpdateQueryNode.is(node) ||
SelectQueryNode.is(node)
)
},
cloneWithWhere<T extends FilterableQueryNode>(
node: T,
operation: OperationNode
): T {
return freeze({
...node,
where: node.where
? WhereNode.cloneWithOperation(node.where, 'And', operation)
: WhereNode.create(operation),
})
},
cloneWithOrWhere<T extends FilterableQueryNode>(
node: T,
operation: OperationNode
): T {
return freeze({
...node,
where: node.where
? WhereNode.cloneWithOperation(node.where, 'Or', operation)
: WhereNode.create(operation),
})
},
cloneWithJoin<T extends FilterableQueryNode>(node: T, join: JoinNode): T {
return freeze({
...node,
joins: node.joins ? freeze([...node.joins, join]) : freeze([join]),
})
},
cloneWithReturning<T extends MutatingQueryNode>(
node: T,
selections: ReadonlyArray<SelectionNode>
): T {
return freeze({
...node,
returning: node.returning
? ReturningNode.cloneWithSelections(node.returning, selections)
: ReturningNode.create(selections),
})
},
cloneWithoutWhere<T extends FilterableQueryNode>(node: T): T {
return freeze({
...node,
where: undefined,
})
},
})