From f8cd38564ac27d44d7605d71abce41b63c7bd4f6 Mon Sep 17 00:00:00 2001 From: Eric Adams Date: Fri, 19 Jun 2015 12:39:55 -0400 Subject: [PATCH] Add _expand parameter when retrieving single resource --- README.md | 6 ++++++ src/router.js | 15 +++++++++++++++ test/index.js | 40 +++++++++++++++++++++++++++++++++++----- 3 files changed, 56 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index f8895b4..fa93499 100644 --- a/README.md +++ b/README.md @@ -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. ``` diff --git a/src/router.js b/src/router.js index 1472c99..463cb1d 100644 --- a/src/router.js +++ b/src/router.js @@ -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) diff --git a/test/index.js b/test/index.js index abc43b0..65d1bf7 100644 --- a/test/index.js +++ b/test/index.js @@ -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) {