1
0
Fork 0
mirror of synced 2024-07-04 14:01:27 +12:00
budibase/packages/server/src/integrations/snowflake.ts

123 lines
2.7 KiB
TypeScript
Raw Normal View History

import { Integration, QueryTypes, SqlQuery } from "../definitions/datasource"
2022-05-31 22:58:03 +12:00
import {
SnowflakeError,
Statement,
createConnection,
Connection,
} from "snowflake-sdk"
2022-05-31 08:13:45 +12: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: {
type: "string",
2022-05-31 08:13:45 +12:00
required: true,
},
username: {
type: "string",
2022-05-31 08:13:45 +12:00
required: true,
},
password: {
type: "password",
2022-05-31 08:13:45 +12:00
required: true,
},
warehouse: {
type: "string",
2022-05-31 08:13:45 +12:00
required: true,
},
database: {
type: "string",
2022-05-31 08:13:45 +12:00
required: true,
},
schema: {
type: "string",
2022-05-31 08:13:45 +12:00
required: true,
},
},
query: {
create: {
type: QueryTypes.SQL,
},
read: {
type: QueryTypes.SQL,
},
update: {
type: QueryTypes.SQL,
},
delete: {
type: QueryTypes.SQL,
},
},
}
class SnowflakeIntegration {
2022-05-31 22:58:03 +12:00
private client: Connection
2022-05-31 08:13:45 +12:00
constructor(config: SnowflakeConfig) {
2022-05-31 22:58:03 +12:00
this.client = createConnection(config)
2022-05-31 08:13:45 +12:00
}
2022-05-31 22:58:03 +12:00
async connectAsync() {
2022-05-31 08:13:45 +12:00
return new Promise((resolve, reject) => {
2022-05-31 22:58:03 +12:00
this.client.connect(function (err: any, conn: any) {
2022-05-31 08:13:45 +12:00
if (err) reject(err)
resolve(conn)
})
})
}
2022-06-01 00:10:16 +12:00
async internalQuery(query: SqlQuery) {
await this.connectAsync()
2022-05-31 08:13:45 +12:00
let response: any = await new Promise((resolve, reject) =>
2022-05-31 22:58:03 +12:00
this.client.execute({
2022-05-31 08:13:45 +12:00
sqlText: query.sql,
streamResult: false,
2022-05-31 22:58:03 +12:00
complete: (
err: SnowflakeError | undefined,
stmt: Statement,
rows: any[] | undefined
) => {
if (err) reject(err?.message.split(":")[1] || err?.message)
2022-05-31 08:13:45 +12:00
resolve({ rows })
},
})
)
return response.rows
}
2022-06-01 00:10:16 +12: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-31 08:13:45 +12:00
}
module.exports = {
schema: SCHEMA,
integration: SnowflakeIntegration,
}
}