snowflake-kysely-dialect 0.1.0 → 0.1.2

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.
@@ -1,8 +1,13 @@
1
1
  import { type CompiledQuery, type DatabaseConnection, type QueryResult } from 'kysely';
2
2
  import { type Connection } from 'snowflake-sdk';
3
+ export declare class SnowflakeQueryTimeoutError extends Error {
4
+ readonly connectionIsUsable: boolean;
5
+ constructor(message: string, connectionIsUsable: boolean);
6
+ }
3
7
  export declare class SnowflakeConnection implements DatabaseConnection {
4
8
  #private;
5
9
  constructor(conn: Connection);
10
+ setQueryTimeout(ms: number | undefined): void;
6
11
  beginTransaction(): Promise<void>;
7
12
  commitTransaction(): Promise<void>;
8
13
  rollbackTransaction(): Promise<void>;
@@ -1,11 +1,24 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SnowflakeConnection = void 0;
3
+ exports.SnowflakeConnection = exports.SnowflakeQueryTimeoutError = void 0;
4
+ class SnowflakeQueryTimeoutError extends Error {
5
+ connectionIsUsable;
6
+ constructor(message, connectionIsUsable) {
7
+ super(message);
8
+ this.name = 'SnowflakeQueryTimeoutError';
9
+ this.connectionIsUsable = connectionIsUsable;
10
+ }
11
+ }
12
+ exports.SnowflakeQueryTimeoutError = SnowflakeQueryTimeoutError;
4
13
  class SnowflakeConnection {
5
14
  #conn;
15
+ #queryTimeoutMs;
6
16
  constructor(conn) {
7
17
  this.#conn = conn;
8
18
  }
19
+ setQueryTimeout(ms) {
20
+ this.#queryTimeoutMs = ms;
21
+ }
9
22
  async #executeRaw(sqlText) {
10
23
  await new Promise((resolve, reject) => {
11
24
  this.#conn.execute({
@@ -34,27 +47,48 @@ class SnowflakeConnection {
34
47
  async executeQuery(compiledQuery) {
35
48
  const sqlText = this.#translatePlaceholders(compiledQuery.sql);
36
49
  const binds = compiledQuery.parameters;
50
+ const timeoutMs = this.#queryTimeoutMs;
37
51
  return new Promise((resolve, reject) => {
38
- this.#conn.execute({
52
+ let settled = false;
53
+ let timer;
54
+ const settle = (fn) => {
55
+ if (settled)
56
+ return;
57
+ settled = true;
58
+ clearTimeout(timer);
59
+ fn();
60
+ };
61
+ const executeOptions = {
39
62
  sqlText,
40
63
  binds,
64
+ // Pass server-side timeout so Snowflake kills the query at the deadline even
65
+ // if the client-side cancel doesn't reach the server in time.
66
+ ...(timeoutMs != null && {
67
+ parameters: { STATEMENT_TIMEOUT_IN_SECONDS: Math.ceil(timeoutMs / 1000) },
68
+ }),
41
69
  complete(err, stmt, rows) {
42
70
  if (err)
43
- return reject(err);
71
+ return settle(() => reject(err));
44
72
  const numUpdated = stmt.getNumUpdatedRows();
45
73
  if (numUpdated != null && numUpdated >= 0) {
46
74
  const n = BigInt(numUpdated);
47
- resolve({
48
- rows: (rows ?? []),
49
- numAffectedRows: n,
50
- numChangedRows: n,
51
- });
75
+ settle(() => resolve({ rows: (rows ?? []), numAffectedRows: n, numChangedRows: n }));
52
76
  }
53
77
  else {
54
- resolve({ rows: (rows ?? []) });
78
+ settle(() => resolve({ rows: (rows ?? []) }));
55
79
  }
56
80
  },
57
- });
81
+ };
82
+ const statement = this.#conn.execute(executeOptions);
83
+ if (timeoutMs != null) {
84
+ timer = setTimeout(() => {
85
+ statement.cancel((cancelErr) => {
86
+ // Cancel succeeded → Snowflake aborted cleanly, session is usable.
87
+ // Cancel failed → session state is unknown, caller should destroy the connection.
88
+ settle(() => reject(new SnowflakeQueryTimeoutError(`Query timed out after ${timeoutMs}ms`, cancelErr == null)));
89
+ });
90
+ }, timeoutMs);
91
+ }
58
92
  });
59
93
  }
60
94
  async *streamQuery(compiledQuery, _chunkSize) {
@@ -10,5 +10,6 @@ export declare class SnowflakeDriver implements Driver {
10
10
  commitTransaction(conn: SnowflakeConnection): Promise<void>;
11
11
  rollbackTransaction(conn: SnowflakeConnection): Promise<void>;
12
12
  releaseConnection(conn: SnowflakeConnection): Promise<void>;
13
+ destroyConnection(conn: SnowflakeConnection): Promise<void>;
13
14
  destroy(): Promise<void>;
14
15
  }
@@ -16,7 +16,8 @@ const DEFAULT_POOL_OPTIONS = {
16
16
  class SnowflakeDriver {
17
17
  #config;
18
18
  #pool = null;
19
- #pendingReleases = new Map();
19
+ // Tracks the underlying SDK connection for each wrapper so we can release or destroy it.
20
+ #resources = new Map();
20
21
  constructor(config) {
21
22
  this.#config = config;
22
23
  }
@@ -24,15 +25,10 @@ class SnowflakeDriver {
24
25
  this.#pool = snowflake_sdk_1.default.createPool(this.#config.connection, { ...DEFAULT_POOL_OPTIONS, ...this.#config.poolOptions });
25
26
  }
26
27
  async acquireConnection() {
27
- return new Promise((resolveAcquire, rejectAcquire) => {
28
- this.#pool.use(async (sdkConn) => {
29
- const conn = new SnowflakeConnection_js_1.SnowflakeConnection(sdkConn);
30
- return new Promise((resolveRelease) => {
31
- this.#pendingReleases.set(conn, resolveRelease);
32
- resolveAcquire(conn);
33
- });
34
- }).catch(rejectAcquire);
35
- });
28
+ const sdkConn = await this.#pool.acquire();
29
+ const conn = new SnowflakeConnection_js_1.SnowflakeConnection(sdkConn);
30
+ this.#resources.set(conn, sdkConn);
31
+ return conn;
36
32
  }
37
33
  async beginTransaction(conn) {
38
34
  return conn.beginTransaction();
@@ -44,10 +40,17 @@ class SnowflakeDriver {
44
40
  return conn.rollbackTransaction();
45
41
  }
46
42
  async releaseConnection(conn) {
47
- const release = this.#pendingReleases.get(conn);
48
- if (release) {
49
- this.#pendingReleases.delete(conn);
50
- release();
43
+ const sdkConn = this.#resources.get(conn);
44
+ if (sdkConn) {
45
+ this.#resources.delete(conn);
46
+ await this.#pool.release(sdkConn);
47
+ }
48
+ }
49
+ async destroyConnection(conn) {
50
+ const sdkConn = this.#resources.get(conn);
51
+ if (sdkConn) {
52
+ this.#resources.delete(conn);
53
+ await this.#pool.destroy(sdkConn);
51
54
  }
52
55
  }
53
56
  async destroy() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "snowflake-kysely-dialect",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "Snowflake dialect for Kysely",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",