2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2023-03-09 09:13:15 -08:00
|
|
|
IExecuteFunctions,
|
2023-01-27 03:22:44 -08:00
|
|
|
IDataObject,
|
|
|
|
INodeExecutionData,
|
|
|
|
INodeType,
|
|
|
|
INodeTypeDescription,
|
|
|
|
} from 'n8n-workflow';
|
2022-08-17 08:50:24 -07:00
|
|
|
|
2023-10-06 07:25:58 -07:00
|
|
|
import { connect, 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';
|
2023-06-22 07:47:28 -07:00
|
|
|
import { getResolvables } from '@utils/utilities';
|
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',
|
2023-06-22 07:47:28 -07:00
|
|
|
noDataExpression: true,
|
2023-04-25 09:18:27 -07:00
|
|
|
typeOptions: {
|
|
|
|
editor: 'sqlEditor',
|
2023-09-19 03:16:35 -07:00
|
|
|
rows: 5,
|
2023-04-25 09:18:27 -07:00
|
|
|
},
|
2020-12-10 01:17:16 -08:00
|
|
|
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;
|
2023-01-31 11:39:20 -08:00
|
|
|
const returnData: INodeExecutionData[] = [];
|
2020-12-10 01:17:16 -08:00
|
|
|
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++) {
|
2023-06-22 07:47:28 -07:00
|
|
|
let query = this.getNodeParameter('query', i) as string;
|
|
|
|
|
|
|
|
for (const resolvable of getResolvables(query)) {
|
|
|
|
query = query.replace(resolvable, this.evaluateExpression(resolvable, i) as string);
|
|
|
|
}
|
|
|
|
|
2020-12-10 01:17:16 -08:00
|
|
|
responseData = await execute(connection, query, []);
|
2023-01-31 11:39:20 -08:00
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray(responseData as IDataObject[]),
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnData.push(...executionData);
|
2020-12-10 01:17:16 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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(',')})`;
|
2023-10-06 07:25:58 -07:00
|
|
|
const data = this.helpers.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);
|
2023-01-31 11:39:20 -08:00
|
|
|
data.forEach((d, i) => {
|
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray(d),
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnData.push(...executionData);
|
|
|
|
});
|
2020-12-10 01:17:16 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
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} = ?;`;
|
2023-10-06 07:25:58 -07:00
|
|
|
const data = this.helpers.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);
|
|
|
|
}
|
2023-01-31 11:39:20 -08:00
|
|
|
data.forEach((d, i) => {
|
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray(d),
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnData.push(...executionData);
|
|
|
|
});
|
2020-12-10 01:17:16 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
await destroy(connection);
|
2023-09-05 03:59:02 -07:00
|
|
|
return [returnData];
|
2020-12-10 01:17:16 -08:00
|
|
|
}
|
|
|
|
}
|