|
| 1 | +import type {Column, Connection, ConnectionOptions, RowStatement} from "snowflake-sdk"; |
| 2 | +import Snowflake from "snowflake-sdk"; |
| 3 | +import type {ColumnSchema, QueryParam} from "../runtime/index.js"; |
| 4 | +import {QueryTemplateFunction, SerializableQueryResult, SnowflakeConfig} from "./index.js"; |
| 5 | +import {optionalString} from "./options.js"; |
| 6 | + |
| 7 | +Snowflake.configure({logLevel: "OFF"}); |
| 8 | + |
| 9 | +export default function snowflake(options: SnowflakeConfig): QueryTemplateFunction { |
| 10 | + return async (strings, ...params) => { |
| 11 | + const connection = await connect({ |
| 12 | + account: String(options.account), |
| 13 | + username: optionalString(options.username), |
| 14 | + password: optionalString(options.password), |
| 15 | + database: optionalString(options.database), |
| 16 | + schema: optionalString(options.schema), |
| 17 | + warehouse: optionalString(options.warehouse), |
| 18 | + role: optionalString(options.role) |
| 19 | + }); |
| 20 | + let result: SerializableQueryResult; |
| 21 | + try { |
| 22 | + result = await execute(connection, strings.join("?"), params); |
| 23 | + } finally { |
| 24 | + await destroy(connection); |
| 25 | + } |
| 26 | + return result; |
| 27 | + }; |
| 28 | +} |
| 29 | + |
| 30 | +async function connect(options: ConnectionOptions): Promise<Connection> { |
| 31 | + const connection = Snowflake.createConnection(options); |
| 32 | + await new Promise<void>((resolve, reject) => { |
| 33 | + connection.connect((error) => { |
| 34 | + if (error) return reject(error); |
| 35 | + resolve(); |
| 36 | + }); |
| 37 | + }); |
| 38 | + return connection; |
| 39 | +} |
| 40 | + |
| 41 | +async function destroy(connection: Connection): Promise<void> { |
| 42 | + await new Promise<void>((resolve, reject) => { |
| 43 | + connection.destroy((error) => { |
| 44 | + if (error) return reject(error); |
| 45 | + resolve(); |
| 46 | + }); |
| 47 | + }); |
| 48 | +} |
| 49 | + |
| 50 | +async function execute( |
| 51 | + connection: Connection, |
| 52 | + sql: string, |
| 53 | + params?: QueryParam[] |
| 54 | +): Promise<SerializableQueryResult> { |
| 55 | + return new Promise<SerializableQueryResult>((resolve, reject) => { |
| 56 | + const date = new Date(); |
| 57 | + connection.execute({ |
| 58 | + sqlText: sql, |
| 59 | + binds: params, |
| 60 | + complete(error, statement, rows) { |
| 61 | + if (error) return reject(error); |
| 62 | + resolve({ |
| 63 | + rows: rows!, |
| 64 | + schema: getStatementSchema(statement), |
| 65 | + duration: Date.now() - +date, |
| 66 | + date |
| 67 | + }); |
| 68 | + } |
| 69 | + }); |
| 70 | + }); |
| 71 | +} |
| 72 | + |
| 73 | +function getStatementSchema(statement: RowStatement): ColumnSchema[] { |
| 74 | + return statement.getColumns().map(getColumnSchema); |
| 75 | +} |
| 76 | + |
| 77 | +function getColumnSchema(column: Column): ColumnSchema { |
| 78 | + return {name: column.getName(), type: getColumnType(column), nullable: column.isNullable()}; |
| 79 | +} |
| 80 | + |
| 81 | +function getColumnType(column: Column): ColumnSchema["type"] { |
| 82 | + const type = column.getType(); |
| 83 | + switch (type.toLowerCase()) { |
| 84 | + case "date": |
| 85 | + case "datetime": |
| 86 | + case "timestamp": |
| 87 | + case "timestamp_ltz": |
| 88 | + case "timestamp_ntz": |
| 89 | + case "timestamp_tz": |
| 90 | + return "date"; |
| 91 | + case "time": |
| 92 | + case "text": |
| 93 | + return "string"; |
| 94 | + case "fixed": |
| 95 | + return column.getScale() === 0 ? "integer" : "number"; |
| 96 | + case "float": |
| 97 | + case "number": |
| 98 | + case "real": |
| 99 | + return "number"; |
| 100 | + case "binary": |
| 101 | + return "buffer"; |
| 102 | + case "array": |
| 103 | + return "array"; |
| 104 | + case "boolean": |
| 105 | + return "boolean"; |
| 106 | + case "object": |
| 107 | + case "variant": |
| 108 | + return "object"; |
| 109 | + default: |
| 110 | + console.warn(`unknown type: ${type}`); |
| 111 | + return "other"; |
| 112 | + } |
| 113 | +} |
0 commit comments