kysely 0.17.0 → 0.17.1

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.
@@ -4,5 +4,5 @@ export interface ConnectionProvider {
4
4
  * Provides a connection for the callback and takes care of disposing
5
5
  * the connection after the callback has been run.
6
6
  */
7
- withConnection<T>(runner: (connection: DatabaseConnection) => Promise<T>): Promise<T>;
7
+ provideConnection<T>(consumer: (connection: DatabaseConnection) => Promise<T>): Promise<T>;
8
8
  }
@@ -8,5 +8,5 @@ export declare class DefaultConnectionProvider implements ConnectionProvider {
8
8
  * Provides a connection for the callback and takes care of disposing
9
9
  * the connection after the callback has been run.
10
10
  */
11
- withConnection<T>(runner: (connection: DatabaseConnection) => Promise<T>): Promise<T>;
11
+ provideConnection<T>(runner: (connection: DatabaseConnection) => Promise<T>): Promise<T>;
12
12
  }
@@ -6,7 +6,7 @@ class DefaultConnectionProvider {
6
6
  constructor(driver) {
7
7
  this.#driver = driver;
8
8
  }
9
- async withConnection(runner) {
9
+ async provideConnection(runner) {
10
10
  const connection = await this.#driver.acquireConnection();
11
11
  try {
12
12
  return await runner(connection);
@@ -7,5 +7,5 @@ export declare class SingleConnectionProvider implements ConnectionProvider {
7
7
  * Provides a connection for the callback and takes care of disposing
8
8
  * the connection after the callback has been run.
9
9
  */
10
- withConnection<T>(runner: (connection: DatabaseConnection) => Promise<T>): Promise<T>;
10
+ provideConnection<T>(runner: (connection: DatabaseConnection) => Promise<T>): Promise<T>;
11
11
  }
@@ -7,7 +7,7 @@ class SingleConnectionProvider {
7
7
  constructor(connection) {
8
8
  this.#connection = connection;
9
9
  }
10
- async withConnection(runner) {
10
+ async provideConnection(runner) {
11
11
  while (this.#runningPromise) {
12
12
  await this.#runningPromise;
13
13
  }
@@ -338,18 +338,14 @@ class ConnectionBuilder {
338
338
  this.#props = (0, object_utils_js_1.freeze)(props);
339
339
  }
340
340
  async execute(callback) {
341
- const connection = await this.#props.driver.acquireConnection();
342
- const connectionProvider = new single_connection_provider_js_1.SingleConnectionProvider(connection);
343
- const db = new Kysely({
344
- ...this.#props,
345
- executor: this.#props.executor.withConnectionProvider(connectionProvider),
346
- });
347
- try {
341
+ return this.#props.executor.provideConnection(async (connection) => {
342
+ const executor = this.#props.executor.withConnectionProvider(new single_connection_provider_js_1.SingleConnectionProvider(connection));
343
+ const db = new Kysely({
344
+ ...this.#props,
345
+ executor,
346
+ });
348
347
  return await callback(db);
349
- }
350
- finally {
351
- await this.#props.driver.releaseConnection(connection);
352
- }
348
+ });
353
349
  }
354
350
  }
355
351
  exports.ConnectionBuilder = ConnectionBuilder;
@@ -369,25 +365,23 @@ class TransactionBuilder {
369
365
  const { isolationLevel, ...kyselyProps } = this.#props;
370
366
  const settings = { isolationLevel };
371
367
  validateTransactionSettings(settings);
372
- const connection = await this.#props.driver.acquireConnection();
373
- const connectionProvider = new single_connection_provider_js_1.SingleConnectionProvider(connection);
374
- const transaction = new Transaction({
375
- ...kyselyProps,
376
- executor: this.#props.executor.withConnectionProvider(connectionProvider),
368
+ return this.#props.executor.provideConnection(async (connection) => {
369
+ const executor = this.#props.executor.withConnectionProvider(new single_connection_provider_js_1.SingleConnectionProvider(connection));
370
+ const transaction = new Transaction({
371
+ ...kyselyProps,
372
+ executor,
373
+ });
374
+ try {
375
+ await this.#props.driver.beginTransaction(connection, settings);
376
+ const result = await callback(transaction);
377
+ await this.#props.driver.commitTransaction(connection);
378
+ return result;
379
+ }
380
+ catch (error) {
381
+ await this.#props.driver.rollbackTransaction(connection);
382
+ throw error;
383
+ }
377
384
  });
378
- try {
379
- await this.#props.driver.beginTransaction(connection, settings);
380
- const result = await callback(transaction);
381
- await this.#props.driver.commitTransaction(connection);
382
- return result;
383
- }
384
- catch (error) {
385
- await this.#props.driver.rollbackTransaction(connection);
386
- throw error;
387
- }
388
- finally {
389
- await this.#props.driver.releaseConnection(connection);
390
- }
391
385
  }
392
386
  }
393
387
  exports.TransactionBuilder = TransactionBuilder;
@@ -1,5 +1,5 @@
1
1
  import { ConnectionProvider } from '../driver/connection-provider.js';
2
- import { QueryResult } from '../driver/database-connection.js';
2
+ import { DatabaseConnection } from '../driver/database-connection.js';
3
3
  import { CompiledQuery } from '../query-compiler/compiled-query.js';
4
4
  import { RootOperationNode, QueryCompiler } from '../query-compiler/query-compiler.js';
5
5
  import { KyselyPlugin } from '../plugin/kysely-plugin.js';
@@ -15,7 +15,11 @@ export declare class DefaultQueryExecutor extends QueryExecutor {
15
15
  * compile any query using this method.
16
16
  */
17
17
  compileQuery(node: RootOperationNode): CompiledQuery;
18
- protected executeQueryImpl<R>(compiledQuery: CompiledQuery): Promise<QueryResult<R>>;
18
+ /**
19
+ * Provides a connection for the callback and takes care of disposing
20
+ * the connection after the callback has been run.
21
+ */
22
+ provideConnection<T>(consumer: (connection: DatabaseConnection) => Promise<T>): Promise<T>;
19
23
  /**
20
24
  * Returns a copy of this executor with a list of plugins added
21
25
  * as the last plugins.
@@ -18,10 +18,8 @@ class DefaultQueryExecutor extends query_executor_js_1.QueryExecutor {
18
18
  compileQuery(node) {
19
19
  return this.#compiler.compileQuery(node);
20
20
  }
21
- async executeQueryImpl(compiledQuery) {
22
- return this.#connectionProvider.withConnection((connection) => {
23
- return connection.executeQuery(compiledQuery);
24
- });
21
+ provideConnection(consumer) {
22
+ return this.#connectionProvider.provideConnection(consumer);
25
23
  }
26
24
  withPlugins(plugins) {
27
25
  return new DefaultQueryExecutor(this.#compiler, this.#adapter, this.#connectionProvider, [...this.plugins, ...plugins]);
@@ -1,4 +1,3 @@
1
- import { QueryResult } from '../driver/database-connection.js';
2
1
  import { CompiledQuery } from '../query-compiler/compiled-query.js';
3
2
  import { QueryExecutor } from './query-executor.js';
4
3
  import { KyselyPlugin } from '../plugin/kysely-plugin.js';
@@ -16,7 +15,11 @@ export declare class NoopQueryExecutor extends QueryExecutor {
16
15
  * compile any query using this method.
17
16
  */
18
17
  compileQuery(): CompiledQuery;
19
- protected executeQueryImpl<R>(): Promise<QueryResult<R>>;
18
+ /**
19
+ * Provides a connection for the callback and takes care of disposing
20
+ * the connection after the callback has been run.
21
+ */
22
+ provideConnection<T>(): Promise<T>;
20
23
  /**
21
24
  * Returns a copy of this executor with a new connection provider.
22
25
  */
@@ -14,7 +14,7 @@ class NoopQueryExecutor extends query_executor_js_1.QueryExecutor {
14
14
  compileQuery() {
15
15
  throw new Error('this query cannot be compiled to SQL');
16
16
  }
17
- async executeQueryImpl() {
17
+ provideConnection() {
18
18
  throw new Error('this query cannot be executed');
19
19
  }
20
20
  withConnectionProvider() {
@@ -1,5 +1,5 @@
1
1
  import { ConnectionProvider } from '../driver/connection-provider.js';
2
- import { QueryResult } from '../driver/database-connection.js';
2
+ import { DatabaseConnection, QueryResult } from '../driver/database-connection.js';
3
3
  import { CompiledQuery } from '../query-compiler/compiled-query.js';
4
4
  import { RootOperationNode } from '../query-compiler/query-compiler.js';
5
5
  import { KyselyPlugin } from '../plugin/kysely-plugin.js';
@@ -11,7 +11,7 @@ import { DialectAdapter } from '../dialect/dialect-adapter.js';
11
11
  * and other classes that execute queries can just pass around and instance of
12
12
  * `QueryExecutor`.
13
13
  */
14
- export declare abstract class QueryExecutor {
14
+ export declare abstract class QueryExecutor implements ConnectionProvider {
15
15
  #private;
16
16
  constructor(plugins?: KyselyPlugin[]);
17
17
  abstract get adapter(): DialectAdapter;
@@ -28,15 +28,16 @@ export declare abstract class QueryExecutor {
28
28
  * compile any query using this method.
29
29
  */
30
30
  abstract compileQuery(node: RootOperationNode, queryId: QueryId): CompiledQuery;
31
+ /**
32
+ * Provides a connection for the callback and takes care of disposing
33
+ * the connection after the callback has been run.
34
+ */
35
+ abstract provideConnection<T>(consumer: (connection: DatabaseConnection) => Promise<T>): Promise<T>;
31
36
  /**
32
37
  * Executes a compiled query and runs the result through all plugins'
33
38
  * `transformResult` method.
34
39
  */
35
40
  executeQuery<R>(compiledQuery: CompiledQuery, queryId: QueryId): Promise<QueryResult<R>>;
36
- /**
37
- * Executes a compiled query.
38
- */
39
- protected abstract executeQueryImpl<R>(compiledQuery: CompiledQuery, queryId: QueryId): Promise<QueryResult<R>>;
40
41
  /**
41
42
  * Returns a copy of this executor with a new connection provider.
42
43
  */
@@ -46,8 +46,10 @@ class QueryExecutor {
46
46
  * `transformResult` method.
47
47
  */
48
48
  async executeQuery(compiledQuery, queryId) {
49
- const result = await this.executeQueryImpl(compiledQuery, queryId);
50
- return this.#transformResult(result, queryId);
49
+ return await this.provideConnection(async (connection) => {
50
+ const result = await connection.executeQuery(compiledQuery);
51
+ return this.#transformResult(result, queryId);
52
+ });
51
53
  }
52
54
  async #transformResult(result, queryId) {
53
55
  for (const plugin of this.#plugins) {
@@ -4,5 +4,5 @@ export interface ConnectionProvider {
4
4
  * Provides a connection for the callback and takes care of disposing
5
5
  * the connection after the callback has been run.
6
6
  */
7
- withConnection<T>(runner: (connection: DatabaseConnection) => Promise<T>): Promise<T>;
7
+ provideConnection<T>(consumer: (connection: DatabaseConnection) => Promise<T>): Promise<T>;
8
8
  }
@@ -8,5 +8,5 @@ export declare class DefaultConnectionProvider implements ConnectionProvider {
8
8
  * Provides a connection for the callback and takes care of disposing
9
9
  * the connection after the callback has been run.
10
10
  */
11
- withConnection<T>(runner: (connection: DatabaseConnection) => Promise<T>): Promise<T>;
11
+ provideConnection<T>(runner: (connection: DatabaseConnection) => Promise<T>): Promise<T>;
12
12
  }
@@ -4,7 +4,7 @@ export class DefaultConnectionProvider {
4
4
  constructor(driver) {
5
5
  this.#driver = driver;
6
6
  }
7
- async withConnection(runner) {
7
+ async provideConnection(runner) {
8
8
  const connection = await this.#driver.acquireConnection();
9
9
  try {
10
10
  return await runner(connection);
@@ -7,5 +7,5 @@ export declare class SingleConnectionProvider implements ConnectionProvider {
7
7
  * Provides a connection for the callback and takes care of disposing
8
8
  * the connection after the callback has been run.
9
9
  */
10
- withConnection<T>(runner: (connection: DatabaseConnection) => Promise<T>): Promise<T>;
10
+ provideConnection<T>(runner: (connection: DatabaseConnection) => Promise<T>): Promise<T>;
11
11
  }
@@ -5,7 +5,7 @@ export class SingleConnectionProvider {
5
5
  constructor(connection) {
6
6
  this.#connection = connection;
7
7
  }
8
- async withConnection(runner) {
8
+ async provideConnection(runner) {
9
9
  while (this.#runningPromise) {
10
10
  await this.#runningPromise;
11
11
  }
@@ -333,18 +333,14 @@ export class ConnectionBuilder {
333
333
  this.#props = freeze(props);
334
334
  }
335
335
  async execute(callback) {
336
- const connection = await this.#props.driver.acquireConnection();
337
- const connectionProvider = new SingleConnectionProvider(connection);
338
- const db = new Kysely({
339
- ...this.#props,
340
- executor: this.#props.executor.withConnectionProvider(connectionProvider),
341
- });
342
- try {
336
+ return this.#props.executor.provideConnection(async (connection) => {
337
+ const executor = this.#props.executor.withConnectionProvider(new SingleConnectionProvider(connection));
338
+ const db = new Kysely({
339
+ ...this.#props,
340
+ executor,
341
+ });
343
342
  return await callback(db);
344
- }
345
- finally {
346
- await this.#props.driver.releaseConnection(connection);
347
- }
343
+ });
348
344
  }
349
345
  }
350
346
  preventAwait(ConnectionBuilder, "don't await ConnectionBuilder instances directly. To execute the query you need to call the `execute` method");
@@ -363,25 +359,23 @@ export class TransactionBuilder {
363
359
  const { isolationLevel, ...kyselyProps } = this.#props;
364
360
  const settings = { isolationLevel };
365
361
  validateTransactionSettings(settings);
366
- const connection = await this.#props.driver.acquireConnection();
367
- const connectionProvider = new SingleConnectionProvider(connection);
368
- const transaction = new Transaction({
369
- ...kyselyProps,
370
- executor: this.#props.executor.withConnectionProvider(connectionProvider),
362
+ return this.#props.executor.provideConnection(async (connection) => {
363
+ const executor = this.#props.executor.withConnectionProvider(new SingleConnectionProvider(connection));
364
+ const transaction = new Transaction({
365
+ ...kyselyProps,
366
+ executor,
367
+ });
368
+ try {
369
+ await this.#props.driver.beginTransaction(connection, settings);
370
+ const result = await callback(transaction);
371
+ await this.#props.driver.commitTransaction(connection);
372
+ return result;
373
+ }
374
+ catch (error) {
375
+ await this.#props.driver.rollbackTransaction(connection);
376
+ throw error;
377
+ }
371
378
  });
372
- try {
373
- await this.#props.driver.beginTransaction(connection, settings);
374
- const result = await callback(transaction);
375
- await this.#props.driver.commitTransaction(connection);
376
- return result;
377
- }
378
- catch (error) {
379
- await this.#props.driver.rollbackTransaction(connection);
380
- throw error;
381
- }
382
- finally {
383
- await this.#props.driver.releaseConnection(connection);
384
- }
385
379
  }
386
380
  }
387
381
  preventAwait(TransactionBuilder, "don't await TransactionBuilder instances directly. To execute the transaction you need to call the `execute` method");
@@ -1,5 +1,5 @@
1
1
  import { ConnectionProvider } from '../driver/connection-provider.js';
2
- import { QueryResult } from '../driver/database-connection.js';
2
+ import { DatabaseConnection } from '../driver/database-connection.js';
3
3
  import { CompiledQuery } from '../query-compiler/compiled-query.js';
4
4
  import { RootOperationNode, QueryCompiler } from '../query-compiler/query-compiler.js';
5
5
  import { KyselyPlugin } from '../plugin/kysely-plugin.js';
@@ -15,7 +15,11 @@ export declare class DefaultQueryExecutor extends QueryExecutor {
15
15
  * compile any query using this method.
16
16
  */
17
17
  compileQuery(node: RootOperationNode): CompiledQuery;
18
- protected executeQueryImpl<R>(compiledQuery: CompiledQuery): Promise<QueryResult<R>>;
18
+ /**
19
+ * Provides a connection for the callback and takes care of disposing
20
+ * the connection after the callback has been run.
21
+ */
22
+ provideConnection<T>(consumer: (connection: DatabaseConnection) => Promise<T>): Promise<T>;
19
23
  /**
20
24
  * Returns a copy of this executor with a list of plugins added
21
25
  * as the last plugins.
@@ -16,10 +16,8 @@ export class DefaultQueryExecutor extends QueryExecutor {
16
16
  compileQuery(node) {
17
17
  return this.#compiler.compileQuery(node);
18
18
  }
19
- async executeQueryImpl(compiledQuery) {
20
- return this.#connectionProvider.withConnection((connection) => {
21
- return connection.executeQuery(compiledQuery);
22
- });
19
+ provideConnection(consumer) {
20
+ return this.#connectionProvider.provideConnection(consumer);
23
21
  }
24
22
  withPlugins(plugins) {
25
23
  return new DefaultQueryExecutor(this.#compiler, this.#adapter, this.#connectionProvider, [...this.plugins, ...plugins]);
@@ -1,4 +1,3 @@
1
- import { QueryResult } from '../driver/database-connection.js';
2
1
  import { CompiledQuery } from '../query-compiler/compiled-query.js';
3
2
  import { QueryExecutor } from './query-executor.js';
4
3
  import { KyselyPlugin } from '../plugin/kysely-plugin.js';
@@ -16,7 +15,11 @@ export declare class NoopQueryExecutor extends QueryExecutor {
16
15
  * compile any query using this method.
17
16
  */
18
17
  compileQuery(): CompiledQuery;
19
- protected executeQueryImpl<R>(): Promise<QueryResult<R>>;
18
+ /**
19
+ * Provides a connection for the callback and takes care of disposing
20
+ * the connection after the callback has been run.
21
+ */
22
+ provideConnection<T>(): Promise<T>;
20
23
  /**
21
24
  * Returns a copy of this executor with a new connection provider.
22
25
  */
@@ -12,7 +12,7 @@ export class NoopQueryExecutor extends QueryExecutor {
12
12
  compileQuery() {
13
13
  throw new Error('this query cannot be compiled to SQL');
14
14
  }
15
- async executeQueryImpl() {
15
+ provideConnection() {
16
16
  throw new Error('this query cannot be executed');
17
17
  }
18
18
  withConnectionProvider() {
@@ -1,5 +1,5 @@
1
1
  import { ConnectionProvider } from '../driver/connection-provider.js';
2
- import { QueryResult } from '../driver/database-connection.js';
2
+ import { DatabaseConnection, QueryResult } from '../driver/database-connection.js';
3
3
  import { CompiledQuery } from '../query-compiler/compiled-query.js';
4
4
  import { RootOperationNode } from '../query-compiler/query-compiler.js';
5
5
  import { KyselyPlugin } from '../plugin/kysely-plugin.js';
@@ -11,7 +11,7 @@ import { DialectAdapter } from '../dialect/dialect-adapter.js';
11
11
  * and other classes that execute queries can just pass around and instance of
12
12
  * `QueryExecutor`.
13
13
  */
14
- export declare abstract class QueryExecutor {
14
+ export declare abstract class QueryExecutor implements ConnectionProvider {
15
15
  #private;
16
16
  constructor(plugins?: KyselyPlugin[]);
17
17
  abstract get adapter(): DialectAdapter;
@@ -28,15 +28,16 @@ export declare abstract class QueryExecutor {
28
28
  * compile any query using this method.
29
29
  */
30
30
  abstract compileQuery(node: RootOperationNode, queryId: QueryId): CompiledQuery;
31
+ /**
32
+ * Provides a connection for the callback and takes care of disposing
33
+ * the connection after the callback has been run.
34
+ */
35
+ abstract provideConnection<T>(consumer: (connection: DatabaseConnection) => Promise<T>): Promise<T>;
31
36
  /**
32
37
  * Executes a compiled query and runs the result through all plugins'
33
38
  * `transformResult` method.
34
39
  */
35
40
  executeQuery<R>(compiledQuery: CompiledQuery, queryId: QueryId): Promise<QueryResult<R>>;
36
- /**
37
- * Executes a compiled query.
38
- */
39
- protected abstract executeQueryImpl<R>(compiledQuery: CompiledQuery, queryId: QueryId): Promise<QueryResult<R>>;
40
41
  /**
41
42
  * Returns a copy of this executor with a new connection provider.
42
43
  */
@@ -44,8 +44,10 @@ export class QueryExecutor {
44
44
  * `transformResult` method.
45
45
  */
46
46
  async executeQuery(compiledQuery, queryId) {
47
- const result = await this.executeQueryImpl(compiledQuery, queryId);
48
- return this.#transformResult(result, queryId);
47
+ return await this.provideConnection(async (connection) => {
48
+ const result = await connection.executeQuery(compiledQuery);
49
+ return this.#transformResult(result, queryId);
50
+ });
49
51
  }
50
52
  async #transformResult(result, queryId) {
51
53
  for (const plugin of this.#plugins) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kysely",
3
- "version": "0.17.0",
3
+ "version": "0.17.1",
4
4
  "description": "Type safe SQL query builder",
5
5
  "repository": {
6
6
  "type": "git",