2023-01-27 03:22:44 -08:00
|
|
|
import type { IDataObject, INodeExecutionData } from 'n8n-workflow';
|
|
|
|
import { deepCopy } from 'n8n-workflow';
|
2020-12-10 01:17:16 -08:00
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type snowflake from 'snowflake-sdk';
|
2020-12-10 01:17:16 -08:00
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
export async function connect(conn: snowflake.Connection) {
|
2020-12-10 01:17:16 -08:00
|
|
|
return new Promise((resolve, reject) => {
|
2022-11-08 06:28:21 -08:00
|
|
|
conn.connect((err, _conn) => {
|
2020-12-10 01:17:16 -08:00
|
|
|
if (!err) {
|
2021-08-29 11:58:11 -07:00
|
|
|
// @ts-ignore
|
2020-12-10 01:17:16 -08:00
|
|
|
resolve();
|
|
|
|
} else {
|
|
|
|
reject(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
export async function destroy(conn: snowflake.Connection) {
|
2020-12-10 01:17:16 -08:00
|
|
|
return new Promise((resolve, reject) => {
|
2022-11-08 06:28:21 -08:00
|
|
|
conn.destroy((err, _conn) => {
|
2020-12-10 01:17:16 -08:00
|
|
|
if (!err) {
|
2021-08-29 11:58:11 -07:00
|
|
|
// @ts-ignore
|
2020-12-10 01:17:16 -08:00
|
|
|
resolve();
|
|
|
|
} else {
|
|
|
|
reject(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
export async function execute(
|
|
|
|
conn: snowflake.Connection,
|
|
|
|
sqlText: string,
|
|
|
|
binds: snowflake.InsertBinds,
|
|
|
|
) {
|
2020-12-10 01:17:16 -08:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
conn.execute({
|
|
|
|
sqlText,
|
|
|
|
binds,
|
|
|
|
complete: (err, stmt, rows) => {
|
|
|
|
if (!err) {
|
|
|
|
resolve(rows);
|
|
|
|
} else {
|
|
|
|
reject(err);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function copyInputItems(items: INodeExecutionData[], properties: string[]): IDataObject[] {
|
|
|
|
// Prepare the data to insert and copy it to be returned
|
|
|
|
let newItem: IDataObject;
|
|
|
|
return items.map((item) => {
|
|
|
|
newItem = {};
|
|
|
|
for (const property of properties) {
|
|
|
|
if (item.json[property] === undefined) {
|
|
|
|
newItem[property] = null;
|
|
|
|
} else {
|
2022-10-21 08:24:58 -07:00
|
|
|
newItem[property] = deepCopy(item.json[property]);
|
2020-12-10 01:17:16 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return newItem;
|
|
|
|
});
|
|
|
|
}
|