Purpose
This package can be use to build a mysql query from json.
Usage:
var json2sql=require('npm-json2sql');
myjson = {
"filters": { //you can add multiple column for filtering
"transaction_number": { //transaction_number is the column name
"value": "56",
"matchMode": "contains" //can be equal== '=' and contains== 'LIKE'
},
"supplier_id": {
"value": "2",
"matchMode": "equal"
},
"supplier_name": {
"value": "jack",
"matchMode": "contains"
}
},
//**Sorting and Pagination are optional features**
"first": 0, // Page Number
"rows": 10, // Num of Rows per page
"sortField": "total_amount", //Order by Field
"sortOrder": 1 //0==ASC and 1==DESC
};
console.log(json2sql.json2sql(myjson,"transactions"));
Output:
Select * from transactions
WHERE transaction_number
LIKE '%56%' AND supplier_id
= '2' AND supplier_name
LIKE '%jack%' ORDER BY total_amount
ASC limit 10 OFFSET 0