kysely 0.16.6 → 0.16.7

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 (74) hide show
  1. package/README.md +118 -5
  2. package/dist/cjs/dialect/mysql/mysql-dialect.js +2 -1
  3. package/dist/cjs/dialect/mysql/mysql-driver.js +1 -1
  4. package/dist/cjs/dialect/postgres/postgres-dialect.js +2 -1
  5. package/dist/cjs/dialect/postgres/postgres-driver.js +1 -1
  6. package/dist/cjs/dialect/sqlite/sqlite-dialect.js +2 -1
  7. package/dist/cjs/dialect/sqlite/sqlite-driver.js +1 -1
  8. package/dist/cjs/driver/dummy-driver.d.ts +28 -0
  9. package/dist/cjs/driver/dummy-driver.js +31 -3
  10. package/dist/cjs/index-nodeless.d.ts +1 -0
  11. package/dist/cjs/index-nodeless.js +1 -0
  12. package/dist/cjs/kysely.d.ts +8 -6
  13. package/dist/cjs/kysely.js +8 -6
  14. package/dist/cjs/plugin/deduplicate-joins/deduplicate-joins-plugin.d.ts +48 -0
  15. package/dist/cjs/plugin/deduplicate-joins/deduplicate-joins-plugin.js +19 -0
  16. package/dist/cjs/plugin/deduplicate-joins/deduplicate-joins-transformer.d.ts +9 -0
  17. package/dist/cjs/plugin/deduplicate-joins/deduplicate-joins-transformer.js +42 -0
  18. package/dist/cjs/plugin/{immediate-value.ts → immediate-value}/immediate-value-plugin.d.ts +0 -0
  19. package/dist/cjs/plugin/{immediate-value.ts → immediate-value}/immediate-value-plugin.js +0 -0
  20. package/dist/cjs/plugin/{immediate-value.ts → immediate-value}/immediate-value-transformer.d.ts +0 -0
  21. package/dist/cjs/plugin/{immediate-value.ts → immediate-value}/immediate-value-transformer.js +0 -0
  22. package/dist/cjs/query-builder/delete-query-builder.d.ts +51 -9
  23. package/dist/cjs/query-builder/delete-query-builder.js +56 -8
  24. package/dist/cjs/query-builder/function-builder.d.ts +29 -5
  25. package/dist/cjs/query-builder/function-builder.js +31 -7
  26. package/dist/cjs/query-builder/insert-query-builder.d.ts +53 -11
  27. package/dist/cjs/query-builder/insert-query-builder.js +58 -10
  28. package/dist/cjs/query-builder/select-query-builder.d.ts +58 -16
  29. package/dist/cjs/query-builder/select-query-builder.js +64 -15
  30. package/dist/cjs/query-builder/update-query-builder.d.ts +52 -9
  31. package/dist/cjs/query-builder/update-query-builder.js +57 -8
  32. package/dist/cjs/query-creator.d.ts +2 -2
  33. package/dist/cjs/query-creator.js +2 -2
  34. package/dist/cjs/schema/create-view-builder.js +1 -1
  35. package/dist/cjs/util/object-utils.d.ts +1 -0
  36. package/dist/cjs/util/object-utils.js +50 -1
  37. package/dist/cjs/util/type-utils.d.ts +21 -0
  38. package/dist/esm/dialect/mysql/mysql-dialect.js +2 -1
  39. package/dist/esm/dialect/mysql/mysql-driver.js +2 -2
  40. package/dist/esm/dialect/postgres/postgres-dialect.js +2 -1
  41. package/dist/esm/dialect/postgres/postgres-driver.js +2 -2
  42. package/dist/esm/dialect/sqlite/sqlite-dialect.js +2 -1
  43. package/dist/esm/dialect/sqlite/sqlite-driver.js +2 -2
  44. package/dist/esm/driver/dummy-driver.d.ts +28 -0
  45. package/dist/esm/driver/dummy-driver.js +31 -3
  46. package/dist/esm/index-nodeless.d.ts +1 -0
  47. package/dist/esm/index-nodeless.js +1 -0
  48. package/dist/esm/kysely.d.ts +8 -6
  49. package/dist/esm/kysely.js +8 -6
  50. package/dist/esm/plugin/deduplicate-joins/deduplicate-joins-plugin.d.ts +48 -0
  51. package/dist/esm/plugin/deduplicate-joins/deduplicate-joins-plugin.js +16 -0
  52. package/dist/esm/plugin/deduplicate-joins/deduplicate-joins-transformer.d.ts +9 -0
  53. package/dist/esm/plugin/deduplicate-joins/deduplicate-joins-transformer.js +39 -0
  54. package/dist/esm/plugin/{immediate-value.ts → immediate-value}/immediate-value-plugin.d.ts +0 -0
  55. package/dist/esm/plugin/{immediate-value.ts → immediate-value}/immediate-value-plugin.js +0 -0
  56. package/dist/esm/plugin/{immediate-value.ts → immediate-value}/immediate-value-transformer.d.ts +0 -0
  57. package/dist/esm/plugin/{immediate-value.ts → immediate-value}/immediate-value-transformer.js +0 -0
  58. package/dist/esm/query-builder/delete-query-builder.d.ts +51 -9
  59. package/dist/esm/query-builder/delete-query-builder.js +56 -8
  60. package/dist/esm/query-builder/function-builder.d.ts +29 -5
  61. package/dist/esm/query-builder/function-builder.js +31 -7
  62. package/dist/esm/query-builder/insert-query-builder.d.ts +53 -11
  63. package/dist/esm/query-builder/insert-query-builder.js +58 -10
  64. package/dist/esm/query-builder/select-query-builder.d.ts +58 -16
  65. package/dist/esm/query-builder/select-query-builder.js +64 -15
  66. package/dist/esm/query-builder/update-query-builder.d.ts +52 -9
  67. package/dist/esm/query-builder/update-query-builder.js +57 -8
  68. package/dist/esm/query-creator.d.ts +2 -2
  69. package/dist/esm/query-creator.js +2 -2
  70. package/dist/esm/schema/create-view-builder.js +1 -1
  71. package/dist/esm/util/object-utils.d.ts +1 -0
  72. package/dist/esm/util/object-utils.js +48 -0
  73. package/dist/esm/util/type-utils.d.ts +21 -0
  74. package/package.json +4 -2
