Skip to content

Commit a028b26

Browse files
author
Raymond Feng
committed
1 parent 12dea6e commit a028b26

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

lib/include.js

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var async = require('async');
22
var utils = require('./utils');
3+
var List = require('./list');
34
var isPlainObject = utils.isPlainObject;
45
var defineCachedRelations = utils.defineCachedRelations;
56

@@ -734,6 +735,9 @@ Inclusion.include = function (objects, include, options, cb) {
734735
*/
735736
function setIncludeData(result, cb) {
736737
if (obj === inst) {
738+
if (Array.isArray(result) && !(result instanceof List)) {
739+
result = new List(result, relation.modelTo);
740+
}
737741
obj.__data[relationName] = result;
738742
obj.setStrict(false);
739743
} else {

test/include.test.js

+95
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// This test written in mocha+should.js
22
var should = require('./init.js');
33
var async = require('async');
4+
var assert = require('assert');
5+
6+
var DataSource = require('../').DataSource;
47

58
var db, User, Profile, AccessToken, Post, Passport, City, Street, Building, Assembly, Part;
69

@@ -690,3 +693,95 @@ function clearAndCreate(model, data, callback) {
690693
itemIndex++;
691694
}
692695
}
696+
697+
describe('Model instance with included relation .toJSON()', function() {
698+
var db, ChallengerModel, GameParticipationModel, ResultModel;
699+
700+
before(function(done) {
701+
db = new DataSource({connector: 'memory'});
702+
ChallengerModel = db.createModel('Challenger',
703+
{
704+
name: String
705+
},
706+
{
707+
relations: {
708+
gameParticipations: {
709+
type: 'hasMany',
710+
model: 'GameParticipation',
711+
foreignKey: ''
712+
}
713+
}
714+
}
715+
);
716+
GameParticipationModel = db.createModel('GameParticipation',
717+
{
718+
date: Date
719+
},
720+
{
721+
relations: {
722+
challenger: {
723+
type: 'belongsTo',
724+
model: 'Challenger',
725+
foreignKey: ''
726+
},
727+
results: {
728+
type: 'hasMany',
729+
model: 'Result',
730+
foreignKey: ''
731+
}
732+
}
733+
}
734+
);
735+
ResultModel = db.createModel('Result', {
736+
points: Number,
737+
}, {
738+
relations: {
739+
gameParticipation: {
740+
type: 'belongsTo',
741+
model: 'GameParticipation',
742+
foreignKey: ''
743+
}
744+
}
745+
});
746+
747+
async.waterfall([
748+
createChallengers,
749+
createGameParticipations,
750+
createResults],
751+
function(err) {
752+
done(err);
753+
});
754+
755+
});
756+
757+
function createChallengers(callback) {
758+
ChallengerModel.create([{name: 'challenger1'}, {name: 'challenger2'}], callback);
759+
}
760+
761+
function createGameParticipations(challengers, callback) {
762+
GameParticipationModel.create([
763+
{challengerId: challengers[0].id, date: Date.now()},
764+
{challengerId: challengers[0].id, date: Date.now()}
765+
], callback);
766+
}
767+
768+
function createResults(gameParticipations, callback) {
769+
ResultModel.create([
770+
{gameParticipationId: gameParticipations[0].id, points: 10},
771+
{gameParticipationId: gameParticipations[0].id, points: 20}
772+
], callback);
773+
}
774+
775+
it('should recursively serialize objects', function(done) {
776+
var filter = {include: {gameParticipations: 'results'}};
777+
ChallengerModel.find(filter, function(err, challengers) {
778+
779+
var levelOneInclusion = challengers[0].toJSON().gameParticipations[0];
780+
assert(levelOneInclusion.__data === undefined, '.__data of a level 1 inclusion is undefined.');
781+
782+
var levelTwoInclusion = challengers[0].toJSON().gameParticipations[0].results[0];
783+
assert(levelTwoInclusion.__data === undefined, '__data of a level 2 inclusion is undefined.');
784+
done();
785+
});
786+
});
787+
});

0 commit comments

Comments
 (0)