mirror of
https://github.com/typicode/json-server.git
synced 2025-07-28 04:32:24 +08:00
Add _expand parameter when retrieving single resource
This commit is contained in:
@ -79,6 +79,12 @@ To embed other resources, add `_embed`.
|
||||
GET /posts/1?_embed=comments
|
||||
```
|
||||
|
||||
To expand related resources, add `_expand`.
|
||||
|
||||
```
|
||||
GET /comments/1?_expand=posts
|
||||
```
|
||||
|
||||
Returns database.
|
||||
|
||||
```
|
||||
|
@ -159,6 +159,7 @@ module.exports = function (source) {
|
||||
// GET /:resource/:id
|
||||
function show (req, res, next) {
|
||||
var _embed = req.query._embed
|
||||
var _expand = req.query._expand
|
||||
var id = utils.toNative(req.params.id)
|
||||
var resource = db(req.params.resource)
|
||||
.getById(id)
|
||||
@ -168,6 +169,7 @@ module.exports = function (source) {
|
||||
resource = _.cloneDeep(resource)
|
||||
// Always use an array
|
||||
_embed = _.isArray(_embed) ? _embed : [_embed]
|
||||
_expand = _.isArray(_expand) ? _expand : [_expand]
|
||||
|
||||
// Embed other resources based on resource id
|
||||
_embed.forEach(function (otherResource) {
|
||||
@ -183,6 +185,19 @@ module.exports = function (source) {
|
||||
}
|
||||
})
|
||||
|
||||
// Expand inner resources based on id
|
||||
_expand.forEach(function (innerResource) {
|
||||
|
||||
if (innerResource
|
||||
&& innerResource.trim().length > 0
|
||||
&& db.object[innerResource]) {
|
||||
var query = {}
|
||||
var prop = pluralize.singular(innerResource) + 'Id'
|
||||
query.id = resource[prop]
|
||||
resource[innerResource] = db(innerResource).where(query)
|
||||
}
|
||||
})
|
||||
|
||||
res.locals.data = resource
|
||||
} else {
|
||||
res.status(404)
|
||||
|
@ -24,12 +24,17 @@ describe('Server', function () {
|
||||
{id: 3, body: 'photo'}
|
||||
]
|
||||
|
||||
db.users = [
|
||||
{id: 1, username: 'Jim'},
|
||||
{id: 2, username: 'George'}
|
||||
]
|
||||
|
||||
db.comments = [
|
||||
{id: 1, published: true, postId: 1},
|
||||
{id: 2, published: false, postId: 1},
|
||||
{id: 3, published: false, postId: 2},
|
||||
{id: 4, published: false, postId: 2},
|
||||
{id: 5, published: false, postId: 2}
|
||||
{id: 1, published: true, postId: 1, userId: 1},
|
||||
{id: 2, published: false, postId: 1, userId: 2},
|
||||
{id: 3, published: false, postId: 2, userId: 1},
|
||||
{id: 4, published: false, postId: 2, userId: 2},
|
||||
{id: 5, published: false, postId: 2, userId: 1}
|
||||
]
|
||||
|
||||
db.refs = [
|
||||
@ -242,6 +247,31 @@ describe('Server', function () {
|
||||
})
|
||||
})
|
||||
|
||||
describe('GET /:resource/:id?_expand=', function () {
|
||||
it('should respond with corresponding resource and expanded inner resources', function (done) {
|
||||
var comments = db.comments[0]
|
||||
comments.posts = [db.posts[0]]
|
||||
request(server)
|
||||
.get('/comments/1?_expand=posts')
|
||||
.expect('Content-Type', /json/)
|
||||
.expect(comments)
|
||||
.expect(200, done)
|
||||
})
|
||||
})
|
||||
|
||||
describe('GET /:resource/:id?_expand=&_expand=', function () {
|
||||
it('should respond with corresponding resource and expanded inner resources', function (done) {
|
||||
var comments = db.comments[0]
|
||||
comments.posts = [db.posts[0]]
|
||||
comments.users = [db.users[0]]
|
||||
request(server)
|
||||
.get('/comments/1?_expand=posts&_expand=users')
|
||||
.expect('Content-Type', /json/)
|
||||
.expect(comments)
|
||||
.expect(200, done)
|
||||
})
|
||||
})
|
||||
|
||||
describe('POST /:resource', function () {
|
||||
it('should respond with json, create a resource and increment id',
|
||||
function (done) {
|
||||
|
Reference in New Issue
Block a user