|
| 1 | +import {json} from "micro"; |
| 2 | +import {URL} from "url"; |
| 3 | +import JSONStream from "JSONStream"; |
| 4 | +import snowflake from "snowflake-sdk"; |
| 5 | + |
| 6 | +export default url => { |
| 7 | + url = new URL(url); |
| 8 | + console.log(url); |
| 9 | + const {host: account, username, password, pathname, searchParams} = new URL( |
| 10 | + url |
| 11 | + ); |
| 12 | + const connection = snowflake.createConnection({ |
| 13 | + account, |
| 14 | + username, |
| 15 | + password, |
| 16 | + database: pathname.slice(1), |
| 17 | + schema: searchParams.get("schema"), |
| 18 | + warehouse: searchParams.get("warehouse"), |
| 19 | + role: searchParams.get("role") |
| 20 | + }); |
| 21 | + |
| 22 | + return async function query(req, res) { |
| 23 | + const body = await json(req); |
| 24 | + const {sql, params} = body; |
| 25 | + |
| 26 | + const client = await new Promise((resolve, reject) => { |
| 27 | + if (connection.isUp()) return resolve(connection); |
| 28 | + snowflake.configure({ocspFailOpen: false}); |
| 29 | + connection.connect((err, conn) => { |
| 30 | + if (err) reject(err); |
| 31 | + else resolve(conn); |
| 32 | + }); |
| 33 | + }); |
| 34 | + |
| 35 | + const statement = client.execute({sqlText: sql, binds: params}); |
| 36 | + try { |
| 37 | + const stream = statement.streamRows(); |
| 38 | + |
| 39 | + await new Promise((resolve, reject) => { |
| 40 | + stream |
| 41 | + .once("end", resolve) |
| 42 | + .on("error", reject) |
| 43 | + .pipe(JSONStream.stringify(`{"data":[`, ",", "]")) |
| 44 | + .pipe(res, {end: false}); |
| 45 | + }); |
| 46 | + } catch (error) { |
| 47 | + if (!error.statusCode) error.statusCode = 400; |
| 48 | + throw error; |
| 49 | + } |
| 50 | + |
| 51 | + const schema = { |
| 52 | + type: "array", |
| 53 | + items: { |
| 54 | + type: "object", |
| 55 | + properties: statement |
| 56 | + .getColumns() |
| 57 | + .reduce( |
| 58 | + (schema, column) => ( |
| 59 | + (schema[column.getName()] = dataTypeSchema(column)), schema |
| 60 | + ), |
| 61 | + {} |
| 62 | + ) |
| 63 | + } |
| 64 | + }; |
| 65 | + res.end(`,"schema":${JSON.stringify(schema)}}`); |
| 66 | + }; |
| 67 | +}; |
| 68 | + |
| 69 | +// https://www.postgresql.org/docs/9.6/datatype.html |
| 70 | +const array = ["null", "array"], |
| 71 | + boolean = ["null", "boolean"], |
| 72 | + // integer = ["null", "integer"], |
| 73 | + number = ["null", "number"], |
| 74 | + object = ["null", "object"], |
| 75 | + string = ["null", "string"]; |
| 76 | +function dataTypeSchema(column) { |
| 77 | + // https://github.com/snowflakedb/snowflake-connector-nodejs/blob/master/lib/connection/result/data_types.js |
| 78 | + switch (column.getType()) { |
| 79 | + case "binary": |
| 80 | + return {type: object, buffer: true}; |
| 81 | + case "boolean": |
| 82 | + return {type: boolean}; |
| 83 | + case "fixed": |
| 84 | + case "real": |
| 85 | + return {type: number}; |
| 86 | + case "date": |
| 87 | + case "time": |
| 88 | + case "timestamp_ltz": |
| 89 | + case "timestamp_ntz": |
| 90 | + case "timestamp_tz": |
| 91 | + return {type: string, date: true}; |
| 92 | + case "variant": |
| 93 | + case "object": |
| 94 | + return {type: object}; |
| 95 | + case "array": |
| 96 | + return {type: array, items: {type: object}}; |
| 97 | + case "text": |
| 98 | + default: |
| 99 | + return {type: string}; |
| 100 | + } |
| 101 | +} |
0 commit comments