2021-04-03 07:53:47 -07:00
const pgPromise = require ( 'pg-promise' ) ;
2023-04-03 08:18:01 -07:00
const PostgresFun = require ( '../../../nodes/Postgres/v1/genericFunctions' ) ;
2021-04-03 07:53:47 -07:00
2023-05-02 01:37:19 -07:00
type NodeParams = Record < string , string | {} > ;
2021-04-03 07:53:47 -07:00
describe ( 'pgUpdate' , ( ) = > {
it ( 'runs query to update db' , async ( ) = > {
2022-06-20 14:51:01 -07:00
const updateItem = { id : 1234 , name : 'test' } ;
2023-05-02 01:37:19 -07:00
const nodeParams : NodeParams = {
2021-04-03 07:53:47 -07:00
table : 'mytable' ,
schema : 'myschema' ,
updateKey : 'id' ,
2021-04-24 13:55:14 -07:00
columns : 'id,name' ,
additionalFields : { } ,
returnFields : '*' ,
2021-04-03 07:53:47 -07:00
} ;
2023-05-02 01:37:19 -07:00
const getNodeParam = ( key : string ) = > nodeParams [ key ] ;
2021-04-03 07:53:47 -07:00
const pgp = pgPromise ( ) ;
2021-04-24 13:55:14 -07:00
const any = jest . fn ( ) ;
2022-06-20 14:51:01 -07:00
const db = { any } ;
2021-04-03 07:53:47 -07:00
const items = [
{
2022-06-20 14:51:01 -07:00
json : updateItem ,
} ,
2021-04-03 07:53:47 -07:00
] ;
2022-06-20 14:51:01 -07:00
await PostgresFun . pgUpdate ( getNodeParam , pgp , db , items ) ;
2021-04-03 07:53:47 -07:00
2022-06-20 14:51:01 -07:00
expect ( db . any ) . toHaveBeenCalledWith (
2023-05-02 01:37:19 -07:00
'update "myschema"."mytable" as t set "id"=v."id","name"=v."name" from (values(1234,\'test\')) as v("id","name") WHERE v."id" = t."id" RETURNING *' ,
2022-06-20 14:51:01 -07:00
) ;
2021-04-03 07:53:47 -07:00
} ) ;
it ( 'runs query to update db if updateKey is not in columns' , async ( ) = > {
2022-06-20 14:51:01 -07:00
const updateItem = { id : 1234 , name : 'test' } ;
2023-05-02 01:37:19 -07:00
const nodeParams : NodeParams = {
2021-04-03 07:53:47 -07:00
table : 'mytable' ,
schema : 'myschema' ,
updateKey : 'id' ,
2021-04-24 13:55:14 -07:00
columns : 'name' ,
additionalFields : { } ,
returnFields : '*' ,
2021-04-03 07:53:47 -07:00
} ;
2023-05-02 01:37:19 -07:00
const getNodeParam = ( key : string ) = > nodeParams [ key ] ;
2021-04-03 07:53:47 -07:00
const pgp = pgPromise ( ) ;
2021-04-24 13:55:14 -07:00
const any = jest . fn ( ) ;
2022-06-20 14:51:01 -07:00
const db = { any } ;
2021-04-03 07:53:47 -07:00
const items = [
{
2022-06-20 14:51:01 -07:00
json : updateItem ,
} ,
2021-04-03 07:53:47 -07:00
] ;
2022-06-20 14:51:01 -07:00
const results = await PostgresFun . pgUpdate ( getNodeParam , pgp , db , items ) ;
2021-04-03 07:53:47 -07:00
2022-06-20 14:51:01 -07:00
expect ( db . any ) . toHaveBeenCalledWith (
2023-05-02 01:37:19 -07:00
'update "myschema"."mytable" as t set "id"=v."id","name"=v."name" from (values(1234,\'test\')) as v("id","name") WHERE v."id" = t."id" RETURNING *' ,
2022-06-20 14:51:01 -07:00
) ;
2021-04-03 07:53:47 -07:00
} ) ;
it ( 'runs query to update db with cast as updateKey' , async ( ) = > {
2022-06-20 14:51:01 -07:00
const updateItem = { id : '1234' , name : 'test' } ;
2023-05-02 01:37:19 -07:00
const nodeParams : NodeParams = {
2021-04-03 07:53:47 -07:00
table : 'mytable' ,
schema : 'myschema' ,
updateKey : 'id:uuid' ,
2021-04-24 13:55:14 -07:00
columns : 'name' ,
additionalFields : { } ,
returnFields : '*' ,
2021-04-03 07:53:47 -07:00
} ;
2023-05-02 01:37:19 -07:00
const getNodeParam = ( key : string ) = > nodeParams [ key ] ;
2021-04-03 07:53:47 -07:00
const pgp = pgPromise ( ) ;
2021-04-24 13:55:14 -07:00
const any = jest . fn ( ) ;
2022-06-20 14:51:01 -07:00
const db = { any } ;
2021-04-03 07:53:47 -07:00
const items = [
{
2022-06-20 14:51:01 -07:00
json : updateItem ,
} ,
2021-04-03 07:53:47 -07:00
] ;
2022-06-20 14:51:01 -07:00
await PostgresFun . pgUpdate ( getNodeParam , pgp , db , items ) ;
2021-04-03 07:53:47 -07:00
2022-06-20 14:51:01 -07:00
expect ( db . any ) . toHaveBeenCalledWith (
2023-05-02 01:37:19 -07:00
'update "myschema"."mytable" as t set "id"=v."id","name"=v."name" from (values(\'1234\'::uuid,\'test\')) as v("id","name") WHERE v."id" = t."id" RETURNING *' ,
2022-06-20 14:51:01 -07:00
) ;
2021-04-03 07:53:47 -07:00
} ) ;
it ( 'runs query to update db with cast in target columns' , async ( ) = > {
2022-06-20 14:51:01 -07:00
const updateItem = { id : '1234' , name : 'test' } ;
2023-05-02 01:37:19 -07:00
const nodeParams : NodeParams = {
2021-04-03 07:53:47 -07:00
table : 'mytable' ,
schema : 'myschema' ,
updateKey : 'id' ,
2021-04-24 13:55:14 -07:00
columns : 'id:uuid,name' ,
additionalFields : { } ,
returnFields : '*' ,
2021-04-03 07:53:47 -07:00
} ;
2023-05-02 01:37:19 -07:00
const getNodeParam = ( key : string ) = > nodeParams [ key ] ;
2021-04-03 07:53:47 -07:00
const pgp = pgPromise ( ) ;
2021-04-24 13:55:14 -07:00
const any = jest . fn ( ) ;
2022-06-20 14:51:01 -07:00
const db = { any } ;
2021-04-03 07:53:47 -07:00
const items = [
{
2022-06-20 14:51:01 -07:00
json : updateItem ,
} ,
2021-04-03 07:53:47 -07:00
] ;
2022-06-20 14:51:01 -07:00
await PostgresFun . pgUpdate ( getNodeParam , pgp , db , items ) ;
2021-04-03 07:53:47 -07:00
2022-06-20 14:51:01 -07:00
expect ( db . any ) . toHaveBeenCalledWith (
2023-05-02 01:37:19 -07:00
'update "myschema"."mytable" as t set "id"=v."id","name"=v."name" from (values(\'1234\'::uuid,\'test\')) as v("id","name") WHERE v."id" = t."id" RETURNING *' ,
2022-06-20 14:51:01 -07:00
) ;
2021-04-03 07:53:47 -07:00
} ) ;
} ) ;
describe ( 'pgInsert' , ( ) = > {
it ( 'runs query to insert' , async ( ) = > {
2022-06-20 14:51:01 -07:00
const insertItem = { id : 1234 , name : 'test' , age : 34 } ;
2023-05-02 01:37:19 -07:00
const nodeParams : NodeParams = {
2021-04-03 07:53:47 -07:00
table : 'mytable' ,
schema : 'myschema' ,
columns : 'id,name,age' ,
returnFields : '*' ,
2021-04-24 13:55:14 -07:00
additionalFields : { } ,
2021-04-03 07:53:47 -07:00
} ;
2023-05-02 01:37:19 -07:00
const getNodeParam = ( key : string ) = > nodeParams [ key ] ;
2021-04-03 07:53:47 -07:00
const pgp = pgPromise ( ) ;
2021-04-24 13:55:14 -07:00
const any = jest . fn ( ) ;
2022-06-20 14:51:01 -07:00
const db = { any } ;
2021-04-03 07:53:47 -07:00
const items = [
{
json : insertItem ,
} ,
] ;
2021-04-24 13:55:14 -07:00
await PostgresFun . pgInsert ( getNodeParam , pgp , db , items ) ;
2021-04-03 07:53:47 -07:00
2022-06-20 14:51:01 -07:00
expect ( db . any ) . toHaveBeenCalledWith (
2023-05-02 01:37:19 -07:00
'insert into "myschema"."mytable"("id","name","age") values(1234,\'test\',34) RETURNING *' ,
2022-06-20 14:51:01 -07:00
) ;
2021-04-03 07:53:47 -07:00
} ) ;
it ( 'runs query to insert with type casting' , async ( ) = > {
2022-06-20 14:51:01 -07:00
const insertItem = { id : 1234 , name : 'test' , age : 34 } ;
2023-05-02 01:37:19 -07:00
const nodeParams : NodeParams = {
2021-04-03 07:53:47 -07:00
table : 'mytable' ,
schema : 'myschema' ,
columns : 'id:int,name:text,age' ,
returnFields : '*' ,
2021-04-24 13:55:14 -07:00
additionalFields : { } ,
2021-04-03 07:53:47 -07:00
} ;
2023-05-02 01:37:19 -07:00
const getNodeParam = ( key : string ) = > nodeParams [ key ] ;
2021-04-03 07:53:47 -07:00
const pgp = pgPromise ( ) ;
2021-04-24 13:55:14 -07:00
const any = jest . fn ( ) ;
2022-06-20 14:51:01 -07:00
const db = { any } ;
2021-04-03 07:53:47 -07:00
const items = [
{
json : insertItem ,
} ,
] ;
2021-04-24 13:55:14 -07:00
await PostgresFun . pgInsert ( getNodeParam , pgp , db , items ) ;
2021-04-03 07:53:47 -07:00
2022-06-20 14:51:01 -07:00
expect ( db . any ) . toHaveBeenCalledWith (
2023-05-02 01:37:19 -07:00
'insert into "myschema"."mytable"("id","name","age") values(1234::int,\'test\'::text,34) RETURNING *' ,
2022-06-20 14:51:01 -07:00
) ;
2021-04-03 07:53:47 -07:00
} ) ;
} ) ;