Skip to content

Query for null #682

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
marukx opened this issue Nov 17, 2017 · 3 comments
Open

Query for null #682

marukx opened this issue Nov 17, 2017 · 3 comments

Comments

@marukx
Copy link

marukx commented Nov 17, 2017

This is a question, something that i didn't manage to find in documentation.

However it seems like a basic functionality and I'm sure that there must be a simple solution/workaround.

I've got a requirement to get all records where date is null, tried

http://localhost:3000/entity_name?date=null

with no luck as it does not return any records.

Had a look at the following issue and i can see that author suggested adding new _is_null operator.
#510

However not sure if this was added.

Any help would be appreciated.

MK

@Nilegfx
Copy link
Contributor

Nilegfx commented Dec 2, 2017

as a temporary solution until this feature merged, you could use json-server as nodejs module not CLI (command line) and override router.render method with the filtration logic

server.js

const jsonServer = require('json-server');
const server = jsonServer.create();
const router = jsonServer.router('db.json');
const middlewares = jsonServer.defaults();

router.render = (req, res) => {
   let data = res.locals.data;

   // a very dirty way to check if there is a filter by date and the response is an array
   if(req.query.date_is_null && typeof data !== 'function' && data.length ) {
     res.send(data.filter(d => !d.date))
   }
}

//default json-server middlewares
server.use(middlewares);

// json-server's magic
server.use(router);

server.listen(3000, () => {
  console.log('http://localhost:3000');
});

run it:

node server.js

Please test the code as I wrote it without running it, so it could have some syntax errors

@marukx
Copy link
Author

marukx commented Dec 28, 2017

thanks will do that

@Kop3sh
Copy link

Kop3sh commented Jul 18, 2020

as a temporary solution until this feature merged, you could use json-server as nodejs module not CLI (command line) and override router.render method with the filtration logic

server.js

const jsonServer = require('json-server');
const server = jsonServer.create();
const router = jsonServer.router('db.json');
const middlewares = jsonServer.defaults();

router.render = (req, res) => {
   let data = res.locals.data;

   // a very dirty way to check if there is a filter by date and the response is an array
   if(req.query.date_is_null && typeof data !== 'function' && data.length ) {
     res.send(data.filter(d => !d.date))
   }
}

//default json-server middlewares
server.use(middlewares);

// json-server's magic
server.use(router);

server.listen(3000, () => {
  console.log('http://localhost:3000');
});

run it:

node server.js

Please test the code as I wrote it without running it, so it could have some syntax errors

could you please elaborate more?

So I tried creating a new file called server.js in the same directory where the db is located and copied the above code and ran the cammand node server.js in the same dir
I got this error

internal/modules/cjs/loader.js:638
throw err;
^

Error: Cannot find module 'json-server'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
at Function.Module._load (internal/modules/cjs/loader.js:562:25)
at Module.require (internal/modules/cjs/loader.js:692:17)
at require (internal/modules/cjs/helpers.js:25:18)
at Object. (/home/ti5a/dev/vlum/fake/server.js:1:20)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants