Skip to content

Commit

Permalink
renames object extracted, added more examples
Browse files Browse the repository at this point in the history
  • Loading branch information
brugos committed Jan 18, 2021
1 parent d096b24 commit 34f0589
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 4 deletions.
69 changes: 67 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Unfortunately [Snowflake SDK](https://www.npmjs.com/package/snowflake-sdk) doesn

- `yarn add snowflake-multisql` or `npm i snowflake-multisql`

---

## Connecting

The constructor extends SnowflakePromise class addin the new method **_executeAll_**.
Expand All @@ -20,11 +22,21 @@ The unique method's param is deconstructed into the variables below:
"sqlText": "your SQL Text string",
"binds": "from the original library, replace tags by sequence",
"replaceTags": "new, replace tags by name, whatever order them appers",
"includeResults": true, // returns field 'data' with results for each chunk.
"preview": true // logs the final order of statements and parsed variables without executing them.
"includeResults": true,
"preview": true
}
```

- **replaceTags**: just spread tags within your sql scripts like {%tag_name%} (please make sure there is no spaces around tag_name). After that, just add the array of tag/value pairs to replaceTags param [Example](https://www.npmjs.com/package/snowflake-multisql)

- **includeResults**: returns field 'data' with results for each chunk.

- **preview**: logs the final order of statements and parsed variables without executing them.

- **loadFiles**: loads all files from a specific folder ended with ".sql" (ex: file1.sql, file2.sql)

---

## Basic usage

```typescript
Expand Down Expand Up @@ -64,3 +76,56 @@ async function main() {

main();
```

---

## Advanced options

```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",
});

// SWISS ARMY KNIFE: Just point your folder and this util method will
// merge all files ended with '.sql'
// IMPORTANT: it merges in the order files appear in your operating system
// i.e. alphabetical order
const sqlText = await loadFiles({
filesPath: path.join(process.cwd(), "./sqlFolder"),
});

// file-1.sql content:
// CREATE OR REPLACE TABLE temp_table_customer as
// SELECT COUNT(*) FROM customer WHERE C_MKTSEGMENT='{%tag_auto%}';

// file-2.sql content:
// SELECT * from temp_table_customer
// WHERE product_name = '{%tag_auto%}'

// file-3.sql content:
// USE SCHEMA demo_schema;
// SELECT COUNT(*) FROM customer WHERE c_mktsegment='{%tag_bike%}';

const replaceTags = [
{ tag: "tag_auto", value: "AUTOMOBILE" },
{ tag: "tag_bike", value: "BIKE" },
];

const rows = await snowflake.executeAll({
sqlText,
replaceTags,
includeResults: true,
});

rows.map((row) => console.dir(row));
}

main();
```
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "snowflake-multisql",
"version": "0.2.0",
"version": "0.3.0",
"description": "Multi-SQL, Promise-based, TypeScript wrapper for Snowflake SDK",
"repository": "github:brugos/snowflake-multisql",
"bugs": "https://github.com/brugos/snowflake-multisql/issues",
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { SnowflakeMultiSql } from "./snowflake-multisql";
export { SnowflakeMultiSql as Snowflake } from "./snowflake-multisql";
export { loadFiles } from "./utils";
export {
ConnectionOptions,
Expand Down

0 comments on commit 34f0589

Please sign in to comment.