kysely 0.12.0 → 0.12.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/dist/cjs/dialect/dialect-adapter-base.d.ts +36 -0
- package/dist/cjs/dialect/mysql/mysql-adapter.d.ts +36 -0
- package/dist/cjs/dialect/mysql/mysql-dialect.d.ts +16 -0
- package/dist/cjs/dialect/mysql/mysql-driver.d.ts +29 -0
- package/dist/cjs/dialect/mysql/mysql-introspector.d.ts +3 -0
- package/dist/cjs/dialect/postgres/postgres-adapter.d.ts +36 -0
- package/dist/cjs/dialect/postgres/postgres-dialect.d.ts +16 -0
- package/dist/cjs/dialect/postgres/postgres-driver.d.ts +29 -0
- package/dist/cjs/dialect/postgres/postgres-introspector.d.ts +3 -0
- package/dist/cjs/dialect/sqlite/sqlite-adapter.d.ts +36 -0
- package/dist/cjs/dialect/sqlite/sqlite-dialect.d.ts +16 -0
- package/dist/cjs/dialect/sqlite/sqlite-driver.d.ts +29 -0
- package/dist/cjs/dialect/sqlite/sqlite-introspector.d.ts +3 -0
- package/dist/cjs/driver/default-connection-provider.d.ts +4 -0
- package/dist/cjs/driver/runtime-driver.d.ts +29 -0
- package/dist/cjs/driver/single-connection-provider.d.ts +4 -0
- package/dist/cjs/package.json +1 -3
- package/dist/cjs/parser/parse-context.d.ts +14 -0
- package/dist/cjs/plugin/camel-case/camel-case-plugin.d.ts +34 -0
- package/dist/cjs/plugin/immediate-value.ts/immediate-value-plugin.d.ts +34 -0
- package/dist/cjs/plugin/with-schema/with-schema-plugin.d.ts +34 -0
- package/dist/cjs/query-builder/delete-query-builder.d.ts +545 -0
- package/dist/cjs/query-builder/insert-query-builder.d.ts +69 -4
- package/dist/cjs/query-builder/join-interface.d.ts +3 -6
- package/dist/cjs/query-builder/returning-interface.d.ts +1 -1
- package/dist/cjs/query-builder/select-query-builder.d.ts +513 -2
- package/dist/cjs/query-builder/select-query-builder.js +2 -2
- package/dist/cjs/query-builder/update-query-builder.d.ts +547 -2
- package/dist/cjs/query-builder/update-query-builder.js +2 -2
- package/dist/cjs/query-builder/where-interface.d.ts +7 -10
- package/dist/cjs/schema/alter-table-builder.d.ts +216 -0
- package/dist/cjs/schema/column-definition-builder.d.ts +108 -0
- package/dist/esm/dialect/dialect-adapter-base.d.ts +36 -0
- package/dist/esm/dialect/mysql/mysql-adapter.d.ts +36 -0
- package/dist/esm/dialect/mysql/mysql-dialect.d.ts +16 -0
- package/dist/esm/dialect/mysql/mysql-driver.d.ts +29 -0
- package/dist/esm/dialect/mysql/mysql-introspector.d.ts +3 -0
- package/dist/esm/dialect/postgres/postgres-adapter.d.ts +36 -0
- package/dist/esm/dialect/postgres/postgres-dialect.d.ts +16 -0
- package/dist/esm/dialect/postgres/postgres-driver.d.ts +29 -0
- package/dist/esm/dialect/postgres/postgres-introspector.d.ts +3 -0
- package/dist/esm/dialect/sqlite/sqlite-adapter.d.ts +36 -0
- package/dist/esm/dialect/sqlite/sqlite-dialect.d.ts +16 -0
- package/dist/esm/dialect/sqlite/sqlite-driver.d.ts +29 -0
- package/dist/esm/dialect/sqlite/sqlite-introspector.d.ts +3 -0
- package/dist/esm/driver/default-connection-provider.d.ts +4 -0
- package/dist/esm/driver/runtime-driver.d.ts +29 -0
- package/dist/esm/driver/single-connection-provider.d.ts +4 -0
- package/dist/esm/package.json +1 -3
- package/dist/esm/parser/parse-context.d.ts +14 -0
- package/dist/esm/plugin/camel-case/camel-case-plugin.d.ts +34 -0
- package/dist/esm/plugin/immediate-value.ts/immediate-value-plugin.d.ts +34 -0
- package/dist/esm/plugin/with-schema/with-schema-plugin.d.ts +34 -0
- package/dist/esm/query-builder/delete-query-builder.d.ts +545 -0
- package/dist/esm/query-builder/insert-query-builder.d.ts +69 -4
- package/dist/esm/query-builder/join-interface.d.ts +3 -6
- package/dist/esm/query-builder/returning-interface.d.ts +1 -1
- package/dist/esm/query-builder/select-query-builder.d.ts +513 -2
- package/dist/esm/query-builder/select-query-builder.js +2 -2
- package/dist/esm/query-builder/update-query-builder.d.ts +547 -2
- package/dist/esm/query-builder/update-query-builder.js +2 -2
- package/dist/esm/query-builder/where-interface.d.ts +7 -10
- package/dist/esm/schema/alter-table-builder.d.ts +216 -0
- package/dist/esm/schema/column-definition-builder.d.ts +108 -0
- package/package.json +7 -5
|
@@ -21,24 +21,503 @@ import { NoResultErrorConstructor } from './no-result-error.js';
|
|
|
21
21
|
export declare class UpdateQueryBuilder<DB, TB extends keyof DB, O> implements WhereInterface<DB, TB>, JoinInterface<DB, TB>, ReturningInterface<DB, TB, O>, OperationNodeSource, Compilable {
|
|
22
22
|
#private;
|
|
23
23
|
constructor(props: UpdateQueryBuilderProps);
|
|
24
|
+
/**
|
|
25
|
+
* Adds a `where` clause to the query.
|
|
26
|
+
*
|
|
27
|
+
* Also see {@link orWhere}, {@link whereExists} and {@link whereRef}.
|
|
28
|
+
*
|
|
29
|
+
* ### Examples
|
|
30
|
+
*
|
|
31
|
+
* Find a row by column value:
|
|
32
|
+
*
|
|
33
|
+
* ```ts
|
|
34
|
+
* const person = await db
|
|
35
|
+
* .selectFrom('person')
|
|
36
|
+
* .selectAll()
|
|
37
|
+
* .where('id', '=', 100)
|
|
38
|
+
* .executeTakeFirst()
|
|
39
|
+
* ```
|
|
40
|
+
*
|
|
41
|
+
* The generated SQL (PostgreSQL):
|
|
42
|
+
*
|
|
43
|
+
* ```sql
|
|
44
|
+
* select * from "person" where "id" = $1
|
|
45
|
+
* ```
|
|
46
|
+
*
|
|
47
|
+
* Operator can be any supported operator or if the typings don't support it
|
|
48
|
+
* you can always use `db.raw('your operator')`.
|
|
49
|
+
*
|
|
50
|
+
* ```ts
|
|
51
|
+
* const persons = await db
|
|
52
|
+
* .selectFrom('person')
|
|
53
|
+
* .selectAll()
|
|
54
|
+
* .where('id', '>', 100)
|
|
55
|
+
* .execute()
|
|
56
|
+
* ```
|
|
57
|
+
*
|
|
58
|
+
* The generated SQL (PostgreSQL):
|
|
59
|
+
*
|
|
60
|
+
* ```sql
|
|
61
|
+
* select * from "person" where "id" > $1
|
|
62
|
+
* ```
|
|
63
|
+
*
|
|
64
|
+
* The `where` methods don't change the type of the query. You can add
|
|
65
|
+
* conditional `where`s easily by doing something like this:
|
|
66
|
+
*
|
|
67
|
+
* ```ts
|
|
68
|
+
* let query = db
|
|
69
|
+
* .selectFrom('person')
|
|
70
|
+
* .selectAll()
|
|
71
|
+
*
|
|
72
|
+
* if (firstName) {
|
|
73
|
+
* query = query.where('first_name', '=', firstName)
|
|
74
|
+
* }
|
|
75
|
+
*
|
|
76
|
+
* const persons = await query.execute()
|
|
77
|
+
* ```
|
|
78
|
+
*
|
|
79
|
+
* This is true for basically all methods execpt the `select` and
|
|
80
|
+
* `returning` methods, which DO change the return type of the
|
|
81
|
+
* query.
|
|
82
|
+
*
|
|
83
|
+
* Both the first and third argument can also be subqueries.
|
|
84
|
+
* A subquery is defined by passing a function and calling
|
|
85
|
+
* the `selectFrom` method of the object passed into the
|
|
86
|
+
* function:
|
|
87
|
+
*
|
|
88
|
+
* ```ts
|
|
89
|
+
* const persons = await db
|
|
90
|
+
* .selectFrom('person')
|
|
91
|
+
* .selectAll()
|
|
92
|
+
* .where(
|
|
93
|
+
* (qb) => qb.selectFrom('pet')
|
|
94
|
+
* .select('pet.name')
|
|
95
|
+
* .whereRef('pet.owner_id', '=', 'person.id')
|
|
96
|
+
* .limit(1),
|
|
97
|
+
* '=',
|
|
98
|
+
* 'Fluffy'
|
|
99
|
+
* )
|
|
100
|
+
* .execute()
|
|
101
|
+
* ```
|
|
102
|
+
*
|
|
103
|
+
* The generated SQL (PostgreSQL):
|
|
104
|
+
*
|
|
105
|
+
* ```sql
|
|
106
|
+
* select *
|
|
107
|
+
* from "person"
|
|
108
|
+
* where (
|
|
109
|
+
* select "pet"."name"
|
|
110
|
+
* from "pet"
|
|
111
|
+
* where "pet"."owner_id" = "person"."id"
|
|
112
|
+
* limit $1
|
|
113
|
+
* ) = $2
|
|
114
|
+
* ```
|
|
115
|
+
*
|
|
116
|
+
* A `where in` query can be built by using the `in` operator and an array
|
|
117
|
+
* of values. The values in the array can also be subqueries or raw
|
|
118
|
+
* instances.
|
|
119
|
+
*
|
|
120
|
+
* ```ts
|
|
121
|
+
* const persons = await db
|
|
122
|
+
* .selectFrom('person')
|
|
123
|
+
* .selectAll()
|
|
124
|
+
* .where('person.id', 'in', [100, 200, 300])
|
|
125
|
+
* .execute()
|
|
126
|
+
* ```
|
|
127
|
+
*
|
|
128
|
+
* The generated SQL (PostgreSQL):
|
|
129
|
+
*
|
|
130
|
+
* ```sql
|
|
131
|
+
* select * from "person" where "id" in ($1, $2, $3)
|
|
132
|
+
* ```
|
|
133
|
+
*
|
|
134
|
+
* If everything else fails, you can always pass {@link Kysely.raw | raw}
|
|
135
|
+
* as any of the arguments, including the operator:
|
|
136
|
+
*
|
|
137
|
+
* ```ts
|
|
138
|
+
* const persons = await db
|
|
139
|
+
* .selectFrom('person')
|
|
140
|
+
* .selectAll()
|
|
141
|
+
* .where(
|
|
142
|
+
* db.raw('coalesce(first_name, last_name)'),
|
|
143
|
+
* 'like',
|
|
144
|
+
* '%' + name + '%',
|
|
145
|
+
* )
|
|
146
|
+
* .execute()
|
|
147
|
+
* ```
|
|
148
|
+
*
|
|
149
|
+
* The generated SQL (PostgreSQL):
|
|
150
|
+
*
|
|
151
|
+
* ```sql
|
|
152
|
+
* select * from "person"
|
|
153
|
+
* where coalesce(first_name, last_name) like $1
|
|
154
|
+
* ```
|
|
155
|
+
*
|
|
156
|
+
* If you only pass one function argument to this method, it can be
|
|
157
|
+
* used to create parentheses around other where statements:
|
|
158
|
+
*
|
|
159
|
+
* ```ts
|
|
160
|
+
* const persons = await db
|
|
161
|
+
* .selectFrom('person')
|
|
162
|
+
* .selectAll()
|
|
163
|
+
* .where((qb) => qb
|
|
164
|
+
* .where('id', '=', 1)
|
|
165
|
+
* .orWhere('id', '=', 2)
|
|
166
|
+
* )
|
|
167
|
+
* .execute()
|
|
168
|
+
* ```
|
|
169
|
+
*
|
|
170
|
+
* The generated SQL (PostgreSQL):
|
|
171
|
+
*
|
|
172
|
+
* ```sql
|
|
173
|
+
* select * from "person" (where "id" = 1 or "id" = 2)
|
|
174
|
+
* ```
|
|
175
|
+
*
|
|
176
|
+
* In all examples above the columns were known at compile time
|
|
177
|
+
* (except for the `raw` expressions). By default kysely only allows
|
|
178
|
+
* you to refer to columns that exist in the database **and** can be
|
|
179
|
+
* referred to in the current query and context.
|
|
180
|
+
*
|
|
181
|
+
* Sometimes you may want to refer to columns that come from the user
|
|
182
|
+
* input and thus are not available at compile time.
|
|
183
|
+
*
|
|
184
|
+
* You have two options, `db.raw` or `db.dynamic`. The example below
|
|
185
|
+
* uses both:
|
|
186
|
+
*
|
|
187
|
+
* ```ts
|
|
188
|
+
* const { ref } = db.dynamic
|
|
189
|
+
*
|
|
190
|
+
* const persons = await db
|
|
191
|
+
* .selectFrom('person')
|
|
192
|
+
* .selectAll()
|
|
193
|
+
* .where(ref(columnFromUserInput), '=', 1)
|
|
194
|
+
* .orWhere(db.raw('??', [columnFromUserInput]), '=', 2)
|
|
195
|
+
* .execute()
|
|
196
|
+
* ```
|
|
197
|
+
*/
|
|
24
198
|
where<RE extends ReferenceExpression<DB, TB>>(lhs: RE, op: FilterOperator, rhs: FilterValueExpressionOrList<DB, TB, RE>): UpdateQueryBuilder<DB, TB, O>;
|
|
25
199
|
where(grouper: WhereGrouper<DB, TB>): UpdateQueryBuilder<DB, TB, O>;
|
|
26
200
|
where(raw: AnyRawBuilder): UpdateQueryBuilder<DB, TB, O>;
|
|
201
|
+
/**
|
|
202
|
+
* Adds a `where` clause where both sides of the operator are references
|
|
203
|
+
* to columns.
|
|
204
|
+
*
|
|
205
|
+
* The normal `where` method treats the right hand side argument as a
|
|
206
|
+
* value by default. `whereRef` treats it as a column reference. This method is
|
|
207
|
+
* expecially useful with joins and correclated subqueries.
|
|
208
|
+
*
|
|
209
|
+
* ### Examples
|
|
210
|
+
*
|
|
211
|
+
* Usage with a join:
|
|
212
|
+
*
|
|
213
|
+
* ```ts
|
|
214
|
+
* db.selectFrom(['person', 'pet'])
|
|
215
|
+
* .selectAll()
|
|
216
|
+
* .whereRef('person.first_name', '=', 'pet.name')
|
|
217
|
+
* ```
|
|
218
|
+
*
|
|
219
|
+
* The generated SQL (PostgreSQL):
|
|
220
|
+
*
|
|
221
|
+
* ```sql
|
|
222
|
+
* select * from "person", "pet" where "person"."first_name" = "pet"."name"
|
|
223
|
+
* ```
|
|
224
|
+
*
|
|
225
|
+
* Usage in a subquery:
|
|
226
|
+
*
|
|
227
|
+
* ```ts
|
|
228
|
+
* const persons = await db
|
|
229
|
+
* .selectFrom('person')
|
|
230
|
+
* .selectAll('person')
|
|
231
|
+
* .select((qb) => qb
|
|
232
|
+
* .selectFrom('pet')
|
|
233
|
+
* .select('name')
|
|
234
|
+
* .whereRef('pet.owner_id', '=', 'person.id')
|
|
235
|
+
* .limit(1)
|
|
236
|
+
* .as('pet_name')
|
|
237
|
+
* )
|
|
238
|
+
* .execute()
|
|
239
|
+
* ```
|
|
240
|
+
*
|
|
241
|
+
* The generated SQL (PostgreSQL):
|
|
242
|
+
*
|
|
243
|
+
* ```sql
|
|
244
|
+
* select "person".*, (
|
|
245
|
+
* select "name"
|
|
246
|
+
* from "pet"
|
|
247
|
+
* where "pet"."owner_id" = "person"."id"
|
|
248
|
+
* limit $1
|
|
249
|
+
* ) as "pet_name"
|
|
250
|
+
* from "person"
|
|
251
|
+
*/
|
|
27
252
|
whereRef(lhs: ReferenceExpression<DB, TB>, op: FilterOperator, rhs: ReferenceExpression<DB, TB>): UpdateQueryBuilder<DB, TB, O>;
|
|
253
|
+
/**
|
|
254
|
+
* Adds an `or where` clause to the query. Otherwise works just like {@link where}.
|
|
255
|
+
*
|
|
256
|
+
* It's often necessary to wrap `or where` clauses in parentheses to control
|
|
257
|
+
* precendence. You can use the one argument version of the `where` method
|
|
258
|
+
* for that. See the examples.
|
|
259
|
+
*
|
|
260
|
+
* ### Examples
|
|
261
|
+
*
|
|
262
|
+
* ```ts
|
|
263
|
+
* const persons = await db
|
|
264
|
+
* .selectFrom('person')
|
|
265
|
+
* .selectAll()
|
|
266
|
+
* .where('id', '=', 1)
|
|
267
|
+
* .orWhere('id', '=', 2)
|
|
268
|
+
* .execute()
|
|
269
|
+
* ```
|
|
270
|
+
*
|
|
271
|
+
* The generated SQL (PostgreSQL):
|
|
272
|
+
*
|
|
273
|
+
* ```sql
|
|
274
|
+
* select * from "person" where "id" = 1 or "id" = 2
|
|
275
|
+
* ```
|
|
276
|
+
*
|
|
277
|
+
* Grouping with parentheses:
|
|
278
|
+
*
|
|
279
|
+
* ```ts
|
|
280
|
+
* const persons = await db
|
|
281
|
+
* .selectFrom('person')
|
|
282
|
+
* .selectAll()
|
|
283
|
+
* .where((qb) => qb
|
|
284
|
+
* .where('id', '=', 1)
|
|
285
|
+
* .orWhere('id', '=', 2)
|
|
286
|
+
* )
|
|
287
|
+
* .execute()
|
|
288
|
+
* ```
|
|
289
|
+
*
|
|
290
|
+
* The generated SQL (PostgreSQL):
|
|
291
|
+
*
|
|
292
|
+
* ```sql
|
|
293
|
+
* select * from "person" (where "id" = 1 or "id" = 2)
|
|
294
|
+
* ```
|
|
295
|
+
*
|
|
296
|
+
* Even the first `where` can be an `orWhere`. This is useful
|
|
297
|
+
* if you are looping through a set of conditions:
|
|
298
|
+
*
|
|
299
|
+
* ```ts
|
|
300
|
+
* const persons = await db
|
|
301
|
+
* .selectFrom('person')
|
|
302
|
+
* .selectAll()
|
|
303
|
+
* .where((qb) => qb
|
|
304
|
+
* .orWhere('id', '=', 1)
|
|
305
|
+
* .orWhere('id', '=', 2)
|
|
306
|
+
* )
|
|
307
|
+
* .execute()
|
|
308
|
+
* ```
|
|
309
|
+
*
|
|
310
|
+
* The generated SQL (PostgreSQL):
|
|
311
|
+
*
|
|
312
|
+
* ```sql
|
|
313
|
+
* select * from "person" (where "id" = 1 or "id" = 2)
|
|
314
|
+
* ```
|
|
315
|
+
*/
|
|
28
316
|
orWhere<RE extends ReferenceExpression<DB, TB>>(lhs: RE, op: FilterOperator, rhs: FilterValueExpressionOrList<DB, TB, RE>): UpdateQueryBuilder<DB, TB, O>;
|
|
29
317
|
orWhere(grouper: WhereGrouper<DB, TB>): UpdateQueryBuilder<DB, TB, O>;
|
|
30
318
|
orWhere(raw: AnyRawBuilder): UpdateQueryBuilder<DB, TB, O>;
|
|
319
|
+
/**
|
|
320
|
+
* Adds an `or where` clause to the query. Otherwise works just like {@link whereRef}.
|
|
321
|
+
*
|
|
322
|
+
* Also see {@link orWhere} and {@link where}.
|
|
323
|
+
*/
|
|
31
324
|
orWhereRef(lhs: ReferenceExpression<DB, TB>, op: FilterOperator, rhs: ReferenceExpression<DB, TB>): UpdateQueryBuilder<DB, TB, O>;
|
|
325
|
+
/**
|
|
326
|
+
* Adds a `where exists` clause to the query.
|
|
327
|
+
*
|
|
328
|
+
* You can either use a subquery or a raw instance.
|
|
329
|
+
*
|
|
330
|
+
* ### Examples
|
|
331
|
+
*
|
|
332
|
+
* The query below selets all persons that own a pet named Catto:
|
|
333
|
+
*
|
|
334
|
+
* ```ts
|
|
335
|
+
* const persons = await db
|
|
336
|
+
* .selectFrom('person')
|
|
337
|
+
* .selectAll()
|
|
338
|
+
* .whereExists((qb) => qb
|
|
339
|
+
* .selectFrom('pet')
|
|
340
|
+
* .select('pet.id')
|
|
341
|
+
* .whereRef('person.id', '=', 'pet.owner_id')
|
|
342
|
+
* .where('pet.name', '=', 'Catto')
|
|
343
|
+
* )
|
|
344
|
+
* .execute()
|
|
345
|
+
* ```
|
|
346
|
+
*
|
|
347
|
+
* The generated SQL (PostgreSQL):
|
|
348
|
+
*
|
|
349
|
+
* ```sql
|
|
350
|
+
* select * from "person"
|
|
351
|
+
* where exists (
|
|
352
|
+
* select "pet"."id"
|
|
353
|
+
* from "pet"
|
|
354
|
+
* where "person"."id" = "pet"."owner_id"
|
|
355
|
+
* and "pet"."name" = $1
|
|
356
|
+
* )
|
|
357
|
+
* ```
|
|
358
|
+
*
|
|
359
|
+
* The same query as in the previous example but with using raw:
|
|
360
|
+
*
|
|
361
|
+
* ```ts
|
|
362
|
+
* db.selectFrom('person')
|
|
363
|
+
* .selectAll()
|
|
364
|
+
* .whereExists(
|
|
365
|
+
* db.raw(
|
|
366
|
+
* '(select pet.id from pet where person.id = pet.owner_id and pet.name = ?)',
|
|
367
|
+
* ['Catto']
|
|
368
|
+
* )
|
|
369
|
+
* )
|
|
370
|
+
* ```
|
|
371
|
+
*
|
|
372
|
+
* The generated SQL (PostgreSQL):
|
|
373
|
+
*
|
|
374
|
+
* ```sql
|
|
375
|
+
* select * from "person"
|
|
376
|
+
* where exists (
|
|
377
|
+
* select pet.id
|
|
378
|
+
* from pet
|
|
379
|
+
* where person.id = pet.owner_id
|
|
380
|
+
* and pet.name = $1
|
|
381
|
+
* )
|
|
382
|
+
* ```
|
|
383
|
+
*/
|
|
32
384
|
whereExists(arg: ExistsExpression<DB, TB>): UpdateQueryBuilder<DB, TB, O>;
|
|
385
|
+
/**
|
|
386
|
+
* Just like {@link whereExists} but creates a `not exists` clause.
|
|
387
|
+
*/
|
|
33
388
|
whereNotExists(arg: ExistsExpression<DB, TB>): UpdateQueryBuilder<DB, TB, O>;
|
|
389
|
+
/**
|
|
390
|
+
* Just like {@link whereExists} but creates a `or exists` clause.
|
|
391
|
+
*/
|
|
34
392
|
orWhereExists(arg: ExistsExpression<DB, TB>): UpdateQueryBuilder<DB, TB, O>;
|
|
393
|
+
/**
|
|
394
|
+
* Just like {@link whereExists} but creates a `or not exists` clause.
|
|
395
|
+
*/
|
|
35
396
|
orWhereNotExists(arg: ExistsExpression<DB, TB>): UpdateQueryBuilder<DB, TB, O>;
|
|
397
|
+
/**
|
|
398
|
+
* Joins another table to the query using an inner join.
|
|
399
|
+
*
|
|
400
|
+
* ### Examples
|
|
401
|
+
*
|
|
402
|
+
* Simple usage by providing a table name and two columns to join:
|
|
403
|
+
*
|
|
404
|
+
* ```ts
|
|
405
|
+
* const result = await db
|
|
406
|
+
* .selectFrom('person')
|
|
407
|
+
* .innerJoin('pet', 'pet.owner_id', 'person.id')
|
|
408
|
+
* // `select` needs to come after the call to `innerJoin` so
|
|
409
|
+
* // that you can select from the joined table.
|
|
410
|
+
* .select('person.id', 'pet.name')
|
|
411
|
+
* .execute()
|
|
412
|
+
*
|
|
413
|
+
* result[0].id
|
|
414
|
+
* result[0].name
|
|
415
|
+
* ```
|
|
416
|
+
*
|
|
417
|
+
* The generated SQL (PostgreSQL):
|
|
418
|
+
*
|
|
419
|
+
* ```sql
|
|
420
|
+
* select "person"."id", "pet"."name"
|
|
421
|
+
* from "person"
|
|
422
|
+
* inner join "pet"
|
|
423
|
+
* on "pet"."owner_id" = "person"."id"
|
|
424
|
+
* ```
|
|
425
|
+
*
|
|
426
|
+
* You can give an alias for the joined table like this:
|
|
427
|
+
*
|
|
428
|
+
* ```ts
|
|
429
|
+
* await db.selectFrom('person')
|
|
430
|
+
* .innerJoin('pet as p', 'p.owner_id', 'person.id')
|
|
431
|
+
* .where('p.name', '=', 'Doggo')
|
|
432
|
+
* .selectAll()
|
|
433
|
+
* .execute()
|
|
434
|
+
* ```
|
|
435
|
+
*
|
|
436
|
+
* The generated SQL (PostgreSQL):
|
|
437
|
+
*
|
|
438
|
+
* ```sql
|
|
439
|
+
* select *
|
|
440
|
+
* from "person"
|
|
441
|
+
* inner join "pet" as "p"
|
|
442
|
+
* on "p"."owner_id" = "person"."id"
|
|
443
|
+
* where "p".name" = $1
|
|
444
|
+
* ```
|
|
445
|
+
*
|
|
446
|
+
* You can provide a function as the second argument to get a join
|
|
447
|
+
* builder for creating more complex joins. The join builder has a
|
|
448
|
+
* bunch of `on*` methods for building the `on` clause of the join.
|
|
449
|
+
* There's basically an equivalent for every `where` method
|
|
450
|
+
* (`on`, `onRef`, `onExists` etc.). You can do all the same things
|
|
451
|
+
* with the `on` method that you can with the corresponding `where`
|
|
452
|
+
* method. See the `where` method documentation for more examples.
|
|
453
|
+
*
|
|
454
|
+
* ```ts
|
|
455
|
+
* await db.selectFrom('person')
|
|
456
|
+
* .innerJoin(
|
|
457
|
+
* 'pet',
|
|
458
|
+
* (join) => join
|
|
459
|
+
* .onRef('pet.owner_id', '=', 'person.id')
|
|
460
|
+
* .on('pet.name', '=', 'Doggo')
|
|
461
|
+
* )
|
|
462
|
+
* .selectAll()
|
|
463
|
+
* .execute()
|
|
464
|
+
* ```
|
|
465
|
+
*
|
|
466
|
+
* The generated SQL (PostgreSQL):
|
|
467
|
+
*
|
|
468
|
+
* ```sql
|
|
469
|
+
* select *
|
|
470
|
+
* from "person"
|
|
471
|
+
* inner join "pet"
|
|
472
|
+
* on "pet"."owner_id" = "person"."id"
|
|
473
|
+
* and "pet"."name" = $1
|
|
474
|
+
* ```
|
|
475
|
+
*
|
|
476
|
+
* You can join a subquery by providing a function as the first
|
|
477
|
+
* argument:
|
|
478
|
+
*
|
|
479
|
+
* ```ts
|
|
480
|
+
* await db.selectFrom('person')
|
|
481
|
+
* .innerJoin(
|
|
482
|
+
* (qb) => qb.selectFrom('pet')
|
|
483
|
+
* .select(['owner_id', 'name'])
|
|
484
|
+
* .where('name', '=', 'Doggo')
|
|
485
|
+
* .as('doggos'),
|
|
486
|
+
* 'doggos.owner_id',
|
|
487
|
+
* 'person.id',
|
|
488
|
+
* )
|
|
489
|
+
* .selectAll()
|
|
490
|
+
* .execute()
|
|
491
|
+
* ```
|
|
492
|
+
*
|
|
493
|
+
* The generated SQL (PostgreSQL):
|
|
494
|
+
*
|
|
495
|
+
* ```sql
|
|
496
|
+
* select *
|
|
497
|
+
* from "person"
|
|
498
|
+
* inner join (
|
|
499
|
+
* select "owner_id", "name"
|
|
500
|
+
* from "pet"
|
|
501
|
+
* where "name" = $1
|
|
502
|
+
* ) as "doggos"
|
|
503
|
+
* on "doggos"."owner_id" = "person"."id"
|
|
504
|
+
* ```
|
|
505
|
+
*/
|
|
36
506
|
innerJoin<TE extends TableExpression<DB, TB>, K1 extends JoinReferenceExpression<DB, TB, TE>, K2 extends JoinReferenceExpression<DB, TB, TE>>(table: TE, k1: K1, k2: K2): UpdateQueryBuilder<TableExpressionDatabase<DB, TE>, TableExpressionTables<DB, TB, TE>, O>;
|
|
37
507
|
innerJoin<TE extends TableExpression<DB, TB>, FN extends JoinCallbackExpression<DB, TB, TE>>(table: TE, callback: FN): UpdateQueryBuilder<TableExpressionDatabase<DB, TE>, TableExpressionTables<DB, TB, TE>, O>;
|
|
508
|
+
/**
|
|
509
|
+
* Just like {@link innerJoin} but adds a left join nstead of an inner join.
|
|
510
|
+
*/
|
|
38
511
|
leftJoin<TE extends TableExpression<DB, TB>, K1 extends JoinReferenceExpression<DB, TB, TE>, K2 extends JoinReferenceExpression<DB, TB, TE>>(table: TE, k1: K1, k2: K2): UpdateQueryBuilder<LeftJoinTableExpressionDatabase<DB, TE>, TableExpressionTables<DB, TB, TE>, O>;
|
|
39
512
|
leftJoin<TE extends TableExpression<DB, TB>, FN extends JoinCallbackExpression<DB, TB, TE>>(table: TE, callback: FN): UpdateQueryBuilder<LeftJoinTableExpressionDatabase<DB, TE>, TableExpressionTables<DB, TB, TE>, O>;
|
|
513
|
+
/**
|
|
514
|
+
* Just like {@link innerJoin} but adds a right join instead of an inner join.
|
|
515
|
+
*/
|
|
40
516
|
rightJoin<TE extends TableExpression<DB, TB>, K1 extends JoinReferenceExpression<DB, TB, TE>, K2 extends JoinReferenceExpression<DB, TB, TE>>(table: TE, k1: K1, k2: K2): UpdateQueryBuilder<RightJoinTableExpressionDatabase<DB, TB, TE>, TableExpressionTables<DB, TB, TE>, O>;
|
|
41
517
|
rightJoin<TE extends TableExpression<DB, TB>, FN extends JoinCallbackExpression<DB, TB, TE>>(table: TE, callback: FN): UpdateQueryBuilder<RightJoinTableExpressionDatabase<DB, TB, TE>, TableExpressionTables<DB, TB, TE>, O>;
|
|
518
|
+
/**
|
|
519
|
+
* Just like {@link innerJoin} but adds a full join instead of an inner join.
|
|
520
|
+
*/
|
|
42
521
|
fullJoin<TE extends TableExpression<DB, TB>, K1 extends JoinReferenceExpression<DB, TB, TE>, K2 extends JoinReferenceExpression<DB, TB, TE>>(table: TE, k1: K1, k2: K2): UpdateQueryBuilder<FullJoinTableExpressionDatabase<DB, TB, TE>, TableExpressionTables<DB, TB, TE>, O>;
|
|
43
522
|
fullJoin<TE extends TableExpression<DB, TB>, FN extends JoinCallbackExpression<DB, TB, TE>>(table: TE, callback: FN): UpdateQueryBuilder<FullJoinTableExpressionDatabase<DB, TB, TE>, TableExpressionTables<DB, TB, TE>, O>;
|
|
44
523
|
/**
|
|
@@ -49,8 +528,8 @@ export declare class UpdateQueryBuilder<DB, TB extends keyof DB, O> implements W
|
|
|
49
528
|
* instances or select queries.
|
|
50
529
|
*
|
|
51
530
|
* The return value of an update query is an instance of {@link UpdateResult}.
|
|
52
|
-
* You can use the {@link
|
|
53
|
-
*
|
|
531
|
+
* You can use the {@link returning} method on supported databases to get out
|
|
532
|
+
* the updated rows.
|
|
54
533
|
*
|
|
55
534
|
* ### Examples
|
|
56
535
|
*
|
|
@@ -127,8 +606,74 @@ export declare class UpdateQueryBuilder<DB, TB extends keyof DB, O> implements W
|
|
|
127
606
|
* ```
|
|
128
607
|
*/
|
|
129
608
|
set(row: MutationObject<DB, TB>): UpdateQueryBuilder<DB, TB, O>;
|
|
609
|
+
/**
|
|
610
|
+
* Allows you to return data from modified rows.
|
|
611
|
+
*
|
|
612
|
+
* On supported databases like PostgreSQL, this method can be chained to
|
|
613
|
+
* `insert`, `update` and `delete` queries to return data.
|
|
614
|
+
*
|
|
615
|
+
* Note that on SQLite you need to give aliases for the expressions to avoid
|
|
616
|
+
* [this bug](https://sqlite.org/forum/forumpost/033daf0b32) in SQLite.
|
|
617
|
+
* For example `.returning('id as id')`.
|
|
618
|
+
*
|
|
619
|
+
* Also see the {@link returningAll} method.
|
|
620
|
+
*
|
|
621
|
+
* ### Examples
|
|
622
|
+
*
|
|
623
|
+
* Return one column:
|
|
624
|
+
*
|
|
625
|
+
* ```ts
|
|
626
|
+
* const { id } = await db
|
|
627
|
+
* .insertInto('person')
|
|
628
|
+
* .values({
|
|
629
|
+
* id: db.generated,
|
|
630
|
+
* first_name: 'Jennifer',
|
|
631
|
+
* last_name: 'Aniston'
|
|
632
|
+
* })
|
|
633
|
+
* .returning('id')
|
|
634
|
+
* .executeTakeFirst()
|
|
635
|
+
* ```
|
|
636
|
+
*
|
|
637
|
+
* Return multiple columns:
|
|
638
|
+
*
|
|
639
|
+
* ```ts
|
|
640
|
+
* const { id, first_name } = await db
|
|
641
|
+
* .insertInto('person')
|
|
642
|
+
* .values({
|
|
643
|
+
* id: db.generated,
|
|
644
|
+
* first_name: 'Jennifer',
|
|
645
|
+
* last_name: 'Aniston'
|
|
646
|
+
* })
|
|
647
|
+
* .returning(['id', 'last_name'])
|
|
648
|
+
* .executeTakeFirst()
|
|
649
|
+
* ```
|
|
650
|
+
*
|
|
651
|
+
* Return arbitrary expressions:
|
|
652
|
+
*
|
|
653
|
+
* ```ts
|
|
654
|
+
* const { raw } = db
|
|
655
|
+
*
|
|
656
|
+
* const { id, full_name, first_pet_id } = await db
|
|
657
|
+
* .insertInto('person')
|
|
658
|
+
* .values({
|
|
659
|
+
* id: db.generated,
|
|
660
|
+
* first_name: 'Jennifer',
|
|
661
|
+
* last_name: 'Aniston'
|
|
662
|
+
* })
|
|
663
|
+
* .returning([
|
|
664
|
+
* 'id as id',
|
|
665
|
+
* raw<string>(`concat(first_name, ' ', last_name)`).as('full_name'),
|
|
666
|
+
* (qb) => qb.selectFrom('pets').select('pet.id').limit(1).as('first_pet_id')
|
|
667
|
+
* ])
|
|
668
|
+
* .executeTakeFirst()
|
|
669
|
+
* ```
|
|
670
|
+
*/
|
|
130
671
|
returning<SE extends SelectExpression<DB, TB>>(selections: ReadonlyArray<SE>): UpdateQueryBuilder<DB, TB, ReturningRow<DB, TB, O, SE>>;
|
|
131
672
|
returning<SE extends SelectExpression<DB, TB>>(selection: SE): UpdateQueryBuilder<DB, TB, ReturningRow<DB, TB, O, SE>>;
|
|
673
|
+
/**
|
|
674
|
+
* Adds a `returning *` to an insert/update/delete query on databases
|
|
675
|
+
* that support `returning` such as PostgreSQL.
|
|
676
|
+
*/
|
|
132
677
|
returningAll(): UpdateQueryBuilder<DB, TB, DB[TB]>;
|
|
133
678
|
/**
|
|
134
679
|
* Simply calls the given function passing `this` as the only argument.
|
|
@@ -96,8 +96,8 @@ class UpdateQueryBuilder {
|
|
|
96
96
|
* instances or select queries.
|
|
97
97
|
*
|
|
98
98
|
* The return value of an update query is an instance of {@link UpdateResult}.
|
|
99
|
-
* You can use the {@link
|
|
100
|
-
*
|
|
99
|
+
* You can use the {@link returning} method on supported databases to get out
|
|
100
|
+
* the updated rows.
|
|
101
101
|
*
|
|
102
102
|
* ### Examples
|
|
103
103
|
*
|
|
@@ -5,8 +5,7 @@ export interface WhereInterface<DB, TB extends keyof DB> {
|
|
|
5
5
|
/**
|
|
6
6
|
* Adds a `where` clause to the query.
|
|
7
7
|
*
|
|
8
|
-
* Also see {@link
|
|
9
|
-
* and {@link WhereInterface.whereRef | orWhere}.
|
|
8
|
+
* Also see {@link orWhere}, {@link whereExists} and {@link whereRef}.
|
|
10
9
|
*
|
|
11
10
|
* ### Examples
|
|
12
11
|
*
|
|
@@ -233,8 +232,7 @@ export interface WhereInterface<DB, TB extends keyof DB> {
|
|
|
233
232
|
*/
|
|
234
233
|
whereRef(lhs: ReferenceExpression<DB, TB>, op: FilterOperator, rhs: ReferenceExpression<DB, TB>): WhereInterface<DB, TB>;
|
|
235
234
|
/**
|
|
236
|
-
* Adds an `or where` clause to the query. Otherwise works just like
|
|
237
|
-
* {@link WhereInterface.where | where}.
|
|
235
|
+
* Adds an `or where` clause to the query. Otherwise works just like {@link where}.
|
|
238
236
|
*
|
|
239
237
|
* It's often necessary to wrap `or where` clauses in parentheses to control
|
|
240
238
|
* precendence. You can use the one argument version of the `where` method
|
|
@@ -300,10 +298,9 @@ export interface WhereInterface<DB, TB extends keyof DB> {
|
|
|
300
298
|
orWhere(grouper: WhereGrouper<DB, TB>): WhereInterface<DB, TB>;
|
|
301
299
|
orWhere(raw: AnyRawBuilder): WhereInterface<DB, TB>;
|
|
302
300
|
/**
|
|
303
|
-
* Adds an `or where` clause to the query. Otherwise works just like
|
|
304
|
-
* {@link WhereInterface.whereRef | whereRef}.
|
|
301
|
+
* Adds an `or where` clause to the query. Otherwise works just like {@link whereRef}.
|
|
305
302
|
*
|
|
306
|
-
* Also see {@link
|
|
303
|
+
* Also see {@link orWhere} and {@link where}.
|
|
307
304
|
*/
|
|
308
305
|
orWhereRef(lhs: ReferenceExpression<DB, TB>, op: FilterOperator, rhs: ReferenceExpression<DB, TB>): WhereInterface<DB, TB>;
|
|
309
306
|
/**
|
|
@@ -367,15 +364,15 @@ export interface WhereInterface<DB, TB extends keyof DB> {
|
|
|
367
364
|
*/
|
|
368
365
|
whereExists(arg: ExistsExpression<DB, TB>): WhereInterface<DB, TB>;
|
|
369
366
|
/**
|
|
370
|
-
* Just like {@link
|
|
367
|
+
* Just like {@link whereExists} but creates a `not exists` clause.
|
|
371
368
|
*/
|
|
372
369
|
whereNotExists(arg: ExistsExpression<DB, TB>): WhereInterface<DB, TB>;
|
|
373
370
|
/**
|
|
374
|
-
* Just like {@link
|
|
371
|
+
* Just like {@link whereExists} but creates a `or exists` clause.
|
|
375
372
|
*/
|
|
376
373
|
orWhereExists(arg: ExistsExpression<DB, TB>): WhereInterface<DB, TB>;
|
|
377
374
|
/**
|
|
378
|
-
* Just like {@link
|
|
375
|
+
* Just like {@link whereExists} but creates a `or not exists` clause.
|
|
379
376
|
*/
|
|
380
377
|
orWhereNotExists(arg: ExistsExpression<DB, TB>): WhereInterface<DB, TB>;
|
|
381
378
|
}
|