7

I have fake api for testing in frontend side.

i have seen that id is required to put or post your data in json-server package, my question is can i use different key instead of id for ex.

{
  id: 1, ---> i want to change this with my custom id
  name: 'Test'
} 
Tameshwar
  • 789
  • 1
  • 10
  • 17

4 Answers4

10

Let's see CLI options of json-server package: $ json-server -h

...
--id, -i   Set database id property (e.g. _id)   [default: "id"]
...

Let's try to start json-server with new id called 'customId' (for example): json-server --id customId testDb.json

Structure of testDb.json file: $ cat testDb.json

{
  "messages": [
    {
      "customId": 1,
      "description": "somedescription",
      "body": "sometext"
    }
  ]
}

Make a simple POST request via $.ajax function (or via Fiddler/Postman/etc.). Content-type of request should be set to application/json - explanation may be found on this project's github page:

A POST, PUT or PATCH request should include a Content-Type: application/json header to use the JSON in the request body. Otherwise it will result in a 200 OK but without changes being made to the data.

So... Make a request from Browser:

$.ajax({
  type: "POST",
  url: 'http://127.0.0.1:3000/messages/',
  data: {body: 'body', description: 'description'},
  success: resp => console.log(resp),
  dataType: 'json'
});

Go to testDb and see the results. New chunk added. id automatically added with the desired name specified in --id key of console cmd.

{ "body": "body", "description": "description", "customId": 12 }

Voila!

dmtrd
  • 101
  • 4
  • Can we update another property as auto increment like id? 'id':1, 'layerNumber':1 Can we updated layerNumber with id's value on save? – Cegone Aug 28 '19 at 14:41
  • 3
    Does the --id attribute set it for every table? Is it possible to have it per table? – Jarrod McGuire Aug 26 '20 at 09:49
5

I've came up with using custom routes in cases where I need custom id: json-server --watch db.json --routes routes.json

routes.json:

{ "/customIDroute/:cusomID" : "/customIDroute?cusomID=:cusomID" }
Eugene Zhukov
  • 71
  • 1
  • 5
2

If you start your server using a server.js file (read more about it in the docs), you can define custom ID routes in server.js like this

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

server.use(middlewares)

// custom routes
server.use(jsonServer.rewriter({
  "/route/:id": "/route?customId=:id"
}))

server.use(router)
server.listen(3000, () => {
  console.log('JSON Server is running')
})

And you would start your server with command:

node server.js
njuki
  • 47
  • 2
  • 6
2

Internaly getById from lodash-id is used.

If you use file-based server version the equivalent to cli --id, -i is router.db._.id = "customId";

If you want to do per resource, you can do it with a middleware like this (put before others):

server.use((req, res, next) => {
    if (req.url.includes("/resourceName/")) {
        router.db._.id = "code";
    } else {
        router.db._.id = "pk";
    }
    next();
});
Boris
  • 79
  • 2