@@ -6,13 +6,14 @@ import { SelectExpression } from '../parser/select-parser.js';
6
6
  import { ExistsExpression, FilterOperator, FilterValueExpressionOrList, WhereGrouper } from '../parser/filter-parser.js';
7
7
  import { ReturningRow } from '../parser/returning-parser.js';
8
8
  import { ReferenceExpression } from '../parser/reference-parser.js';
9
- import { AnyRawBuilder, SingleResultType } from '../util/type-utils.js';
9
+ import { AnyRawBuilder, MergePartial, SingleResultType } from '../util/type-utils.js';
10
10
  import { UpdateQueryNode } from '../operation-node/update-query-node.js';
11
11
  import { MutationObject } from '../parser/update-set-parser.js';
12
12
  import { Compilable } from '../util/compilable.js';
13
13
  import { QueryExecutor } from '../query-executor/query-executor.js';
14
14
  import { QueryId } from '../util/query-id.js';
15
15
  import { ParseContext } from '../parser/parse-context.js';
16
+ import { UpdateResult } from './update-result.js';
16
17
  import { KyselyPlugin } from '../plugin/kysely-plugin.js';
17
18
  import { WhereInterface } from './where-interface.js';
18
19
  import { JoinInterface } from './join-interface.js';
@@ -676,24 +677,63 @@ export declare class UpdateQueryBuilder<DB, TB extends keyof DB, O> implements W
676
677
  /**
677
678
  * Simply calls the given function passing `this` as the only argument.
678
679
  *
679
- * This method can be useful when adding optional method calls:
680
+ * If you want to conditionally call a method on `this`, see
681
+ * the {@link if} method.
680
682
  *
681
683
  * ### Examples
682
684
  *
685
+ * The next example uses a helper funtion `log` to log a query:
686
+ *
683
687
  * ```ts
688
+ * function log<T extends Compilable>(qb: T): T {
689
+ * console.log(qb.compile())
690
+ * return qb
691
+ * }
692
+ *
684
693
  * db.updateTable('person')
685
694
  * .set(values)
686
- * .call((qb) => {
687
- * if (something) {
688
- * return qb.where('something', '=', something)
689
- * } else {
690
- * return qb.where('somethingElse', '=', somethingElse)
691
- * }
692
- * })
695
+ * .call(log)
693
696
  * .execute()
694
697
  * ```
695
698
  */
696
699
  call<T>(func: (qb: this) => T): T;
