Skip to content

Instantly share code, notes, and snippets.

@palanisamym14
Created April 22, 2020 04:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save palanisamym14/42dcdd9b7b504fc73c19f04ad39bd72f to your computer and use it in GitHub Desktop.
Save palanisamym14/42dcdd9b7b504fc73c19f04ad39bd72f to your computer and use it in GitHub Desktop.
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))
@palanisamym14
Copy link
Author

Format key value pair into array values in mysql query

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment