From fe3a774db36c46ff99df6e696dba13077c5686d3 Mon Sep 17 00:00:00 2001 From: Mel O'Hagan Date: Tue, 31 May 2022 11:58:03 +0100 Subject: [PATCH] Added types and error handling --- packages/server/package.json | 1 + packages/server/src/integrations/snowflake.ts | 28 ++++++++++++------- packages/server/yarn.lock | 15 ++++++++++ 3 files changed, 34 insertions(+), 10 deletions(-) diff --git a/packages/server/package.json b/packages/server/package.json index 363ecec795..4a262c47b4 100644 --- a/packages/server/package.json +++ b/packages/server/package.json @@ -158,6 +158,7 @@ "@types/node": "^15.12.4", "@types/oracledb": "^5.2.1", "@types/redis": "^4.0.11", + "@types/snowflake-sdk": "^1.6.2", "@typescript-eslint/parser": "5.12.0", "apidoc": "^0.50.2", "babel-jest": "^27.0.2", diff --git a/packages/server/src/integrations/snowflake.ts b/packages/server/src/integrations/snowflake.ts index cdde54e2f2..a4014ff364 100644 --- a/packages/server/src/integrations/snowflake.ts +++ b/packages/server/src/integrations/snowflake.ts @@ -1,5 +1,10 @@ import { Integration, QueryTypes, SqlQuery } from "../definitions/datasource" -const snowflake = require("snowflake-sdk") +import { + SnowflakeError, + Statement, + createConnection, + Connection, +} from "snowflake-sdk" module SnowflakeModule { interface SnowflakeConfig { @@ -59,17 +64,15 @@ module SnowflakeModule { } class SnowflakeIntegration { - private config: SnowflakeConfig - private client: any + private client: Connection constructor(config: SnowflakeConfig) { - this.config = config - this.client = snowflake.createConnection(config) + this.client = createConnection(config) } - async connAsync(connection: any) { + async connectAsync() { return new Promise((resolve, reject) => { - connection.connect(function (err: any, conn: any) { + this.client.connect(function (err: any, conn: any) { if (err) reject(err) resolve(conn) }) @@ -77,12 +80,17 @@ module SnowflakeModule { } async read(query: SqlQuery) { - let connection: any = await this.connAsync(this.client) + await this.connectAsync() let response: any = await new Promise((resolve, reject) => - connection.execute({ + this.client.execute({ sqlText: query.sql, streamResult: false, - complete: (err: any, statement: any, rows: any) => { + complete: ( + err: SnowflakeError | undefined, + stmt: Statement, + rows: any[] | undefined + ) => { + if (err) reject(err?.message.split(":")[1] || err?.message) resolve({ rows }) }, }) diff --git a/packages/server/yarn.lock b/packages/server/yarn.lock index 92f1c9b65f..3ad8b8a30f 100644 --- a/packages/server/yarn.lock +++ b/packages/server/yarn.lock @@ -2545,6 +2545,13 @@ dependencies: "@types/node" "*" +"@types/generic-pool@*": + version "3.1.10" + resolved "https://registry.yarnpkg.com/@types/generic-pool/-/generic-pool-3.1.10.tgz#ed21e0ec798bc5f77bb324e1a5c5c9bf335c1d9e" + integrity sha512-WRT/9taXh9XJRA9yvrbC02IqGZhK9GbFE/vuP2LeSLrqmDzz5wdXsH0Ige/F+3+rbbZfwH3LEazDsU0JiSV3vA== + dependencies: + "@types/node" "*" + "@types/global-agent@^2.1.1": version "2.1.1" resolved "https://registry.yarnpkg.com/@types/global-agent/-/global-agent-2.1.1.tgz#3f93185e48a3a36e377a52a8301320cd162a831b" @@ -2770,6 +2777,14 @@ "@types/mime" "^1" "@types/node" "*" +"@types/snowflake-sdk@^1.6.2": + version "1.6.6" + resolved "https://registry.yarnpkg.com/@types/snowflake-sdk/-/snowflake-sdk-1.6.6.tgz#df066a660695d0689c5d36cb4e1dbde8f815a9e8" + integrity sha512-oGdzQJONiv5UxaSsrIS49IvdcQRoPt6HTqIM8c0JTCIqEwGrsuqBq317PdLntM9SVc8BcDOYeAZTNdlISSSzwQ== + dependencies: + "@types/generic-pool" "*" + "@types/node" "*" + "@types/stack-utils@^1.0.1": version "1.0.1" resolved "https://registry.yarnpkg.com/@types/stack-utils/-/stack-utils-1.0.1.tgz#0a851d3bd96498fa25c33ab7278ed3bd65f06c3e"