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.
Files changed (41) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +21 -9
  3. package/dist/cjs/kysely.js +2 -2
  4. package/dist/cjs/migration/migrator.d.ts +22 -0
  5. package/dist/cjs/migration/migrator.js +23 -1
  6. package/dist/cjs/operation-node/delete-query-node.d.ts +7 -0
  7. package/dist/cjs/operation-node/delete-query-node.js +15 -0
  8. package/dist/cjs/query-builder/delete-query-builder.d.ts +56 -3
  9. package/dist/cjs/query-builder/delete-query-builder.js +67 -0
  10. package/dist/cjs/query-builder/insert-query-builder.d.ts +1 -3
  11. package/dist/cjs/query-builder/returning-interface.d.ts +1 -3
  12. package/dist/cjs/query-builder/select-query-builder.d.ts +7 -8
  13. package/dist/cjs/query-builder/update-query-builder.d.ts +1 -3
  14. package/dist/cjs/query-compiler/default-query-compiler.js +16 -7
  15. package/dist/cjs/query-creator.d.ts +14 -9
  16. package/dist/cjs/query-creator.js +14 -9
  17. package/dist/cjs/query-executor/query-executor.js +3 -2
  18. package/dist/cjs/raw-builder/raw-builder.d.ts +2 -2
  19. package/dist/cjs/raw-builder/raw-builder.js +8 -1
  20. package/dist/cjs/schema/alter-table-builder.js +3 -3
  21. package/dist/cjs/schema/create-table-builder.js +2 -2
  22. package/dist/esm/kysely.js +2 -2
  23. package/dist/esm/migration/migrator.d.ts +22 -0
  24. package/dist/esm/migration/migrator.js +23 -1
  25. package/dist/esm/operation-node/delete-query-node.d.ts +7 -0
  26. package/dist/esm/operation-node/delete-query-node.js +15 -0
  27. package/dist/esm/query-builder/delete-query-builder.d.ts +56 -3
  28. package/dist/esm/query-builder/delete-query-builder.js +67 -0
  29. package/dist/esm/query-builder/insert-query-builder.d.ts +1 -3
  30. package/dist/esm/query-builder/returning-interface.d.ts +1 -3
  31. package/dist/esm/query-builder/select-query-builder.d.ts +7 -8
  32. package/dist/esm/query-builder/update-query-builder.d.ts +1 -3
  33. package/dist/esm/query-compiler/default-query-compiler.js +16 -7
  34. package/dist/esm/query-creator.d.ts +14 -9
  35. package/dist/esm/query-creator.js +14 -9
  36. package/dist/esm/query-executor/query-executor.js +3 -2
  37. package/dist/esm/raw-builder/raw-builder.d.ts +2 -2
  38. package/dist/esm/raw-builder/raw-builder.js +8 -1
  39. package/dist/esm/schema/alter-table-builder.js +3 -3
  40. package/dist/esm/schema/create-table-builder.js +2 -2
  41. package/package.json +1 -1
@@ -9,7 +9,6 @@ const data_type_node_js_1 = require("../operation-node/data-type-node.js");
9
9
  const drop_column_node_js_1 = require("../operation-node/drop-column-node.js");
10
10
  const identifier_node_js_1 = require("../operation-node/identifier-node.js");
11
11
  const rename_column_node_js_1 = require("../operation-node/rename-column-node.js");
12
- const table_node_js_1 = require("../operation-node/table-node.js");
13
12
  const object_utils_js_1 = require("../util/object-utils.js");
14
13
  const prevent_await_js_1 = require("../util/prevent-await.js");
15
14
  const column_definition_builder_js_1 = require("./column-definition-builder.js");
@@ -22,6 +21,7 @@ const check_constraint_node_js_1 = require("../operation-node/check-constraint-n
22
21
  const foreign_key_constraint_node_js_1 = require("../operation-node/foreign-key-constraint-node.js");
23
22
  const column_node_js_1 = require("../operation-node/column-node.js");
24
23
  const default_value_parser_js_1 = require("../parser/default-value-parser.js");
24
+ const table_parser_js_1 = require("../parser/table-parser.js");
25
25
  /**
26
26
  * This builder can be used to create a `alter table` query.
27
27
  */
@@ -34,7 +34,7 @@ class AlterTableBuilder {
34
34
  return new AlterTableExecutor({
35
35
  ...this.#props,
36
36
  alterTableNode: alter_table_node_js_1.AlterTableNode.cloneWith(this.#props.alterTableNode, {
37
- renameTo: table_node_js_1.TableNode.create(newTableName),
37
+ renameTo: (0, table_parser_js_1.parseTable)(newTableName),
38
38
  }),
39
39
  });
40
40
  }
