kysely 0.11.1 → 0.12.0
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 +1 -1
- package/dist/cjs/dialect/mysql/mysql-query-compiler.d.ts +0 -2
- package/dist/cjs/dialect/mysql/mysql-query-compiler.js +0 -6
- package/dist/cjs/dialect/postgres/postgres-query-compiler.d.ts +3 -0
- package/dist/cjs/dialect/postgres/postgres-query-compiler.js +9 -1
- package/dist/cjs/dialect/sqlite/sqlite-adapter.js +6 -2
- package/dist/cjs/dialect/sqlite/sqlite-driver.js +7 -7
- package/dist/cjs/dialect/sqlite/sqlite-query-compiler.d.ts +2 -2
- package/dist/cjs/dialect/sqlite/sqlite-query-compiler.js +6 -6
- package/dist/cjs/driver/runtime-driver.js +1 -1
- package/dist/cjs/dynamic/dynamic-reference-builder.d.ts +1 -0
- package/dist/cjs/dynamic/dynamic-reference-builder.js +9 -1
- package/dist/cjs/dynamic/dynamic.d.ts +6 -7
- package/dist/cjs/dynamic/dynamic.js +6 -7
- package/dist/cjs/index.d.ts +51 -33
- package/dist/cjs/index.js +48 -30
- package/dist/cjs/kysely.d.ts +39 -15
- package/dist/cjs/kysely.js +32 -9
- package/dist/cjs/migration/migration.d.ts +2 -2
- package/dist/cjs/operation-node/alias-node.d.ts +1 -1
- package/dist/cjs/operation-node/create-table-node.d.ts +6 -2
- package/dist/cjs/operation-node/create-table-node.js +6 -6
- package/dist/cjs/operation-node/create-view-node.d.ts +1 -0
- package/dist/cjs/operation-node/insert-query-node.d.ts +4 -4
- package/dist/cjs/operation-node/operation-node-transformer.d.ts +2 -0
- package/dist/cjs/operation-node/operation-node-transformer.js +11 -1
- package/dist/cjs/operation-node/operation-node-utils.d.ts +1 -1
- package/dist/cjs/operation-node/operation-node-visitor.d.ts +2 -0
- package/dist/cjs/operation-node/operation-node-visitor.js +1 -0
- package/dist/cjs/operation-node/operation-node.d.ts +1 -1
- package/dist/cjs/operation-node/query-node.d.ts +0 -2
- package/dist/cjs/operation-node/query-node.js +0 -10
- package/dist/cjs/operation-node/values-node.d.ts +15 -0
- package/dist/cjs/operation-node/values-node.js +18 -0
- package/dist/cjs/parser/complex-expression-parser.d.ts +9 -0
- package/dist/cjs/parser/complex-expression-parser.js +25 -0
- package/dist/cjs/parser/filter-parser.d.ts +13 -3
- package/dist/cjs/parser/filter-parser.js +56 -62
- package/dist/cjs/parser/insert-values-parser.d.ts +7 -10
- package/dist/cjs/parser/insert-values-parser.js +31 -39
- package/dist/cjs/parser/order-by-parser.d.ts +1 -1
- package/dist/cjs/parser/parse-context.d.ts +8 -8
- package/dist/cjs/parser/parse-context.js +10 -10
- package/dist/cjs/parser/reference-parser.d.ts +8 -7
- package/dist/cjs/parser/reference-parser.js +8 -17
- package/dist/cjs/parser/returning-parser.d.ts +1 -5
- package/dist/cjs/parser/select-parser.d.ts +12 -14
- package/dist/cjs/parser/select-parser.js +5 -9
- package/dist/cjs/parser/table-parser.d.ts +9 -11
- package/dist/cjs/parser/table-parser.js +2 -8
- package/dist/cjs/parser/union-parser.d.ts +2 -2
- package/dist/cjs/parser/union-parser.js +1 -6
- package/dist/cjs/parser/update-set-parser.d.ts +2 -8
- package/dist/cjs/parser/update-set-parser.js +6 -26
- package/dist/cjs/parser/value-parser.d.ts +9 -6
- package/dist/cjs/parser/value-parser.js +7 -30
- package/dist/cjs/parser/with-parser.d.ts +6 -3
- package/dist/cjs/plugin/immediate-value.ts/immediate-value-plugin.js +2 -2
- package/dist/cjs/query-builder/delete-query-builder.d.ts +102 -0
- package/dist/cjs/query-builder/delete-query-builder.js +185 -0
- package/dist/cjs/query-builder/expression-builder.d.ts +8 -6
- package/dist/cjs/query-builder/expression-builder.js +3 -3
- package/dist/cjs/query-builder/function-builder.d.ts +2 -1
- package/dist/cjs/query-builder/function-builder.js +2 -1
- package/dist/cjs/query-builder/having-interface.d.ts +49 -0
- package/dist/cjs/query-builder/having-interface.js +2 -0
- package/dist/cjs/query-builder/insert-query-builder.d.ts +439 -0
- package/dist/cjs/query-builder/insert-query-builder.js +256 -0
- package/dist/cjs/query-builder/insert-result.d.ts +3 -2
- package/dist/cjs/query-builder/insert-result.js +3 -2
- package/dist/cjs/query-builder/join-builder.d.ts +22 -20
- package/dist/cjs/query-builder/join-builder.js +12 -12
- package/dist/cjs/query-builder/join-interface.d.ts +133 -0
- package/dist/cjs/query-builder/join-interface.js +2 -0
- package/dist/cjs/query-builder/no-result-error.d.ts +9 -0
- package/dist/cjs/query-builder/no-result-error.js +14 -0
- package/dist/cjs/query-builder/returning-interface.d.ts +73 -0
- package/dist/cjs/query-builder/returning-interface.js +2 -0
- package/dist/cjs/query-builder/select-query-builder.d.ts +711 -0
- package/dist/cjs/query-builder/select-query-builder.js +571 -0
- package/dist/cjs/query-builder/update-query-builder.d.ts +190 -0
- package/dist/cjs/query-builder/update-query-builder.js +279 -0
- package/dist/cjs/query-builder/where-interface.d.ts +381 -0
- package/dist/cjs/query-builder/where-interface.js +2 -0
- package/dist/cjs/query-compiler/default-query-compiler.d.ts +2 -0
- package/dist/cjs/query-compiler/default-query-compiler.js +22 -5
- package/dist/cjs/query-creator.d.ts +42 -40
- package/dist/cjs/query-creator.js +33 -28
- package/dist/cjs/query-executor/query-executor.d.ts +1 -1
- package/dist/cjs/query-executor/query-executor.js +1 -1
- package/dist/cjs/raw-builder/raw-builder.d.ts +2 -0
- package/dist/cjs/raw-builder/raw-builder.js +6 -0
- package/dist/cjs/schema/alter-table-builder.d.ts +2 -2
- package/dist/cjs/schema/column-definition-builder.d.ts +27 -20
- package/dist/cjs/schema/create-index-builder.d.ts +2 -1
- package/dist/cjs/schema/create-index-builder.js +2 -1
- package/dist/cjs/schema/create-table-builder.d.ts +26 -9
- package/dist/cjs/schema/create-table-builder.js +42 -9
- package/dist/cjs/schema/create-view-builder.d.ts +17 -3
- package/dist/cjs/schema/create-view-builder.js +25 -16
- package/dist/cjs/schema/schema.d.ts +18 -11
- package/dist/cjs/schema/schema.js +18 -11
- package/dist/cjs/util/dynamic-import.js +1 -1
- package/dist/cjs/util/log.d.ts +11 -8
- package/dist/cjs/util/log.js +25 -12
- package/dist/cjs/util/type-utils.d.ts +7 -7
- package/dist/esm/dialect/mysql/mysql-query-compiler.d.ts +0 -2
- package/dist/esm/dialect/mysql/mysql-query-compiler.js +0 -6
- package/dist/esm/dialect/postgres/postgres-query-compiler.d.ts +3 -0
- package/dist/esm/dialect/postgres/postgres-query-compiler.js +9 -1
- package/dist/esm/dialect/sqlite/sqlite-adapter.js +6 -2
- package/dist/esm/dialect/sqlite/sqlite-driver.js +7 -7
- package/dist/esm/dialect/sqlite/sqlite-query-compiler.d.ts +2 -2
- package/dist/esm/dialect/sqlite/sqlite-query-compiler.js +6 -6
- package/dist/esm/driver/runtime-driver.js +1 -1
- package/dist/esm/dynamic/dynamic-reference-builder.d.ts +1 -0
- package/dist/esm/dynamic/dynamic-reference-builder.js +7 -0
- package/dist/esm/dynamic/dynamic.d.ts +6 -7
- package/dist/esm/dynamic/dynamic.js +6 -7
- package/dist/esm/index.d.ts +51 -33
- package/dist/esm/index.js +48 -30
- package/dist/esm/kysely.d.ts +39 -15
- package/dist/esm/kysely.js +32 -9
- package/dist/esm/migration/migration.d.ts +2 -2
- package/dist/esm/operation-node/alias-node.d.ts +1 -1
- package/dist/esm/operation-node/create-table-node.d.ts +6 -2
- package/dist/esm/operation-node/create-table-node.js +6 -6
- package/dist/esm/operation-node/create-view-node.d.ts +1 -0
- package/dist/esm/operation-node/insert-query-node.d.ts +4 -4
- package/dist/esm/operation-node/operation-node-transformer.d.ts +2 -0
- package/dist/esm/operation-node/operation-node-transformer.js +11 -1
- package/dist/esm/operation-node/operation-node-utils.d.ts +1 -1
- package/dist/esm/operation-node/operation-node-visitor.d.ts +2 -0
- package/dist/esm/operation-node/operation-node-visitor.js +1 -0
- package/dist/esm/operation-node/operation-node.d.ts +1 -1
- package/dist/esm/operation-node/query-node.d.ts +0 -2
- package/dist/esm/operation-node/query-node.js +0 -10
- package/dist/esm/operation-node/values-node.d.ts +15 -0
- package/dist/esm/operation-node/values-node.js +15 -0
- package/dist/esm/parser/complex-expression-parser.d.ts +9 -0
- package/dist/esm/parser/complex-expression-parser.js +20 -0
- package/dist/esm/parser/filter-parser.d.ts +13 -3
- package/dist/esm/parser/filter-parser.js +55 -62
- package/dist/esm/parser/insert-values-parser.d.ts +7 -10
- package/dist/esm/parser/insert-values-parser.js +31 -38
- package/dist/esm/parser/order-by-parser.d.ts +1 -1
- package/dist/esm/parser/parse-context.d.ts +8 -8
- package/dist/esm/parser/parse-context.js +10 -10
- package/dist/esm/parser/reference-parser.d.ts +8 -7
- package/dist/esm/parser/reference-parser.js +9 -18
- package/dist/esm/parser/returning-parser.d.ts +1 -5
- package/dist/esm/parser/select-parser.d.ts +12 -14
- package/dist/esm/parser/select-parser.js +6 -10
- package/dist/esm/parser/table-parser.d.ts +9 -11
- package/dist/esm/parser/table-parser.js +3 -9
- package/dist/esm/parser/union-parser.d.ts +2 -2
- package/dist/esm/parser/union-parser.js +1 -6
- package/dist/esm/parser/update-set-parser.d.ts +2 -8
- package/dist/esm/parser/update-set-parser.js +5 -24
- package/dist/esm/parser/value-parser.d.ts +9 -6
- package/dist/esm/parser/value-parser.js +8 -31
- package/dist/esm/parser/with-parser.d.ts +6 -3
- package/dist/esm/plugin/immediate-value.ts/immediate-value-plugin.js +2 -2
- package/dist/esm/query-builder/delete-query-builder.d.ts +102 -0
- package/dist/esm/query-builder/delete-query-builder.js +181 -0
- package/dist/esm/query-builder/expression-builder.d.ts +8 -6
- package/dist/esm/query-builder/expression-builder.js +3 -3
- package/dist/esm/query-builder/function-builder.d.ts +2 -1
- package/dist/esm/query-builder/function-builder.js +2 -1
- package/dist/esm/query-builder/having-interface.d.ts +49 -0
- package/dist/esm/query-builder/having-interface.js +1 -0
- package/dist/esm/query-builder/insert-query-builder.d.ts +439 -0
- package/dist/esm/query-builder/insert-query-builder.js +252 -0
- package/dist/esm/query-builder/insert-result.d.ts +3 -2
- package/dist/esm/query-builder/insert-result.js +3 -2
- package/dist/esm/query-builder/join-builder.d.ts +22 -20
- package/dist/esm/query-builder/join-builder.js +12 -12
- package/dist/esm/query-builder/join-interface.d.ts +133 -0
- package/dist/esm/query-builder/join-interface.js +1 -0
- package/dist/esm/query-builder/no-result-error.d.ts +9 -0
- package/dist/esm/query-builder/no-result-error.js +10 -0
- package/dist/esm/query-builder/returning-interface.d.ts +73 -0
- package/dist/esm/query-builder/returning-interface.js +1 -0
- package/dist/esm/query-builder/select-query-builder.d.ts +711 -0
- package/dist/esm/query-builder/select-query-builder.js +566 -0
- package/dist/esm/query-builder/update-query-builder.d.ts +190 -0
- package/dist/esm/query-builder/update-query-builder.js +275 -0
- package/dist/esm/query-builder/where-interface.d.ts +381 -0
- package/dist/esm/query-builder/where-interface.js +1 -0
- package/dist/esm/query-compiler/default-query-compiler.d.ts +2 -0
- package/dist/esm/query-compiler/default-query-compiler.js +22 -5
- package/dist/esm/query-creator.d.ts +42 -40
- package/dist/esm/query-creator.js +33 -28
- package/dist/esm/query-executor/query-executor.d.ts +1 -1
- package/dist/esm/query-executor/query-executor.js +1 -1
- package/dist/esm/raw-builder/raw-builder.d.ts +2 -0
- package/dist/esm/raw-builder/raw-builder.js +6 -0
- package/dist/esm/schema/alter-table-builder.d.ts +2 -2
- package/dist/esm/schema/column-definition-builder.d.ts +27 -20
- package/dist/esm/schema/create-index-builder.d.ts +2 -1
- package/dist/esm/schema/create-index-builder.js +2 -1
- package/dist/esm/schema/create-table-builder.d.ts +26 -9
- package/dist/esm/schema/create-table-builder.js +43 -10
- package/dist/esm/schema/create-view-builder.d.ts +17 -3
- package/dist/esm/schema/create-view-builder.js +25 -16
- package/dist/esm/schema/schema.d.ts +18 -11
- package/dist/esm/schema/schema.js +18 -11
- package/dist/esm/util/dynamic-import.js +1 -1
- package/dist/esm/util/log.d.ts +11 -8
- package/dist/esm/util/log.js +25 -12
- package/dist/esm/util/type-utils.d.ts +7 -7
- package/package.json +3 -3
- package/dist/cjs/query-builder/query-builder.d.ts +0 -1707
- package/dist/cjs/query-builder/query-builder.js +0 -1067
- package/dist/esm/query-builder/query-builder.d.ts +0 -1707
- package/dist/esm/query-builder/query-builder.js +0 -1061
|
@@ -0,0 +1,439 @@
|
|
|
1
|
+
import { OperationNodeSource } from '../operation-node/operation-node-source.js';
|
|
2
|
+
import { CompiledQuery } from '../query-compiler/compiled-query.js';
|
|
3
|
+
import { SelectExpression } from '../parser/select-parser.js';
|
|
4
|
+
import { InsertObject } from '../parser/insert-values-parser.js';
|
|
5
|
+
import { InsertQueryNode } from '../operation-node/insert-query-node.js';
|
|
6
|
+
import { AnyColumn, SingleResultType } from '../util/type-utils.js';
|
|
7
|
+
import { MutationObject } from '../parser/update-set-parser.js';
|
|
8
|
+
import { Compilable } from '../util/compilable.js';
|
|
9
|
+
import { QueryExecutor } from '../query-executor/query-executor.js';
|
|
10
|
+
import { QueryId } from '../util/query-id.js';
|
|
11
|
+
import { OnConflictConstraintTarget } from '../parser/on-conflict-parser.js';
|
|
12
|
+
import { ParseContext } from '../parser/parse-context.js';
|
|
13
|
+
import { KyselyPlugin } from '../plugin/kysely-plugin.js';
|
|
14
|
+
import { ReturningRow } from '../parser/returning-parser.js';
|
|
15
|
+
import { NoResultErrorConstructor } from './no-result-error.js';
|
|
16
|
+
import { ComplexExpression } from '../parser/complex-expression-parser.js';
|
|
17
|
+
import { ReturningInterface } from './returning-interface.js';
|
|
18
|
+
export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements ReturningInterface<DB, TB, O>, OperationNodeSource, Compilable {
|
|
19
|
+
#private;
|
|
20
|
+
constructor(props: InsertQueryBuilderProps);
|
|
21
|
+
/**
|
|
22
|
+
* Sets the values to insert for an {@link Kysely.insertInto | insert} query.
|
|
23
|
+
*
|
|
24
|
+
* This method takes an object whose keys are column names and values are
|
|
25
|
+
* values to insert. In addition to the column's type, the values can be
|
|
26
|
+
* {@link Kysely.raw | raw} instances, select queries or the
|
|
27
|
+
* {@link Kysely.generated | generated} placeholder.
|
|
28
|
+
*
|
|
29
|
+
* You must provide all values defined by the interface of the table you
|
|
30
|
+
* are inserting into. Values that are generated by the database like auto incrementing
|
|
31
|
+
* identifiers can be marked with the {@link Kysely.generated} placeholder unless you
|
|
32
|
+
* want to insert a specific value instead of the generated default.
|
|
33
|
+
*
|
|
34
|
+
* The return value of an `insert` query is an instance of {@link InsertResult}. The
|
|
35
|
+
* {@link InsertResult.insertId | insertId} field holds the auto incremented primary
|
|
36
|
+
* key if the database returned one.
|
|
37
|
+
*
|
|
38
|
+
* On PostgreSQL and some other dialects, you need to call `returning` to get
|
|
39
|
+
* something out of the query.
|
|
40
|
+
*
|
|
41
|
+
* Also see the {@link expression} method for inserting the result of a select
|
|
42
|
+
* query or any other expression.
|
|
43
|
+
*
|
|
44
|
+
* ### Examples
|
|
45
|
+
*
|
|
46
|
+
* Insert a row into `person`:
|
|
47
|
+
* ```ts
|
|
48
|
+
* const id = await db
|
|
49
|
+
* .insertInto('person')
|
|
50
|
+
* .values({
|
|
51
|
+
* id: db.generated,
|
|
52
|
+
* first_name: 'Jennifer',
|
|
53
|
+
* last_name: 'Aniston'
|
|
54
|
+
* })
|
|
55
|
+
* .executeTakeFirstOrThrow()
|
|
56
|
+
* ```
|
|
57
|
+
*
|
|
58
|
+
* The generated SQL (PostgreSQL):
|
|
59
|
+
*
|
|
60
|
+
* ```sql
|
|
61
|
+
* insert into "person" ("first_name", "last_name") values ($1, $2)
|
|
62
|
+
* ```
|
|
63
|
+
*
|
|
64
|
+
* On dialects that support it (for example PostgreSQL) you can insert multiple
|
|
65
|
+
* rows by providing an array. Note that the return value is once again very
|
|
66
|
+
* dialect-specific. Some databases may only return the id of the *last* inserted
|
|
67
|
+
* row and some return nothing at all unless you call `returning`.
|
|
68
|
+
*
|
|
69
|
+
* ```ts
|
|
70
|
+
* await db
|
|
71
|
+
* .insertInto('person')
|
|
72
|
+
* .values([{
|
|
73
|
+
* id: db.generated,
|
|
74
|
+
* first_name: 'Jennifer',
|
|
75
|
+
* last_name: 'Aniston'
|
|
76
|
+
* }, {
|
|
77
|
+
* id: db.generated,
|
|
78
|
+
* first_name: 'Arnold',
|
|
79
|
+
* last_name: 'Schwarzenegger',
|
|
80
|
+
* }])
|
|
81
|
+
* .execute()
|
|
82
|
+
* ```
|
|
83
|
+
*
|
|
84
|
+
* The generated SQL (PostgreSQL):
|
|
85
|
+
*
|
|
86
|
+
* ```sql
|
|
87
|
+
* insert into "person" ("first_name", "last_name") values (($1, $2), ($3, $4))
|
|
88
|
+
* ```
|
|
89
|
+
*
|
|
90
|
+
* On PostgreSQL you need to chain `returning` to the query to get
|
|
91
|
+
* the inserted row's columns (or any other expression) as the
|
|
92
|
+
* return value:
|
|
93
|
+
*
|
|
94
|
+
* ```ts
|
|
95
|
+
* const row = await db
|
|
96
|
+
* .insertInto('person')
|
|
97
|
+
* .values({
|
|
98
|
+
* id: db.generated,
|
|
99
|
+
* first_name: 'Jennifer',
|
|
100
|
+
* last_name: 'Aniston'
|
|
101
|
+
* })
|
|
102
|
+
* .returning('id')
|
|
103
|
+
* .executeTakeFirstOrThrow()
|
|
104
|
+
*
|
|
105
|
+
* row.id
|
|
106
|
+
* ```
|
|
107
|
+
*
|
|
108
|
+
* The generated SQL (PostgreSQL):
|
|
109
|
+
*
|
|
110
|
+
* ```sql
|
|
111
|
+
* insert into "person" ("first_name", "last_name") values ($1, $2) returning "id"
|
|
112
|
+
* ```
|
|
113
|
+
*
|
|
114
|
+
* In addition to primitives, the values can also be `raw` expressions or
|
|
115
|
+
* select queries:
|
|
116
|
+
*
|
|
117
|
+
* ```ts
|
|
118
|
+
* const result = await db
|
|
119
|
+
* .insertInto('person')
|
|
120
|
+
* .values({
|
|
121
|
+
* id: db.generated,
|
|
122
|
+
* first_name: 'Jennifer',
|
|
123
|
+
* last_name: db.raw('? || ?', ['Ani', 'ston']),
|
|
124
|
+
* age: db.selectFrom('person').select(raw('avg(age)')),
|
|
125
|
+
* })
|
|
126
|
+
* .executeTakeFirst()
|
|
127
|
+
*
|
|
128
|
+
* console.log(result.insertId)
|
|
129
|
+
* ```
|
|
130
|
+
*
|
|
131
|
+
* The generated SQL (PostgreSQL):
|
|
132
|
+
*
|
|
133
|
+
* ```sql
|
|
134
|
+
* insert into "person" ("first_name", "last_name", "age")
|
|
135
|
+
* values ($1, $2 || $3, (select avg(age) from "person"))
|
|
136
|
+
* ```
|
|
137
|
+
*
|
|
138
|
+
* You can also use the callback version of subqueries or raw expressions:
|
|
139
|
+
*
|
|
140
|
+
* ```ts
|
|
141
|
+
* db.with('jennifer', (db) => db
|
|
142
|
+
* .selectFrom('person')
|
|
143
|
+
* .where('first_name', '=', 'Jennifer')
|
|
144
|
+
* .select(['id', 'first_name', 'gender'])
|
|
145
|
+
* .limit(1)
|
|
146
|
+
* ).insertInto('pet').values({
|
|
147
|
+
* id: db.generated,
|
|
148
|
+
* owner_id: (eb) => eb.selectFrom('jennifer').select('id'),
|
|
149
|
+
* name: (eb) => eb.selectFrom('jennifer').select('first_name'),
|
|
150
|
+
* species: 'cat',
|
|
151
|
+
* })
|
|
152
|
+
* ```
|
|
153
|
+
*/
|
|
154
|
+
values(row: InsertObject<DB, TB>): InsertQueryBuilder<DB, TB, O>;
|
|
155
|
+
values(row: ReadonlyArray<InsertObject<DB, TB>>): InsertQueryBuilder<DB, TB, O>;
|
|
156
|
+
/**
|
|
157
|
+
* Sets the columns to insert.
|
|
158
|
+
*
|
|
159
|
+
* The {@link values} method sets both the columns and the values and this method
|
|
160
|
+
* is not needed. But if you are using the {@link expression} method, you can use
|
|
161
|
+
* this method to set the columns to insert.
|
|
162
|
+
*
|
|
163
|
+
* ### Examples
|
|
164
|
+
*
|
|
165
|
+
* ```ts
|
|
166
|
+
* db.insertInto('person')
|
|
167
|
+
* .columns(['first_name'])
|
|
168
|
+
* .expression((eb) => eb.selectFrom('pet').select('pet.name'))
|
|
169
|
+
* ```
|
|
170
|
+
*
|
|
171
|
+
* The generated SQL (PostgreSQL):
|
|
172
|
+
*
|
|
173
|
+
* ```sql
|
|
174
|
+
* insert into "person" ("first_name")
|
|
175
|
+
* select "pet"."name" from "pet"
|
|
176
|
+
* ```
|
|
177
|
+
*/
|
|
178
|
+
columns(columns: ReadonlyArray<keyof DB[TB] & string>): InsertQueryBuilder<DB, TB, O>;
|
|
179
|
+
/**
|
|
180
|
+
* Insert an arbitrary expression. For example the result of a select query.
|
|
181
|
+
*
|
|
182
|
+
* ### Examples
|
|
183
|
+
*
|
|
184
|
+
* ```ts
|
|
185
|
+
* db.insertInto('person')
|
|
186
|
+
* .columns(['first_name'])
|
|
187
|
+
* .expression((eb) => eb.selectFrom('pet').select('pet.name'))
|
|
188
|
+
* ```
|
|
189
|
+
*
|
|
190
|
+
* The generated SQL (PostgreSQL):
|
|
191
|
+
*
|
|
192
|
+
* ```sql
|
|
193
|
+
* insert into "person" ("first_name")
|
|
194
|
+
* select "pet"."name" from "pet"
|
|
195
|
+
* ```
|
|
196
|
+
*/
|
|
197
|
+
expression(expression: ComplexExpression<DB, TB>): InsertQueryBuilder<DB, TB, O>;
|
|
198
|
+
/**
|
|
199
|
+
* Ignores the insert if the column or constraint given as the first
|
|
200
|
+
* argument conflicts with the current rows in the database. The
|
|
201
|
+
* default behavior without this method call is to throw an error.
|
|
202
|
+
*
|
|
203
|
+
* For example if a table has a field `name` that has a unique constraint
|
|
204
|
+
* and you try to insert a row with a `name` that already exists in the
|
|
205
|
+
* database, calling `onConflictDoNothing('name')` will ignore the conflict
|
|
206
|
+
* and do nothing. By default the query would throw.
|
|
207
|
+
*
|
|
208
|
+
* Only some dialects like PostgreSQL and SQLite implement the `on conflict`
|
|
209
|
+
* statement. On MySQL you should use the {@link ignore} method to achieve
|
|
210
|
+
* similar results.
|
|
211
|
+
*
|
|
212
|
+
* Also see the {@link InsertQueryBuilder.onConflictUpdate | onConflictUpdate}
|
|
213
|
+
* method if you want to perform an update in case of a conflict (upsert).
|
|
214
|
+
*
|
|
215
|
+
* ### Examples
|
|
216
|
+
*
|
|
217
|
+
* ```ts
|
|
218
|
+
* await db
|
|
219
|
+
* .insertInto('pet')
|
|
220
|
+
* .values({
|
|
221
|
+
* id: db.generated,
|
|
222
|
+
* name: 'Catto',
|
|
223
|
+
* species: 'cat',
|
|
224
|
+
* })
|
|
225
|
+
* .onConflictDoNothing('name')
|
|
226
|
+
* .execute()
|
|
227
|
+
* ```
|
|
228
|
+
*
|
|
229
|
+
* The generated SQL (PostgreSQL):
|
|
230
|
+
*
|
|
231
|
+
* ```sql
|
|
232
|
+
* insert into "pet" ("name", "species")
|
|
233
|
+
* values ($1, $2)
|
|
234
|
+
* on conflict ("name") do nothing
|
|
235
|
+
* ```
|
|
236
|
+
*
|
|
237
|
+
* You can provide the name of the constraint instead of a column name
|
|
238
|
+
*
|
|
239
|
+
* ```ts
|
|
240
|
+
* await db
|
|
241
|
+
* .insertInto('pet')
|
|
242
|
+
* .values({
|
|
243
|
+
* id: db.generated,
|
|
244
|
+
* name: 'Catto',
|
|
245
|
+
* species: 'cat',
|
|
246
|
+
* })
|
|
247
|
+
* .onConflictDoNothing({
|
|
248
|
+
* constraint: 'pet_name_key'
|
|
249
|
+
* })
|
|
250
|
+
* .execute()
|
|
251
|
+
* ```
|
|
252
|
+
*
|
|
253
|
+
* The generated SQL (PostgreSQL):
|
|
254
|
+
*
|
|
255
|
+
* ```sql
|
|
256
|
+
* insert into "pet" ("name", "species")
|
|
257
|
+
* values ($1, $2)
|
|
258
|
+
* on conflict on constraint "pet_name_key"
|
|
259
|
+
* do nothing
|
|
260
|
+
* ```
|
|
261
|
+
*/
|
|
262
|
+
onConflictDoNothing(column: AnyColumn<DB, TB>): InsertQueryBuilder<DB, TB, O>;
|
|
263
|
+
onConflictDoNothing(columns: ReadonlyArray<AnyColumn<DB, TB>>): InsertQueryBuilder<DB, TB, O>;
|
|
264
|
+
onConflictDoNothing(constraint: OnConflictConstraintTarget): InsertQueryBuilder<DB, TB, O>;
|
|
265
|
+
/**
|
|
266
|
+
* Changes an `insert into` query to an `insert ignore into` query.
|
|
267
|
+
*
|
|
268
|
+
* If you use the ignore modifier, ignorable errors that occur while executing the
|
|
269
|
+
* insert statement are ignored. For example, without ignore, a row that duplicates
|
|
270
|
+
* an existing unique index or primary key value in the table causes a duplicate-key
|
|
271
|
+
* error and the statement is aborted. With ignore, the row is discarded and no error
|
|
272
|
+
* occurs.
|
|
273
|
+
*
|
|
274
|
+
* This is only supported on some dialects like MySQL. On most dialects you should
|
|
275
|
+
* use the {@link onConflictDoNothing} method.
|
|
276
|
+
*
|
|
277
|
+
* ### Examples
|
|
278
|
+
*
|
|
279
|
+
* ```ts
|
|
280
|
+
* await db.insertInto('person')
|
|
281
|
+
* .ignore()
|
|
282
|
+
* .values(values)
|
|
283
|
+
* .execute()
|
|
284
|
+
* ```
|
|
285
|
+
*/
|
|
286
|
+
ignore(): InsertQueryBuilder<DB, TB, O>;
|
|
287
|
+
/**
|
|
288
|
+
* Ignores an insert if the column or constraint given as the first
|
|
289
|
+
* argument conflicts with the current rows in the database and
|
|
290
|
+
* performs an update on the conflicting row instead. This method
|
|
291
|
+
* can be used to implement an upsert operation.
|
|
292
|
+
*
|
|
293
|
+
* For example if a table has a field `name` that has a unique constraint
|
|
294
|
+
* and you try to insert a row with a `name` that already exists in the
|
|
295
|
+
* database, calling `onConfictUpdate('name', { species: 'hamster' })`
|
|
296
|
+
* will set the conflicting row's `species` field to value `'hamster'`.
|
|
297
|
+
* By default the query would throw.
|
|
298
|
+
*
|
|
299
|
+
* The second argument (updates) can be anything the {@link UpdateQueryBuilder.set | set}
|
|
300
|
+
* method accepts.
|
|
301
|
+
*
|
|
302
|
+
* The `on conflict do update` statement is only implemented by some dialects
|
|
303
|
+
* like PostgreSQL and SQLite. On MySQL you should use the {@link onDuplicateKeyUpdate}
|
|
304
|
+
* method instead.
|
|
305
|
+
*
|
|
306
|
+
* Also see the {@link InsertQueryBuilder.onConflictDoNothing | onConflictDoNothing}
|
|
307
|
+
* method.
|
|
308
|
+
*
|
|
309
|
+
* ### Examples
|
|
310
|
+
*
|
|
311
|
+
* ```ts
|
|
312
|
+
* await db
|
|
313
|
+
* .insertInto('pet')
|
|
314
|
+
* .values({
|
|
315
|
+
* id: db.generated,
|
|
316
|
+
* name: 'Catto',
|
|
317
|
+
* species: 'cat',
|
|
318
|
+
* })
|
|
319
|
+
* .onConflictUpdate('name', { species: 'hamster' })
|
|
320
|
+
* .execute()
|
|
321
|
+
* ```
|
|
322
|
+
*
|
|
323
|
+
* The generated SQL (PostgreSQL):
|
|
324
|
+
*
|
|
325
|
+
* ```sql
|
|
326
|
+
* insert into "pet" ("name", "species")
|
|
327
|
+
* values ($1, $2)
|
|
328
|
+
* on conflict ("name") do update set "species" = $3
|
|
329
|
+
* ```
|
|
330
|
+
*
|
|
331
|
+
* You can provied the name of the constraint instead of a column name
|
|
332
|
+
*
|
|
333
|
+
* ```ts
|
|
334
|
+
* await db
|
|
335
|
+
* .insertInto('pet')
|
|
336
|
+
* .values({
|
|
337
|
+
* id: db.generated,
|
|
338
|
+
* name: 'Catto',
|
|
339
|
+
* species: 'cat',
|
|
340
|
+
* })
|
|
341
|
+
* .onConflictUpdate(
|
|
342
|
+
* { constraint: 'pet_name_key' },
|
|
343
|
+
* { species: 'hamster' }
|
|
344
|
+
* )
|
|
345
|
+
* .execute()
|
|
346
|
+
* ```
|
|
347
|
+
*
|
|
348
|
+
* The generated SQL (PostgreSQL):
|
|
349
|
+
*
|
|
350
|
+
* ```sql
|
|
351
|
+
* insert into "pet" ("name", "species")
|
|
352
|
+
* values ($1, $2)
|
|
353
|
+
* on conflict on constraint "pet_name_key"
|
|
354
|
+
* do update set "species" = $3
|
|
355
|
+
* ```
|
|
356
|
+
*/
|
|
357
|
+
onConflictUpdate(column: AnyColumn<DB, TB>, updates: MutationObject<DB, TB>): InsertQueryBuilder<DB, TB, O>;
|
|
358
|
+
onConflictUpdate(columns: ReadonlyArray<AnyColumn<DB, TB>>, updates: MutationObject<DB, TB>): InsertQueryBuilder<DB, TB, O>;
|
|
359
|
+
onConflictUpdate(constraint: OnConflictConstraintTarget, updates: MutationObject<DB, TB>): InsertQueryBuilder<DB, TB, O>;
|
|
360
|
+
/**
|
|
361
|
+
* Adds `on duplicate key update` to the query.
|
|
362
|
+
*
|
|
363
|
+
* If you specify `on duplicate key update`, and a row is inserted that would cause
|
|
364
|
+
* a duplicate value in a unique index or primary key, an update of the old row occurs.
|
|
365
|
+
*
|
|
366
|
+
* This is only implemented by some dialects like MySQL. On most dialects you should
|
|
367
|
+
* use {@link onConflictUpdate} instead.
|
|
368
|
+
*
|
|
369
|
+
* ### Examples
|
|
370
|
+
*
|
|
371
|
+
* ```ts
|
|
372
|
+
* await db
|
|
373
|
+
* .insertInto('person')
|
|
374
|
+
* .values(values)
|
|
375
|
+
* .onDuplicateKeyUpdate({ species: 'hamster' })
|
|
376
|
+
* ```
|
|
377
|
+
*/
|
|
378
|
+
onDuplicateKeyUpdate(updates: MutationObject<DB, TB>): InsertQueryBuilder<DB, TB, O>;
|
|
379
|
+
returning<SE extends SelectExpression<DB, TB>>(selections: ReadonlyArray<SE>): InsertQueryBuilder<DB, TB, ReturningRow<DB, TB, O, SE>>;
|
|
380
|
+
returning<SE extends SelectExpression<DB, TB>>(selection: SE): InsertQueryBuilder<DB, TB, ReturningRow<DB, TB, O, SE>>;
|
|
381
|
+
returningAll(): InsertQueryBuilder<DB, TB, DB[TB]>;
|
|
382
|
+
/**
|
|
383
|
+
* Simply calls the given function passing `this` as the only argument.
|
|
384
|
+
*
|
|
385
|
+
* This method can be useful when adding optional method calls:
|
|
386
|
+
*
|
|
387
|
+
* ### Examples
|
|
388
|
+
*
|
|
389
|
+
* ```ts
|
|
390
|
+
* db.insertInto('person')
|
|
391
|
+
* .selectAll()
|
|
392
|
+
* .call((qb) => {
|
|
393
|
+
* if (something) {
|
|
394
|
+
* return qb.onConflictDoNothing('something')
|
|
395
|
+
* } else {
|
|
396
|
+
* return qb.onConflictDoNothing('something_else')
|
|
397
|
+
* }
|
|
398
|
+
* })
|
|
399
|
+
* .execute()
|
|
400
|
+
* ```
|
|
401
|
+
*/
|
|
402
|
+
call<T>(func: (qb: this) => T): T;
|
|
403
|
+
/**
|
|
404
|
+
* Change the output type of the query.
|
|
405
|
+
*
|
|
406
|
+
* You should only use this method as the last resort if the types
|
|
407
|
+
* don't support your use case.
|
|
408
|
+
*/
|
|
409
|
+
castTo<T>(): InsertQueryBuilder<DB, TB, T>;
|
|
410
|
+
withPlugin(plugin: KyselyPlugin): InsertQueryBuilder<DB, TB, O>;
|
|
411
|
+
toOperationNode(): InsertQueryNode;
|
|
412
|
+
compile(): CompiledQuery;
|
|
413
|
+
/**
|
|
414
|
+
* Executes the query and returns an array of rows.
|
|
415
|
+
*
|
|
416
|
+
* Also see the {@link executeTakeFirst} and {@link executeTakeFirstOrThrow} methods.
|
|
417
|
+
*/
|
|
418
|
+
execute(): Promise<O[]>;
|
|
419
|
+
/**
|
|
420
|
+
* Executes the query and returns the first result or undefined if
|
|
421
|
+
* the query returned no result.
|
|
422
|
+
*/
|
|
423
|
+
executeTakeFirst(): Promise<SingleResultType<O>>;
|
|
424
|
+
/**
|
|
425
|
+
* Executes the query and returns the first result or throws if
|
|
426
|
+
* the query returned no result.
|
|
427
|
+
*
|
|
428
|
+
* By default an instance of {@link NoResultError} is thrown, but you can
|
|
429
|
+
* provide a custom error class as the only argument to throw a different
|
|
430
|
+
* error.
|
|
431
|
+
*/
|
|
432
|
+
executeTakeFirstOrThrow(errorConstructor?: NoResultErrorConstructor): Promise<O>;
|
|
433
|
+
}
|
|
434
|
+
export interface InsertQueryBuilderProps {
|
|
435
|
+
readonly queryId: QueryId;
|
|
436
|
+
readonly queryNode: InsertQueryNode;
|
|
437
|
+
readonly executor: QueryExecutor;
|
|
438
|
+
readonly parseContext: ParseContext;
|
|
439
|
+
}
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
import { parseSelectExpressionOrList, parseSelectAll, } from '../parser/select-parser.js';
|
|
2
|
+
import { parseInsertObjectOrList, } from '../parser/insert-values-parser.js';
|
|
3
|
+
import { InsertQueryNode } from '../operation-node/insert-query-node.js';
|
|
4
|
+
import { QueryNode } from '../operation-node/query-node.js';
|
|
5
|
+
import { parseUpdateObject, } from '../parser/update-set-parser.js';
|
|
6
|
+
import { preventAwait } from '../util/prevent-await.js';
|
|
7
|
+
import { parseOnConflictDoNothing, parseOnConflictUpdate, } from '../parser/on-conflict-parser.js';
|
|
8
|
+
import { freeze } from '../util/object-utils.js';
|
|
9
|
+
import { OnDuplicateKeyNode } from '../operation-node/on-duplicate-key-node.js';
|
|
10
|
+
import { InsertResult } from './insert-result.js';
|
|
11
|
+
import { NoResultError } from './no-result-error.js';
|
|
12
|
+
import { parseComplexExpression, } from '../parser/complex-expression-parser.js';
|
|
13
|
+
import { ColumnNode } from '../operation-node/column-node.js';
|
|
14
|
+
export class InsertQueryBuilder {
|
|
15
|
+
#props;
|
|
16
|
+
constructor(props) {
|
|
17
|
+
this.#props = freeze(props);
|
|
18
|
+
}
|
|
19
|
+
values(args) {
|
|
20
|
+
const [columns, values] = parseInsertObjectOrList(this.#props.parseContext, args);
|
|
21
|
+
return new InsertQueryBuilder({
|
|
22
|
+
...this.#props,
|
|
23
|
+
queryNode: InsertQueryNode.cloneWith(this.#props.queryNode, {
|
|
24
|
+
columns,
|
|
25
|
+
values,
|
|
26
|
+
}),
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Sets the columns to insert.
|
|
31
|
+
*
|
|
32
|
+
* The {@link values} method sets both the columns and the values and this method
|
|
33
|
+
* is not needed. But if you are using the {@link expression} method, you can use
|
|
34
|
+
* this method to set the columns to insert.
|
|
35
|
+
*
|
|
36
|
+
* ### Examples
|
|
37
|
+
*
|
|
38
|
+
* ```ts
|
|
39
|
+
* db.insertInto('person')
|
|
40
|
+
* .columns(['first_name'])
|
|
41
|
+
* .expression((eb) => eb.selectFrom('pet').select('pet.name'))
|
|
42
|
+
* ```
|
|
43
|
+
*
|
|
44
|
+
* The generated SQL (PostgreSQL):
|
|
45
|
+
*
|
|
46
|
+
* ```sql
|
|
47
|
+
* insert into "person" ("first_name")
|
|
48
|
+
* select "pet"."name" from "pet"
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
columns(columns) {
|
|
52
|
+
return new InsertQueryBuilder({
|
|
53
|
+
...this.#props,
|
|
54
|
+
queryNode: InsertQueryNode.cloneWith(this.#props.queryNode, {
|
|
55
|
+
columns: freeze(columns.map(ColumnNode.create)),
|
|
56
|
+
}),
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Insert an arbitrary expression. For example the result of a select query.
|
|
61
|
+
*
|
|
62
|
+
* ### Examples
|
|
63
|
+
*
|
|
64
|
+
* ```ts
|
|
65
|
+
* db.insertInto('person')
|
|
66
|
+
* .columns(['first_name'])
|
|
67
|
+
* .expression((eb) => eb.selectFrom('pet').select('pet.name'))
|
|
68
|
+
* ```
|
|
69
|
+
*
|
|
70
|
+
* The generated SQL (PostgreSQL):
|
|
71
|
+
*
|
|
72
|
+
* ```sql
|
|
73
|
+
* insert into "person" ("first_name")
|
|
74
|
+
* select "pet"."name" from "pet"
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
expression(expression) {
|
|
78
|
+
return new InsertQueryBuilder({
|
|
79
|
+
...this.#props,
|
|
80
|
+
queryNode: InsertQueryNode.cloneWith(this.#props.queryNode, {
|
|
81
|
+
values: parseComplexExpression(this.#props.parseContext, expression),
|
|
82
|
+
}),
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
onConflictDoNothing(target) {
|
|
86
|
+
return new InsertQueryBuilder({
|
|
87
|
+
...this.#props,
|
|
88
|
+
queryNode: InsertQueryNode.cloneWith(this.#props.queryNode, {
|
|
89
|
+
onConflict: parseOnConflictDoNothing(target),
|
|
90
|
+
}),
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Changes an `insert into` query to an `insert ignore into` query.
|
|
95
|
+
*
|
|
96
|
+
* If you use the ignore modifier, ignorable errors that occur while executing the
|
|
97
|
+
* insert statement are ignored. For example, without ignore, a row that duplicates
|
|
98
|
+
* an existing unique index or primary key value in the table causes a duplicate-key
|
|
99
|
+
* error and the statement is aborted. With ignore, the row is discarded and no error
|
|
100
|
+
* occurs.
|
|
101
|
+
*
|
|
102
|
+
* This is only supported on some dialects like MySQL. On most dialects you should
|
|
103
|
+
* use the {@link onConflictDoNothing} method.
|
|
104
|
+
*
|
|
105
|
+
* ### Examples
|
|
106
|
+
*
|
|
107
|
+
* ```ts
|
|
108
|
+
* await db.insertInto('person')
|
|
109
|
+
* .ignore()
|
|
110
|
+
* .values(values)
|
|
111
|
+
* .execute()
|
|
112
|
+
* ```
|
|
113
|
+
*/
|
|
114
|
+
ignore() {
|
|
115
|
+
return new InsertQueryBuilder({
|
|
116
|
+
...this.#props,
|
|
117
|
+
queryNode: InsertQueryNode.cloneWith(this.#props.queryNode, {
|
|
118
|
+
ignore: true,
|
|
119
|
+
}),
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
onConflictUpdate(target, updates) {
|
|
123
|
+
return new InsertQueryBuilder({
|
|
124
|
+
...this.#props,
|
|
125
|
+
queryNode: InsertQueryNode.cloneWith(this.#props.queryNode, {
|
|
126
|
+
onConflict: parseOnConflictUpdate(this.#props.parseContext, target, updates),
|
|
127
|
+
}),
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Adds `on duplicate key update` to the query.
|
|
132
|
+
*
|
|
133
|
+
* If you specify `on duplicate key update`, and a row is inserted that would cause
|
|
134
|
+
* a duplicate value in a unique index or primary key, an update of the old row occurs.
|
|
135
|
+
*
|
|
136
|
+
* This is only implemented by some dialects like MySQL. On most dialects you should
|
|
137
|
+
* use {@link onConflictUpdate} instead.
|
|
138
|
+
*
|
|
139
|
+
* ### Examples
|
|
140
|
+
*
|
|
141
|
+
* ```ts
|
|
142
|
+
* await db
|
|
143
|
+
* .insertInto('person')
|
|
144
|
+
* .values(values)
|
|
145
|
+
* .onDuplicateKeyUpdate({ species: 'hamster' })
|
|
146
|
+
* ```
|
|
147
|
+
*/
|
|
148
|
+
onDuplicateKeyUpdate(updates) {
|
|
149
|
+
return new InsertQueryBuilder({
|
|
150
|
+
...this.#props,
|
|
151
|
+
queryNode: InsertQueryNode.cloneWith(this.#props.queryNode, {
|
|
152
|
+
onDuplicateKey: OnDuplicateKeyNode.create(parseUpdateObject(this.#props.parseContext, updates)),
|
|
153
|
+
}),
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
returning(selection) {
|
|
157
|
+
return new InsertQueryBuilder({
|
|
158
|
+
...this.#props,
|
|
159
|
+
queryNode: QueryNode.cloneWithReturning(this.#props.queryNode, parseSelectExpressionOrList(this.#props.parseContext, selection)),
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
returningAll() {
|
|
163
|
+
return new InsertQueryBuilder({
|
|
164
|
+
...this.#props,
|
|
165
|
+
queryNode: QueryNode.cloneWithReturning(this.#props.queryNode, parseSelectAll()),
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Simply calls the given function passing `this` as the only argument.
|
|
170
|
+
*
|
|
171
|
+
* This method can be useful when adding optional method calls:
|
|
172
|
+
*
|
|
173
|
+
* ### Examples
|
|
174
|
+
*
|
|
175
|
+
* ```ts
|
|
176
|
+
* db.insertInto('person')
|
|
177
|
+
* .selectAll()
|
|
178
|
+
* .call((qb) => {
|
|
179
|
+
* if (something) {
|
|
180
|
+
* return qb.onConflictDoNothing('something')
|
|
181
|
+
* } else {
|
|
182
|
+
* return qb.onConflictDoNothing('something_else')
|
|
183
|
+
* }
|
|
184
|
+
* })
|
|
185
|
+
* .execute()
|
|
186
|
+
* ```
|
|
187
|
+
*/
|
|
188
|
+
call(func) {
|
|
189
|
+
return func(this);
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Change the output type of the query.
|
|
193
|
+
*
|
|
194
|
+
* You should only use this method as the last resort if the types
|
|
195
|
+
* don't support your use case.
|
|
196
|
+
*/
|
|
197
|
+
castTo() {
|
|
198
|
+
return new InsertQueryBuilder(this.#props);
|
|
199
|
+
}
|
|
200
|
+
withPlugin(plugin) {
|
|
201
|
+
return new InsertQueryBuilder({
|
|
202
|
+
...this.#props,
|
|
203
|
+
executor: this.#props.executor.withPlugin(plugin),
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
toOperationNode() {
|
|
207
|
+
return this.#props.executor.transformQuery(this.#props.queryNode, this.#props.queryId);
|
|
208
|
+
}
|
|
209
|
+
compile() {
|
|
210
|
+
return this.#props.executor.compileQuery(this.toOperationNode(), this.#props.queryId);
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Executes the query and returns an array of rows.
|
|
214
|
+
*
|
|
215
|
+
* Also see the {@link executeTakeFirst} and {@link executeTakeFirstOrThrow} methods.
|
|
216
|
+
*/
|
|
217
|
+
async execute() {
|
|
218
|
+
const compildQuery = this.compile();
|
|
219
|
+
const query = compildQuery.query;
|
|
220
|
+
const result = await this.#props.executor.executeQuery(compildQuery, this.#props.queryId);
|
|
221
|
+
if (this.#props.parseContext.adapter.supportsReturning && query.returning) {
|
|
222
|
+
return result.rows;
|
|
223
|
+
}
|
|
224
|
+
else {
|
|
225
|
+
return [new InsertResult(result.insertId)];
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* Executes the query and returns the first result or undefined if
|
|
230
|
+
* the query returned no result.
|
|
231
|
+
*/
|
|
232
|
+
async executeTakeFirst() {
|
|
233
|
+
const [result] = await this.execute();
|
|
234
|
+
return result;
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* Executes the query and returns the first result or throws if
|
|
238
|
+
* the query returned no result.
|
|
239
|
+
*
|
|
240
|
+
* By default an instance of {@link NoResultError} is thrown, but you can
|
|
241
|
+
* provide a custom error class as the only argument to throw a different
|
|
242
|
+
* error.
|
|
243
|
+
*/
|
|
244
|
+
async executeTakeFirstOrThrow(errorConstructor = NoResultError) {
|
|
245
|
+
const result = await this.executeTakeFirst();
|
|
246
|
+
if (result === undefined) {
|
|
247
|
+
throw new errorConstructor(this.toOperationNode());
|
|
248
|
+
}
|
|
249
|
+
return result;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
preventAwait(InsertQueryBuilder, "don't await InsertQueryBuilder instances directly. To execute the query you need to call `execute` or `executeTakeFirst`.");
|
|
@@ -4,10 +4,11 @@
|
|
|
4
4
|
* If the table has an auto incrementing primary key {@link insertId} will hold
|
|
5
5
|
* the generated id on dialects that support it. For example PostgreSQL doesn't
|
|
6
6
|
* return the id by default and {@link insertId} is undefined. On PostgreSQL you
|
|
7
|
-
* need to use {@link
|
|
7
|
+
* need to use {@link ReturningInterface.returning} or {@link ReturningInterface.returningAll}
|
|
8
8
|
* to get out the inserted id.
|
|
9
9
|
*
|
|
10
|
-
*
|
|
10
|
+
* ### Examples
|
|
11
|
+
*
|
|
11
12
|
* ```ts
|
|
12
13
|
* const result = await db
|
|
13
14
|
* .insertInto('person')
|