Json-server: Possible to default from "id" to "_id"?

Created on 25 May 2015  路  11Comments  路  Source: typicode/json-server

This would be handy for example mongodb/couchdb servers.

It would be nice to make this configurable.

Most helpful comment

@akymtl It's in the readme, under "cli usage", listed as one of the options; specifically the line:
--id, -i Set database id property (e.g. _id) [default: "id"].

All 11 comments

Not right now.
I agree, I'll see what can be done.

I would definitely geek out over having this available; emphasis on the configurability.

:grinning:

Just released some new features and it's possible now :)

json-server --id _id --watch db.json 

W00t! Thank you.

Awesome, thanks for this.

You're welcome ;)

Thank you!

@typicode It's work by the commond --id _id and json-server.json file, But i use json-server as a module,and how to config by configuration file where to put the file,
{ "id":"_id", "port":3000 }
can you help me? thanks!

@fresherchen

I was also wondering how to do that when using json-server as a module.

I needed:

  • --id _id
  • --foreignKeySuffix _id

(MongoDB style)

Here is my JSON Server:

const path = require('path');
const jsonServer = require('json-server')
const data = require('./server.json')

const app = jsonServer.create()
const port = process.env.PORT || 3000

const router = jsonServer.router(data, {foreignKeySuffix: '_id'})
router.db._.id = '_id';

console.log(path.join(__dirname, '../../dist'));

app.use(jsonServer.defaults({
    logger: process.env.NODE_ENV !== 'production',
    static: path.join(__dirname, '../../dist')
}))

app.use(router)

app.listen(port, () => {
  console.log('JSONPlaceholder listening on http://localhost:' + port)
})

This allowed me to properly use relationships and _expand such as in:

GET http://localhost:3000/categories/3/albums
GET http://localhost:3000/albums/5?_expand=category

Could t

Just released some new features and it's possible now :)

json-server --id _id --watch db.json 

Could this be mentioned in the README.md as well at someplace, if possible.

@akymtl It's in the readme, under "cli usage", listed as one of the options; specifically the line:
--id, -i Set database id property (e.g. _id) [default: "id"].

Was this page helpful?
0 / 5 - 0 ratings