-
Notifications
You must be signed in to change notification settings - Fork 279
/
insert-values-parser.ts
95 lines (80 loc) · 2.82 KB
/
insert-values-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
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
import { ColumnNode } from '../operation-node/column-node.js'
import { InsertValuesNode } from '../operation-node/insert-query-node.js'
import { isOperationNodeSource } from '../operation-node/operation-node-source.js'
import { PrimitiveValueListNode } from '../operation-node/primitive-value-list-node.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 { ValueListNode } from '../operation-node/value-list-node.js'
import { ValueNode } from '../operation-node/value-node.js'
import {
AnyQueryBuilder,
AnyRawBuilder,
GeneratedPlaceholder,
} from '../util/type-utils.js'
import { isGeneratedPlaceholder } from '../util/generated-placeholder.js'
import { isPrimitive, PrimitiveValue } from '../util/object-utils.js'
export type InsertObject<DB, TB extends keyof DB> = {
[C in keyof DB[TB]]: InsertValueExpression<DB[TB][C]>
}
export type InsertObjectOrList<DB, TB extends keyof DB> =
| InsertObject<DB, TB>
| InsertObject<DB, TB>[]
type InsertValueExpression<T> =
| T
| AnyQueryBuilder
| AnyRawBuilder
| GeneratedPlaceholder
export function parseInsertObjectOrList(
args: InsertObjectOrList<any, any>
): [ReadonlyArray<ColumnNode>, ReadonlyArray<InsertValuesNode>] {
return parseInsertColumnsAndValues(Array.isArray(args) ? args : [args])
}
function parseInsertColumnsAndValues(
rows: InsertObject<any, any>[]
): [ReadonlyArray<ColumnNode>, ReadonlyArray<InsertValuesNode>] {
const columns: string[] = []
const values: InsertValuesNode[] = []
for (const row of rows) {
for (const column of Object.keys(row)) {
const value = row[column]
if (!columns.includes(column) && !isGeneratedPlaceholder(value)) {
columns.push(column)
}
}
}
for (const row of rows) {
const rowValues: InsertValueExpression<PrimitiveValue>[] = columns.map(
() => null
)
for (const column of Object.keys(row)) {
const columnIdx = columns.indexOf(column)
if (columnIdx !== -1) {
rowValues[columnIdx] = row[column]
}
}
if (rowValues.every(isPrimitive)) {
values.push(PrimitiveValueListNode.create(rowValues))
} else {
values.push(
ValueListNode.create(rowValues.map(parseInsertValueExpression))
)
}
}
return [columns.map(ColumnNode.create), values]
}
export function parseInsertValueExpression(
value: InsertValueExpression<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
}
}
throw new Error(
`unsupported value for insert object ${JSON.stringify(value)}`
)
}