Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Raymond Feng committed May 22, 2015
1 parent 12dea6e commit a028b26
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/include.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var async = require('async');
var utils = require('./utils');
var List = require('./list');
var isPlainObject = utils.isPlainObject;
var defineCachedRelations = utils.defineCachedRelations;

Expand Down Expand Up @@ -734,6 +735,9 @@ Inclusion.include = function (objects, include, options, cb) {
*/
function setIncludeData(result, cb) {
if (obj === inst) {
if (Array.isArray(result) && !(result instanceof List)) {
result = new List(result, relation.modelTo);
}
obj.__data[relationName] = result;
obj.setStrict(false);
} else {
Expand Down
95 changes: 95 additions & 0 deletions test/include.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// This test written in mocha+should.js
var should = require('./init.js');
var async = require('async');
var assert = require('assert');

var DataSource = require('../').DataSource;

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

Expand Down Expand Up @@ -690,3 +693,95 @@ function clearAndCreate(model, data, callback) {
itemIndex++;
}
}

describe('Model instance with included relation .toJSON()', function() {
var db, ChallengerModel, GameParticipationModel, ResultModel;

before(function(done) {
db = new DataSource({connector: 'memory'});
ChallengerModel = db.createModel('Challenger',
{
name: String
},
{
relations: {
gameParticipations: {
type: 'hasMany',
model: 'GameParticipation',
foreignKey: ''
}
}
}
);
GameParticipationModel = db.createModel('GameParticipation',
{
date: Date
},
{
relations: {
challenger: {
type: 'belongsTo',
model: 'Challenger',
foreignKey: ''
},
results: {
type: 'hasMany',
model: 'Result',
foreignKey: ''
}
}
}
);
ResultModel = db.createModel('Result', {
points: Number,
}, {
relations: {
gameParticipation: {
type: 'belongsTo',
model: 'GameParticipation',
foreignKey: ''
}
}
});

async.waterfall([
createChallengers,
createGameParticipations,
createResults],
function(err) {
done(err);
});

});

function createChallengers(callback) {
ChallengerModel.create([{name: 'challenger1'}, {name: 'challenger2'}], callback);
}

function createGameParticipations(challengers, callback) {
GameParticipationModel.create([
{challengerId: challengers[0].id, date: Date.now()},
{challengerId: challengers[0].id, date: Date.now()}
], callback);
}

function createResults(gameParticipations, callback) {
ResultModel.create([
{gameParticipationId: gameParticipations[0].id, points: 10},
{gameParticipationId: gameParticipations[0].id, points: 20}
], callback);
}

it('should recursively serialize objects', function(done) {
var filter = {include: {gameParticipations: 'results'}};
ChallengerModel.find(filter, function(err, challengers) {

var levelOneInclusion = challengers[0].toJSON().gameParticipations[0];
assert(levelOneInclusion.__data === undefined, '.__data of a level 1 inclusion is undefined.');

var levelTwoInclusion = challengers[0].toJSON().gameParticipations[0].results[0];
assert(levelTwoInclusion.__data === undefined, '__data of a level 2 inclusion is undefined.');
done();
});
});
});

0 comments on commit a028b26

Please sign in to comment.