kysely 0.16.10 → 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/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 +55 -0
- package/dist/cjs/query-builder/delete-query-builder.js +67 -0
- package/dist/cjs/query-compiler/default-query-compiler.js +8 -0
- package/dist/cjs/query-creator.d.ts +14 -9
- package/dist/cjs/query-creator.js +14 -9
- 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 +55 -0
- package/dist/esm/query-builder/delete-query-builder.js +67 -0
- package/dist/esm/query-compiler/default-query-compiler.js +8 -0
- package/dist/esm/query-creator.d.ts +14 -9
- package/dist/esm/query-creator.js +14 -9
- package/package.json +1 -1
|
@@ -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);
|
|
@@ -585,6 +586,60 @@ export declare class DeleteQueryBuilder<DB, TB extends keyof DB, O> implements W
|
|
|
585
586
|
* that support `returning` such as PostgreSQL.
|
|
586
587
|
*/
|
|
587
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>;
|
|
588
643
|
/**
|
|
589
644
|
* Simply calls the given function passing `this` as the only argument.
|
|
590
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
|
*
|
|
@@ -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);
|
|
@@ -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):
|
|
@@ -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
|
}>;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/// <reference types="./delete-query-node.d.ts" />
|
|
2
2
|
import { freeze } from '../util/object-utils.js';
|
|
3
3
|
import { FromNode } from './from-node.js';
|
|
4
|
+
import { OrderByNode } from './order-by-node.js';
|
|
4
5
|
/**
|
|
5
6
|
* @internal
|
|
6
7
|
*/
|
|
@@ -15,4 +16,18 @@ export const DeleteQueryNode = freeze({
|
|
|
15
16
|
...(withNode && { with: withNode }),
|
|
16
17
|
});
|
|
17
18
|
},
|
|
19
|
+
cloneWithOrderByItem(deleteNode, item) {
|
|
20
|
+
return freeze({
|
|
21
|
+
...deleteNode,
|
|
22
|
+
orderBy: deleteNode.orderBy
|
|
23
|
+
? OrderByNode.cloneWithItem(deleteNode.orderBy, item)
|
|
24
|
+
: OrderByNode.create(item),
|
|
25
|
+
});
|
|
26
|
+
},
|
|
27
|
+
cloneWithLimit(deleteNode, limit) {
|
|
28
|
+
return freeze({
|
|
29
|
+
...deleteNode,
|
|
30
|
+
limit,
|
|
31
|
+
});
|
|
32
|
+
},
|
|
18
33
|
});
|
|
@@ -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);
|
|
@@ -585,6 +586,60 @@ export declare class DeleteQueryBuilder<DB, TB extends keyof DB, O> implements W
|
|
|
585
586
|
* that support `returning` such as PostgreSQL.
|
|
586
587
|
*/
|
|
587
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>;
|
|
588
643
|
/**
|
|
589
644
|
* Simply calls the given function passing `this` as the only argument.
|
|
590
645
|
*
|
|
@@ -7,6 +7,9 @@ import { preventAwait } from '../util/prevent-await.js';
|
|
|
7
7
|
import { freeze } from '../util/object-utils.js';
|
|
8
8
|
import { NoResultError } from './no-result-error.js';
|
|
9
9
|
import { DeleteResult } from './delete-result.js';
|
|
10
|
+
import { DeleteQueryNode } from '../operation-node/delete-query-node.js';
|
|
11
|
+
import { LimitNode } from '../operation-node/limit-node.js';
|
|
12
|
+
import { parseOrderBy, } from '../parser/order-by-parser.js';
|
|
10
13
|
export class DeleteQueryBuilder {
|
|
11
14
|
#props;
|
|
12
15
|
constructor(props) {
|
|
@@ -96,6 +99,70 @@ export class DeleteQueryBuilder {
|
|
|
96
99
|
queryNode: QueryNode.cloneWithReturning(this.#props.queryNode, parseSelectAll()),
|
|
97
100
|
});
|
|
98
101
|
}
|
|
102
|
+
/**
|
|
103
|
+
* Adds an `order by` clause to the query.
|
|
104
|
+
*
|
|
105
|
+
* `orderBy` calls are additive. To order by multiple columns, call `orderBy`
|
|
106
|
+
* multiple times.
|
|
107
|
+
*
|
|
108
|
+
* The first argument is the expression to order by and the second is the
|
|
109
|
+
* order (`asc` or `desc`).
|
|
110
|
+
*
|
|
111
|
+
* An `order by` clause in a delete query is only supported by some dialects
|
|
112
|
+
* like MySQL.
|
|
113
|
+
*
|
|
114
|
+
* See {@link SelectQueryBuilder.orderBy} for more examples.
|
|
115
|
+
*
|
|
116
|
+
* ### Examples
|
|
117
|
+
*
|
|
118
|
+
* Delete 5 oldest items in a table:
|
|
119
|
+
*
|
|
120
|
+
* ```ts
|
|
121
|
+
* await db
|
|
122
|
+
* .deleteFrom('pet')
|
|
123
|
+
* .orderBy('created_at')
|
|
124
|
+
* .limit(5)
|
|
125
|
+
* .execute()
|
|
126
|
+
* ```
|
|
127
|
+
*
|
|
128
|
+
* The generated SQL (MySQL):
|
|
129
|
+
*
|
|
130
|
+
* ```sql
|
|
131
|
+
* delete from `pet`
|
|
132
|
+
* order by `created_at`
|
|
133
|
+
* limit ?
|
|
134
|
+
* ```
|
|
135
|
+
*/
|
|
136
|
+
orderBy(orderBy, direction) {
|
|
137
|
+
return new DeleteQueryBuilder({
|
|
138
|
+
...this.#props,
|
|
139
|
+
queryNode: DeleteQueryNode.cloneWithOrderByItem(this.#props.queryNode, parseOrderBy(this.#props.parseContext, orderBy, direction)),
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Adds a limit clause to the query.
|
|
144
|
+
*
|
|
145
|
+
* A limit clause in a delete query is only supported by some dialects
|
|
146
|
+
* like MySQL.
|
|
147
|
+
*
|
|
148
|
+
* ### Examples
|
|
149
|
+
*
|
|
150
|
+
* Delete 5 oldest items in a table:
|
|
151
|
+
*
|
|
152
|
+
* ```ts
|
|
153
|
+
* await db
|
|
154
|
+
* .deleteFrom('pet')
|
|
155
|
+
* .orderBy('created_at')
|
|
156
|
+
* .limit(5)
|
|
157
|
+
* .execute()
|
|
158
|
+
* ```
|
|
159
|
+
*/
|
|
160
|
+
limit(limit) {
|
|
161
|
+
return new DeleteQueryBuilder({
|
|
162
|
+
...this.#props,
|
|
163
|
+
queryNode: DeleteQueryNode.cloneWithLimit(this.#props.queryNode, LimitNode.create(limit)),
|
|
164
|
+
});
|
|
165
|
+
}
|
|
99
166
|
/**
|
|
100
167
|
* Simply calls the given function passing `this` as the only argument.
|
|
101
168
|
*
|
|
@@ -188,6 +188,14 @@ export class DefaultQueryCompiler extends OperationNodeVisitor {
|
|
|
188
188
|
this.append(' ');
|
|
189
189
|
this.visitNode(node.where);
|
|
190
190
|
}
|
|
191
|
+
if (node.orderBy) {
|
|
192
|
+
this.append(' ');
|
|
193
|
+
this.visitNode(node.orderBy);
|
|
194
|
+
}
|
|
195
|
+
if (node.limit) {
|
|
196
|
+
this.append(' ');
|
|
197
|
+
this.visitNode(node.limit);
|
|
198
|
+
}
|
|
191
199
|
if (node.returning) {
|
|
192
200
|
this.append(' ');
|
|
193
201
|
this.visitNode(node.returning);
|
|
@@ -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):
|
|
@@ -202,14 +202,18 @@ export class QueryCreator {
|
|
|
202
202
|
* This only affects the query created through the builder returned from
|
|
203
203
|
* this method and doesn't modify the `db` instance.
|
|
204
204
|
*
|
|
205
|
+
* See [this recipe](https://github.com/koskimas/kysely/tree/master/recipes/schemas.md)
|
|
206
|
+
* for a more detailed explanation.
|
|
207
|
+
*
|
|
205
208
|
* ### Examples
|
|
206
209
|
*
|
|
207
210
|
* ```
|
|
208
|
-
* await db
|
|
209
|
-
*
|
|
210
|
-
*
|
|
211
|
-
*
|
|
212
|
-
*
|
|
211
|
+
* await db
|
|
212
|
+
* .withSchema('mammals')
|
|
213
|
+
* .selectFrom('pet')
|
|
214
|
+
* .selectAll()
|
|
215
|
+
* .innerJoin('public.person', 'public.person.id', 'pet.owner_id')
|
|
216
|
+
* .execute()
|
|
213
217
|
* ```
|
|
214
218
|
*
|
|
215
219
|
* The generated SQL (PostgreSQL):
|
|
@@ -225,10 +229,11 @@ export class QueryCreator {
|
|
|
225
229
|
* doesn't belong to:
|
|
226
230
|
*
|
|
227
231
|
* ```
|
|
228
|
-
* await db
|
|
229
|
-
*
|
|
230
|
-
*
|
|
231
|
-
*
|
|
232
|
+
* await db
|
|
233
|
+
* .withSchema('mammals')
|
|
234
|
+
* .selectFrom('pet as p')
|
|
235
|
+
* .select('p.name')
|
|
236
|
+
* .execute()
|
|
232
237
|
* ```
|
|
233
238
|
*
|
|
234
239
|
* The generated SQL (PostgreSQL):
|