json-server icon indicating copy to clipboard operation
json-server copied to clipboard

Add alias, _split, effect function

Open zuojiang opened this issue 5 years ago • 0 comments

_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
  },
});

zuojiang avatar Nov 21 '19 06:11 zuojiang