Promise-based API wrapper for Snowflake Node SDK
@dylburger
code:
data:privatelast updated:4 years ago
today
Build integrations remarkably fast!
You're viewing a public workflow template.
Sign up to customize, add steps, modify code and more.
Join 1,000,000+ developers using the Pipedream platform
steps.
trigger
Cron Scheduler
Deploy to configure a custom schedule
This workflow runs on Pipedream's servers and is triggered on a custom schedule.
steps.
nodejs
auth
to use OAuth tokens and API keys in code via theauths object
(auths.snowflake)
code
Write any Node.jscodeand use anynpm package. You can alsoexport datafor use in later steps via return or this.key = 'value', pass input data to your code viaparams, and maintain state across executions with$checkpoint.
async (event, steps, auths) => {
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
59
60
61
}
62
// Example query
const sqlText = `SELECT CURRENT_TIMESTAMP();`

const { promisify } = require('util')
const snowflake = require('snowflake-sdk')

const connection = snowflake.createConnection(auths.snowflake)

const connectAsync = promisify(connection.connect)

try {
  await connectAsync()
} catch (err) {
  console.error('Unable to connect: ' + err.message)
}

// util.promisify requires the last argument of the function you pass to 
// it be the callback function, according to Node convention — see the docs at
// https://nodejs.org/dist/latest-v8.x/docs/api/util.html#util_util_promisify_original .
// Since Snowflake's connection.execute function accepts a single argument — 
// options, an object — using promisify won't work. So we wrap the callback 
// with a Promise and await it below.
//
// Pass the same options object (e.g. sqlText, binds), _except_ for the callback
// parameter. Instead, you'll want to await the result, which contains the 
// stmt and rows object the callback version of the function passes, using 
// a catch block to catch any errors, e.g.
//
//   try {
//     const { stmt, rows } = await connExecuteAsync({
//       sqlText: `insert into your_table select parse_json('{"foo": "bar"}')`,
//     })
//   } catch (err) {
//     console.error(`Failed to execute statement due to the following error: ${err.message}`)
//   }
//
async function connExecuteAsync(options) {
  return new Promise((resolve, reject) => {
    const statement = connection.execute({
      ...options,
      complete: function(err, stmt, rows) {
        if (err) {
          reject(err)
        } else {
          resolve({stmt, rows})
        }
      }
    })
  })
}

try {
  const { stmt, rows } = await connExecuteAsync({
    sqlText,
    fetchAsString: ['Number', 'Date'],
  })
  console.log(JSON.stringify(rows, null, 2))
} catch (err) {
  console.error(`Failed to execute statement due to the following error: ${err.message}`)
}