From 00d54088fc34e3d0d064b3d83299a904ed217224 Mon Sep 17 00:00:00 2001 From: Julio Brugos Date: Wed, 24 Feb 2021 22:51:35 +0000 Subject: [PATCH] adding generic types to executeAll --- README.md | 39 ++++++++++++++++++++++++++++++ package.json | 4 ++-- src/index.ts | 1 - src/snowflake-multisql.ts | 12 +++++----- tests/snowflake-multisql.spec.ts | 41 +++++++++++++++++++++++--------- 5 files changed, 77 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 4540822..0804f0a 100644 --- a/README.md +++ b/README.md @@ -158,3 +158,42 @@ main(); main(); ``` + +--- + +## Using GENERIC TYPES + +```typescript + import { Snowflake, loadFiles } from "snowflake-multisql" + + const snowflake = new Snowflake({ + account: "", + username: "", + password: "", + database: "DEMO_DATABASE", + schema: "DEMO_SCHEMA", + warehouse: "DEMO_WH", + }); + + const sqlText = await loadFiles({ + filesPath: path.join(process.cwd(), "./sqlFolder"), + }); + + type MyType = { + id: string; + num: number; + date: Date; + obj?: any; + }; + + const rows = await snowflake.executeAll({ + sqlText, + tags, + includeResults: true, + }); + + rows.map((rows) => console.log("Your number is: ",rows[0].data[0].num)); +} + +main(); +``` diff --git a/package.json b/package.json index 7963054..cc5004a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "snowflake-multisql", - "version": "1.3.1", + "version": "1.4.0", "description": "Multi SQL Statement, Promise-based, TypeScript wrapper for Snowflake SDK", "repository": "github:brugos/snowflake-multisql", "bugs": "https://github.com/brugos/snowflake-multisql/issues", @@ -18,7 +18,7 @@ "scripts": { "prepare": "del-cli build && tsc", "test": "mocha --timeout 60000", - "test:watch": "yarn test:mocha -w", + "test:watch": "yarn test -w", "test:coverage": "yarn test && open && open ./coverage/lcov-report/index.ts.html", "lint:eslint": "eslint ./ --ext .ts --max-warnings 0", "lint": "tsc --noEmit" diff --git a/src/index.ts b/src/index.ts index 3fa89ff..3654085 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,5 @@ export { SnowflakeMultiSql as Snowflake, - data, ITag, IMultiSqlResult, IPreview, diff --git a/src/snowflake-multisql.ts b/src/snowflake-multisql.ts index e9778d2..ac3b0ed 100644 --- a/src/snowflake-multisql.ts +++ b/src/snowflake-multisql.ts @@ -10,10 +10,10 @@ export interface IPreview { } export type data = Record; -export interface IMultiSqlResult extends IPreview { +export interface IMultiSqlResult extends IPreview { duration?: number; totalDuration?: number; - data?: data[]; + data?: T[]; } export type ITag = { @@ -31,15 +31,15 @@ export class SnowflakeMultiSql extends Mixin(Snowflake, EventEmitter) { constructor(conn: ConnectionOptions) { super(conn); } - public async executeAll({ + public async executeAll({ sqlText, tags, preview, includeResults = false, - }: IExecuteAll): Promise { + }: IExecuteAll): Promise[]> { const chunks = this.getChunks(sqlText); const chunksTotal = chunks.length; - const results: IMultiSqlResult[] = []; + const results: IMultiSqlResult[] = []; let totalDuration: number = 0; for (let _i = 0; _i < chunks.length; _i++) { if (chunks[_i].trim().length > 0) { @@ -61,7 +61,7 @@ export class SnowflakeMultiSql extends Mixin(Snowflake, EventEmitter) { const duration = new Date().valueOf() - startTime; totalDuration += duration; - const toPush: IMultiSqlResult = { + const toPush: IMultiSqlResult = { ...previewObj, duration, totalDuration, diff --git a/tests/snowflake-multisql.spec.ts b/tests/snowflake-multisql.spec.ts index 6913574..f551b60 100644 --- a/tests/snowflake-multisql.spec.ts +++ b/tests/snowflake-multisql.spec.ts @@ -21,6 +21,20 @@ describe("checks tagsToBinds function", () => { { tag: "tag2", value: 123 }, { tag: "tag4", value: new Date(1610976670682) }, ]; + type Conversion = { + ID: string; + NUM: number; + DATE: Date; + OBJ?: any; + }; + const mockedResponse: any[] = [ + { + ID: "ID", + NUM: 123, + DATE: new Date(), + OBJ: { a: "a", b: 1, c: new Date() }, + }, + ]; beforeEach(() => { snowflake = new Snowflake({ @@ -32,30 +46,35 @@ describe("checks tagsToBinds function", () => { warehouse: "DEMO_WH", }); const stubSF = sinon.stub(snowflake, "execute"); - stubSF.resolves([ - { - FIELD_1: "VALUE_FIELD_1", - NumField: 123, - DateField: new Date(), - ObjField: { a: "a", b: 1, c: new Date() }, - }, - ]); + stubSF.resolves(mockedResponse); }); afterEach(() => { sinon.restore(); }); + it("executeAll with Generics", async () => { + const spyProgress = sinon.spy(); + snowflake.on("progress", spyProgress); + + const ret = await snowflake.executeAll({ + sqlText, + tags, + includeResults: true, + }); + expect(spyProgress.called).to.be.true; + expect(ret[0].data).to.deep.equal(mockedResponse); + }); + it("executeAll must emmit progress event at least once", async () => { const spyProgress = sinon.spy(); snowflake.on("progress", spyProgress); - const ret: IMultiSqlResult[] = await snowflake.executeAll({ + const ret = await snowflake.executeAll({ sqlText, tags, preview: true, }); - expect(spyProgress.called).to.be.true; }); @@ -73,7 +92,7 @@ describe("checks tagsToBinds function", () => { new Date(1610976670682), ], }; - const ret: IMultiSqlResult[] = await snowflake.executeAll({ + const ret = await snowflake.executeAll({ sqlText, tags, includeResults: false,