@@ -104,7 +104,7 @@ class AlterTableBuilder {
104
104
  addForeignKeyConstraint(constraintName, columns, targetTable, targetColumns) {
105
105
  return new AlterTableAddForeignKeyConstraintBuilder({
106
106
  ...this.#props,
107
- constraintBuilder: new foreign_key_constraint_builder_js_1.ForeignKeyConstraintBuilder(foreign_key_constraint_node_js_1.ForeignKeyConstraintNode.create(columns.map(column_node_js_1.ColumnNode.create), table_node_js_1.TableNode.create(targetTable), targetColumns.map(column_node_js_1.ColumnNode.create), constraintName)),
107
+ constraintBuilder: new foreign_key_constraint_builder_js_1.ForeignKeyConstraintBuilder(foreign_key_constraint_node_js_1.ForeignKeyConstraintNode.create(columns.map(column_node_js_1.ColumnNode.create), (0, table_parser_js_1.parseTable)(targetTable), targetColumns.map(column_node_js_1.ColumnNode.create), constraintName)),
108
108
  });
109
109
  }
110
110
  }
@@ -8,12 +8,12 @@ const column_definition_builder_js_1 = require("./column-definition-builder.js")
8
8
  const object_utils_js_1 = require("../util/object-utils.js");
9
9
  const foreign_key_constraint_node_js_1 = require("../operation-node/foreign-key-constraint-node.js");
10
10
  const column_node_js_1 = require("../operation-node/column-node.js");
11
- const table_node_js_1 = require("../operation-node/table-node.js");
12
11
  const foreign_key_constraint_builder_js_1 = require("./foreign-key-constraint-builder.js");
13
12
  const data_type_parser_js_1 = require("../parser/data-type-parser.js");
14
13
  const primary_constraint_node_js_1 = require("../operation-node/primary-constraint-node.js");
15
14
  const unique_constraint_node_js_1 = require("../operation-node/unique-constraint-node.js");
16
15
  const check_constraint_node_js_1 = require("../operation-node/check-constraint-node.js");
16
+ const table_parser_js_1 = require("../parser/table-parser.js");
17
17
  /**
18
18
  * This builder can be used to create a `create table` query.
19
19
  */
