kysely 0.12.1 → 0.14.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +34 -10
- package/dist/cjs/{introspection → dialect}/database-introspector.d.ts +0 -0
- package/dist/cjs/{introspection → dialect}/database-introspector.js +0 -0
- package/dist/cjs/dialect/dialect.d.ts +1 -1
- package/dist/cjs/dialect/mysql/mysql-adapter.d.ts +2 -2
- package/dist/cjs/dialect/mysql/mysql-adapter.js +2 -1
- package/dist/cjs/dialect/mysql/mysql-dialect.d.ts +1 -1
- package/dist/cjs/dialect/mysql/mysql-introspector.d.ts +1 -1
- package/dist/cjs/dialect/postgres/postgres-adapter.d.ts +2 -2
- package/dist/cjs/dialect/postgres/postgres-adapter.js +2 -1
- package/dist/cjs/dialect/postgres/postgres-dialect.d.ts +1 -1
- package/dist/cjs/dialect/postgres/postgres-introspector.d.ts +1 -1
- package/dist/cjs/dialect/sqlite/sqlite-adapter.d.ts +2 -2
- package/dist/cjs/dialect/sqlite/sqlite-dialect.d.ts +1 -1
- package/dist/cjs/dialect/sqlite/sqlite-introspector.d.ts +1 -1
- package/dist/cjs/index.d.ts +5 -3
- package/dist/cjs/index.js +4 -2
- package/dist/cjs/kysely.d.ts +152 -1
- package/dist/cjs/kysely.js +37 -0
- package/dist/cjs/migration/migration.d.ts +154 -11
- package/dist/cjs/migration/migration.js +256 -135
- package/dist/cjs/operation-node/check-constraint-node.d.ts +1 -1
- package/dist/cjs/operation-node/check-constraint-node.js +2 -3
- package/dist/cjs/operation-node/column-definition-node.d.ts +8 -7
- package/dist/cjs/operation-node/column-definition-node.js +0 -4
- package/dist/cjs/operation-node/common-table-expression-name-node.d.ts +15 -0
- package/dist/cjs/operation-node/common-table-expression-name-node.js +23 -0
- package/dist/cjs/operation-node/common-table-expression-node.d.ts +3 -3
- package/dist/cjs/operation-node/data-type-node.d.ts +1 -1
- package/dist/cjs/operation-node/generated-node.d.ts +20 -0
- package/dist/cjs/operation-node/generated-node.js +31 -0
- package/dist/cjs/operation-node/on-conflict-node.d.ts +13 -2
- package/dist/cjs/operation-node/on-conflict-node.js +39 -1
- package/dist/cjs/operation-node/operation-node-transformer.d.ts +4 -2
- package/dist/cjs/operation-node/operation-node-transformer.js +22 -6
- package/dist/cjs/operation-node/operation-node-visitor.d.ts +4 -2
- package/dist/cjs/operation-node/operation-node-visitor.js +2 -1
- package/dist/cjs/operation-node/operation-node.d.ts +1 -1
- package/dist/cjs/operation-node/with-node.d.ts +3 -1
- package/dist/cjs/operation-node/with-node.js +2 -1
- package/dist/cjs/parser/reference-parser.d.ts +2 -2
- package/dist/cjs/parser/select-parser.d.ts +2 -2
- package/dist/cjs/parser/with-parser.d.ts +32 -5
- package/dist/cjs/parser/with-parser.js +13 -2
- package/dist/cjs/plugin/camel-case/camel-case-plugin.d.ts +2 -2
- package/dist/cjs/plugin/with-schema/with-schema-transformer.js +8 -0
- package/dist/cjs/query-builder/insert-query-builder.d.ts +76 -75
- package/dist/cjs/query-builder/insert-query-builder.js +150 -15
- package/dist/cjs/query-builder/on-conflict-builder.d.ts +686 -0
- package/dist/cjs/query-builder/on-conflict-builder.js +323 -0
- package/dist/cjs/query-compiler/default-query-compiler.d.ts +4 -2
- package/dist/cjs/query-compiler/default-query-compiler.js +48 -8
- package/dist/cjs/query-creator.d.ts +27 -3
- package/dist/cjs/query-creator.js +33 -1
- package/dist/cjs/query-executor/default-query-executor.d.ts +19 -0
- package/dist/cjs/query-executor/noop-query-executor.d.ts +19 -0
- package/dist/cjs/raw-builder/raw-builder.js +1 -1
- package/dist/cjs/schema/alter-table-builder.d.ts +41 -9
- package/dist/cjs/schema/alter-table-builder.js +45 -9
- package/dist/cjs/schema/column-definition-builder.d.ts +40 -8
- package/dist/cjs/schema/column-definition-builder.js +21 -8
- package/dist/cjs/schema/create-index-builder.d.ts +3 -2
- package/dist/cjs/schema/create-index-builder.js +2 -2
- package/dist/cjs/schema/create-table-builder.d.ts +3 -2
- package/dist/cjs/schema/create-table-builder.js +2 -2
- package/dist/cjs/util/type-utils.d.ts +11 -7
- package/dist/esm/{introspection → dialect}/database-introspector.d.ts +0 -0
- package/dist/esm/{introspection → dialect}/database-introspector.js +0 -0
- package/dist/esm/dialect/dialect.d.ts +1 -1
- package/dist/esm/dialect/mysql/mysql-adapter.d.ts +2 -2
- package/dist/esm/dialect/mysql/mysql-adapter.js +2 -1
- package/dist/esm/dialect/mysql/mysql-dialect.d.ts +1 -1
- package/dist/esm/dialect/mysql/mysql-introspector.d.ts +1 -1
- package/dist/esm/dialect/postgres/postgres-adapter.d.ts +2 -2
- package/dist/esm/dialect/postgres/postgres-adapter.js +2 -1
- package/dist/esm/dialect/postgres/postgres-dialect.d.ts +1 -1
- package/dist/esm/dialect/postgres/postgres-introspector.d.ts +1 -1
- package/dist/esm/dialect/sqlite/sqlite-adapter.d.ts +2 -2
- package/dist/esm/dialect/sqlite/sqlite-dialect.d.ts +1 -1
- package/dist/esm/dialect/sqlite/sqlite-introspector.d.ts +1 -1
- package/dist/esm/index.d.ts +5 -3
- package/dist/esm/index.js +4 -2
- package/dist/esm/kysely.d.ts +152 -1
- package/dist/esm/kysely.js +37 -0
- package/dist/esm/migration/migration.d.ts +154 -11
- package/dist/esm/migration/migration.js +255 -134
- package/dist/esm/operation-node/check-constraint-node.d.ts +1 -1
- package/dist/esm/operation-node/check-constraint-node.js +2 -3
- package/dist/esm/operation-node/column-definition-node.d.ts +8 -7
- package/dist/esm/operation-node/column-definition-node.js +0 -4
- package/dist/esm/operation-node/common-table-expression-name-node.d.ts +15 -0
- package/dist/esm/operation-node/common-table-expression-name-node.js +20 -0
- package/dist/esm/operation-node/common-table-expression-node.d.ts +3 -3
- package/dist/esm/operation-node/data-type-node.d.ts +1 -1
- package/dist/esm/operation-node/generated-node.d.ts +20 -0
- package/dist/esm/operation-node/generated-node.js +28 -0
- package/dist/esm/operation-node/on-conflict-node.d.ts +13 -2
- package/dist/esm/operation-node/on-conflict-node.js +39 -1
- package/dist/esm/operation-node/operation-node-transformer.d.ts +4 -2
- package/dist/esm/operation-node/operation-node-transformer.js +22 -6
- package/dist/esm/operation-node/operation-node-visitor.d.ts +4 -2
- package/dist/esm/operation-node/operation-node-visitor.js +2 -1
- package/dist/esm/operation-node/operation-node.d.ts +1 -1
- package/dist/esm/operation-node/with-node.d.ts +3 -1
- package/dist/esm/operation-node/with-node.js +2 -1
- package/dist/esm/parser/reference-parser.d.ts +2 -2
- package/dist/esm/parser/select-parser.d.ts +2 -2
- package/dist/esm/parser/with-parser.d.ts +32 -5
- package/dist/esm/parser/with-parser.js +13 -2
- package/dist/esm/plugin/camel-case/camel-case-plugin.d.ts +2 -2
- package/dist/esm/plugin/with-schema/with-schema-transformer.js +8 -0
- package/dist/esm/query-builder/insert-query-builder.d.ts +76 -75
- package/dist/esm/query-builder/insert-query-builder.js +150 -15
- package/dist/esm/query-builder/on-conflict-builder.d.ts +686 -0
- package/dist/esm/query-builder/on-conflict-builder.js +317 -0
- package/dist/esm/query-compiler/default-query-compiler.d.ts +4 -2
- package/dist/esm/query-compiler/default-query-compiler.js +48 -8
- package/dist/esm/query-creator.d.ts +27 -3
- package/dist/esm/query-creator.js +33 -1
- package/dist/esm/query-executor/default-query-executor.d.ts +19 -0
- package/dist/esm/query-executor/noop-query-executor.d.ts +19 -0
- package/dist/esm/raw-builder/raw-builder.js +1 -1
- package/dist/esm/schema/alter-table-builder.d.ts +41 -9
- package/dist/esm/schema/alter-table-builder.js +45 -9
- package/dist/esm/schema/column-definition-builder.d.ts +40 -8
- package/dist/esm/schema/column-definition-builder.js +21 -8
- package/dist/esm/schema/create-index-builder.d.ts +3 -2
- package/dist/esm/schema/create-index-builder.js +2 -2
- package/dist/esm/schema/create-table-builder.d.ts +3 -2
- package/dist/esm/schema/create-table-builder.js +2 -2
- package/dist/esm/util/type-utils.d.ts +11 -7
- package/package.json +2 -2
- package/dist/cjs/operation-node/generated-always-as-node.d.ts +0 -16
- package/dist/cjs/operation-node/generated-always-as-node.js +0 -25
- package/dist/cjs/parser/on-conflict-parser.d.ts +0 -10
- package/dist/cjs/parser/on-conflict-parser.js +0 -35
- package/dist/esm/operation-node/generated-always-as-node.d.ts +0 -16
- package/dist/esm/operation-node/generated-always-as-node.js +0 -22
- package/dist/esm/parser/on-conflict-parser.d.ts +0 -10
- package/dist/esm/parser/on-conflict-parser.js +0 -30
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
import { ColumnNode } from '../operation-node/column-node.js';
|
|
2
|
+
import { IdentifierNode } from '../operation-node/identifier-node.js';
|
|
3
|
+
import { OnConflictNode } from '../operation-node/on-conflict-node.js';
|
|
4
|
+
import { parseWhereFilter, parseReferenceFilter, parseExistFilter, parseNotExistFilter, } from '../parser/filter-parser.js';
|
|
5
|
+
import { parseUpdateObject, } from '../parser/update-set-parser.js';
|
|
6
|
+
import { freeze } from '../util/object-utils.js';
|
|
7
|
+
import { preventAwait } from '../util/prevent-await.js';
|
|
8
|
+
export class OnConflictBuilder {
|
|
9
|
+
#props;
|
|
10
|
+
constructor(props) {
|
|
11
|
+
this.#props = freeze(props);
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Specify a single column as the conflict target.
|
|
15
|
+
*
|
|
16
|
+
* Also see the {@link columns}, {@link constraint} and {@link expression}
|
|
17
|
+
* methods for alternative ways to specify the conflict target.
|
|
18
|
+
*/
|
|
19
|
+
column(column) {
|
|
20
|
+
const columnNode = ColumnNode.create(column);
|
|
21
|
+
return new OnConflictBuilder({
|
|
22
|
+
...this.#props,
|
|
23
|
+
onConflictNode: OnConflictNode.cloneWith(this.#props.onConflictNode, {
|
|
24
|
+
columns: this.#props.onConflictNode.columns
|
|
25
|
+
? freeze([...this.#props.onConflictNode.columns, columnNode])
|
|
26
|
+
: freeze([columnNode]),
|
|
27
|
+
}),
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Specify a list of columns as the conflict target.
|
|
32
|
+
*
|
|
33
|
+
* Also see the {@link column}, {@link constraint} and {@link expression}
|
|
34
|
+
* methods for alternative ways to specify the conflict target.
|
|
35
|
+
*/
|
|
36
|
+
columns(columns) {
|
|
37
|
+
const columnNodes = columns.map(ColumnNode.create);
|
|
38
|
+
return new OnConflictBuilder({
|
|
39
|
+
...this.#props,
|
|
40
|
+
onConflictNode: OnConflictNode.cloneWith(this.#props.onConflictNode, {
|
|
41
|
+
columns: this.#props.onConflictNode.columns
|
|
42
|
+
? freeze([...this.#props.onConflictNode.columns, ...columnNodes])
|
|
43
|
+
: freeze(columnNodes),
|
|
44
|
+
}),
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Specify a specific constraint by name as the conflict target.
|
|
49
|
+
*
|
|
50
|
+
* Also see the {@link column}, {@link columns} and {@link expression}
|
|
51
|
+
* methods for alternative ways to specify the conflict target.
|
|
52
|
+
*/
|
|
53
|
+
constraint(constraintName) {
|
|
54
|
+
return new OnConflictBuilder({
|
|
55
|
+
...this.#props,
|
|
56
|
+
onConflictNode: OnConflictNode.cloneWith(this.#props.onConflictNode, {
|
|
57
|
+
constraint: IdentifierNode.create(constraintName),
|
|
58
|
+
}),
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Specify an expression as the conflict target.
|
|
63
|
+
*
|
|
64
|
+
* This can be used if the unique index is an expression index.
|
|
65
|
+
*
|
|
66
|
+
* Also see the {@link column}, {@link columns} and {@link constraint}
|
|
67
|
+
* methods for alternative ways to specify the conflict target.
|
|
68
|
+
*/
|
|
69
|
+
expression(expression) {
|
|
70
|
+
return new OnConflictBuilder({
|
|
71
|
+
...this.#props,
|
|
72
|
+
onConflictNode: OnConflictNode.cloneWith(this.#props.onConflictNode, {
|
|
73
|
+
indexExpression: expression.toOperationNode(),
|
|
74
|
+
}),
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
where(...args) {
|
|
78
|
+
return new OnConflictBuilder({
|
|
79
|
+
...this.#props,
|
|
80
|
+
onConflictNode: OnConflictNode.cloneWithIndexWhere(this.#props.onConflictNode, parseWhereFilter(this.#props.parseContext, args)),
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Specify an index predicate for the index target.
|
|
85
|
+
*
|
|
86
|
+
* See {@link WhereInterface.whereRef} for more info.
|
|
87
|
+
*/
|
|
88
|
+
whereRef(lhs, op, rhs) {
|
|
89
|
+
return new OnConflictBuilder({
|
|
90
|
+
...this.#props,
|
|
91
|
+
onConflictNode: OnConflictNode.cloneWithIndexWhere(this.#props.onConflictNode, parseReferenceFilter(this.#props.parseContext, lhs, op, rhs)),
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
orWhere(...args) {
|
|
95
|
+
return new OnConflictBuilder({
|
|
96
|
+
...this.#props,
|
|
97
|
+
onConflictNode: OnConflictNode.cloneWithIndexOrWhere(this.#props.onConflictNode, parseWhereFilter(this.#props.parseContext, args)),
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Specify an index predicate for the index target.
|
|
102
|
+
*
|
|
103
|
+
* See {@link WhereInterface.orWhereRef} for more info.
|
|
104
|
+
*/
|
|
105
|
+
orWhereRef(lhs, op, rhs) {
|
|
106
|
+
return new OnConflictBuilder({
|
|
107
|
+
...this.#props,
|
|
108
|
+
onConflictNode: OnConflictNode.cloneWithIndexOrWhere(this.#props.onConflictNode, parseReferenceFilter(this.#props.parseContext, lhs, op, rhs)),
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Specify an index predicate for the index target.
|
|
113
|
+
*
|
|
114
|
+
* See {@link WhereInterface.whereExists} for more info.
|
|
115
|
+
*/
|
|
116
|
+
whereExists(arg) {
|
|
117
|
+
return new OnConflictBuilder({
|
|
118
|
+
...this.#props,
|
|
119
|
+
onConflictNode: OnConflictNode.cloneWithIndexWhere(this.#props.onConflictNode, parseExistFilter(this.#props.parseContext, arg)),
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Specify an index predicate for the index target.
|
|
124
|
+
*
|
|
125
|
+
* See {@link WhereInterface.whereNotExists} for more info.
|
|
126
|
+
*/
|
|
127
|
+
whereNotExists(arg) {
|
|
128
|
+
return new OnConflictBuilder({
|
|
129
|
+
...this.#props,
|
|
130
|
+
onConflictNode: OnConflictNode.cloneWithIndexWhere(this.#props.onConflictNode, parseNotExistFilter(this.#props.parseContext, arg)),
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Specify an index predicate for the index target.
|
|
135
|
+
*
|
|
136
|
+
* See {@link WhereInterface.orWhereExists} for more info.
|
|
137
|
+
*/
|
|
138
|
+
orWhereExists(arg) {
|
|
139
|
+
return new OnConflictBuilder({
|
|
140
|
+
...this.#props,
|
|
141
|
+
onConflictNode: OnConflictNode.cloneWithIndexOrWhere(this.#props.onConflictNode, parseExistFilter(this.#props.parseContext, arg)),
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Specify an index predicate for the index target.
|
|
146
|
+
*
|
|
147
|
+
* See {@link WhereInterface.orWhereNotExists} for more info.
|
|
148
|
+
*/
|
|
149
|
+
orWhereNotExists(arg) {
|
|
150
|
+
return new OnConflictBuilder({
|
|
151
|
+
...this.#props,
|
|
152
|
+
onConflictNode: OnConflictNode.cloneWithIndexOrWhere(this.#props.onConflictNode, parseNotExistFilter(this.#props.parseContext, arg)),
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Adds the "do nothing" conflict action.
|
|
157
|
+
*
|
|
158
|
+
* ### Examples
|
|
159
|
+
*
|
|
160
|
+
* ```ts
|
|
161
|
+
* await db
|
|
162
|
+
* .insertInto('person')
|
|
163
|
+
* .values({ id: db.generated, first_name, pic })
|
|
164
|
+
* .onConflict((oc) => oc
|
|
165
|
+
* .column('pic')
|
|
166
|
+
* .doNothing()
|
|
167
|
+
* )
|
|
168
|
+
* ```
|
|
169
|
+
*
|
|
170
|
+
* The generated SQL (PostgreSQL):
|
|
171
|
+
*
|
|
172
|
+
* ```sql
|
|
173
|
+
* insert into "person" ("first_name", "pic")
|
|
174
|
+
* values ($1, $2)
|
|
175
|
+
* on conflict ("pic") do nothing
|
|
176
|
+
* ```
|
|
177
|
+
*/
|
|
178
|
+
doNothing() {
|
|
179
|
+
return new OnConflictDoNothingBuilder({
|
|
180
|
+
...this.#props,
|
|
181
|
+
onConflictNode: OnConflictNode.cloneWith(this.#props.onConflictNode, {
|
|
182
|
+
doNothing: true,
|
|
183
|
+
}),
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Adds the "do update set" conflict action.
|
|
188
|
+
*
|
|
189
|
+
* ### Examples
|
|
190
|
+
*
|
|
191
|
+
* ```ts
|
|
192
|
+
* await db
|
|
193
|
+
* .insertInto('person')
|
|
194
|
+
* .values({ id: db.generated, first_name, pic })
|
|
195
|
+
* .onConflict((oc) => oc
|
|
196
|
+
* .column('pic')
|
|
197
|
+
* .doUpdateSet({ first_name })
|
|
198
|
+
* )
|
|
199
|
+
* ```
|
|
200
|
+
*
|
|
201
|
+
* The generated SQL (PostgreSQL):
|
|
202
|
+
*
|
|
203
|
+
* ```sql
|
|
204
|
+
* insert into "person" ("first_name", "pic")
|
|
205
|
+
* values ($1, $2)
|
|
206
|
+
* on conflict ("pic")
|
|
207
|
+
* do update set "first_name" = $3
|
|
208
|
+
* ```
|
|
209
|
+
*/
|
|
210
|
+
doUpdateSet(updates) {
|
|
211
|
+
return new OnConflictUpdateBuilder({
|
|
212
|
+
...this.#props,
|
|
213
|
+
onConflictNode: OnConflictNode.cloneWith(this.#props.onConflictNode, {
|
|
214
|
+
updates: parseUpdateObject(this.#props.parseContext, updates),
|
|
215
|
+
}),
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
preventAwait(OnConflictBuilder, "don't await OnConflictBuilder instances.");
|
|
220
|
+
export class OnConflictDoNothingBuilder {
|
|
221
|
+
#props;
|
|
222
|
+
constructor(props) {
|
|
223
|
+
this.#props = freeze(props);
|
|
224
|
+
}
|
|
225
|
+
toOperationNode() {
|
|
226
|
+
return this.#props.onConflictNode;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
preventAwait(OnConflictDoNothingBuilder, "don't await OnConflictDoNothingBuilder instances.");
|
|
230
|
+
export class OnConflictUpdateBuilder {
|
|
231
|
+
#props;
|
|
232
|
+
constructor(props) {
|
|
233
|
+
this.#props = freeze(props);
|
|
234
|
+
}
|
|
235
|
+
where(...args) {
|
|
236
|
+
return new OnConflictUpdateBuilder({
|
|
237
|
+
...this.#props,
|
|
238
|
+
onConflictNode: OnConflictNode.cloneWithUpdateWhere(this.#props.onConflictNode, parseWhereFilter(this.#props.parseContext, args)),
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* Specify a where condition for the update operation.
|
|
243
|
+
*
|
|
244
|
+
* See {@link WhereInterface.whereRef} for more info.
|
|
245
|
+
*/
|
|
246
|
+
whereRef(lhs, op, rhs) {
|
|
247
|
+
return new OnConflictUpdateBuilder({
|
|
248
|
+
...this.#props,
|
|
249
|
+
onConflictNode: OnConflictNode.cloneWithUpdateWhere(this.#props.onConflictNode, parseReferenceFilter(this.#props.parseContext, lhs, op, rhs)),
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
orWhere(...args) {
|
|
253
|
+
return new OnConflictUpdateBuilder({
|
|
254
|
+
...this.#props,
|
|
255
|
+
onConflictNode: OnConflictNode.cloneWithUpdateOrWhere(this.#props.onConflictNode, parseWhereFilter(this.#props.parseContext, args)),
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* Specify a where condition for the update operation.
|
|
260
|
+
*
|
|
261
|
+
* See {@link WhereInterface.orWhereRef} for more info.
|
|
262
|
+
*/
|
|
263
|
+
orWhereRef(lhs, op, rhs) {
|
|
264
|
+
return new OnConflictUpdateBuilder({
|
|
265
|
+
...this.#props,
|
|
266
|
+
onConflictNode: OnConflictNode.cloneWithUpdateOrWhere(this.#props.onConflictNode, parseReferenceFilter(this.#props.parseContext, lhs, op, rhs)),
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* Specify a where condition for the update operation.
|
|
271
|
+
*
|
|
272
|
+
* See {@link WhereInterface.whereExists} for more info.
|
|
273
|
+
*/
|
|
274
|
+
whereExists(arg) {
|
|
275
|
+
return new OnConflictUpdateBuilder({
|
|
276
|
+
...this.#props,
|
|
277
|
+
onConflictNode: OnConflictNode.cloneWithUpdateWhere(this.#props.onConflictNode, parseExistFilter(this.#props.parseContext, arg)),
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* Specify a where condition for the update operation.
|
|
282
|
+
*
|
|
283
|
+
* See {@link WhereInterface.whereNotExists} for more info.
|
|
284
|
+
*/
|
|
285
|
+
whereNotExists(arg) {
|
|
286
|
+
return new OnConflictUpdateBuilder({
|
|
287
|
+
...this.#props,
|
|
288
|
+
onConflictNode: OnConflictNode.cloneWithUpdateWhere(this.#props.onConflictNode, parseNotExistFilter(this.#props.parseContext, arg)),
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
/**
|
|
292
|
+
* Specify a where condition for the update operation.
|
|
293
|
+
*
|
|
294
|
+
* See {@link WhereInterface.orWhereExists} for more info.
|
|
295
|
+
*/
|
|
296
|
+
orWhereExists(arg) {
|
|
297
|
+
return new OnConflictUpdateBuilder({
|
|
298
|
+
...this.#props,
|
|
299
|
+
onConflictNode: OnConflictNode.cloneWithUpdateOrWhere(this.#props.onConflictNode, parseExistFilter(this.#props.parseContext, arg)),
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
/**
|
|
303
|
+
* Specify a where condition for the update operation.
|
|
304
|
+
*
|
|
305
|
+
* See {@link WhereInterface.orWhereNotExists} for more info.
|
|
306
|
+
*/
|
|
307
|
+
orWhereNotExists(arg) {
|
|
308
|
+
return new OnConflictUpdateBuilder({
|
|
309
|
+
...this.#props,
|
|
310
|
+
onConflictNode: OnConflictNode.cloneWithUpdateOrWhere(this.#props.onConflictNode, parseNotExistFilter(this.#props.parseContext, arg)),
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
toOperationNode() {
|
|
314
|
+
return this.#props.onConflictNode;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
preventAwait(OnConflictUpdateBuilder, "don't await OnConflictUpdateBuilder instances.");
|
|
@@ -64,10 +64,11 @@ import { ColumnNode } from '../operation-node/column-node.js';
|
|
|
64
64
|
import { UnionNode } from '../operation-node/union-node.js';
|
|
65
65
|
import { CreateViewNode } from '../operation-node/create-view-node.js';
|
|
66
66
|
import { DropViewNode } from '../operation-node/drop-view-node.js';
|
|
67
|
-
import {
|
|
67
|
+
import { GeneratedNode } from '../operation-node/generated-node.js';
|
|
68
68
|
import { DefaultValueNode } from '../operation-node/default-value-node.js';
|
|
69
69
|
import { OnNode } from '../operation-node/on-node.js';
|
|
70
70
|
import { ValuesNode } from '../operation-node/values-node.js';
|
|
71
|
+
import { CommonTableExpressionNameNode } from '../operation-node/common-table-expression-name-node.js';
|
|
71
72
|
export declare class DefaultQueryCompiler extends OperationNodeVisitor implements QueryCompiler {
|
|
72
73
|
#private;
|
|
73
74
|
protected get numParameters(): number;
|
|
@@ -129,6 +130,7 @@ export declare class DefaultQueryCompiler extends OperationNodeVisitor implement
|
|
|
129
130
|
protected visitList(node: ListNode): void;
|
|
130
131
|
protected visitWith(node: WithNode): void;
|
|
131
132
|
protected visitCommonTableExpression(node: CommonTableExpressionNode): void;
|
|
133
|
+
protected visitCommonTableExpressionName(node: CommonTableExpressionNameNode): void;
|
|
132
134
|
protected visitAlterTable(node: AlterTableNode): void;
|
|
133
135
|
protected visitAddColumn(node: AddColumnNode): void;
|
|
134
136
|
protected visitRenameColumn(node: RenameColumnNode): void;
|
|
@@ -140,7 +142,7 @@ export declare class DefaultQueryCompiler extends OperationNodeVisitor implement
|
|
|
140
142
|
protected visitUnion(node: UnionNode): void;
|
|
141
143
|
protected visitCreateView(node: CreateViewNode): void;
|
|
142
144
|
protected visitDropView(node: DropViewNode): void;
|
|
143
|
-
protected
|
|
145
|
+
protected visitGenerated(node: GeneratedNode): void;
|
|
144
146
|
protected visitDefaultValue(node: DefaultValueNode): void;
|
|
145
147
|
protected append(str: string): void;
|
|
146
148
|
protected appendValue(parameter: unknown): void;
|
|
@@ -321,15 +321,18 @@ export class DefaultQueryCompiler extends OperationNodeVisitor {
|
|
|
321
321
|
this.visitNode(node.column);
|
|
322
322
|
this.append(' ');
|
|
323
323
|
this.visitNode(node.dataType);
|
|
324
|
-
if (node.
|
|
324
|
+
if (node.unsigned) {
|
|
325
|
+
this.append(' unsigned');
|
|
326
|
+
}
|
|
327
|
+
if (node.generated) {
|
|
325
328
|
this.append(' ');
|
|
326
|
-
this.visitNode(node.
|
|
329
|
+
this.visitNode(node.generated);
|
|
327
330
|
}
|
|
328
331
|
if (node.defaultTo) {
|
|
329
332
|
this.append(' ');
|
|
330
333
|
this.visitNode(node.defaultTo);
|
|
331
334
|
}
|
|
332
|
-
if (
|
|
335
|
+
if (node.notNull) {
|
|
333
336
|
this.append(' not null');
|
|
334
337
|
}
|
|
335
338
|
if (node.unique) {
|
|
@@ -448,16 +451,29 @@ export class DefaultQueryCompiler extends OperationNodeVisitor {
|
|
|
448
451
|
this.compileList(node.columns);
|
|
449
452
|
this.append(')');
|
|
450
453
|
}
|
|
451
|
-
if (node.constraint) {
|
|
454
|
+
else if (node.constraint) {
|
|
452
455
|
this.append(' on constraint ');
|
|
453
456
|
this.visitNode(node.constraint);
|
|
454
457
|
}
|
|
458
|
+
else if (node.indexExpression) {
|
|
459
|
+
this.append(' (');
|
|
460
|
+
this.visitNode(node.indexExpression);
|
|
461
|
+
this.append(')');
|
|
462
|
+
}
|
|
463
|
+
if (node.indexWhere) {
|
|
464
|
+
this.append(' ');
|
|
465
|
+
this.visitNode(node.indexWhere);
|
|
466
|
+
}
|
|
455
467
|
if (node.doNothing === true) {
|
|
456
468
|
this.append(' do nothing');
|
|
457
469
|
}
|
|
458
470
|
else if (node.updates) {
|
|
459
471
|
this.append(' do update set ');
|
|
460
472
|
this.compileList(node.updates);
|
|
473
|
+
if (node.updateWhere) {
|
|
474
|
+
this.append(' ');
|
|
475
|
+
this.visitNode(node.updateWhere);
|
|
476
|
+
}
|
|
461
477
|
}
|
|
462
478
|
}
|
|
463
479
|
visitOnDuplicateKey(node) {
|
|
@@ -570,6 +586,9 @@ export class DefaultQueryCompiler extends OperationNodeVisitor {
|
|
|
570
586
|
}
|
|
571
587
|
visitWith(node) {
|
|
572
588
|
this.append('with ');
|
|
589
|
+
if (node.recursive) {
|
|
590
|
+
this.append('recursive ');
|
|
591
|
+
}
|
|
573
592
|
this.compileList(node.expressions);
|
|
574
593
|
}
|
|
575
594
|
visitCommonTableExpression(node) {
|
|
@@ -577,6 +596,14 @@ export class DefaultQueryCompiler extends OperationNodeVisitor {
|
|
|
577
596
|
this.append(' as ');
|
|
578
597
|
this.visitNode(node.expression);
|
|
579
598
|
}
|
|
599
|
+
visitCommonTableExpressionName(node) {
|
|
600
|
+
this.visitNode(node.table);
|
|
601
|
+
if (node.columns) {
|
|
602
|
+
this.append('(');
|
|
603
|
+
this.compileList(node.columns);
|
|
604
|
+
this.append(')');
|
|
605
|
+
}
|
|
606
|
+
}
|
|
580
607
|
visitAlterTable(node) {
|
|
581
608
|
this.append('alter table ');
|
|
582
609
|
this.visitNode(node.table);
|
|
@@ -708,10 +735,23 @@ export class DefaultQueryCompiler extends OperationNodeVisitor {
|
|
|
708
735
|
}
|
|
709
736
|
this.visitNode(node.name);
|
|
710
737
|
}
|
|
711
|
-
|
|
712
|
-
this.append('generated
|
|
713
|
-
|
|
714
|
-
|
|
738
|
+
visitGenerated(node) {
|
|
739
|
+
this.append('generated ');
|
|
740
|
+
if (node.always) {
|
|
741
|
+
this.append('always ');
|
|
742
|
+
}
|
|
743
|
+
if (node.byDefault) {
|
|
744
|
+
this.append('by default ');
|
|
745
|
+
}
|
|
746
|
+
this.append('as ');
|
|
747
|
+
if (node.identity) {
|
|
748
|
+
this.append('identity');
|
|
749
|
+
}
|
|
750
|
+
if (node.expression) {
|
|
751
|
+
this.append('(');
|
|
752
|
+
this.visitNode(node.expression);
|
|
753
|
+
this.append(')');
|
|
754
|
+
}
|
|
715
755
|
if (node.stored) {
|
|
716
756
|
this.append(' stored');
|
|
717
757
|
}
|
|
@@ -5,7 +5,7 @@ import { UpdateQueryBuilder } from './query-builder/update-query-builder.js';
|
|
|
5
5
|
import { TableExpression, TableExpressionDatabase, TableExpressionTables, TableReference } from './parser/table-parser.js';
|
|
6
6
|
import { QueryExecutor } from './query-executor/query-executor.js';
|
|
7
7
|
import { RawBuilder } from './raw-builder/raw-builder.js';
|
|
8
|
-
import { CommonTableExpression, QueryCreatorWithCommonTableExpression } from './parser/with-parser.js';
|
|
8
|
+
import { CommonTableExpression, QueryCreatorWithCommonTableExpression, RecursiveCommonTableExpression } from './parser/with-parser.js';
|
|
9
9
|
import { WithNode } from './operation-node/with-node.js';
|
|
10
10
|
import { ParseContext } from './parser/parse-context.js';
|
|
11
11
|
import { InsertResult } from './query-builder/insert-result.js';
|
|
@@ -206,7 +206,7 @@ export declare class QueryCreator<DB> {
|
|
|
206
206
|
*/
|
|
207
207
|
updateTable<TR extends TableReference<DB>>(table: TR): UpdateQueryBuilder<TableExpressionDatabase<DB, TR>, TableExpressionTables<DB, never, TR>, UpdateResult>;
|
|
208
208
|
/**
|
|
209
|
-
* Creates a `with` query (
|
|
209
|
+
* Creates a `with` query (Common Table Expression).
|
|
210
210
|
*
|
|
211
211
|
* ### Examples
|
|
212
212
|
*
|
|
@@ -227,8 +227,32 @@ export declare class QueryCreator<DB> {
|
|
|
227
227
|
* .selectAll()
|
|
228
228
|
* .execute()
|
|
229
229
|
* ```
|
|
230
|
+
*
|
|
231
|
+
* The CTE name can optionally specify column names in addition to
|
|
232
|
+
* a name. In that case Kysely requires the expression to retun
|
|
233
|
+
* rows with the same columns.
|
|
234
|
+
*
|
|
235
|
+
* ```ts
|
|
236
|
+
* await db
|
|
237
|
+
* .with('jennifers(id, age)', (db) => db
|
|
238
|
+
* .selectFrom('person')
|
|
239
|
+
* .where('first_name', '=', 'Jennifer')
|
|
240
|
+
* // This is ok since we return columns with the same
|
|
241
|
+
* // names as specified by `jennifers(id, age)`.
|
|
242
|
+
* .select(['id', 'age'])
|
|
243
|
+
* )
|
|
244
|
+
* .selectFrom('jennifers')
|
|
245
|
+
* .selectAll()
|
|
246
|
+
* .execute()
|
|
247
|
+
* ```
|
|
248
|
+
*/
|
|
249
|
+
with<N extends string, E extends CommonTableExpression<DB, N>>(name: N, expression: E): QueryCreatorWithCommonTableExpression<DB, N, E>;
|
|
250
|
+
/**
|
|
251
|
+
* Creates a recursive `with` query (Common Table Expression).
|
|
252
|
+
*
|
|
253
|
+
* See the {@link with} method for examples and more documentation.
|
|
230
254
|
*/
|
|
231
|
-
|
|
255
|
+
withRecursive<N extends string, E extends RecursiveCommonTableExpression<DB, N>>(name: N, expression: E): QueryCreatorWithCommonTableExpression<DB, N, E>;
|
|
232
256
|
/**
|
|
233
257
|
* Sets the schema to be used for all table references that don't explicitly
|
|
234
258
|
* specify a schema.
|
|
@@ -133,7 +133,7 @@ export class QueryCreator {
|
|
|
133
133
|
});
|
|
134
134
|
}
|
|
135
135
|
/**
|
|
136
|
-
* Creates a `with` query (
|
|
136
|
+
* Creates a `with` query (Common Table Expression).
|
|
137
137
|
*
|
|
138
138
|
* ### Examples
|
|
139
139
|
*
|
|
@@ -154,6 +154,24 @@ export class QueryCreator {
|
|
|
154
154
|
* .selectAll()
|
|
155
155
|
* .execute()
|
|
156
156
|
* ```
|
|
157
|
+
*
|
|
158
|
+
* The CTE name can optionally specify column names in addition to
|
|
159
|
+
* a name. In that case Kysely requires the expression to retun
|
|
160
|
+
* rows with the same columns.
|
|
161
|
+
*
|
|
162
|
+
* ```ts
|
|
163
|
+
* await db
|
|
164
|
+
* .with('jennifers(id, age)', (db) => db
|
|
165
|
+
* .selectFrom('person')
|
|
166
|
+
* .where('first_name', '=', 'Jennifer')
|
|
167
|
+
* // This is ok since we return columns with the same
|
|
168
|
+
* // names as specified by `jennifers(id, age)`.
|
|
169
|
+
* .select(['id', 'age'])
|
|
170
|
+
* )
|
|
171
|
+
* .selectFrom('jennifers')
|
|
172
|
+
* .selectAll()
|
|
173
|
+
* .execute()
|
|
174
|
+
* ```
|
|
157
175
|
*/
|
|
158
176
|
with(name, expression) {
|
|
159
177
|
const cte = parseCommonTableExpression(this.#props.parseContext, name, expression);
|
|
@@ -164,6 +182,20 @@ export class QueryCreator {
|
|
|
164
182
|
: WithNode.create(cte),
|
|
165
183
|
});
|
|
166
184
|
}
|
|
185
|
+
/**
|
|
186
|
+
* Creates a recursive `with` query (Common Table Expression).
|
|
187
|
+
*
|
|
188
|
+
* See the {@link with} method for examples and more documentation.
|
|
189
|
+
*/
|
|
190
|
+
withRecursive(name, expression) {
|
|
191
|
+
const cte = parseCommonTableExpression(this.#props.parseContext, name, expression);
|
|
192
|
+
return new QueryCreator({
|
|
193
|
+
...this.#props,
|
|
194
|
+
withNode: this.#props.withNode
|
|
195
|
+
? WithNode.cloneWithExpression(this.#props.withNode, cte)
|
|
196
|
+
: WithNode.create(cte, { recursive: true }),
|
|
197
|
+
});
|
|
198
|
+
}
|
|
167
199
|
/**
|
|
168
200
|
* Sets the schema to be used for all table references that don't explicitly
|
|
169
201
|
* specify a schema.
|
|
@@ -7,10 +7,29 @@ import { QueryExecutor } from './query-executor.js';
|
|
|
7
7
|
export declare class DefaultQueryExecutor extends QueryExecutor {
|
|
8
8
|
#private;
|
|
9
9
|
constructor(compiler: QueryCompiler, connectionProvider: ConnectionProvider, plugins?: KyselyPlugin[]);
|
|
10
|
+
/**
|
|
11
|
+
* Compiles the transformed query into SQL. You usually want to pass
|
|
12
|
+
* the output of {@link transformQuery} into this method but you can
|
|
13
|
+
* compile any query using this method.
|
|
14
|
+
*/
|
|
10
15
|
compileQuery(node: RootOperationNode): CompiledQuery;
|
|
11
16
|
protected executeQueryImpl<R>(compiledQuery: CompiledQuery): Promise<QueryResult<R>>;
|
|
17
|
+
/**
|
|
18
|
+
* Returns a copy of this executor with a plugin added as the
|
|
19
|
+
* last plugin.
|
|
20
|
+
*/
|
|
12
21
|
withPlugin(plugin: KyselyPlugin): DefaultQueryExecutor;
|
|
22
|
+
/**
|
|
23
|
+
* Returns a copy of this executor with a plugin added as the
|
|
24
|
+
* first plugin.
|
|
25
|
+
*/
|
|
13
26
|
withPluginAtFront(plugin: KyselyPlugin): DefaultQueryExecutor;
|
|
27
|
+
/**
|
|
28
|
+
* Returns a copy of this executor with a new connection provider.
|
|
29
|
+
*/
|
|
14
30
|
withConnectionProvider(connectionProvider: ConnectionProvider): QueryExecutor;
|
|
31
|
+
/**
|
|
32
|
+
* Returns a copy of this executor without any plugins.
|
|
33
|
+
*/
|
|
15
34
|
withoutPlugins(): QueryExecutor;
|
|
16
35
|
}
|
|
@@ -8,10 +8,29 @@ import { KyselyPlugin } from '../plugin/kysely-plugin.js';
|
|
|
8
8
|
* other needed things to actually execute queries.
|
|
9
9
|
*/
|
|
10
10
|
export declare class NoopQueryExecutor extends QueryExecutor {
|
|
11
|
+
/**
|
|
12
|
+
* Compiles the transformed query into SQL. You usually want to pass
|
|
13
|
+
* the output of {@link transformQuery} into this method but you can
|
|
14
|
+
* compile any query using this method.
|
|
15
|
+
*/
|
|
11
16
|
compileQuery(): CompiledQuery;
|
|
12
17
|
protected executeQueryImpl<R>(): Promise<QueryResult<R>>;
|
|
18
|
+
/**
|
|
19
|
+
* Returns a copy of this executor with a new connection provider.
|
|
20
|
+
*/
|
|
13
21
|
withConnectionProvider(): QueryExecutor;
|
|
22
|
+
/**
|
|
23
|
+
* Returns a copy of this executor with a plugin added as the
|
|
24
|
+
* last plugin.
|
|
25
|
+
*/
|
|
14
26
|
withPlugin(plugin: KyselyPlugin): NoopQueryExecutor;
|
|
27
|
+
/**
|
|
28
|
+
* Returns a copy of this executor with a plugin added as the
|
|
29
|
+
* first plugin.
|
|
30
|
+
*/
|
|
15
31
|
withPluginAtFront(plugin: KyselyPlugin): NoopQueryExecutor;
|
|
32
|
+
/**
|
|
33
|
+
* Returns a copy of this executor without any plugins.
|
|
34
|
+
*/
|
|
16
35
|
withoutPlugins(): QueryExecutor;
|
|
17
36
|
}
|
|
@@ -47,7 +47,7 @@ export class RawBuilder {
|
|
|
47
47
|
if (match.index > 0 && sql[match.index - 1] === '\\') {
|
|
48
48
|
continue;
|
|
49
49
|
}
|
|
50
|
-
sqlFragments.push(sql.slice(sqlIdx, match.index).
|
|
50
|
+
sqlFragments.push(sql.slice(sqlIdx, match.index).replace(/\\\?/g, '?'));
|
|
51
51
|
argNodes.push(parseRawArg(str, parameters[paramIdx]));
|
|
52
52
|
sqlIdx = match.index + str.length;
|
|
53
53
|
++paramIdx;
|