700
+ /**
701
+ * Call `func(this)` if `condition` is true.
702
+ *
703
+ * This method is especially handy with optional selects. Any `returning` or `returningAll`
704
+ * method calls add columns as optional fields to the output type when called inside
705
+ * the `func` callback. This is because we can't know if those selections were actually
706
+ * made before running the code.
707
+ *
708
+ * You can also call any other methods inside the callback.
709
+ *
710
+ * ### Examples
711
+ *
712
+ * ```ts
713
+ * async function updatePerson(id: number, updates: UpdateablePerson, returnLastName: boolean) {
714
+ * return await db
715
+ * .updateTable('person')
716
+ * .set(updates)
717
+ * .where('id', '=', id)
718
+ * .returning(['id', 'first_name'])
719
+ * .if(returnLastName, (qb) => qb.returning('last_name'))
720
+ * .executeTakeFirstOrThrow()
721
+ * }
722
+ * ```
723
+ *
724
+ * Any selections added inside the `if` callback will be added as optional fields to the
725
+ * output type since we can't know if the selections were actually made before running
726
+ * the code. In the example above the return type of the `updatePerson` function is:
727
+ *
728
+ * ```ts
729
+ * {
730
+ * id: number
731
+ * first_name: string
732
+ * last_name?: string
733
+ * }
734
+ * ```
735
+ */
736
+ if<O2>(condition: boolean, func: (qb: this) => UpdateQueryBuilder<DB, TB, O2>): UpdateQueryBuilder<DB, TB, O2 extends UpdateResult ? UpdateResult : O extends UpdateResult ? Partial<O2> : MergePartial<O, O2>>;
697
737
  /**
698
738
  * Change the output type of the query.
699
739
  *
@@ -701,6 +741,9 @@ export declare class UpdateQueryBuilder<DB, TB extends keyof DB, O> implements W
701
741
  * don't support your use case.
702
742
  */
703
743
  castTo<T>(): UpdateQueryBuilder<DB, TB, T>;
744
+ /**
745
+ * Returns a copy of this UpdateQueryBuilder instance with the given plugin installed.
746
+ */
704
747
  withPlugin(plugin: KyselyPlugin): UpdateQueryBuilder<DB, TB, O>;
705
748
  toOperationNode(): UpdateQueryNode;
706
749
  compile(): CompiledQuery;
@@ -194,26 +194,72 @@ class UpdateQueryBuilder {
194
194
  /**
195
195
  * Simply calls the given function passing `this` as the only argument.
196
196
  *
197
- * This method can be useful when adding optional method calls:
197
+ * If you want to conditionally call a method on `this`, see
198
+ * the {@link if} method.
198
199
  *
199
200
  * ### Examples
200
201
  *
202
+ * The next example uses a helper funtion `log` to log a query:
203
+ *
201
204
  * ```ts
205
+ * function log<T extends Compilable>(qb: T): T {
206
+ * console.log(qb.compile())
207
+ * return qb
208
+ * }
209
+ *
202
210
  * db.updateTable('person')
203
211
  * .set(values)
204
- * .call((qb) => {
205
- * if (something) {
206
- * return qb.where('something', '=', something)
207
- * } else {
208
- * return qb.where('somethingElse', '=', somethingElse)
209
- * }
210
- * })
212
+ * .call(log)
211
213
  * .execute()
212
214
  * ```
213
215
  */
214
216
  call(func) {
215
217
  return func(this);
216
218
  }
219
+ /**
220
+ * Call `func(this)` if `condition` is true.
221
+ *
222
+ * This method is especially handy with optional selects. Any `returning` or `returningAll`
223
+ * method calls add columns as optional fields to the output type when called inside
224
+ * the `func` callback. This is because we can't know if those selections were actually
225
+ * made before running the code.
226
+ *
227
+ * You can also call any other methods inside the callback.
228
+ *
229
+ * ### Examples
230
+ *
231
+ * ```ts
232
+ * async function updatePerson(id: number, updates: UpdateablePerson, returnLastName: boolean) {
233
+ * return await db
234
+ * .updateTable('person')
235
+ * .set(updates)
236
+ * .where('id', '=', id)
237
+ * .returning(['id', 'first_name'])
238
+ * .if(returnLastName, (qb) => qb.returning('last_name'))
239
+ * .executeTakeFirstOrThrow()
240
+ * }
241
+ * ```
242
+ *
243
+ * Any selections added inside the `if` callback will be added as optional fields to the
244
+ * output type since we can't know if the selections were actually made before running
245
+ * the code. In the example above the return type of the `updatePerson` function is:
246
+ *
247
+ * ```ts
248
+ * {
249
+ * id: number
250
+ * first_name: string
251
+ * last_name?: string
252
+ * }
253
+ * ```
254
+ */
255
+ if(condition, func) {
256
+ if (condition) {
257
+ return func(this);
258
+ }
259
+ return new UpdateQueryBuilder({
260
+ ...this.#props,
261
+ });
262
+ }
217
263
  /**
218
264
  * Change the output type of the query.
219
265
  *
@@ -223,6 +269,9 @@ class UpdateQueryBuilder {
223
269
  castTo() {
224
270
  return new UpdateQueryBuilder(this.#props);
225
271
  }
272
+ /**
273
+ * Returns a copy of this UpdateQueryBuilder instance with the given plugin installed.
274
+ */
226
275
  withPlugin(plugin) {
227
276
  return new UpdateQueryBuilder({
228
277
  ...this.#props,
@@ -162,7 +162,7 @@ export declare class QueryCreator<DB> {
162
162
  /**
163
163
  * Creates a delete query.
164
164
  *
165
- * See the {@link WhereInterface.where} method for examples on how to specify
165
+ * See the {@link DeleteQueryBuilder.where} method for examples on how to specify
166
166
  * a where clause for the delete operation.
167
167
  *
168
168
  * The return value of the query is an instance of {@link DeleteResult}.
@@ -182,7 +182,7 @@ export declare class QueryCreator<DB> {
182
182
  /**
183
183
  * Creates an update query.
184
184
  *
185
- * See the {@link WhereInterface.where} method for examples on how to specify
185
+ * See the {@link UpdateQueryBuilder.where} method for examples on how to specify
186
186
  * a where clause for the update operation.
187
187
  *
188
188
  * See the {@link UpdateQueryBuilder.set} method for examples on how to
@@ -78,7 +78,7 @@ class QueryCreator {
78
78
  /**
79
79
  * Creates a delete query.
80
80
  *
81
- * See the {@link WhereInterface.where} method for examples on how to specify
81
+ * See the {@link DeleteQueryBuilder.where} method for examples on how to specify
82
82
  * a where clause for the delete operation.
83
83
  *
84
84
  * The return value of the query is an instance of {@link DeleteResult}.
@@ -105,7 +105,7 @@ class QueryCreator {
105
105
  /**
106
106
  * Creates an update query.
107
107
  *
108
- * See the {@link WhereInterface.where} method for examples on how to specify
108
+ * See the {@link UpdateQueryBuilder.where} method for examples on how to specify
109
109
  * a where clause for the update operation.
110
110
  *
111
111
  * See the {@link UpdateQueryBuilder.set} method for examples on how to
@@ -5,7 +5,7 @@ const prevent_await_js_1 = require("../util/prevent-await.js");
5
5
  const object_utils_js_1 = require("../util/object-utils.js");
6
6
  const create_view_node_js_1 = require("../operation-node/create-view-node.js");
7
7
  const reference_parser_js_1 = require("../parser/reference-parser.js");
8
- const immediate_value_plugin_js_1 = require("../plugin/immediate-value.ts/immediate-value-plugin.js");
8
+ const immediate_value_plugin_js_1 = require("../plugin/immediate-value/immediate-value-plugin.js");
9
9
  class CreateViewBuilder {
10
10
  #props;
11
11
  constructor(props) {
@@ -16,3 +16,4 @@ export declare function asArray<T>(arg: T | T[]): T[];
16
16
  export declare function asReadonlyArray<T>(arg: T | ReadonlyArray<T>): ReadonlyArray<T>;
17
17
  export declare function isReadonlyArray(arg: unknown): arg is ReadonlyArray<unknown>;
18
18
  export declare function noop<T>(obj: T): T;
19
+ export declare function compare(obj1: unknown, obj2: unknown): boolean;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.noop = exports.isReadonlyArray = exports.asReadonlyArray = exports.asArray = exports.freeze = exports.getLast = exports.isObject = exports.isFunction = exports.isBuffer = exports.isBigInt = exports.isDate = exports.isNull = exports.isBoolean = exports.isNumber = exports.isString = exports.isEmpty = void 0;
3
+ exports.compare = exports.noop = exports.isReadonlyArray = exports.asReadonlyArray = exports.asArray = exports.freeze = exports.getLast = exports.isObject = exports.isFunction = exports.isBuffer = exports.isBigInt = exports.isDate = exports.isNull = exports.isBoolean = exports.isNumber = exports.isString = exports.isEmpty = void 0;
4
4
  function isEmpty(obj) {
5
5
  if (Array.isArray(obj) || isString(obj) || isBuffer(obj)) {
6
6
  return obj.length === 0;
@@ -83,3 +83,52 @@ function noop(obj) {
83
83
  return obj;
84
84
  }
85
85
  exports.noop = noop;
86
+ function compare(obj1, obj2) {
87
+ if (isReadonlyArray(obj1) && isReadonlyArray(obj2)) {
88
+ return compareArrays(obj1, obj2);
89
+ }
90
+ else if (isObject(obj1) && isObject(obj2)) {
91
+ return compareObjects(obj1, obj2);
92
+ }
93
+ return obj1 === obj2;
94
+ }
95
+ exports.compare = compare;
96
+ function compareArrays(arr1, arr2) {
97
+ if (arr1.length !== arr2.length) {
98
+ return false;
99
+ }
100
+ for (let i = 0; i < arr1.length; ++i) {
101
+ if (!compare(arr1[i], arr2[i])) {
102
+ return false;
103
+ }
104
+ }
105
+ return true;
106
+ }
107
+ function compareObjects(obj1, obj2) {
108
+ if (isBuffer(obj1) && isBuffer(obj2)) {
109
+ return compareBuffers(obj1, obj2);
110
+ }
111
+ else if (isDate(obj1) && isDate(obj2)) {
112
+ return compareDates(obj1, obj2);
113
+ }
114
+ return compareGenericObjects(obj1, obj2);
115
+ }
116
+ function compareBuffers(buf1, buf2) {
117
+ return Buffer.compare(buf1, buf2) === 0;
118
+ }
119
+ function compareDates(date1, date2) {
120
+ return date1.getTime() === date2.getTime();
121
+ }
122
+ function compareGenericObjects(obj1, obj2) {
123
+ const keys1 = Object.keys(obj1);
124
+ const keys2 = Object.keys(obj2);
125
+ if (keys1.length !== keys2.length) {
126
+ return false;
127
+ }
128
+ for (const key of keys1) {
129
+ if (!compare(obj1[key], obj2[key])) {
130
+ return false;
131
+ }
132
+ }
133
+ return true;
134
+ }
@@ -111,3 +111,24 @@ export declare type UnknownRow = Record<string, unknown>;
111
111
  export declare type Nullable<T> = {
112
112
  [P in keyof T]: T[P] | null;
113
113
  };
114
+ /**
115
+ * Takes all properties from T1 and merges all properties from T2
116
+ * that don't exist in T1 as optional properties.
117
+ *
118
+ * Example:
119
+ *
120
+ * interface Person {
121
+ * name: string
122
+ * age: number
123
+ * }
124
+ *
125
+ * interface Pet {
126
+ * name: string
127
+ * species: 'cat' | 'dog'
128
+ * }
129
+ *
130
+ * type Merged = MergePartial<Person, Pet>
131
+ *
132
+ * // { name: string, age: number, species?: 'cat' | 'dog' }
133
+ */
134
+ export declare type MergePartial<T1, T2> = T1 & Partial<Omit<T2, keyof T1>>;
@@ -3,6 +3,7 @@ import { MysqlDriver } from './mysql-driver.js';
3
3
  import { MysqlQueryCompiler } from './mysql-query-compiler.js';
4
4
  import { MysqlIntrospector } from './mysql-introspector.js';
5
5
  import { MysqlAdapter } from './mysql-adapter.js';
6
+ import { freeze } from '../../util/object-utils.js';
6
7
  /**
7
8
  * MySQL dialect that uses the [mysql2](https://github.com/sidorares/node-mysql2#readme) library.
8
9
  *
@@ -13,7 +14,7 @@ import { MysqlAdapter } from './mysql-adapter.js';
13
14
  export class MysqlDialect {
14
15
  #config;
15
16
  constructor(config) {
16
- this.#config = config;
17
+ this.#config = freeze({ ...config });
17
18
  }
18
19
  createDriver() {
19
20
  return new MysqlDriver(this.#config);
@@ -1,13 +1,13 @@
1
1
  /// <reference types="./mysql-driver.d.ts" />
2
2
  import { CompiledQuery } from '../../query-compiler/compiled-query.js';
3
- import { isFunction, isObject } from '../../util/object-utils.js';
3
+ import { isFunction, isObject, freeze } from '../../util/object-utils.js';
4
4
  const PRIVATE_RELEASE_METHOD = Symbol();
5
5
  export class MysqlDriver {
6
6
  #config;
7
7
  #connections = new WeakMap();
8
8
  #pool;
9
9
  constructor(config) {
10
- this.#config = config;
10
+ this.#config = freeze({ ...config });
11
11
  }
12
12
  async init() {
13
13
  // Import the `mysql2` module here instead at the top of the file
@@ -3,6 +3,7 @@ import { PostgresDriver } from './postgres-driver.js';
3
3
  import { PostgresIntrospector } from './postgres-introspector.js';
4
4
  import { PostgresQueryCompiler } from './postgres-query-compiler.js';
5
5
  import { PostgresAdapter } from './postgres-adapter.js';
6
+ import { freeze } from '../../util/object-utils.js';
6
7
  /**
7
8
  * PostgreSQL dialect that uses the [pg](https://node-postgres.com/) library.
8
9
  *
@@ -16,7 +17,7 @@ import { PostgresAdapter } from './postgres-adapter.js';
16
17
  export class PostgresDialect {
17
18
  #config;
18
19
  constructor(config) {
19
- this.#config = config;
20
+ this.#config = freeze({ ...config });
20
21
  }
21
22
  createDriver() {
22
23
  return new PostgresDriver(this.#config);
@@ -1,13 +1,13 @@
1
1
  /// <reference types="./postgres-driver.d.ts" />
2
2
  import { CompiledQuery } from '../../query-compiler/compiled-query.js';
3
- import { isFunction } from '../../util/object-utils.js';
3
+ import { isFunction, freeze } from '../../util/object-utils.js';
4
4
  const PRIVATE_RELEASE_METHOD = Symbol();
5
5
  export class PostgresDriver {
6
6
  #config;
7
7
  #connections = new WeakMap();
8
8
  #pool = null;
9
9
  constructor(config) {
10
- this.#config = config;
10
+ this.#config = freeze({ ...config });
11
11
  }
12
12
  async init() {
13
13
  // Import the `pg` module here instead at the top of the file
@@ -3,6 +3,7 @@ import { SqliteDriver } from './sqlite-driver.js';
3
3
  import { SqliteQueryCompiler } from './sqlite-query-compiler.js';
4
4
  import { SqliteIntrospector } from './sqlite-introspector.js';
5
5
  import { SqliteAdapter } from './sqlite-adapter.js';
6
+ import { freeze } from '../../util/object-utils.js';
6
7
  /**
7
8
  * SQLite dialect that uses the [better-sqlite3](https://github.com/JoshuaWise/better-sqlite3) library.
8
9
  *
@@ -13,7 +14,7 @@ import { SqliteAdapter } from './sqlite-adapter.js';
13
14
  export class SqliteDialect {
14
15
  #config;
15
16
  constructor(config) {
16
- this.#config = config;
17
+ this.#config = freeze({ ...config });
17
18
  }
18
19
  createDriver() {
19
20
  return new SqliteDriver(this.#config);
@@ -1,13 +1,13 @@
1
1
  /// <reference types="./sqlite-driver.d.ts" />
2
2
  import { CompiledQuery } from '../../query-compiler/compiled-query.js';
3
- import { isBoolean, isFunction, isNumber } from '../../util/object-utils.js';
3
+ import { freeze, isBoolean, isFunction, isNumber, } from '../../util/object-utils.js';
4
4
  export class SqliteDriver {
5
5
  #config;
6
6
  #connectionMutex = new ConnectionMutex();
7
7
  #db;
8
8
  #connection;
9
9
  constructor(config) {
10
- this.#config = config;
10
+ this.#config = freeze({ ...config });
11
11
  }
12
12
  async init() {
13
13
  // Import the `better-sqlite3` module here instead at the top of the file
@@ -1,5 +1,33 @@
1
1
  import { DatabaseConnection } from './database-connection.js';
2
2
  import { Driver } from './driver.js';
3
+ /**
4
+ * A driver that does absolutely nothing.
5
+ *
6
+ * You can use this to create Kysely instances solely for building queries
7
+ *
8
+ * ### Examples
9
+ *
10
+ * This example creates a Kysely instance for building postgres queries:
11
+ *
12
+ * ```ts
13
+ * const db = new Kysely<Database>({
14
+ * dialect: {
15
+ * createAdapter() {
16
+ * return new PostgresAdapter()
17
+ * },
18
+ * createDriver() {
19
+ * return new DummyDriver()
20
+ * },
21
+ * createIntrospector(db: Kysely<any>) {
22
+ * return new PostgresIntrospector(db)
23
+ * },
24
+ * createQueryCompiler() {
25
+ * return new PostgresQueryCompiler()
26
+ * },
27
+ * },
28
+ * })
29
+ * ```
30
+ */
3
31
  export declare class DummyDriver implements Driver {
4
32
  /**
5
33
  * Initializes the driver.
@@ -1,7 +1,35 @@
1
1
  /// <reference types="./dummy-driver.d.ts" />
2
+ /**
3
+ * A driver that does absolutely nothing.
4
+ *
5
+ * You can use this to create Kysely instances solely for building queries
6
+ *
7
+ * ### Examples
8
+ *
9
+ * This example creates a Kysely instance for building postgres queries:
10
+ *
11
+ * ```ts
12
+ * const db = new Kysely<Database>({
13
+ * dialect: {
14
+ * createAdapter() {
15
+ * return new PostgresAdapter()
16
+ * },
17
+ * createDriver() {
18
+ * return new DummyDriver()
19
+ * },
20
+ * createIntrospector(db: Kysely<any>) {
21
+ * return new PostgresIntrospector(db)
22
+ * },
23
+ * createQueryCompiler() {
24
+ * return new PostgresQueryCompiler()
25
+ * },
26
+ * },
27
+ * })
28
+ * ```
29
+ */
2
30
  export class DummyDriver {
3
- init() {
4
- throw new Error('Method not implemented.');
31
+ async init() {
32
+ // Nothing to do here.
5
33
  }
6
34
  async acquireConnection() {
7
35
  return new DummyConnection();
@@ -23,7 +51,7 @@ export class DummyDriver {
23
51
  }
24
52
  }
25
53
  class DummyConnection {
26
- async executeQuery(compiledQuery) {
54
+ async executeQuery() {
27
55
  return {
28
56
  rows: [],
29
57
  };
@@ -59,6 +59,7 @@ export * from './query-compiler/query-compiler.js';
59
59
  export * from './migration/migrator.js';
60
60
  export * from './plugin/kysely-plugin.js';
61
61
  export * from './plugin/camel-case/camel-case-plugin.js';
62
+ export * from './plugin/deduplicate-joins/deduplicate-joins-plugin.js';
62
63
  export * from './operation-node/add-column-node.js';
63
64
  export * from './operation-node/add-constraint-node.js';
64
65
  export * from './operation-node/alias-node.js';
@@ -60,6 +60,7 @@ export * from './query-compiler/query-compiler.js';
60
60
  export * from './migration/migrator.js';
61
61
  export * from './plugin/kysely-plugin.js';
62
62
  export * from './plugin/camel-case/camel-case-plugin.js';
63
+ export * from './plugin/deduplicate-joins/deduplicate-joins-plugin.js';
63
64
  export * from './operation-node/add-column-node.js';
64
65
  export * from './operation-node/add-constraint-node.js';
65
66
  export * from './operation-node/alias-node.js';
@@ -22,22 +22,24 @@ import { DialectAdapter } from './dialect/dialect-adapter.js';
22
22
  * This example assumes your database has tables `person` and `pet`:
23
23
  *
24
24
  * ```ts
25
- * interface Person {
26
- * id: number
25
+ * import { Kysely, Generated, PostgresDialect } from 'kysely'
26
+ *
27
+ * interface PersonTable {
28
+ * id: Generated<number>
27
29
  * first_name: string
28
30
  * last_name: string
29
31
  * }
30
32
  *
31
- * interface Pet {
32
- * id: number
33
+ * interface PetTable {
34
+ * id: Generated<number>
33
35
  * owner_id: number
34
36
  * name: string
35
37
  * species 'cat' | 'dog'
36
38
  * }
37
39
  *
38
40
  * interface Database {
39
- * person: Person,
40
- * pet: Pet
41
+ * person: PersonTable,
42
+ * pet: PetTable
41
43
  * }
42
44
  *
43
45
  * const db = new Kysely<Database>({
@@ -24,22 +24,24 @@ import { PRIVATE_ADAPTER } from './util/private-symbols.js';
24
24
  * This example assumes your database has tables `person` and `pet`:
25
25
  *
26
26
  * ```ts
27
- * interface Person {
28
- * id: number
27
+ * import { Kysely, Generated, PostgresDialect } from 'kysely'
28
+ *
29
+ * interface PersonTable {
30
+ * id: Generated<number>
29
31
  * first_name: string
30
32
  * last_name: string
31
33
  * }
32
34
  *
33
- * interface Pet {
34
- * id: number
35
+ * interface PetTable {
36
+ * id: Generated<number>
35
37
  * owner_id: number
36
38
  * name: string
37
39
  * species 'cat' | 'dog'
38
40
  * }
39
41
  *
40
42
  * interface Database {
41
- * person: Person,
42
- * pet: Pet
43
+ * person: PersonTable,
44
+ * pet: PetTable
43
45
  * }
44
46
  *
45
47
  * const db = new Kysely<Database>({
@@ -0,0 +1,48 @@
1
+ import { QueryResult } from '../../driver/database-connection.js';
2
+ import { RootOperationNode } from '../../query-compiler/query-compiler.js';
3
+ import { UnknownRow } from '../../util/type-utils.js';
4
+ import { KyselyPlugin, PluginTransformQueryArgs, PluginTransformResultArgs } from '../kysely-plugin.js';
5
+ /**
6
+ * Plugin that removes duplicate joins from queries.
7
+ *
8
+ * See [this recipe](https://github.com/koskimas/kysely/tree/master/recipes/deduplicate-joins.md)
9
+ */
10
+ export declare class DeduplicateJoinsPlugin implements KyselyPlugin {
11
+ #private;
12
+ /**
13
+ * This is called for each query before it is executed. You can modify the query by
14
+ * transforming its {@link OperationNode} tree provided in {@link PluginTransformQueryArgs.node | args.node}
15
+ * and returning the transformed tree. You'd usually want to use an {@link OperationNodeTransformer}
16
+ * for this.
17
+ *
18
+ * If you need to pass some query-related data between this method and `transformResult` you
19
+ * can use a `WeakMap` with {@link PluginTransformQueryArgs.queryId | args.queryId} as the key:
20
+ *
21
+ * ```ts
22
+ * const plugin = {
23
+ * data: new WeakMap<QueryId, SomeData>(),
24
+ *
25
+ * transformQuery(args: PluginTransformQueryArgs): RootOperationNode {
26
+ * this.data.set(args.queryId, something)
27
+ * return args.node
28
+ * },
29
+ *
30
+ * transformResult(args: PluginTransformResultArgs): QueryResult<UnknownRow> {
31
+ * const data = this.data.get(args.queryId)
32
+ * return args.result
33
+ * }
34
+ * }
35
+ * ```
36
+ *
37
+ * You should use a `WeakMap` instead of a `Map` or some other strong references because `transformQuery`
38
+ * is not always matched by a call to `transformResult` which would leave orphaned items in the map
39
+ * and cause a memory leak.
40
+ */
41
+ transformQuery(args: PluginTransformQueryArgs): RootOperationNode;
42
+ /**
43
+ * This method is called for each query after it has been executed. The result
44
+ * of the query can be accessed through {@link PluginTransformResultArgs.result | args.result}.
45
+ * You can modify the result and return the modifier result.
46
+ */
47
+ transformResult(args: PluginTransformResultArgs): Promise<QueryResult<UnknownRow>>;
48
+ }
@@ -0,0 +1,16 @@
1
+ /// <reference types="./deduplicate-joins-plugin.d.ts" />
2
+ import { DeduplicateJoinsTransformer } from './deduplicate-joins-transformer.js';
3
+ /**
4
+ * Plugin that removes duplicate joins from queries.
5
+ *
6
+ * See [this recipe](https://github.com/koskimas/kysely/tree/master/recipes/deduplicate-joins.md)
7
+ */
8
+ export class DeduplicateJoinsPlugin {
9
+ #transformer = new DeduplicateJoinsTransformer();
10
+ transformQuery(args) {
11
+ return this.#transformer.transformNode(args.node);
12
+ }
13
+ transformResult(args) {
14
+ return Promise.resolve(args.result);
15
+ }
16
+ }