@@ -195,7 +195,7 @@ class CreateTableBuilder {
195
195
  * ```
196
196
  */
197
197
  addForeignKeyConstraint(constraintName, columns, targetTable, targetColumns, build = object_utils_js_1.noop) {
198
- const builder = build(new foreign_key_constraint_builder_js_1.ForeignKeyConstraintBuilder(foreign_key_constraint_node_js_1.ForeignKeyConstraintNode.create(columns.map(column_node_js_1.ColumnNode.create), table_node_js_1.TableNode.create(targetTable), targetColumns.map(column_node_js_1.ColumnNode.create), constraintName)));
198
+ const builder = build(new foreign_key_constraint_builder_js_1.ForeignKeyConstraintBuilder(foreign_key_constraint_node_js_1.ForeignKeyConstraintNode.create(columns.map(column_node_js_1.ColumnNode.create), (0, table_parser_js_1.parseTable)(targetTable), targetColumns.map(column_node_js_1.ColumnNode.create), constraintName)));
199
199
  return new CreateTableBuilder({
200
200
  ...this.#props,
201
201
  createTableNode: create_table_node_js_1.CreateTableNode.cloneWithConstraint(this.#props.createTableNode, builder.toOperationNode()),
@@ -340,12 +340,12 @@ export class ConnectionBuilder {
340
340
  async execute(callback) {
341
341
  const connection = await this.#props.driver.acquireConnection();
342
342
  const connectionProvider = new SingleConnectionProvider(connection);
343
- const transaction = new Kysely({
343
+ const db = new Kysely({
344
344
  ...this.#props,
345
345
  executor: this.#props.executor.withConnectionProvider(connectionProvider),
346
346
  });
347
347
  try {
348
- return await callback(transaction);
348
+ return await callback(db);
349
349
  }
350
350
  finally {
351
351
  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
+ }
@@ -30,6 +30,28 @@ export class Migrator {
30
30
  constructor(props) {
31
31
  this.#props = freeze(props);
32
32
  }
33
+ /**
34
+ * Returns a {@link MigrationInfo} object for each migration.
35
+ *
36
+ * The returned array is sorted by migration name.
37
+ */
38
+ async getMigrations() {
39
+ const executedMigrations = (await this.#doesTableExists(MIGRATION_TABLE))
40
+ ? await this.#props.db
41
+ .selectFrom(MIGRATION_TABLE)
42
+ .select(['name', 'timestamp'])
43
+ .execute()
44
+ : [];
45
+ const migrations = await this.#resolveMigrations();
46
+ return migrations.map(({ name, ...migration }) => {
47
+ const executed = executedMigrations.find((it) => it.name === name);
48
+ return {
49
+ name,
50
+ migration,
51
+ executedAt: executed ? new Date(executed.timestamp) : undefined,
52
+ };
53
+ });
54
+ }
33
55
  /**
34
56
  * Runs all migrations that have not yet been run.
35
57
  *
@@ -213,7 +235,7 @@ export class Migrator {
213
235
  try {
214
236
  await this.#props.db
215
237
  .insertInto(MIGRATION_LOCK_TABLE)
216
- .values({ id: MIGRATION_LOCK_ID })
238
+ .values({ id: MIGRATION_LOCK_ID, is_locked: 0 })
217
239
  .execute();
218
240
  }
219
241
  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
  }>;
@@ -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);
@@ -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
  *
@@ -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
  *
@@ -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 SelectQueryBuilder.selectAll | selectAll} method.
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()
@@ -27,9 +27,9 @@ export class DefaultQueryCompiler extends OperationNodeVisitor {
27
27
  }
28
28
  visitSelectQuery(node) {
29
29
  const wrapInParens = this.parentNode !== undefined &&
30
+ !InsertQueryNode.is(this.parentNode) &&
30
31
  !CreateViewNode.is(this.parentNode) &&
31
- !UnionNode.is(this.parentNode) &&
32
- !InsertQueryNode.is(this.parentNode);
32
+ !UnionNode.is(this.parentNode);
33
33
  if (wrapInParens) {
34
34
  this.append('(');
35
35
  }
@@ -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);
@@ -283,12 +291,13 @@ export class DefaultQueryCompiler extends OperationNodeVisitor {
283
291
  this.visitNode(node.on);
284
292
  }
285
293
  visitRaw(node) {
286
- node.sqlFragments.forEach((sql, i) => {
287
- this.append(sql);
288
- if (node.params.length > i) {
289
- this.visitNode(node.params[i]);
294
+ const { sqlFragments, params } = node;
295
+ for (let i = 0; i < sqlFragments.length; ++i) {
296
+ this.append(sqlFragments[i]);
297
+ if (params.length > i) {
298
+ this.visitNode(params[i]);
290
299
  }
291
- });
300
+ }
292
301
  }
293
302
  visitOperator(node) {
294
303
  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.withSchema('mammals')
265
- * .selectFrom('pet')
266
- * .selectAll()
267
- * .innerJoin('public.person', 'public.person.id', 'pet.owner_id')
268
- * .execute()
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.withSchema('mammals')
285
- * .selectFrom('pet as p')
286
- * .select('p.name')
287
- * .execute()
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.withSchema('mammals')
209
- * .selectFrom('pet')
210
- * .selectAll()
211
- * .innerJoin('public.person', 'public.person.id', 'pet.owner_id')
212
- * .execute()
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.withSchema('mammals')
229
- * .selectFrom('pet as p')
230
- * .select('p.name')
231
- * .execute()
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):
@@ -1,5 +1,6 @@
1
1
  /// <reference types="./query-executor.d.ts" />
2
2
  import { freeze } from '../util/object-utils.js';
3
+ const NO_PLUGINS = freeze([]);
3
4
  /**
4
5
  * This class abstracts away the details of how to compile a query into SQL
5
6
  * and execute it. Instead of passing around all those details, {@link SelectQueryBuilder}
@@ -8,8 +9,8 @@ import { freeze } from '../util/object-utils.js';
8
9
  */
9
10
  export class QueryExecutor {
10
11
  #plugins;
11
- constructor(plugins = []) {
12
- this.#plugins = freeze([...plugins]);
12
+ constructor(plugins) {
13
+ this.#plugins = plugins ?? NO_PLUGINS;
13
14
  }
14
15
  get plugins() {
15
16
  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: QueryId;
44
- readonly executor: QueryExecutor;
43
+ readonly queryId?: QueryId;
44
+ readonly executor?: QueryExecutor;
45
45
  readonly sql: string;
46
46
  readonly parameters?: ReadonlyArray<unknown>;
47
47
  }
@@ -5,11 +5,18 @@ import { RawNode } from '../operation-node/raw-node.js';
5
5
  import { ValueNode } from '../operation-node/value-node.js';
6
6
  import { parseStringReference } from '../parser/reference-parser.js';
7
7
  import { preventAwait } from '../util/prevent-await.js';
8
+ import { createQueryId } from '../util/query-id.js';
8
9
  import { freeze } from '../util/object-utils.js';
10
+ import { NoopQueryExecutor } from '../query-executor/noop-query-executor.js';
9
11
  export class RawBuilder {
10
12
  #props;
11
13
  constructor(props) {
12
- this.#props = freeze(props);
14
+ this.#props = freeze({
15
+ queryId: props.queryId ?? createQueryId(),
16
+ executor: props.executor ?? new NoopQueryExecutor(),
17
+ sql: props.sql,
18
+ parameters: props.parameters,
19
+ });
13
20
  }
14
21
  as(alias) {
15
22
  return new AliasedRawBuilder(this, alias);