2022-08-17 08:50:24 -07:00
|
|
|
import { IExecuteFunctions } from 'n8n-core';
|
|
|
|
|
|
|
|
import { IDataObject, INodeExecutionData, INodeType, INodeTypeDescription } from 'n8n-workflow';
|
|
|
|
|
|
|
|
import { connect, copyInputItems, destroy, execute } from './GenericFunctions';
|
2020-12-10 01:17:16 -08:00
|
|
|
|
2022-04-08 14:32:08 -07:00
|
|
|
import snowflake from 'snowflake-sdk';
|
2020-12-10 01:17:16 -08:00
|
|
|
|
|
|
|
export class Snowflake implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'Snowflake',
|
|
|
|
name: 'snowflake',
|
2021-06-12 12:00:37 -07:00
|
|
|
icon: 'file:snowflake.svg',
|
2020-12-10 01:17:16 -08:00
|
|
|
group: ['input'],
|
|
|
|
version: 1,
|
2021-07-03 05:40:16 -07:00
|
|
|
description: 'Get, add and update data in Snowflake',
|
2020-12-10 01:17:16 -08:00
|
|
|
defaults: {
|
|
|
|
name: 'Snowflake',
|
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'snowflake',
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Operation',
|
|
|
|
name: 'operation',
|
|
|
|
type: 'options',
|
2022-05-20 14:47:24 -07:00
|
|
|
noDataExpression: true,
|
2020-12-10 01:17:16 -08:00
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Execute Query',
|
|
|
|
value: 'executeQuery',
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'Execute an SQL query',
|
2022-07-10 13:50:51 -07:00
|
|
|
action: 'Execute a SQL query',
|
2020-12-10 01:17:16 -08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Insert',
|
|
|
|
value: 'insert',
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'Insert rows in database',
|
2022-07-10 13:50:51 -07:00
|
|
|
action: 'Insert rows in database',
|
2020-12-10 01:17:16 -08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Update',
|
|
|
|
value: 'update',
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'Update rows in database',
|
2022-07-10 13:50:51 -07:00
|
|
|
action: 'Update rows in database',
|
2020-12-10 01:17:16 -08:00
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'insert',
|
|
|
|
},
|
|
|
|
|
|
|
|
// ----------------------------------
|
|
|
|
// executeQuery
|
|
|
|
// ----------------------------------
|
|
|
|
{
|
|
|
|
displayName: 'Query',
|
|
|
|
name: 'query',
|
|
|
|
type: 'string',
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-17 08:50:24 -07:00
|
|
|
operation: ['executeQuery'],
|
2020-12-10 01:17:16 -08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
default: '',
|
|
|
|
placeholder: 'SELECT id, name FROM product WHERE id < 40',
|
|
|
|
required: true,
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'The SQL query to execute',
|
2020-12-10 01:17:16 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
// ----------------------------------
|
|
|
|
// insert
|
|
|
|
// ----------------------------------
|
|
|
|
{
|
|
|
|
displayName: 'Table',
|
|
|
|
name: 'table',
|
|
|
|
type: 'string',
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-17 08:50:24 -07:00
|
|
|
operation: ['insert'],
|
2020-12-10 01:17:16 -08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
default: '',
|
|
|
|
required: true,
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'Name of the table in which to insert data to',
|
2020-12-10 01:17:16 -08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Columns',
|
|
|
|
name: 'columns',
|
|
|
|
type: 'string',
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-17 08:50:24 -07:00
|
|
|
operation: ['insert'],
|
2020-12-10 01:17:16 -08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
default: '',
|
|
|
|
placeholder: 'id,name,description',
|
2022-08-17 08:50:24 -07:00
|
|
|
description:
|
|
|
|
'Comma-separated list of the properties which should used as columns for the new rows',
|
2020-12-10 01:17:16 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
// ----------------------------------
|
|
|
|
// update
|
|
|
|
// ----------------------------------
|
|
|
|
{
|
|
|
|
displayName: 'Table',
|
|
|
|
name: 'table',
|
|
|
|
type: 'string',
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-17 08:50:24 -07:00
|
|
|
operation: ['update'],
|
2020-12-10 01:17:16 -08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
default: '',
|
|
|
|
required: true,
|
|
|
|
description: 'Name of the table in which to update data in',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Update Key',
|
|
|
|
name: 'updateKey',
|
|
|
|
type: 'string',
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-17 08:50:24 -07:00
|
|
|
operation: ['update'],
|
2020-12-10 01:17:16 -08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
default: 'id',
|
|
|
|
required: true,
|
2022-05-06 14:01:25 -07:00
|
|
|
// eslint-disable-next-line n8n-nodes-base/node-param-description-miscased-id
|
2022-08-17 08:50:24 -07:00
|
|
|
description:
|
|
|
|
'Name of the property which decides which rows in the database should be updated. Normally that would be "id".',
|
2020-12-10 01:17:16 -08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Columns',
|
|
|
|
name: 'columns',
|
|
|
|
type: 'string',
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-17 08:50:24 -07:00
|
|
|
operation: ['update'],
|
2020-12-10 01:17:16 -08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
default: '',
|
|
|
|
placeholder: 'name,description',
|
2022-08-17 08:50:24 -07:00
|
|
|
description:
|
|
|
|
'Comma-separated list of the properties which should used as columns for rows to update',
|
2020-12-10 01:17:16 -08:00
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
2022-08-17 08:50:24 -07:00
|
|
|
const credentials = (await this.getCredentials(
|
|
|
|
'snowflake',
|
|
|
|
)) as unknown as snowflake.ConnectionOptions;
|
2020-12-10 01:17:16 -08:00
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
let responseData;
|
|
|
|
|
|
|
|
const connection = snowflake.createConnection(credentials);
|
|
|
|
|
|
|
|
await connect(connection);
|
|
|
|
|
|
|
|
const items = this.getInputData();
|
2022-12-02 03:53:59 -08:00
|
|
|
const operation = this.getNodeParameter('operation', 0);
|
2020-12-10 01:17:16 -08:00
|
|
|
|
|
|
|
if (operation === 'executeQuery') {
|
|
|
|
// ----------------------------------
|
|
|
|
// executeQuery
|
|
|
|
// ----------------------------------
|
|
|
|
|
|
|
|
for (let i = 0; i < items.length; i++) {
|
|
|
|
const query = this.getNodeParameter('query', i) as string;
|
|
|
|
responseData = await execute(connection, query, []);
|
|
|
|
returnData.push.apply(returnData, responseData as IDataObject[]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (operation === 'insert') {
|
|
|
|
// ----------------------------------
|
|
|
|
// insert
|
|
|
|
// ----------------------------------
|
|
|
|
|
|
|
|
const table = this.getNodeParameter('table', 0) as string;
|
|
|
|
const columnString = this.getNodeParameter('columns', 0) as string;
|
2022-08-17 08:50:24 -07:00
|
|
|
const columns = columnString.split(',').map((column) => column.trim());
|
|
|
|
const query = `INSERT INTO ${table}(${columns.join(',')}) VALUES (${columns
|
2022-11-08 06:28:21 -08:00
|
|
|
.map((_column) => '?')
|
2022-08-17 08:50:24 -07:00
|
|
|
.join(',')})`;
|
2020-12-10 01:17:16 -08:00
|
|
|
const data = copyInputItems(items, columns);
|
2022-08-17 08:50:24 -07:00
|
|
|
const binds = data.map((element) => Object.values(element));
|
2020-12-10 01:17:16 -08:00
|
|
|
await execute(connection, query, binds as unknown as snowflake.InsertBinds);
|
|
|
|
returnData.push.apply(returnData, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (operation === 'update') {
|
|
|
|
// ----------------------------------
|
|
|
|
// update
|
|
|
|
// ----------------------------------
|
|
|
|
|
|
|
|
const table = this.getNodeParameter('table', 0) as string;
|
|
|
|
const updateKey = this.getNodeParameter('updateKey', 0) as string;
|
|
|
|
const columnString = this.getNodeParameter('columns', 0) as string;
|
2022-08-17 08:50:24 -07:00
|
|
|
const columns = columnString.split(',').map((column) => column.trim());
|
2020-12-10 01:17:16 -08:00
|
|
|
|
|
|
|
if (!columns.includes(updateKey)) {
|
|
|
|
columns.unshift(updateKey);
|
|
|
|
}
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
const query = `UPDATE ${table} SET ${columns
|
|
|
|
.map((column) => `${column} = ?`)
|
|
|
|
.join(',')} WHERE ${updateKey} = ?;`;
|
2020-12-10 01:17:16 -08:00
|
|
|
const data = copyInputItems(items, columns);
|
2022-08-17 08:50:24 -07:00
|
|
|
const binds = data.map((element) => Object.values(element).concat(element[updateKey]));
|
2020-12-10 01:17:16 -08:00
|
|
|
for (let i = 0; i < binds.length; i++) {
|
|
|
|
await execute(connection, query, binds[i] as unknown as snowflake.InsertBinds);
|
|
|
|
}
|
|
|
|
returnData.push.apply(returnData, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
await destroy(connection);
|
|
|
|
|
|
|
|
return [this.helpers.returnJsonArray(returnData)];
|
|
|
|
}
|
|
|
|
}
|