typicode / json-server

Get a full fake REST API with zero coding in less than 30 seconds (seriously)
Other
72.83k stars 7.02k forks source link

fetch value from db.json #401

Open abhi-rana opened 7 years ago

abhi-rana commented 7 years ago

how can i get values from multiple resource stored in db.json ?

Nilegfx commented 7 years ago

very simple example to give you an idea

giving the following in db.json

{
  "posts": [
    { "title": "post title1", "comment": "post comment1" },
    { "title": "post title2", "comment": "post comment2" },
    { "title": "post title3", "comment": "post comment3" },
    { "title": "post title4", "comment": "post comment4" },
    { "title": "post title5", "comment": "post comment5" }
  ],
  "users": [
    { "name": "user1", "age": 21 },
    { "name": "user2", "age": 22 },
    { "name": "user3", "age": 23 },
    { "name": "user4", "age": 24 },
    { "name": "user5", "age": 25 }
  ],
  "comments": [
    { "id": 1, "content": "content 1" },
    { "id": 2, "content": "content 2" },
    { "id": 3, "content": "content 3" },
    { "id": 4, "content": "content 4" },
    { "id": 5, "content": "content 5" }
  ]

}
// server.js
var jsonServer = require('json-server')
var server = jsonServer.create()
var router = jsonServer.router('db.json')
var middlewares = jsonServer.defaults()

// create a custom middleware that looks for 
// any request that has querystring `getall`
server.use(function(req, res, next) {
  if (req.query['getall']) {
    var resourcesArr = req.query['getall'].split(","),
      db = router.db, //lowdb instance
      allResources; //to store the aggregated resources

    allResources = resourcesArr.reduce(function(accumulator, resource) {
       // you can use any lodash api to dig deeper
      // in this example we will use the simple `get`
      // read more here https://lodash.com/docs/4.16.6
      accumulator[resource] = db.get(resource).value()
      return accumulator;
    }, {});
    res.json(allResources);
  } else {
  //proceed normally if no `getall` querystring
    next()
  }
})
server.use(middlewares)
server.use(router)

server.listen(3000, function() {
  console.log('JSON Server is running')
})

make a request like /?getall=comments,users

you will get

{
  "comments": [
    {
      "id": 1,
      "content": "content 1"
    },
    {
      "id": 2,
      "content": "content 2"
    },
    {
      "id": 3,
      "content": "content 3"
    },
    {
      "id": 4,
      "content": "content 4"
    },
    {
      "id": 5,
      "content": "content 5"
    }
  ],
  "users": [
    {
      "name": "user1",
      "age": 21
    },
    {
      "name": "user2",
      "age": 22
    },
    {
      "name": "user3",
      "age": 23
    },
    {
      "name": "user4",
      "age": 24
    },
    {
      "name": "user5",
      "age": 25
    }
  ]
}

router.db is an instance of lowdb which is powered by lodash API

Please note that this is a very basic example to demonstrate the idea

limsim commented 7 years ago

Thanks @Nilegfx this example was a life saver. My js foo is weak and I tried to access router.db directly and it didn't work e.g. router.db.get

Nilegfx commented 7 years ago

do you mean it still not working? if so, please post your code here.