Skip to content

Commit 9b22c91

Browse files
committed
implement scope include
1 parent 4faa153 commit 9b22c91

File tree

2 files changed

+60
-6
lines changed

2 files changed

+60
-6
lines changed

lib/serialize.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,25 @@ module.exports = function (app, defaults) {
9292
if (ctx.req.isSideloadingRelationships) {
9393
requestedIncludes = ctx.req.remotingContext.args.filter.include;
9494
}
95+
96+
if (model.definition.settings.scope) {
97+
// bring requestedIncludes in array form
98+
if (typeof requestedIncludes === 'undefined') {
99+
requestedIncludes = [];
100+
} else if (typeof requestedIncludes === 'string') {
101+
requestedIncludes = [requestedIncludes];
102+
}
103+
104+
// add include from model
105+
var include = model.definition.settings.scope.include;
106+
107+
if (typeof include === 'string') {
108+
requestedIncludes.push(include);
109+
} else if (_.isArray(include)) {
110+
requestedIncludes = requestedIncludes.concat(include);
111+
}
112+
113+
}
95114
options = {
96115
app: app,
97116
model: model,

test/scopeInclude.test.js

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ var request = require('supertest');
22
var loopback = require('loopback');
33
var expect = require('chai').expect;
44
var JSONAPIComponent = require('../');
5-
var ds;
6-
var app;
7-
var Post;
8-
var Comment;
5+
var ds, app, Post, Author, Comment, Category;
96

107
describe('include option', function () {
118
beforeEach(function () {
@@ -26,11 +23,30 @@ describe('include option', function () {
2623
Comment = ds.createModel('comment', {
2724
id: {type: Number, id: true},
2825
postId: Number,
26+
authorId: Number,
2927
title: String,
3028
comment: String
3129
});
30+
3231
app.model(Comment);
32+
33+
Author = ds.createModel('author', {
34+
id: {type: Number, id: true},
35+
name: String
36+
});
37+
38+
app.model(Author);
39+
40+
Category = ds.createModel('category', {
41+
id: {type: Number, id: true},
42+
name: String
43+
});
44+
45+
app.model(Author);
46+
3347
Post.hasMany(Comment, {as: 'comments', foreignKey: 'postId'});
48+
Post.belongsTo(Author, {as: 'author', foreignKey: 'authorId'});
49+
Post.belongsTo(Category, {as: 'category', foreignKey: 'categoryId'});
3450
Comment.settings.plural = 'comments';
3551

3652
app.use(loopback.rest());
@@ -51,12 +67,20 @@ describe('include option', function () {
5167
post.comments.create({
5268
title: 'My second comment',
5369
comment: 'My second comment text'
54-
}, done);
70+
}, function () {
71+
post.author.create({
72+
name: 'Joe'
73+
}, function () {
74+
post.category.create({
75+
name: 'Programming'
76+
}, done);
77+
});
78+
});
5579
});
5680
});
5781
});
5882

59-
describe('hasMany response', function () {
83+
describe('response', function () {
6084

6185
it('should have key `included`', function (done) {
6286
request(app).get('/posts/1')
@@ -76,7 +100,18 @@ describe('include option', function () {
76100
});
77101
});
78102

103+
it('with include paramter should have both models', function (done) {
104+
request(app).get('/posts/1?filter[include]=author')
105+
.end(function (err, res) {
106+
expect(err).to.equal(null);
107+
expect(res.body.included.length).equal(3);
108+
expect(res.body.included[0].type).equal('authors');
109+
expect(res.body.included[1].type).equal('comments');
110+
done();
111+
});
112+
});
79113
});
114+
80115
});
81116

82117
});

0 commit comments

Comments
 (0)