snowflake-kysely-dialect 0.1.1 → 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) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "snowflake-kysely-dialect",
3
- "version": "0.1.1",
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",