-
Notifications
You must be signed in to change notification settings - Fork 290
/
Copy pathupdate-set-parser.ts
67 lines (60 loc) · 1.94 KB
/
update-set-parser.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
import { ColumnNode } from '../operation-node/column-node.js'
import { ColumnUpdateNode } from '../operation-node/column-update-node.js'
import { isOperationNodeSource } from '../operation-node/operation-node-source.js'
import { QueryNode } from '../operation-node/query-node.js'
import { RawNode } from '../operation-node/raw-node.js'
import { SelectQueryNode } from '../operation-node/select-query-node.js'
import { ValueNode } from '../operation-node/value-node.js'
import {
AnyQueryBuilder,
AnyRawBuilder,
QueryBuilderFactory,
RawBuilderFactory,
} from '../util/type-utils.js'
import {
isFunction,
isPrimitive,
PrimitiveValue,
} from '../util/object-utils.js'
import { ParseContext } from './parse-context.js'
export type MutationObject<DB, TB extends keyof DB> = {
[C in keyof DB[TB]]?: MutationValueExpression<DB, TB, DB[TB][C]>
}
export type MutationValueExpression<DB, TB extends keyof DB, T> =
| T
| AnyQueryBuilder
| QueryBuilderFactory<DB, TB>
| AnyRawBuilder
| RawBuilderFactory<DB, TB>
export function parseUpdateObject(
ctx: ParseContext,
row: MutationObject<any, any>
): ReadonlyArray<ColumnUpdateNode> {
return Object.entries(row).map(([key, value]) => {
return ColumnUpdateNode.create(
ColumnNode.create(key),
parseMutationValueExpression(ctx, value)
)
})
}
export function parseMutationValueExpression(
ctx: ParseContext,
value: MutationValueExpression<any, any, PrimitiveValue>
): ValueNode | RawNode | SelectQueryNode {
if (isPrimitive(value)) {
return ValueNode.create(value)
} else if (isOperationNodeSource(value)) {
const node = value.toOperationNode()
if (!QueryNode.isMutating(node)) {
return node
}
} else if (isFunction(value)) {
const node = value(ctx.createExpressionBuilder()).toOperationNode()
if (!QueryNode.isMutating(node)) {
return node
}
}
throw new Error(
`unsupported value for mutation object ${JSON.stringify(value)}`
)
}