2023-03-09 09:13:15 -08:00
import type {
IExecuteFunctions ,
INodeExecutionData ,
INodeType ,
INodeTypeDescription ,
} from 'n8n-workflow' ;
2023-01-27 03:22:44 -08:00
import { NodeOperationError } from 'n8n-workflow' ;
2021-01-31 09:57:03 -08:00
2022-04-08 14:32:08 -07:00
import pgPromise from 'pg-promise' ;
2023-06-22 07:47:28 -07:00
import { pgInsert , pgQueryV2 , pgUpdate } from '../Postgres/v1/genericFunctions' ;
2021-01-31 09:57:03 -08:00
export class TimescaleDb implements INodeType {
description : INodeTypeDescription = {
displayName : 'TimescaleDB' ,
name : 'timescaleDb' ,
icon : 'file:timescale.svg' ,
group : [ 'input' ] ,
version : 1 ,
description : 'Add and update data in TimescaleDB' ,
defaults : {
name : 'TimescaleDB' ,
} ,
inputs : [ 'main' ] ,
outputs : [ 'main' ] ,
2024-01-05 03:31:11 -08:00
parameterPane : 'wide' ,
2021-01-31 09:57:03 -08:00
credentials : [
{
name : 'timescaleDb' ,
required : true ,
} ,
] ,
properties : [
2022-05-20 14:47:24 -07:00
// eslint-disable-next-line n8n-nodes-base/node-param-operation-without-no-data-expression
2021-01-31 09:57:03 -08:00
{
displayName : 'Operation' ,
name : 'operation' ,
type : 'options' ,
options : [
{
name : 'Execute Query' ,
value : 'executeQuery' ,
description : 'Execute an SQL query' ,
2022-07-10 13:50:51 -07:00
action : 'Execute a SQL query' ,
2021-01-31 09:57:03 -08:00
} ,
{
name : 'Insert' ,
value : 'insert' ,
description : 'Insert rows in database' ,
2022-07-10 13:50:51 -07:00
action : 'Insert rows in database' ,
2021-01-31 09:57:03 -08:00
} ,
{
name : 'Update' ,
value : 'update' ,
description : 'Update rows in database' ,
2022-07-10 13:50:51 -07:00
action : 'Update rows in database' ,
2021-01-31 09:57:03 -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-06-22 07:47:28 -07:00
sqlDialect : 'PostgreSQL' ,
2023-04-25 09:18:27 -07:00
} ,
2021-01-31 09:57:03 -08:00
displayOptions : {
show : {
2021-04-30 15:35:34 -07:00
operation : [ 'executeQuery' ] ,
2021-01-31 09:57:03 -08:00
} ,
} ,
default : '' ,
2021-04-30 15:35:34 -07:00
placeholder : 'SELECT id, name FROM product WHERE quantity > $1 AND price <= $2' ,
2021-01-31 09:57:03 -08:00
required : true ,
2022-08-17 08:50:24 -07:00
description :
'The SQL query to execute. You can use n8n expressions or $1 and $2 in conjunction with query parameters.' ,
2021-01-31 09:57:03 -08:00
} ,
// ----------------------------------
// insert
// ----------------------------------
{
displayName : 'Schema' ,
name : 'schema' ,
type : 'string' ,
displayOptions : {
show : {
2022-08-17 08:50:24 -07:00
operation : [ 'insert' ] ,
2021-01-31 09:57:03 -08:00
} ,
} ,
default : 'public' ,
required : true ,
description : 'Name of the schema the table belongs to' ,
} ,
{
displayName : 'Table' ,
name : 'table' ,
type : 'string' ,
displayOptions : {
show : {
2022-08-17 08:50:24 -07:00
operation : [ 'insert' ] ,
2021-01-31 09:57:03 -08:00
} ,
} ,
default : '' ,
required : true ,
2022-05-06 14:01:25 -07:00
description : 'Name of the table in which to insert data to' ,
2021-01-31 09:57:03 -08:00
} ,
{
displayName : 'Columns' ,
name : 'columns' ,
type : 'string' ,
displayOptions : {
show : {
2022-08-17 08:50:24 -07:00
operation : [ 'insert' ] ,
2021-01-31 09:57:03 -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' ,
2021-01-31 09:57:03 -08:00
} ,
// ----------------------------------
// update
// ----------------------------------
{
displayName : 'Schema' ,
name : 'schema' ,
type : 'string' ,
displayOptions : {
show : {
2022-08-17 08:50:24 -07:00
operation : [ 'update' ] ,
2021-01-31 09:57:03 -08:00
} ,
} ,
default : 'public' ,
required : true ,
description : 'Name of the schema the table belongs to' ,
} ,
{
displayName : 'Table' ,
name : 'table' ,
type : 'string' ,
displayOptions : {
show : {
2022-08-17 08:50:24 -07:00
operation : [ 'update' ] ,
2021-01-31 09:57:03 -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' ] ,
2021-01-31 09:57:03 -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".' ,
2021-01-31 09:57:03 -08:00
} ,
{
displayName : 'Columns' ,
name : 'columns' ,
type : 'string' ,
displayOptions : {
show : {
2022-08-17 08:50:24 -07:00
operation : [ 'update' ] ,
2021-01-31 09:57:03 -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' ,
2021-01-31 09:57:03 -08:00
} ,
2021-04-24 13:55:14 -07:00
// ----------------------------------
// insert,update
// ----------------------------------
{
displayName : 'Return Fields' ,
name : 'returnFields' ,
type : 'string' ,
displayOptions : {
show : {
operation : [ 'insert' , 'update' ] ,
} ,
} ,
default : '*' ,
2022-04-22 09:29:51 -07:00
description : 'Comma-separated list of the fields that the operation will return' ,
2021-04-24 13:55:14 -07:00
} ,
// ----------------------------------
// additional fields
// ----------------------------------
{
displayName : 'Additional Fields' ,
name : 'additionalFields' ,
type : 'collection' ,
placeholder : 'Add Field' ,
default : { } ,
options : [
{
displayName : 'Mode' ,
name : 'mode' ,
type : 'options' ,
options : [
{
name : 'Independently' ,
value : 'independently' ,
description : 'Execute each query independently' ,
} ,
{
2022-06-03 10:23:49 -07:00
name : 'Multiple Queries' ,
2021-04-24 13:55:14 -07:00
value : 'multiple' ,
description : '<b>Default</b>. Sends multiple queries at once to database.' ,
} ,
{
name : 'Transaction' ,
value : 'transaction' ,
description : 'Executes all queries in a single transaction' ,
} ,
] ,
default : 'multiple' ,
2022-08-17 08:50:24 -07:00
description :
2022-09-29 03:33:16 -07:00
'The way queries should be sent to database. Can be used in conjunction with <b>Continue on Fail</b>. See <a href="https://docs.n8n.io/integrations/builtin/app-nodes/n8n-nodes-base.timescaledb/">the docs</a> for more examples' ,
2021-04-24 13:55:14 -07:00
} ,
2021-04-30 15:35:34 -07:00
{
displayName : 'Query Parameters' ,
name : 'queryParams' ,
type : 'string' ,
displayOptions : {
show : {
2022-08-17 08:50:24 -07:00
'/operation' : [ 'executeQuery' ] ,
2021-04-30 15:35:34 -07:00
} ,
} ,
default : '' ,
placeholder : 'quantity,price' ,
2022-08-17 08:50:24 -07:00
description :
'Comma-separated list of properties which should be used as query parameters' ,
2021-04-30 15:35:34 -07:00
} ,
2021-04-24 13:55:14 -07:00
] ,
} ,
2021-01-31 09:57:03 -08:00
] ,
} ;
async execute ( this : IExecuteFunctions ) : Promise < INodeExecutionData [ ] [ ] > {
2021-08-20 09:57:30 -07:00
const credentials = await this . getCredentials ( 'timescaleDb' ) ;
2021-01-31 09:57:03 -08:00
const pgp = pgPromise ( ) ;
const config = {
host : credentials.host as string ,
port : credentials.port as number ,
database : credentials.database as string ,
user : credentials.user as string ,
password : credentials.password as string ,
ssl : ! [ 'disable' , undefined ] . includes ( credentials . ssl as string | undefined ) ,
sslmode : ( credentials . ssl as string ) || 'disable' ,
} ;
const db = pgp ( config ) ;
let returnItems = [ ] ;
const items = this . getInputData ( ) ;
2022-12-02 03:53:59 -08:00
const operation = this . getNodeParameter ( 'operation' , 0 ) ;
2021-01-31 09:57:03 -08:00
if ( operation === 'executeQuery' ) {
// ----------------------------------
// executeQuery
// ----------------------------------
2023-06-22 07:47:28 -07:00
const queryResult = await pgQueryV2 . call ( this , pgp , db , items , this . continueOnFail ( ) , {
resolveExpression : true ,
} ) ;
2021-01-31 09:57:03 -08:00
2021-04-24 13:55:14 -07:00
returnItems = this . helpers . returnJsonArray ( queryResult ) ;
2021-01-31 09:57:03 -08:00
} else if ( operation === 'insert' ) {
// ----------------------------------
// insert
// ----------------------------------
2022-08-17 08:50:24 -07:00
const insertData = await pgInsert (
this . getNodeParameter ,
pgp ,
db ,
items ,
this . continueOnFail ( ) ,
) ;
2021-01-31 09:57:03 -08:00
// Add the id to the data
for ( let i = 0 ; i < insertData . length ; i ++ ) {
returnItems . push ( {
2021-04-24 13:55:14 -07:00
json : insertData [ i ] ,
2021-01-31 09:57:03 -08:00
} ) ;
}
} else if ( operation === 'update' ) {
// ----------------------------------
// update
// ----------------------------------
2022-08-17 08:50:24 -07:00
const updateItems = await pgUpdate (
this . getNodeParameter ,
pgp ,
db ,
items ,
this . continueOnFail ( ) ,
) ;
2021-01-31 09:57:03 -08:00
returnItems = this . helpers . returnJsonArray ( updateItems ) ;
} else {
2023-10-12 02:10:14 -07:00
await db . $pool . end ( ) ;
2022-08-17 08:50:24 -07:00
throw new NodeOperationError (
this . getNode ( ) ,
` The operation " ${ operation } " is not supported! ` ,
) ;
2021-01-31 09:57:03 -08:00
}
2023-10-12 02:10:14 -07:00
// shuts down the connection pool associated with the db object to allow the process to finish
await db . $pool . end ( ) ;
2021-01-31 09:57:03 -08:00
2023-09-05 03:59:02 -07:00
return [ returnItems ] ;
2021-01-31 09:57:03 -08:00
}
}