0

I would like something similar to what node-odata offers, but I do not want to wrap it around my database (I am using Cassandra and already have an Express app set up with routes, etc).

Currently, I grab data from the database (which will ultimately return a JSON object to the user) and then using the values passed in the query string I modify the results with JavaScript and pass the modified JSON object on through to the user.

I cannot pass in a query string like this http://localhost:3001/getSomeData?name=jim&age=21||eyeColor=red which includes logical operators in the query string, and would grab all data and filter it where the name is "jim", the age is "21" OR eyeColor is "red". So this would give me all Jims that have either eyeColor red and/or age of 21. If I used this age=21&&eyeColor=red I would expect to get all Jims that have BOTH eye color of red and are 21 years old.

I was thinking of using a custom query string that can be passed in (i.e. inclusive=age&inclusive=eyeColor appended at the end of the query string) and in Node, I would modify the filter results to treat these properties (age and eyeColor) as if they were passed in with the || OR operator). However, this is quite verbose, and I was hoping there was a library or another simpler implementation out there that solves this problem, or somehow lets me pass in simple logical operators into the query string.

2
  • 2
    The mistake you're making is trying to change how the query string works. Instead, pass the value you want to parse in the query string such as ?query=name%3Djim%26age%3D21%7C%7CeyeColor%3Dred and then you can access that value and parse it yourself however you'd like. Commented Dec 20, 2017 at 21:19
  • @zzzzBov Okay, that makes sense, thanks for reply! Would you happen to know of a library that can parse and filter strings like that (I couldn't find any). Otherwise, I will write my own logic to handle the query string like you suggested. Commented Dec 21, 2017 at 15:38

2 Answers 2

1

I ended up using this library to achieve what I wanted: https://www.npmjs.com/package/jspath

It's well document and worked perfectly for my situation.

Sign up to request clarification or add additional context in comments.

Comments

0
npm i querystringify  //or
https://cdnjs.cloudflare.com/ajax/libs/qs/6.7.0/qs.min.js


//it will will return an object
const myObject = Qs.parse(location.search, {ignoreQueryPrefix: true}); 
//you can use object destructuring.
const {age,eyeColor}=Qs.parse(location.search, {ignoreQueryPrefix: true})

By default, parsing will include "?" too.

{ignoreQueryPrefix: true} this option will omit "?".

1 Comment

Hey @Yilmaz, thanks for the reply. I had forgotten to update this question when I wrote it over a year ago. I found a suitable NPM package, please see my accepted answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.