kysely 0.16.7 → 0.16.11
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/LICENSE +21 -0
- package/README.md +21 -9
- package/dist/cjs/kysely.js +2 -2
- package/dist/cjs/migration/migrator.d.ts +22 -0
- package/dist/cjs/migration/migrator.js +23 -1
- package/dist/cjs/operation-node/delete-query-node.d.ts +7 -0
- package/dist/cjs/operation-node/delete-query-node.js +15 -0
- package/dist/cjs/query-builder/delete-query-builder.d.ts +56 -3
- package/dist/cjs/query-builder/delete-query-builder.js +67 -0
- package/dist/cjs/query-builder/insert-query-builder.d.ts +1 -3
- package/dist/cjs/query-builder/returning-interface.d.ts +1 -3
- package/dist/cjs/query-builder/select-query-builder.d.ts +7 -8
- package/dist/cjs/query-builder/update-query-builder.d.ts +1 -3
- package/dist/cjs/query-compiler/default-query-compiler.js +16 -7
- package/dist/cjs/query-creator.d.ts +14 -9
- package/dist/cjs/query-creator.js +14 -9
- package/dist/cjs/query-executor/query-executor.js +3 -2
- package/dist/cjs/raw-builder/raw-builder.d.ts +2 -2
- package/dist/cjs/raw-builder/raw-builder.js +8 -1
- package/dist/cjs/schema/alter-table-builder.js +3 -3
- package/dist/cjs/schema/create-table-builder.js +2 -2
- package/dist/esm/kysely.js +2 -2
- package/dist/esm/migration/migrator.d.ts +22 -0
- package/dist/esm/migration/migrator.js +23 -1
- package/dist/esm/operation-node/delete-query-node.d.ts +7 -0
- package/dist/esm/operation-node/delete-query-node.js +15 -0
- package/dist/esm/query-builder/delete-query-builder.d.ts +56 -3
- package/dist/esm/query-builder/delete-query-builder.js +67 -0
- package/dist/esm/query-builder/insert-query-builder.d.ts +1 -3
- package/dist/esm/query-builder/returning-interface.d.ts +1 -3
- package/dist/esm/query-builder/select-query-builder.d.ts +7 -8
- package/dist/esm/query-builder/update-query-builder.d.ts +1 -3
- package/dist/esm/query-compiler/default-query-compiler.js +16 -7
- package/dist/esm/query-creator.d.ts +14 -9
- package/dist/esm/query-creator.js +14 -9
- package/dist/esm/query-executor/query-executor.js +3 -2
- package/dist/esm/raw-builder/raw-builder.d.ts +2 -2
- package/dist/esm/raw-builder/raw-builder.js +8 -1
- package/dist/esm/schema/alter-table-builder.js +3 -3
- package/dist/esm/schema/create-table-builder.js +2 -2
- package/package.json +1 -1
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Sami Koskimäki
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -204,14 +204,24 @@ export async function down(db: Kysely<any>): Promise<void> {
|
|
|
204
204
|
```
|
|
205
205
|
|
|
206
206
|
The `up` function is called when you update your database schema to next version and `down`
|
|
207
|
-
when you go back to previous version. The only argument
|
|
208
|
-
`Kysely<any>`. It
|
|
209
|
-
should never depend on the current code of your app because they need to work even
|
|
210
|
-
changes
|
|
207
|
+
when you go back to previous version. The only argument for the functions is an instance of
|
|
208
|
+
`Kysely<any>`. It's important to use `Kysely<any>` and not `Kysely<YourDatabase>`. Migrations
|
|
209
|
+
should never depend on the current code of your app because they need to work even when the app
|
|
210
|
+
changes. Migrations need to be "frozen in time".
|
|
211
211
|
|
|
212
212
|
The migrations can use the [Kysely.schema](https://koskimas.github.io/kysely/classes/SchemaModule.html)
|
|
213
213
|
module to modify the schema. Migrations can also run normal queries to modify data.
|
|
214
214
|
|
|
215
|
+
Execution order of the migrations is the alpabetical order of their names. An excellent way to name your
|
|
216
|
+
migrations is to prefix them with an ISO 8601 date string. A date prefix works well in large teams
|
|
217
|
+
where multiple team members may add migrations at the same time in parallel commits without knowing
|
|
218
|
+
about the other migrations.
|
|
219
|
+
|
|
220
|
+
You don't need to store your migrations as separate files if you don't want to. You can easily
|
|
221
|
+
implement your own [MigrationProvider](https://koskimas.github.io/kysely/interfaces/MigrationProvider.html)
|
|
222
|
+
and give it to the [Migrator](https://koskimas.github.io/kysely/classes/Migrator.html) class
|
|
223
|
+
when you instantiate one.
|
|
224
|
+
|
|
215
225
|
### PostgreSQL migration example
|
|
216
226
|
|
|
217
227
|
```ts
|
|
@@ -295,9 +305,9 @@ const migrator = new Migrator(migratorConfig);
|
|
|
295
305
|
await migrator.migrateToLatest(pathToMigrationsFolder)
|
|
296
306
|
```
|
|
297
307
|
|
|
298
|
-
to run all migrations that have not yet been run.
|
|
299
|
-
|
|
300
|
-
documentation for more info.
|
|
308
|
+
to run all migrations that have not yet been run. See the
|
|
309
|
+
[Migrator](https://koskimas.github.io/kysely/classes/Migrator.html)
|
|
310
|
+
class's documentation for more info.
|
|
301
311
|
|
|
302
312
|
Kysely doesn't have a CLI for running migrations and probably never will. This is because Kysely's
|
|
303
313
|
migrations are also written in typescript. To run the migrations, you need to first build the
|
|
@@ -352,9 +362,11 @@ async function migrateToLatest() {
|
|
|
352
362
|
migrateToLatest()
|
|
353
363
|
```
|
|
354
364
|
|
|
355
|
-
The migration methods use a lock on the database level
|
|
365
|
+
The migration methods use a lock on the database level and parallel calls are executed serially.
|
|
356
366
|
This means that you can safely call `migrateToLatest` and other migration methods from multiple
|
|
357
|
-
server instances simultaneously and the migrations are guaranteed to only be executed once.
|
|
367
|
+
server instances simultaneously and the migrations are guaranteed to only be executed once. The
|
|
368
|
+
locks are also automatically released if the migration process crashes or the connection to the
|
|
369
|
+
database fails.
|
|
358
370
|
|
|
359
371
|
# Deno
|
|
360
372
|
|
package/dist/cjs/kysely.js
CHANGED
|
@@ -345,12 +345,12 @@ class ConnectionBuilder {
|
|
|
345
345
|
async execute(callback) {
|
|
346
346
|
const connection = await this.#props.driver.acquireConnection();
|
|
347
347
|
const connectionProvider = new single_connection_provider_js_1.SingleConnectionProvider(connection);
|
|
348
|
-
const
|
|
348
|
+
const db = new Kysely({
|
|
349
349
|
...this.#props,
|
|
350
350
|
executor: this.#props.executor.withConnectionProvider(connectionProvider),
|
|
351
351
|
});
|
|
352
352
|
try {
|
|
353
|
-
return await callback(
|
|
353
|
+
return await callback(db);
|
|
354
354
|
}
|
|
355
355
|
finally {
|
|
356
356
|
await this.#props.driver.releaseConnection(connection);
|
|
@@ -36,6 +36,12 @@ export interface Migration {
|
|
|
36
36
|
export declare class Migrator {
|
|
37
37
|
#private;
|
|
38
38
|
constructor(props: MigratorProps);
|
|
39
|
+
/**
|
|
40
|
+
* Returns a {@link MigrationInfo} object for each migration.
|
|
41
|
+
*
|
|
42
|
+
* The returned array is sorted by migration name.
|
|
43
|
+
*/
|
|
44
|
+
getMigrations(): Promise<ReadonlyArray<MigrationInfo>>;
|
|
39
45
|
/**
|
|
40
46
|
* Runs all migrations that have not yet been run.
|
|
41
47
|
*
|
|
@@ -219,3 +225,19 @@ export interface MigrationProvider {
|
|
|
219
225
|
export interface NoMigrations {
|
|
220
226
|
readonly __noMigrations__: true;
|
|
221
227
|
}
|
|
228
|
+
export interface MigrationInfo {
|
|
229
|
+
/**
|
|
230
|
+
* Name of the migration.
|
|
231
|
+
*/
|
|
232
|
+
name: string;
|
|
233
|
+
/**
|
|
234
|
+
* The actual migration.
|
|
235
|
+
*/
|
|
236
|
+
migration: Migration;
|
|
237
|
+
/**
|
|
238
|
+
* When was the migration executed.
|
|
239
|
+
*
|
|
240
|
+
* If this is undefined, the migration hasn't been executed yet.
|
|
241
|
+
*/
|
|
242
|
+
executedAt?: Date;
|
|
243
|
+
}
|
|
@@ -32,6 +32,28 @@ class Migrator {
|
|
|
32
32
|
constructor(props) {
|
|
33
33
|
this.#props = (0, object_utils_js_1.freeze)(props);
|
|
34
34
|
}
|
|
35
|
+
/**
|
|
36
|
+
* Returns a {@link MigrationInfo} object for each migration.
|
|
37
|
+
*
|
|
38
|
+
* The returned array is sorted by migration name.
|
|
39
|
+
*/
|
|
40
|
+
async getMigrations() {
|
|
41
|
+
const executedMigrations = (await this.#doesTableExists(exports.MIGRATION_TABLE))
|
|
42
|
+
? await this.#props.db
|
|
43
|
+
.selectFrom(exports.MIGRATION_TABLE)
|
|
44
|
+
.select(['name', 'timestamp'])
|
|
45
|
+
.execute()
|
|
46
|
+
: [];
|
|
47
|
+
const migrations = await this.#resolveMigrations();
|
|
48
|
+
return migrations.map(({ name, ...migration }) => {
|
|
49
|
+
const executed = executedMigrations.find((it) => it.name === name);
|
|
50
|
+
return {
|
|
51
|
+
name,
|
|
52
|
+
migration,
|
|
53
|
+
executedAt: executed ? new Date(executed.timestamp) : undefined,
|
|
54
|
+
};
|
|
55
|
+
});
|
|
56
|
+
}
|
|
35
57
|
/**
|
|
36
58
|
* Runs all migrations that have not yet been run.
|
|
37
59
|
*
|
|
@@ -215,7 +237,7 @@ class Migrator {
|
|
|
215
237
|
try {
|
|
216
238
|
await this.#props.db
|
|
217
239
|
.insertInto(exports.MIGRATION_LOCK_TABLE)
|
|
218
|
-
.values({ id: exports.MIGRATION_LOCK_ID })
|
|
240
|
+
.values({ id: exports.MIGRATION_LOCK_ID, is_locked: 0 })
|
|
219
241
|
.execute();
|
|
220
242
|
}
|
|
221
243
|
catch (error) {
|
|
@@ -5,6 +5,9 @@ import { TableExpressionNode } from './operation-node-utils.js';
|
|
|
5
5
|
import { ReturningNode } from './returning-node.js';
|
|
6
6
|
import { WhereNode } from './where-node.js';
|
|
7
7
|
import { WithNode } from './with-node.js';
|
|
8
|
+
import { LimitNode } from './limit-node.js';
|
|
9
|
+
import { OrderByNode } from './order-by-node.js';
|
|
10
|
+
import { OrderByItemNode } from './order-by-item-node.js';
|
|
8
11
|
export interface DeleteQueryNode extends OperationNode {
|
|
9
12
|
readonly kind: 'DeleteQueryNode';
|
|
10
13
|
readonly from: FromNode;
|
|
@@ -12,6 +15,8 @@ export interface DeleteQueryNode extends OperationNode {
|
|
|
12
15
|
readonly where?: WhereNode;
|
|
13
16
|
readonly returning?: ReturningNode;
|
|
14
17
|
readonly with?: WithNode;
|
|
18
|
+
readonly orderBy?: OrderByNode;
|
|
19
|
+
readonly limit?: LimitNode;
|
|
15
20
|
}
|
|
16
21
|
/**
|
|
17
22
|
* @internal
|
|
@@ -19,4 +24,6 @@ export interface DeleteQueryNode extends OperationNode {
|
|
|
19
24
|
export declare const DeleteQueryNode: Readonly<{
|
|
20
25
|
is(node: OperationNode): node is DeleteQueryNode;
|
|
21
26
|
create(fromItem: TableExpressionNode, withNode?: WithNode | undefined): DeleteQueryNode;
|
|
27
|
+
cloneWithOrderByItem(deleteNode: DeleteQueryNode, item: OrderByItemNode): DeleteQueryNode;
|
|
28
|
+
cloneWithLimit(deleteNode: DeleteQueryNode, limit: LimitNode): DeleteQueryNode;
|
|
22
29
|
}>;
|
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.DeleteQueryNode = void 0;
|
|
4
4
|
const object_utils_js_1 = require("../util/object-utils.js");
|
|
5
5
|
const from_node_js_1 = require("./from-node.js");
|
|
6
|
+
const order_by_node_js_1 = require("./order-by-node.js");
|
|
6
7
|
/**
|
|
7
8
|
* @internal
|
|
8
9
|
*/
|
|
@@ -17,4 +18,18 @@ exports.DeleteQueryNode = (0, object_utils_js_1.freeze)({
|
|
|
17
18
|
...(withNode && { with: withNode }),
|
|
18
19
|
});
|
|
19
20
|
},
|
|
21
|
+
cloneWithOrderByItem(deleteNode, item) {
|
|
22
|
+
return (0, object_utils_js_1.freeze)({
|
|
23
|
+
...deleteNode,
|
|
24
|
+
orderBy: deleteNode.orderBy
|
|
25
|
+
? order_by_node_js_1.OrderByNode.cloneWithItem(deleteNode.orderBy, item)
|
|
26
|
+
: order_by_node_js_1.OrderByNode.create(item),
|
|
27
|
+
});
|
|
28
|
+
},
|
|
29
|
+
cloneWithLimit(deleteNode, limit) {
|
|
30
|
+
return (0, object_utils_js_1.freeze)({
|
|
31
|
+
...deleteNode,
|
|
32
|
+
limit,
|
|
33
|
+
});
|
|
34
|
+
},
|
|
20
35
|
});
|
|
@@ -19,6 +19,7 @@ import { NoResultErrorConstructor } from './no-result-error.js';
|
|
|
19
19
|
import { DeleteResult } from './delete-result.js';
|
|
20
20
|
import { DeleteQueryNode } from '../operation-node/delete-query-node.js';
|
|
21
21
|
import { Selectable } from '../util/column-type.js';
|
|
22
|
+
import { OrderByDirectionExpression, OrderByExpression } from '../parser/order-by-parser.js';
|
|
22
23
|
export declare class DeleteQueryBuilder<DB, TB extends keyof DB, O> implements WhereInterface<DB, TB>, JoinInterface<DB, TB>, ReturningInterface<DB, TB, O>, OperationNodeSource, Compilable {
|
|
23
24
|
#private;
|
|
24
25
|
constructor(props: DeleteQueryBuilderProps);
|
|
@@ -564,8 +565,6 @@ export declare class DeleteQueryBuilder<DB, TB extends keyof DB, O> implements W
|
|
|
564
565
|
* Return arbitrary expressions:
|
|
565
566
|
*
|
|
566
567
|
* ```ts
|
|
567
|
-
* const { raw } = db
|
|
568
|
-
*
|
|
569
568
|
* const { id, full_name, first_pet_id } = await db
|
|
570
569
|
* .insertInto('person')
|
|
571
570
|
* .values({
|
|
@@ -574,7 +573,7 @@ export declare class DeleteQueryBuilder<DB, TB extends keyof DB, O> implements W
|
|
|
574
573
|
* })
|
|
575
574
|
* .returning([
|
|
576
575
|
* 'id as id',
|
|
577
|
-
* raw<string>(`concat(first_name, ' ', last_name)`).as('full_name'),
|
|
576
|
+
* db.raw<string>(`concat(first_name, ' ', last_name)`).as('full_name'),
|
|
578
577
|
* (qb) => qb.selectFrom('pets').select('pet.id').limit(1).as('first_pet_id')
|
|
579
578
|
* ])
|
|
580
579
|
* .executeTakeFirst()
|
|
@@ -587,6 +586,60 @@ export declare class DeleteQueryBuilder<DB, TB extends keyof DB, O> implements W
|
|
|
587
586
|
* that support `returning` such as PostgreSQL.
|
|
588
587
|
*/
|
|
589
588
|
returningAll(): DeleteQueryBuilder<DB, TB, Selectable<DB[TB]>>;
|
|
589
|
+
/**
|
|
590
|
+
* Adds an `order by` clause to the query.
|
|
591
|
+
*
|
|
592
|
+
* `orderBy` calls are additive. To order by multiple columns, call `orderBy`
|
|
593
|
+
* multiple times.
|
|
594
|
+
*
|
|
595
|
+
* The first argument is the expression to order by and the second is the
|
|
596
|
+
* order (`asc` or `desc`).
|
|
597
|
+
*
|
|
598
|
+
* An `order by` clause in a delete query is only supported by some dialects
|
|
599
|
+
* like MySQL.
|
|
600
|
+
*
|
|
601
|
+
* See {@link SelectQueryBuilder.orderBy} for more examples.
|
|
602
|
+
*
|
|
603
|
+
* ### Examples
|
|
604
|
+
*
|
|
605
|
+
* Delete 5 oldest items in a table:
|
|
606
|
+
*
|
|
607
|
+
* ```ts
|
|
608
|
+
* await db
|
|
609
|
+
* .deleteFrom('pet')
|
|
610
|
+
* .orderBy('created_at')
|
|
611
|
+
* .limit(5)
|
|
612
|
+
* .execute()
|
|
613
|
+
* ```
|
|
614
|
+
*
|
|
615
|
+
* The generated SQL (MySQL):
|
|
616
|
+
*
|
|
617
|
+
* ```sql
|
|
618
|
+
* delete from `pet`
|
|
619
|
+
* order by `created_at`
|
|
620
|
+
* limit ?
|
|
621
|
+
* ```
|
|
622
|
+
*/
|
|
623
|
+
orderBy(orderBy: OrderByExpression<DB, TB, O>, direction?: OrderByDirectionExpression): DeleteQueryBuilder<DB, TB, O>;
|
|
624
|
+
/**
|
|
625
|
+
* Adds a limit clause to the query.
|
|
626
|
+
*
|
|
627
|
+
* A limit clause in a delete query is only supported by some dialects
|
|
628
|
+
* like MySQL.
|
|
629
|
+
*
|
|
630
|
+
* ### Examples
|
|
631
|
+
*
|
|
632
|
+
* Delete 5 oldest items in a table:
|
|
633
|
+
*
|
|
634
|
+
* ```ts
|
|
635
|
+
* await db
|
|
636
|
+
* .deleteFrom('pet')
|
|
637
|
+
* .orderBy('created_at')
|
|
638
|
+
* .limit(5)
|
|
639
|
+
* .execute()
|
|
640
|
+
* ```
|
|
641
|
+
*/
|
|
642
|
+
limit(limit: number): DeleteQueryBuilder<DB, TB, O>;
|
|
590
643
|
/**
|
|
591
644
|
* Simply calls the given function passing `this` as the only argument.
|
|
592
645
|
*
|
|
@@ -9,6 +9,9 @@ const prevent_await_js_1 = require("../util/prevent-await.js");
|
|
|
9
9
|
const object_utils_js_1 = require("../util/object-utils.js");
|
|
10
10
|
const no_result_error_js_1 = require("./no-result-error.js");
|
|
11
11
|
const delete_result_js_1 = require("./delete-result.js");
|
|
12
|
+
const delete_query_node_js_1 = require("../operation-node/delete-query-node.js");
|
|
13
|
+
const limit_node_js_1 = require("../operation-node/limit-node.js");
|
|
14
|
+
const order_by_parser_js_1 = require("../parser/order-by-parser.js");
|
|
12
15
|
class DeleteQueryBuilder {
|
|
13
16
|
#props;
|
|
14
17
|
constructor(props) {
|
|
@@ -98,6 +101,70 @@ class DeleteQueryBuilder {
|
|
|
98
101
|
queryNode: query_node_js_1.QueryNode.cloneWithReturning(this.#props.queryNode, (0, select_parser_js_1.parseSelectAll)()),
|
|
99
102
|
});
|
|
100
103
|
}
|
|
104
|
+
/**
|
|
105
|
+
* Adds an `order by` clause to the query.
|
|
106
|
+
*
|
|
107
|
+
* `orderBy` calls are additive. To order by multiple columns, call `orderBy`
|
|
108
|
+
* multiple times.
|
|
109
|
+
*
|
|
110
|
+
* The first argument is the expression to order by and the second is the
|
|
111
|
+
* order (`asc` or `desc`).
|
|
112
|
+
*
|
|
113
|
+
* An `order by` clause in a delete query is only supported by some dialects
|
|
114
|
+
* like MySQL.
|
|
115
|
+
*
|
|
116
|
+
* See {@link SelectQueryBuilder.orderBy} for more examples.
|
|
117
|
+
*
|
|
118
|
+
* ### Examples
|
|
119
|
+
*
|
|
120
|
+
* Delete 5 oldest items in a table:
|
|
121
|
+
*
|
|
122
|
+
* ```ts
|
|
123
|
+
* await db
|
|
124
|
+
* .deleteFrom('pet')
|
|
125
|
+
* .orderBy('created_at')
|
|
126
|
+
* .limit(5)
|
|
127
|
+
* .execute()
|
|
128
|
+
* ```
|
|
129
|
+
*
|
|
130
|
+
* The generated SQL (MySQL):
|
|
131
|
+
*
|
|
132
|
+
* ```sql
|
|
133
|
+
* delete from `pet`
|
|
134
|
+
* order by `created_at`
|
|
135
|
+
* limit ?
|
|
136
|
+
* ```
|
|
137
|
+
*/
|
|
138
|
+
orderBy(orderBy, direction) {
|
|
139
|
+
return new DeleteQueryBuilder({
|
|
140
|
+
...this.#props,
|
|
141
|
+
queryNode: delete_query_node_js_1.DeleteQueryNode.cloneWithOrderByItem(this.#props.queryNode, (0, order_by_parser_js_1.parseOrderBy)(this.#props.parseContext, orderBy, direction)),
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Adds a limit clause to the query.
|
|
146
|
+
*
|
|
147
|
+
* A limit clause in a delete query is only supported by some dialects
|
|
148
|
+
* like MySQL.
|
|
149
|
+
*
|
|
150
|
+
* ### Examples
|
|
151
|
+
*
|
|
152
|
+
* Delete 5 oldest items in a table:
|
|
153
|
+
*
|
|
154
|
+
* ```ts
|
|
155
|
+
* await db
|
|
156
|
+
* .deleteFrom('pet')
|
|
157
|
+
* .orderBy('created_at')
|
|
158
|
+
* .limit(5)
|
|
159
|
+
* .execute()
|
|
160
|
+
* ```
|
|
161
|
+
*/
|
|
162
|
+
limit(limit) {
|
|
163
|
+
return new DeleteQueryBuilder({
|
|
164
|
+
...this.#props,
|
|
165
|
+
queryNode: delete_query_node_js_1.DeleteQueryNode.cloneWithLimit(this.#props.queryNode, limit_node_js_1.LimitNode.create(limit)),
|
|
166
|
+
});
|
|
167
|
+
}
|
|
101
168
|
/**
|
|
102
169
|
* Simply calls the given function passing `this` as the only argument.
|
|
103
170
|
*
|
|
@@ -423,8 +423,6 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
423
423
|
* Return arbitrary expressions:
|
|
424
424
|
*
|
|
425
425
|
* ```ts
|
|
426
|
-
* const { raw } = db
|
|
427
|
-
*
|
|
428
426
|
* const { id, full_name, first_pet_id } = await db
|
|
429
427
|
* .insertInto('person')
|
|
430
428
|
* .values({
|
|
@@ -433,7 +431,7 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
|
|
|
433
431
|
* })
|
|
434
432
|
* .returning([
|
|
435
433
|
* 'id as id',
|
|
436
|
-
* raw<string>(`concat(first_name, ' ', last_name)`).as('full_name'),
|
|
434
|
+
* db.raw<string>(`concat(first_name, ' ', last_name)`).as('full_name'),
|
|
437
435
|
* (qb) => qb.selectFrom('pets').select('pet.id').limit(1).as('first_pet_id')
|
|
438
436
|
* ])
|
|
439
437
|
* .executeTakeFirst()
|
|
@@ -45,8 +45,6 @@ export interface ReturningInterface<DB, TB extends keyof DB, O> {
|
|
|
45
45
|
* Return arbitrary expressions:
|
|
46
46
|
*
|
|
47
47
|
* ```ts
|
|
48
|
-
* const { raw } = db
|
|
49
|
-
*
|
|
50
48
|
* const { id, full_name, first_pet_id } = await db
|
|
51
49
|
* .insertInto('person')
|
|
52
50
|
* .values({
|
|
@@ -55,7 +53,7 @@ export interface ReturningInterface<DB, TB extends keyof DB, O> {
|
|
|
55
53
|
* })
|
|
56
54
|
* .returning([
|
|
57
55
|
* 'id as id',
|
|
58
|
-
* raw<string>(`concat(first_name, ' ', last_name)`).as('full_name'),
|
|
56
|
+
* db.raw<string>(`concat(first_name, ' ', last_name)`).as('full_name'),
|
|
59
57
|
* (qb) => qb.selectFrom('pets').select('pet.id').limit(1).as('first_pet_id')
|
|
60
58
|
* ])
|
|
61
59
|
* .executeTakeFirst()
|
|
@@ -456,7 +456,10 @@ export declare class SelectQueryBuilder<DB, TB extends keyof DB, O> implements W
|
|
|
456
456
|
* same as calling `select(['id', 'first_name']).
|
|
457
457
|
*
|
|
458
458
|
* To select all columns of the query or specific tables see the
|
|
459
|
-
* {@link
|
|
459
|
+
* {@link selectAll} method.
|
|
460
|
+
*
|
|
461
|
+
* See the {@link if} method if you are looking for a way to add selections
|
|
462
|
+
* based on a runtime condition.
|
|
460
463
|
*
|
|
461
464
|
* ### Examples
|
|
462
465
|
*
|
|
@@ -971,14 +974,12 @@ export declare class SelectQueryBuilder<DB, TB extends keyof DB, O> implements W
|
|
|
971
974
|
* `groupBy` also accepts an array:
|
|
972
975
|
*
|
|
973
976
|
* ```ts
|
|
974
|
-
* const { raw } = db
|
|
975
|
-
*
|
|
976
977
|
* await db
|
|
977
978
|
* .selectFrom('person')
|
|
978
979
|
* .select([
|
|
979
980
|
* 'first_name',
|
|
980
981
|
* 'last_name',
|
|
981
|
-
* raw('max(id)').as('max_id')
|
|
982
|
+
* db.raw('max(id)').as('max_id')
|
|
982
983
|
* ])
|
|
983
984
|
* .groupBy([
|
|
984
985
|
* 'first_name',
|
|
@@ -999,17 +1000,15 @@ export declare class SelectQueryBuilder<DB, TB extends keyof DB, O> implements W
|
|
|
999
1000
|
* raw expressions:
|
|
1000
1001
|
*
|
|
1001
1002
|
* ```ts
|
|
1002
|
-
* const { raw } = db
|
|
1003
|
-
*
|
|
1004
1003
|
* await db
|
|
1005
1004
|
* .selectFrom('person')
|
|
1006
1005
|
* .select([
|
|
1007
1006
|
* 'first_name',
|
|
1008
1007
|
* 'last_name',
|
|
1009
|
-
* raw('max(id)').as('max_id')
|
|
1008
|
+
* db.raw('max(id)').as('max_id')
|
|
1010
1009
|
* ])
|
|
1011
1010
|
* .groupBy([
|
|
1012
|
-
* raw('concat(first_name, last_name)'),
|
|
1011
|
+
* db.raw('concat(first_name, last_name)'),
|
|
1013
1012
|
* (qb) => qb.selectFrom('pet').select('id').limit(1)
|
|
1014
1013
|
* ])
|
|
1015
1014
|
* .execute()
|
|
@@ -651,8 +651,6 @@ export declare class UpdateQueryBuilder<DB, TB extends keyof DB, O> implements W
|
|
|
651
651
|
* Return arbitrary expressions:
|
|
652
652
|
*
|
|
653
653
|
* ```ts
|
|
654
|
-
* const { raw } = db
|
|
655
|
-
*
|
|
656
654
|
* const { id, full_name, first_pet_id } = await db
|
|
657
655
|
* .insertInto('person')
|
|
658
656
|
* .values({
|
|
@@ -661,7 +659,7 @@ export declare class UpdateQueryBuilder<DB, TB extends keyof DB, O> implements W
|
|
|
661
659
|
* })
|
|
662
660
|
* .returning([
|
|
663
661
|
* 'id as id',
|
|
664
|
-
* raw<string>(`concat(first_name, ' ', last_name)`).as('full_name'),
|
|
662
|
+
* db.raw<string>(`concat(first_name, ' ', last_name)`).as('full_name'),
|
|
665
663
|
* (qb) => qb.selectFrom('pets').select('pet.id').limit(1).as('first_pet_id')
|
|
666
664
|
* ])
|
|
667
665
|
* .executeTakeFirst()
|
|
@@ -29,9 +29,9 @@ class DefaultQueryCompiler extends operation_node_visitor_js_1.OperationNodeVisi
|
|
|
29
29
|
}
|
|
30
30
|
visitSelectQuery(node) {
|
|
31
31
|
const wrapInParens = this.parentNode !== undefined &&
|
|
32
|
+
!insert_query_node_js_1.InsertQueryNode.is(this.parentNode) &&
|
|
32
33
|
!create_view_node_js_1.CreateViewNode.is(this.parentNode) &&
|
|
33
|
-
!union_node_js_1.UnionNode.is(this.parentNode)
|
|
34
|
-
!insert_query_node_js_1.InsertQueryNode.is(this.parentNode);
|
|
34
|
+
!union_node_js_1.UnionNode.is(this.parentNode);
|
|
35
35
|
if (wrapInParens) {
|
|
36
36
|
this.append('(');
|
|
37
37
|
}
|
|
@@ -190,6 +190,14 @@ class DefaultQueryCompiler extends operation_node_visitor_js_1.OperationNodeVisi
|
|
|
190
190
|
this.append(' ');
|
|
191
191
|
this.visitNode(node.where);
|
|
192
192
|
}
|
|
193
|
+
if (node.orderBy) {
|
|
194
|
+
this.append(' ');
|
|
195
|
+
this.visitNode(node.orderBy);
|
|
196
|
+
}
|
|
197
|
+
if (node.limit) {
|
|
198
|
+
this.append(' ');
|
|
199
|
+
this.visitNode(node.limit);
|
|
200
|
+
}
|
|
193
201
|
if (node.returning) {
|
|
194
202
|
this.append(' ');
|
|
195
203
|
this.visitNode(node.returning);
|
|
@@ -285,12 +293,13 @@ class DefaultQueryCompiler extends operation_node_visitor_js_1.OperationNodeVisi
|
|
|
285
293
|
this.visitNode(node.on);
|
|
286
294
|
}
|
|
287
295
|
visitRaw(node) {
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
296
|
+
const { sqlFragments, params } = node;
|
|
297
|
+
for (let i = 0; i < sqlFragments.length; ++i) {
|
|
298
|
+
this.append(sqlFragments[i]);
|
|
299
|
+
if (params.length > i) {
|
|
300
|
+
this.visitNode(params[i]);
|
|
292
301
|
}
|
|
293
|
-
}
|
|
302
|
+
}
|
|
294
303
|
}
|
|
295
304
|
visitOperator(node) {
|
|
296
305
|
this.append(node.operator);
|
|
@@ -258,14 +258,18 @@ export declare class QueryCreator<DB> {
|
|
|
258
258
|
* This only affects the query created through the builder returned from
|
|
259
259
|
* this method and doesn't modify the `db` instance.
|
|
260
260
|
*
|
|
261
|
+
* See [this recipe](https://github.com/koskimas/kysely/tree/master/recipes/schemas.md)
|
|
262
|
+
* for a more detailed explanation.
|
|
263
|
+
*
|
|
261
264
|
* ### Examples
|
|
262
265
|
*
|
|
263
266
|
* ```
|
|
264
|
-
* await db
|
|
265
|
-
*
|
|
266
|
-
*
|
|
267
|
-
*
|
|
268
|
-
*
|
|
267
|
+
* await db
|
|
268
|
+
* .withSchema('mammals')
|
|
269
|
+
* .selectFrom('pet')
|
|
270
|
+
* .selectAll()
|
|
271
|
+
* .innerJoin('public.person', 'public.person.id', 'pet.owner_id')
|
|
272
|
+
* .execute()
|
|
269
273
|
* ```
|
|
270
274
|
*
|
|
271
275
|
* The generated SQL (PostgreSQL):
|
|
@@ -281,10 +285,11 @@ export declare class QueryCreator<DB> {
|
|
|
281
285
|
* doesn't belong to:
|
|
282
286
|
*
|
|
283
287
|
* ```
|
|
284
|
-
* await db
|
|
285
|
-
*
|
|
286
|
-
*
|
|
287
|
-
*
|
|
288
|
+
* await db
|
|
289
|
+
* .withSchema('mammals')
|
|
290
|
+
* .selectFrom('pet as p')
|
|
291
|
+
* .select('p.name')
|
|
292
|
+
* .execute()
|
|
288
293
|
* ```
|
|
289
294
|
*
|
|
290
295
|
* The generated SQL (PostgreSQL):
|
|
@@ -204,14 +204,18 @@ class QueryCreator {
|
|
|
204
204
|
* This only affects the query created through the builder returned from
|
|
205
205
|
* this method and doesn't modify the `db` instance.
|
|
206
206
|
*
|
|
207
|
+
* See [this recipe](https://github.com/koskimas/kysely/tree/master/recipes/schemas.md)
|
|
208
|
+
* for a more detailed explanation.
|
|
209
|
+
*
|
|
207
210
|
* ### Examples
|
|
208
211
|
*
|
|
209
212
|
* ```
|
|
210
|
-
* await db
|
|
211
|
-
*
|
|
212
|
-
*
|
|
213
|
-
*
|
|
214
|
-
*
|
|
213
|
+
* await db
|
|
214
|
+
* .withSchema('mammals')
|
|
215
|
+
* .selectFrom('pet')
|
|
216
|
+
* .selectAll()
|
|
217
|
+
* .innerJoin('public.person', 'public.person.id', 'pet.owner_id')
|
|
218
|
+
* .execute()
|
|
215
219
|
* ```
|
|
216
220
|
*
|
|
217
221
|
* The generated SQL (PostgreSQL):
|
|
@@ -227,10 +231,11 @@ class QueryCreator {
|
|
|
227
231
|
* doesn't belong to:
|
|
228
232
|
*
|
|
229
233
|
* ```
|
|
230
|
-
* await db
|
|
231
|
-
*
|
|
232
|
-
*
|
|
233
|
-
*
|
|
234
|
+
* await db
|
|
235
|
+
* .withSchema('mammals')
|
|
236
|
+
* .selectFrom('pet as p')
|
|
237
|
+
* .select('p.name')
|
|
238
|
+
* .execute()
|
|
234
239
|
* ```
|
|
235
240
|
*
|
|
236
241
|
* The generated SQL (PostgreSQL):
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.QueryExecutor = void 0;
|
|
4
4
|
const object_utils_js_1 = require("../util/object-utils.js");
|
|
5
|
+
const NO_PLUGINS = (0, object_utils_js_1.freeze)([]);
|
|
5
6
|
/**
|
|
6
7
|
* This class abstracts away the details of how to compile a query into SQL
|
|
7
8
|
* and execute it. Instead of passing around all those details, {@link SelectQueryBuilder}
|
|
@@ -10,8 +11,8 @@ const object_utils_js_1 = require("../util/object-utils.js");
|
|
|
10
11
|
*/
|
|
11
12
|
class QueryExecutor {
|
|
12
13
|
#plugins;
|
|
13
|
-
constructor(plugins
|
|
14
|
-
this.#plugins =
|
|
14
|
+
constructor(plugins) {
|
|
15
|
+
this.#plugins = plugins ?? NO_PLUGINS;
|
|
15
16
|
}
|
|
16
17
|
get plugins() {
|
|
17
18
|
return this.#plugins;
|
|
@@ -40,8 +40,8 @@ export declare class AliasedRawBuilder<O = unknown, A extends string = never> im
|
|
|
40
40
|
constructor(rawBuilder: RawBuilder<O>, alias: A);
|
|
41
41
|
}
|
|
42
42
|
export interface RawBuilderProps {
|
|
43
|
-
readonly queryId
|
|
44
|
-
readonly executor
|
|
43
|
+
readonly queryId?: QueryId;
|
|
44
|
+
readonly executor?: QueryExecutor;
|
|
45
45
|
readonly sql: string;
|
|
46
46
|
readonly parameters?: ReadonlyArray<unknown>;
|
|
47
47
|
}
|
|
@@ -7,11 +7,18 @@ const raw_node_js_1 = require("../operation-node/raw-node.js");
|
|
|
7
7
|
const value_node_js_1 = require("../operation-node/value-node.js");
|
|
8
8
|
const reference_parser_js_1 = require("../parser/reference-parser.js");
|
|
9
9
|
const prevent_await_js_1 = require("../util/prevent-await.js");
|
|
10
|
+
const query_id_js_1 = require("../util/query-id.js");
|
|
10
11
|
const object_utils_js_1 = require("../util/object-utils.js");
|
|
12
|
+
const noop_query_executor_js_1 = require("../query-executor/noop-query-executor.js");
|
|
11
13
|
class RawBuilder {
|
|
12
14
|
#props;
|
|
13
15
|
constructor(props) {
|
|
14
|
-
this.#props = (0, object_utils_js_1.freeze)(
|
|
16
|
+
this.#props = (0, object_utils_js_1.freeze)({
|
|
17
|
+
queryId: props.queryId ?? (0, query_id_js_1.createQueryId)(),
|
|
18
|
+
executor: props.executor ?? new noop_query_executor_js_1.NoopQueryExecutor(),
|
|
19
|
+
sql: props.sql,
|
|
20
|
+
parameters: props.parameters,
|
|
21
|
+
});
|
|
15
22
|
}
|
|
16
23
|
as(alias) {
|
|
17
24
|
return new AliasedRawBuilder(this, alias);
|