Add _expand parameter when retrieving single resource

This commit is contained in:
Eric Adams
2015-06-19 12:39:55 -04:00
parent 682b1bca14
commit f8cd38564a
3 changed files with 56 additions and 5 deletions

View File

@ -79,6 +79,12 @@ To embed other resources, add `_embed`.
GET /posts/1?_embed=comments GET /posts/1?_embed=comments
``` ```
To expand related resources, add `_expand`.
```
GET /comments/1?_expand=posts
```
Returns database. Returns database.
``` ```

View File

@ -159,6 +159,7 @@ module.exports = function (source) {
// GET /:resource/:id // GET /:resource/:id
function show (req, res, next) { function show (req, res, next) {
var _embed = req.query._embed var _embed = req.query._embed
var _expand = req.query._expand
var id = utils.toNative(req.params.id) var id = utils.toNative(req.params.id)
var resource = db(req.params.resource) var resource = db(req.params.resource)
.getById(id) .getById(id)
@ -168,6 +169,7 @@ module.exports = function (source) {
resource = _.cloneDeep(resource) resource = _.cloneDeep(resource)
// Always use an array // Always use an array
_embed = _.isArray(_embed) ? _embed : [_embed] _embed = _.isArray(_embed) ? _embed : [_embed]
_expand = _.isArray(_expand) ? _expand : [_expand]
// Embed other resources based on resource id // Embed other resources based on resource id
_embed.forEach(function (otherResource) { _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 res.locals.data = resource
} else { } else {
res.status(404) res.status(404)

View File

@ -24,12 +24,17 @@ describe('Server', function () {
{id: 3, body: 'photo'} {id: 3, body: 'photo'}
] ]
db.users = [
{id: 1, username: 'Jim'},
{id: 2, username: 'George'}
]
db.comments = [ db.comments = [
{id: 1, published: true, postId: 1}, {id: 1, published: true, postId: 1, userId: 1},
{id: 2, published: false, postId: 1}, {id: 2, published: false, postId: 1, userId: 2},
{id: 3, published: false, postId: 2}, {id: 3, published: false, postId: 2, userId: 1},
{id: 4, published: false, postId: 2}, {id: 4, published: false, postId: 2, userId: 2},
{id: 5, published: false, postId: 2} {id: 5, published: false, postId: 2, userId: 1}
] ]
db.refs = [ 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 () { describe('POST /:resource', function () {
it('should respond with json, create a resource and increment id', it('should respond with json, create a resource and increment id',
function (done) { function (done) {