forked from PipedreamHQ/pipedream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnowflake.app.js
58 lines (56 loc) · 1.51 KB
/
snowflake.app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
const { promisify } = require("util");
const snowflake = require("snowflake-sdk");
module.exports = {
app: "snowflake",
type: "app",
methods: {
async _getConnection() {
if (this.connection) {
return this.connection;
}
this.connection = snowflake.createConnection(this.$auth);
await promisify(this.connection.connect).bind(this.connection)();
return this.connection;
},
async getRows(statement) {
const connection = await this._getConnection();
const executedStatement = connection.execute(statement);
return executedStatement.streamRows();
},
async collectRows(statement) {
const rowStream = await this.getRows(statement);
const rows = [];
for await (const row of rowStream) {
rows.push(row);
}
return rows;
},
async *collectRowsPaginated(statement, pageSize = 1) {
const rowStream = await this.getRows(statement);
let rows = [];
for await (const row of rowStream) {
rows.push(row);
if (rows.length === pageSize) {
yield rows;
rows = [];
}
}
yield rows;
},
async listTables() {
const sqlText = "SHOW TABLES";
return this.collectRows({ sqlText });
},
async listFieldsForTable(tableName) {
const sqlText = "DESCRIBE TABLE IDENTIFIER(:1)";
const binds = [
tableName,
];
const statement = {
sqlText,
binds,
};
return this.collectRows(statement);
},
},
};