2021-06-03 17:31:24 +02:00
const Sql = require ( "../base/sql" )
const TABLE _NAME = "test"
function endpoint ( table , operation ) {
return {
datasourceId : "Postgres" ,
operation : operation ,
entityId : table || TABLE _NAME ,
}
}
function generateReadJson ( { table , fields , filters , sort , paginate } = { } ) {
return {
endpoint : endpoint ( table || TABLE _NAME , "READ" ) ,
resource : {
fields : fields || [ ] ,
} ,
filters : filters || { } ,
sort : sort || { } ,
paginate : paginate || { } ,
}
}
function generateCreateJson ( table = TABLE _NAME , body = { } ) {
return {
endpoint : endpoint ( table , "CREATE" ) ,
body ,
}
}
function generateUpdateJson ( table = TABLE _NAME , body = { } , filters = { } ) {
return {
endpoint : endpoint ( table , "UPDATE" ) ,
filters ,
body ,
}
}
function generateDeleteJson ( table = TABLE _NAME , filters = { } ) {
return {
endpoint : endpoint ( table , "DELETE" ) ,
filters ,
}
}
describe ( "SQL query builder" , ( ) => {
const limit = 500
const client = "pg"
let sql
beforeEach ( ( ) => {
sql = new Sql ( client , limit )
} )
it ( "should test a basic read" , ( ) => {
2021-06-03 18:45:19 +02:00
const query = sql . _query ( generateReadJson ( ) )
expect ( query ) . toEqual ( {
bindings : [ limit ] ,
2021-09-23 17:56:13 +02:00
sql : ` select * from (select * from " ${ TABLE _NAME } " limit $ 1) as " ${ TABLE _NAME } " `
2021-06-03 18:45:19 +02:00
} )
2021-06-03 17:31:24 +02:00
} )
it ( "should test a read with specific columns" , ( ) => {
2021-08-06 13:33:04 +02:00
const nameProp = ` ${ TABLE _NAME } .name ` , ageProp = ` ${ TABLE _NAME } .age `
2021-06-03 18:45:19 +02:00
const query = sql . _query ( generateReadJson ( {
2021-08-06 13:33:04 +02:00
fields : [ nameProp , ageProp ]
2021-06-03 17:31:24 +02:00
} ) )
2021-06-03 18:45:19 +02:00
expect ( query ) . toEqual ( {
bindings : [ limit ] ,
2021-09-23 17:56:13 +02:00
sql : ` select " ${ TABLE _NAME } "."name" as " ${ nameProp } ", " ${ TABLE _NAME } "."age" as " ${ ageProp } " from (select * from " ${ TABLE _NAME } " limit $ 1) as " ${ TABLE _NAME } " `
2021-06-03 18:45:19 +02:00
} )
2021-06-03 17:31:24 +02:00
} )
it ( "should test a where string starts with read" , ( ) => {
2021-06-03 18:45:19 +02:00
const query = sql . _query ( generateReadJson ( {
2021-06-03 17:31:24 +02:00
filters : {
string : {
name : "John" ,
}
}
} ) )
2021-06-03 18:45:19 +02:00
expect ( query ) . toEqual ( {
bindings : [ "John%" , limit ] ,
2021-09-23 17:56:13 +02:00
sql : ` select * from (select * from " ${ TABLE _NAME } " where " ${ TABLE _NAME } "."name" ilike $ 1 limit $ 2) as " ${ TABLE _NAME } " `
2021-06-03 18:45:19 +02:00
} )
2021-06-03 17:31:24 +02:00
} )
it ( "should test a where range read" , ( ) => {
2021-06-03 18:45:19 +02:00
const query = sql . _query ( generateReadJson ( {
2021-06-03 17:31:24 +02:00
filters : {
range : {
age : {
low : 2 ,
high : 10 ,
}
}
}
} ) )
2021-06-03 18:45:19 +02:00
expect ( query ) . toEqual ( {
bindings : [ 2 , 10 , limit ] ,
2021-09-23 17:56:13 +02:00
sql : ` select * from (select * from " ${ TABLE _NAME } " where " ${ TABLE _NAME } "."age" between $ 1 and $ 2 limit $ 3) as " ${ TABLE _NAME } " `
2021-06-03 18:45:19 +02:00
} )
2021-06-03 17:31:24 +02:00
} )
2021-06-15 14:03:55 +02:00
it ( "should test for multiple IDs with OR" , ( ) => {
const query = sql . _query ( generateReadJson ( {
filters : {
equal : {
age : 10 ,
name : "John" ,
} ,
allOr : true ,
}
} ) )
expect ( query ) . toEqual ( {
bindings : [ 10 , "John" , limit ] ,
2021-09-23 17:56:13 +02:00
sql : ` select * from (select * from " ${ TABLE _NAME } " where (" ${ TABLE _NAME } "."age" = $ 1) or (" ${ TABLE _NAME } "."name" = $ 2) limit $ 3) as " ${ TABLE _NAME } " `
2021-06-15 14:03:55 +02:00
} )
} )
2021-06-03 17:31:24 +02:00
it ( "should test an create statement" , ( ) => {
2021-06-03 18:45:19 +02:00
const query = sql . _query ( generateCreateJson ( TABLE _NAME , {
2021-06-03 17:31:24 +02:00
name : "Michael" ,
age : 45 ,
} ) )
2021-06-03 18:45:19 +02:00
expect ( query ) . toEqual ( {
bindings : [ 45 , "Michael" ] ,
2021-06-17 15:42:30 +02:00
sql : ` insert into " ${ TABLE _NAME } " ("age", "name") values ( $ 1, $ 2) returning * `
2021-06-03 18:45:19 +02:00
} )
2021-06-03 17:31:24 +02:00
} )
it ( "should test an update statement" , ( ) => {
2021-06-03 18:45:19 +02:00
const query = sql . _query ( generateUpdateJson ( TABLE _NAME , {
2021-06-03 17:31:24 +02:00
name : "John"
} , {
equal : {
id : 1001 ,
}
} ) )
2021-06-03 18:45:19 +02:00
expect ( query ) . toEqual ( {
bindings : [ "John" , 1001 ] ,
2021-07-02 15:49:47 +02:00
sql : ` update " ${ TABLE _NAME } " set "name" = $ 1 where " ${ TABLE _NAME } "."id" = $ 2 returning * `
2021-06-03 18:45:19 +02:00
} )
2021-06-03 17:31:24 +02:00
} )
it ( "should test a delete statement" , ( ) => {
2021-06-03 18:45:19 +02:00
const query = sql . _query ( generateDeleteJson ( TABLE _NAME , {
2021-06-03 17:31:24 +02:00
equal : {
id : 1001 ,
}
} ) )
2021-06-03 18:45:19 +02:00
expect ( query ) . toEqual ( {
bindings : [ 1001 ] ,
2021-07-02 15:49:47 +02:00
sql : ` delete from " ${ TABLE _NAME } " where " ${ TABLE _NAME } "."id" = $ 1 returning * `
2021-06-03 18:45:19 +02:00
} )
2021-06-03 17:31:24 +02:00
} )
2021-06-03 17:45:43 +02:00
it ( "should work with MS-SQL" , ( ) => {
2021-06-03 18:45:19 +02:00
const query = new Sql ( "mssql" , 10 ) . _query ( generateReadJson ( ) )
expect ( query ) . toEqual ( {
bindings : [ 10 ] ,
2021-09-23 17:56:13 +02:00
sql : ` select * from (select top (@p0) * from [ ${ TABLE _NAME } ]) as [ ${ TABLE _NAME } ] `
2021-06-03 18:45:19 +02:00
} )
2021-06-03 17:45:43 +02:00
} )
it ( "should work with mySQL" , ( ) => {
2021-06-03 18:45:19 +02:00
const query = new Sql ( "mysql" , 10 ) . _query ( generateReadJson ( ) )
expect ( query ) . toEqual ( {
bindings : [ 10 ] ,
2021-09-23 17:56:13 +02:00
sql : ` select * from (select * from \` ${ TABLE _NAME } \` limit ?) as \` ${ TABLE _NAME } \` `
2021-06-03 18:45:19 +02:00
} )
2021-06-03 17:45:43 +02:00
} )
} )