Created
September 30, 2020 22:12
-
-
Save htunnicliff/a7b90348b4d15634b0bf84ecae7bfeae to your computer and use it in GitHub Desktop.
WIP Knex dialect for Snowflake in
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Knex, { MySqlConnectionConfig } from "knex"; | |
import * as Snowflake from "snowflake-sdk"; | |
import { promisify } from "util"; | |
const Dialect = require("knex/lib/dialects/mysql/index.js"); | |
Dialect.prototype._driver = () => Snowflake; | |
Dialect.prototype.wrapIdentifierImpl = (value: string) => value; | |
Dialect.prototype.validateConnection = () => true; | |
Dialect.prototype.acquireRawConnection = async function () { | |
const connection = Snowflake.createConnection({ | |
account: this.connectionSettings.account, | |
username: this.connectionSettings.username, | |
password: this.connectionSettings.password, | |
warehouse: this.connectionSettings.warehouse, | |
role: this.connectionSettings.role, | |
database: this.connectionSettings.database | |
}); | |
await promisify(connection.connect)(); | |
return connection; | |
}; | |
Dialect.prototype.destroyRawConnection = async function (connection: any) { | |
return promisify(connection.destroy)(); | |
}; | |
Dialect.prototype._query = async function ( | |
connection: any, | |
obj: any | |
) { | |
const queryConfig = { | |
sqlText: obj.sql, | |
binds: obj.bindings || [] | |
}; | |
if (obj.options) { | |
Object.assign(queryConfig, obj.options); | |
} | |
const rows = await new Promise((resolve, reject) => { | |
connection.execute({ | |
...queryConfig, | |
complete: (err, _, rows) => { | |
if (err) reject(err); | |
else resolve(rows); | |
} | |
}); | |
}); | |
obj.response = [rows, []]; | |
return obj; | |
}; | |
export default Knex({ | |
dialect: Dialect, | |
client: "mysql", | |
connection: { | |
account: "", | |
username: "", | |
password: "", | |
warehouse: "", | |
role: "", | |
database: "" | |
} as MySqlConnectionConfig | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment