Skip to content

Commit

Permalink
adding generic types to executeAll
Browse files Browse the repository at this point in the history
  • Loading branch information
brugos committed Feb 24, 2021
1 parent c84e0ef commit 00d5408
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 20 deletions.
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,42 @@ main();

main();
```

---

## Using GENERIC TYPES

```typescript
import { Snowflake, loadFiles } from "snowflake-multisql"

const snowflake = new Snowflake({
account: "<account name>",
username: "<username>",
password: "<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<MyType>({
sqlText,
tags,
includeResults: true,
});

rows.map((rows) => console.log("Your number is: ",rows[0].data[0].num));
}

main();
```
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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"
Expand Down
1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export {
SnowflakeMultiSql as Snowflake,
data,
ITag,
IMultiSqlResult,
IPreview,
Expand Down
12 changes: 6 additions & 6 deletions src/snowflake-multisql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ export interface IPreview {
}

export type data = Record<string, any>;
export interface IMultiSqlResult extends IPreview {
export interface IMultiSqlResult<T> extends IPreview {
duration?: number;
totalDuration?: number;
data?: data[];
data?: T[];
}

export type ITag = {
Expand All @@ -31,15 +31,15 @@ export class SnowflakeMultiSql extends Mixin(Snowflake, EventEmitter) {
constructor(conn: ConnectionOptions) {
super(conn);
}
public async executeAll({
public async executeAll<T>({
sqlText,
tags,
preview,
includeResults = false,
}: IExecuteAll): Promise<IMultiSqlResult[]> {
}: IExecuteAll): Promise<IMultiSqlResult<T>[]> {
const chunks = this.getChunks(sqlText);
const chunksTotal = chunks.length;
const results: IMultiSqlResult[] = [];
const results: IMultiSqlResult<T>[] = [];
let totalDuration: number = 0;
for (let _i = 0; _i < chunks.length; _i++) {
if (chunks[_i].trim().length > 0) {
Expand All @@ -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<T> = {
...previewObj,
duration,
totalDuration,
Expand Down
41 changes: 30 additions & 11 deletions tests/snowflake-multisql.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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<Conversion>({
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;
});

Expand All @@ -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,
Expand Down

0 comments on commit 00d5408

Please sign in to comment.