DEV Community

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

Posted on

2

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.

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

nextjs tutorial video

Youtube Tutorial Series 📺

So you built a Next.js app, but you need a clear view of the entire operation flow to be able to identify performance bottlenecks before you launch. But how do you get started? Get the essentials on tracing for Next.js from @nikolovlazar in this video series 👀

Watch the Youtube series

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay