Skip to content

Commit f8cd385

Browse files
committed
Add _expand parameter when retrieving single resource
1 parent 682b1bc commit f8cd385

File tree

3 files changed

+56
-5
lines changed

3 files changed

+56
-5
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@ To embed other resources, add `_embed`.
7979
GET /posts/1?_embed=comments
8080
```
8181

82+
To expand related resources, add `_expand`.
83+
84+
```
85+
GET /comments/1?_expand=posts
86+
```
87+
8288
Returns database.
8389

8490
```

src/router.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ module.exports = function (source) {
159159
// GET /:resource/:id
160160
function show (req, res, next) {
161161
var _embed = req.query._embed
162+
var _expand = req.query._expand
162163
var id = utils.toNative(req.params.id)
163164
var resource = db(req.params.resource)
164165
.getById(id)
@@ -168,6 +169,7 @@ module.exports = function (source) {
168169
resource = _.cloneDeep(resource)
169170
// Always use an array
170171
_embed = _.isArray(_embed) ? _embed : [_embed]
172+
_expand = _.isArray(_expand) ? _expand : [_expand]
171173

172174
// Embed other resources based on resource id
173175
_embed.forEach(function (otherResource) {
@@ -183,6 +185,19 @@ module.exports = function (source) {
183185
}
184186
})
185187

188+
// Expand inner resources based on id
189+
_expand.forEach(function (innerResource) {
190+
191+
if (innerResource
192+
&& innerResource.trim().length > 0
193+
&& db.object[innerResource]) {
194+
var query = {}
195+
var prop = pluralize.singular(innerResource) + 'Id'
196+
query.id = resource[prop]
197+
resource[innerResource] = db(innerResource).where(query)
198+
}
199+
})
200+
186201
res.locals.data = resource
187202
} else {
188203
res.status(404)

test/index.js

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,17 @@ describe('Server', function () {
2424
{id: 3, body: 'photo'}
2525
]
2626

27+
db.users = [
28+
{id: 1, username: 'Jim'},
29+
{id: 2, username: 'George'}
30+
]
31+
2732
db.comments = [
28-
{id: 1, published: true, postId: 1},
29-
{id: 2, published: false, postId: 1},
30-
{id: 3, published: false, postId: 2},
31-
{id: 4, published: false, postId: 2},
32-
{id: 5, published: false, postId: 2}
33+
{id: 1, published: true, postId: 1, userId: 1},
34+
{id: 2, published: false, postId: 1, userId: 2},
35+
{id: 3, published: false, postId: 2, userId: 1},
36+
{id: 4, published: false, postId: 2, userId: 2},
37+
{id: 5, published: false, postId: 2, userId: 1}
3338
]
3439

3540
db.refs = [
@@ -242,6 +247,31 @@ describe('Server', function () {
242247
})
243248
})
244249

250+
describe('GET /:resource/:id?_expand=', function () {
251+
it('should respond with corresponding resource and expanded inner resources', function (done) {
252+
var comments = db.comments[0]
253+
comments.posts = [db.posts[0]]
254+
request(server)
255+
.get('/comments/1?_expand=posts')
256+
.expect('Content-Type', /json/)
257+
.expect(comments)
258+
.expect(200, done)
259+
})
260+
})
261+
262+
describe('GET /:resource/:id?_expand=&_expand=', function () {
263+
it('should respond with corresponding resource and expanded inner resources', function (done) {
264+
var comments = db.comments[0]
265+
comments.posts = [db.posts[0]]
266+
comments.users = [db.users[0]]
267+
request(server)
268+
.get('/comments/1?_expand=posts&_expand=users')
269+
.expect('Content-Type', /json/)
270+
.expect(comments)
271+
.expect(200, done)
272+
})
273+
})
274+
245275
describe('POST /:resource', function () {
246276
it('should respond with json, create a resource and increment id',
247277
function (done) {

0 commit comments

Comments
 (0)