Created
April 22, 2020 04:58
-
-
Save palanisamym14/42dcdd9b7b504fc73c19f04ad39bd72f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let query = 'select * from data where id=:id and test=:test'; | |
let data = { | |
id:1, | |
test:11, | |
} | |
let data1 = { | |
id:1 | |
} | |
const validateQueryInput= (query:any, data:any):Promise<any> => { | |
let objectList = query.split(':'); | |
let values:any[]= []; | |
let missingKeys :any[]= []; | |
return new Promise((resolve, reject) => { | |
for (let i = 1; i < objectList.length; i++) { | |
const key = objectList[i].split(' ')[0]; | |
var myRegExp = new RegExp(`:${key}`,'i'); | |
query = query.replace(myRegExp,'?'); | |
data[key] === undefined?missingKeys.push(key):values.push(data[key]) | |
} | |
console.log(query, values) | |
console.log(missingKeys.toString()) | |
missingKeys.length?reject(missingKeys):resolve({query, values}); | |
}); | |
} | |
validateQueryInput(query, data).then(e=> console.log(e)).catch(err=>console.log(err)) | |
validateQueryInput(query, data1).then(e=> console.log(e)).catch(err=>console.log(err)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Format key value pair into array values in mysql query