2022-06-01 10:36:42 +02:00
import { Integration , QueryTypes , SqlQuery } from "../definitions/datasource"
2022-05-31 12:58:03 +02:00
import {
SnowflakeError ,
Statement ,
createConnection ,
Connection ,
} from "snowflake-sdk"
2022-05-30 22:13:45 +02:00
module SnowflakeModule {
interface SnowflakeConfig {
account : string
username : string
password : string
warehouse : string
database : string
schema : string
}
const SCHEMA : Integration = {
docs : "https://developers.snowflake.com/" ,
description :
"Snowflake is a solution for data warehousing, data lakes, data engineering, data science, data application development, and securely sharing and consuming shared data." ,
friendlyName : "Snowflake" ,
datasource : {
account : {
2022-06-01 10:36:42 +02:00
type : "string" ,
2022-05-30 22:13:45 +02:00
required : true ,
} ,
username : {
2022-06-01 10:36:42 +02:00
type : "string" ,
2022-05-30 22:13:45 +02:00
required : true ,
} ,
password : {
2022-06-01 10:36:42 +02:00
type : "password" ,
2022-05-30 22:13:45 +02:00
required : true ,
} ,
warehouse : {
2022-06-01 10:36:42 +02:00
type : "string" ,
2022-05-30 22:13:45 +02:00
required : true ,
} ,
database : {
2022-06-01 10:36:42 +02:00
type : "string" ,
2022-05-30 22:13:45 +02:00
required : true ,
} ,
schema : {
2022-06-01 10:36:42 +02:00
type : "string" ,
2022-05-30 22:13:45 +02:00
required : true ,
} ,
} ,
query : {
create : {
type : QueryTypes . SQL ,
} ,
read : {
type : QueryTypes . SQL ,
} ,
update : {
type : QueryTypes . SQL ,
} ,
delete : {
type : QueryTypes . SQL ,
} ,
} ,
}
2022-06-01 10:36:42 +02:00
class SnowflakeIntegration {
2022-05-31 12:58:03 +02:00
private client : Connection
2022-05-30 22:13:45 +02:00
constructor ( config : SnowflakeConfig ) {
2022-05-31 12:58:03 +02:00
this . client = createConnection ( config )
2022-05-30 22:13:45 +02:00
}
2022-05-31 12:58:03 +02:00
async connectAsync() {
2022-05-30 22:13:45 +02:00
return new Promise ( ( resolve , reject ) = > {
2022-05-31 12:58:03 +02:00
this . client . connect ( function ( err : any , conn : any ) {
2022-05-30 22:13:45 +02:00
if ( err ) reject ( err )
resolve ( conn )
} )
} )
}
2022-05-31 14:10:16 +02:00
async internalQuery ( query : SqlQuery ) {
2022-06-01 10:36:42 +02:00
await this . connectAsync ( )
2022-05-30 22:13:45 +02:00
let response : any = await new Promise ( ( resolve , reject ) = >
2022-05-31 12:58:03 +02:00
this . client . execute ( {
2022-05-30 22:13:45 +02:00
sqlText : query.sql ,
streamResult : false ,
2022-05-31 12:58:03 +02:00
complete : (
err : SnowflakeError | undefined ,
stmt : Statement ,
rows : any [ ] | undefined
) = > {
if ( err ) reject ( err ? . message . split ( ":" ) [ 1 ] || err ? . message )
2022-05-30 22:13:45 +02:00
resolve ( { rows } )
} ,
} )
)
return response . rows
}
2022-05-31 14:10:16 +02:00
async create ( query : SqlQuery ) {
return this . internalQuery ( query )
}
async read ( query : SqlQuery ) {
return this . internalQuery ( query )
}
async update ( query : SqlQuery ) {
return this . internalQuery ( query )
}
async delete ( query : SqlQuery ) {
return this . internalQuery ( query )
}
2022-05-30 22:13:45 +02:00
}
module .exports = {
schema : SCHEMA ,
integration : SnowflakeIntegration ,
}
}