Created
June 30, 2022 23:48
-
-
Save fiote/90d81a800dc9d748ba89779c01d50d45 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const filters = { | |
tamanho: 'G', | |
cor: 'red' | |
}; | |
const validkeys = ['tamanho','cor']; | |
const wheres = []; | |
const params = []; | |
Object.keys(filters).forEach(key => { | |
if (!validkeys.includes(key)) return; | |
wheres.push(`${key} = ?`); | |
params.push(filters[key]); | |
}); | |
const result = await mysql.query(` | |
SELECT * | |
FROM camisas | |
WHERE ${wheres.join(' OR ')} | |
`, params); | |
const matches = result.rows.map(row => { | |
let qtmatches = 0; | |
Object.keys(filters).forEach(key => { | |
if (!validkeys.includes(key)) return; | |
if (row[key] == filters[key]) qtmatches++; | |
}); | |
return qtmatches; | |
}); | |
console.log(matches); | |
// [2,1,0]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment