2020-07-17 07:10:31 -07:00
import { IExecuteFunctions } from 'n8n-core' ;
2020-08-02 23:21:42 -07:00
import {
IDataObject ,
INodeExecutionData ,
INodeType ,
INodeTypeDescription ,
2021-04-16 09:33:36 -07:00
NodeOperationError ,
2020-08-02 23:21:42 -07:00
} from 'n8n-workflow' ;
2020-07-17 07:10:31 -07:00
2020-08-02 23:21:42 -07:00
import {
2021-04-24 13:55:14 -07:00
generateReturning ,
2020-08-02 23:21:42 -07:00
getItemCopy ,
2021-04-24 13:55:14 -07:00
getItemsCopy ,
2020-08-02 23:21:42 -07:00
pgInsert ,
pgQuery ,
2021-04-24 13:55:14 -07:00
pgUpdate ,
2020-08-02 23:21:42 -07:00
} from '../Postgres/Postgres.node.functions' ;
2020-07-17 07:10:31 -07:00
2022-04-08 14:32:08 -07:00
import pgPromise from 'pg-promise' ;
2020-07-17 07:10:31 -07:00
export class CrateDb implements INodeType {
description : INodeTypeDescription = {
displayName : 'CrateDB' ,
name : 'crateDb' ,
icon : 'file:cratedb.png' ,
group : [ 'input' ] ,
version : 1 ,
2021-07-03 05:40:16 -07:00
description : 'Add and update data in CrateDB' ,
2020-07-17 07:10:31 -07:00
defaults : {
name : 'CrateDB' ,
} ,
inputs : [ 'main' ] ,
outputs : [ 'main' ] ,
credentials : [
{
name : 'crateDb' ,
required : true ,
} ,
] ,
properties : [
{
displayName : 'Operation' ,
name : 'operation' ,
type : 'options' ,
options : [
{
name : 'Execute Query' ,
value : 'executeQuery' ,
2020-09-14 00:58:01 -07:00
description : 'Execute an SQL query' ,
2020-07-17 07:10:31 -07:00
} ,
{
name : 'Insert' ,
value : 'insert' ,
2020-09-14 00:58:01 -07:00
description : 'Insert rows in database' ,
2020-07-17 07:10:31 -07:00
} ,
{
name : 'Update' ,
value : 'update' ,
2020-09-14 00:58:01 -07:00
description : 'Update rows in database' ,
2020-07-17 07:10:31 -07:00
} ,
] ,
default : 'insert' ,
description : 'The operation to perform.' ,
} ,
// ----------------------------------
// executeQuery
// ----------------------------------
{
displayName : 'Query' ,
name : 'query' ,
type : 'string' ,
typeOptions : {
2021-09-11 03:58:48 -07:00
alwaysOpenEditWindow : true ,
2020-07-17 07:10:31 -07:00
} ,
displayOptions : {
show : {
operation : [ 'executeQuery' ] ,
} ,
} ,
default : '' ,
2021-04-30 15:35:34 -07:00
placeholder : 'SELECT id, name FROM product WHERE quantity > $1 AND price <= $2' ,
2020-07-17 07:10:31 -07:00
required : true ,
2021-04-30 15:35:34 -07:00
description : 'The SQL query to execute. You can use n8n expressions or $1 and $2 in conjunction with query parameters.' ,
2020-07-17 07:10:31 -07:00
} ,
// ----------------------------------
// insert
// ----------------------------------
{
displayName : 'Schema' ,
name : 'schema' ,
type : 'string' ,
displayOptions : {
show : {
operation : [ 'insert' ] ,
} ,
} ,
2020-07-26 05:35:19 -07:00
default : 'doc' ,
2020-07-17 07:10:31 -07:00
required : true ,
description : 'Name of the schema the table belongs to' ,
} ,
{
displayName : 'Table' ,
name : 'table' ,
type : 'string' ,
displayOptions : {
show : {
operation : [ 'insert' ] ,
} ,
} ,
default : '' ,
required : true ,
description : 'Name of the table in which to insert data to.' ,
} ,
{
displayName : 'Columns' ,
name : 'columns' ,
type : 'string' ,
displayOptions : {
show : {
operation : [ 'insert' ] ,
} ,
} ,
default : '' ,
placeholder : 'id,name,description' ,
2022-04-22 09:29:51 -07:00
description : 'Comma-separated list of the properties which should used as columns for the new rows.' ,
2020-07-17 07:10:31 -07:00
} ,
2021-04-24 13:55:14 -07:00
// ----------------------------------
// update
// ----------------------------------
2020-07-17 07:10:31 -07:00
{
2021-04-24 13:55:14 -07:00
displayName : 'Schema' ,
name : 'schema' ,
2020-07-17 07:10:31 -07:00
type : 'string' ,
displayOptions : {
show : {
2021-04-24 13:55:14 -07:00
operation : [ 'update' ] ,
2020-07-17 07:10:31 -07:00
} ,
} ,
2021-04-24 13:55:14 -07:00
default : 'doc' ,
required : true ,
description : 'Name of the schema the table belongs to' ,
2020-07-17 07:10:31 -07:00
} ,
{
displayName : 'Table' ,
name : 'table' ,
type : 'string' ,
displayOptions : {
show : {
operation : [ 'update' ] ,
} ,
} ,
default : '' ,
required : true ,
description : 'Name of the table in which to update data in' ,
} ,
{
displayName : 'Update Key' ,
name : 'updateKey' ,
type : 'string' ,
displayOptions : {
show : {
operation : [ 'update' ] ,
} ,
} ,
default : 'id' ,
required : true ,
2022-04-22 09:29:51 -07:00
description : ` Comma-separated list of the properties which decides which rows in the database should be updated. Normally that would be "id". ` ,
2020-07-17 07:10:31 -07:00
} ,
{
displayName : 'Columns' ,
name : 'columns' ,
type : 'string' ,
displayOptions : {
show : {
operation : [ 'update' ] ,
} ,
} ,
default : '' ,
placeholder : 'name,description' ,
2022-04-22 09:29:51 -07:00
description : ` Comma-separated list of the properties which should used as columns for rows to update. ` ,
2020-07-17 07:10:31 -07: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' ,
} ,
{
name : 'Multiple queries' ,
value : 'multiple' ,
description : '<b>Default</b>. Sends multiple queries at once to database.' ,
} ,
] ,
default : 'multiple' ,
2021-11-25 09:10:06 -08:00
description : '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/nodes/n8n-nodes-base.crateDb/">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 : {
'/operation' : [
'executeQuery' ,
] ,
} ,
} ,
default : '' ,
placeholder : 'quantity,price' ,
2022-04-22 09:29:51 -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
] ,
} ,
2020-07-17 07:10:31 -07:00
] ,
} ;
async execute ( this : IExecuteFunctions ) : Promise < INodeExecutionData [ ] [ ] > {
2021-08-20 09:57:30 -07:00
const credentials = await this . getCredentials ( 'crateDb' ) ;
2020-07-17 07:10:31 -07: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 ) ;
2021-04-24 13:55:14 -07:00
let returnItems : INodeExecutionData [ ] = [ ] ;
2020-07-17 07:10:31 -07:00
const items = this . getInputData ( ) ;
const operation = this . getNodeParameter ( 'operation' , 0 ) as string ;
if ( operation === 'executeQuery' ) {
// ----------------------------------
// executeQuery
// ----------------------------------
2021-04-24 13:55:14 -07:00
const queryResult = await pgQuery ( this . getNodeParameter , pgp , db , items , this . continueOnFail ( ) ) ;
2020-07-17 07:10:31 -07:00
2021-04-24 13:55:14 -07:00
returnItems = this . helpers . returnJsonArray ( queryResult ) ;
2020-07-17 07:10:31 -07:00
} else if ( operation === 'insert' ) {
// ----------------------------------
// insert
// ----------------------------------
2021-04-24 13:55:14 -07:00
const insertData = await pgInsert ( this . getNodeParameter , pgp , db , items , this . continueOnFail ( ) ) ;
2020-07-17 07:10:31 -07:00
for ( let i = 0 ; i < insertData . length ; i ++ ) {
returnItems . push ( {
2021-04-24 13:55:14 -07:00
json : insertData [ i ] ,
2020-07-17 07:10:31 -07:00
} ) ;
}
} else if ( operation === 'update' ) {
// ----------------------------------
// update
// ----------------------------------
2021-04-24 13:55:14 -07:00
const additionalFields = this . getNodeParameter ( 'additionalFields' , 0 ) as IDataObject ;
const mode = additionalFields . mode ? ? 'multiple' as string ;
2020-07-31 07:24:43 -07:00
2021-04-24 13:55:14 -07:00
if ( mode === 'independently' ) {
const updateItems = await pgUpdate ( this . getNodeParameter , pgp , db , items , this . continueOnFail ( ) ) ;
2020-07-31 07:24:43 -07:00
2021-04-24 13:55:14 -07:00
returnItems = this . helpers . returnJsonArray ( updateItems ) ;
} else if ( mode === 'multiple' ) {
// Crate db does not support multiple-update queries
// Therefore we cannot invoke `pgUpdate` using multiple mode
// so we have to call multiple updates manually here
2020-07-31 07:24:43 -07:00
2021-04-24 13:55:14 -07:00
const table = this . getNodeParameter ( 'table' , 0 ) as string ;
const schema = this . getNodeParameter ( 'schema' , 0 ) as string ;
const updateKeys = ( this . getNodeParameter ( 'updateKey' , 0 ) as string ) . split ( ',' ) . map ( column = > column . trim ( ) ) ;
const columns = ( this . getNodeParameter ( 'columns' , 0 ) as string ) . split ( ',' ) . map ( column = > column . trim ( ) ) ;
const queryColumns = columns . slice ( ) ;
2020-08-02 12:23:53 -07:00
2021-04-24 13:55:14 -07:00
updateKeys . forEach ( updateKey = > {
if ( ! queryColumns . includes ( updateKey ) ) {
columns . unshift ( updateKey ) ;
queryColumns . unshift ( '?' + updateKey ) ;
}
} ) ;
2020-07-31 07:24:43 -07:00
2021-04-24 13:55:14 -07:00
const cs = new pgp . helpers . ColumnSet ( queryColumns , { table : { table , schema } } ) ;
2020-08-02 23:21:42 -07:00
2021-04-24 13:55:14 -07:00
const where = ' WHERE ' + updateKeys . map ( updateKey = > pgp . as . name ( updateKey ) + ' = ${' + updateKey + '}' ) . join ( ' AND ' ) ;
// updateKeyValue = item.json[updateKey] as string | number;
// if (updateKeyValue === undefined) {
// throw new NodeOperationError(this.getNode(), 'No value found for update key!');
// }
2020-08-02 23:21:42 -07:00
2021-04-24 13:55:14 -07:00
const returning = generateReturning ( pgp , this . getNodeParameter ( 'returnFields' , 0 ) as string ) ;
const queries :string [ ] = [ ] ;
for ( let i = 0 ; i < items . length ; i ++ ) {
const itemCopy = getItemCopy ( items [ i ] , columns ) ;
queries . push ( pgp . helpers . update ( itemCopy , cs ) + pgp . as . format ( where , itemCopy ) + returning ) ;
}
const updateItems = await db . multi ( pgp . helpers . concat ( queries ) ) ;
returnItems = this . helpers . returnJsonArray ( getItemsCopy ( items , columns ) as IDataObject [ ] ) ;
}
2020-07-17 07:10:31 -07:00
} else {
await pgp . end ( ) ;
2021-04-16 09:33:36 -07:00
throw new NodeOperationError ( this . getNode ( ) , ` The operation " ${ operation } " is not supported! ` ) ;
2020-07-17 07:10:31 -07:00
}
// Close the connection
await pgp . end ( ) ;
return this . prepareOutputData ( returnItems ) ;
}
}