DEV Community

Nhân Quách
Nhân Quách

Posted on

Does any one knows the Magic behind the Query languages?

Hi guys/girls,
Recently I have been assigned a task which I need to translate 2, 3 rules of mapping logic from Speaking language into JavaScript expression and conditions.
The way I managed to make it works is defining a bunch of possible answers then write many ifs statements. But this seem to be quite hard for both the clients who want to change the mapping logic later on and me who need to maintain and explain to other members of the team.

answer1 answer2 result
Yes No 1
No Yes 2

mapping json

  "answer1": "Yes",
  "answer2": "No",
  "result": 1
}, {
  "answer1": "No",
  "answer2": "Yes",
  "result": 2
// FE will send to BE an array of answers and BE needs to return the result

// This is terrible for scaling or dynamic mapping logic 
if(answer1 === 'Yes' && answer2 === 'No') {
  return 1;
} else if(answer1 === 'No' && answer2 === 'Yes') {
  return 2

This really get me thinking about the magic behind of the SQL/non-SQL query languages, I really admire them.

What if the result is not just a number, may be it is another rule? How can we define a rule in json format (e.g: if the result is 1, I want to get all the odd numbers that is greater than 10) then read and execute it in JavaScript?

I wonder if anyone in the community can give me a hint/a solution for this?

Thank you so much.

Top comments (0)