json-server
json-server copied to clipboard
Add alias, _split, effect function
_split
To split children resources, add _split
POST /posts?_split=comments
PUT /posts/1?_split=comments
PATCH /posts/1?_split=comments
Request body
{ "id": 1, "title": "json-server", "author": "typicode",
"comments": [ { "body": "some comment" } ]
}
Alias and effect function
const router = jsonServer.router(jsonFile, {
resourceAlias: {
'creator': 'user'
},
effectWhenCreate ({data, req}) {
data.created_at =
data.updated_at = Date.now()
data.creator_id = req.session.user.id || ''
return data
},
effectWhenUpdate({data}) {
data.updated_at = Date.now()
return data
},
});