不要怂,就是干,撸起袖子干!

Commit 8a3013c5 by Mick Hansen

Merge pull request #2254 from findhit/patch/instance-get

Instance.get: should only be recursive if it is an instance
2 parents a6d8d0fb 46d6ebfa
Showing with 55 additions and 2 deletions
......@@ -184,8 +184,10 @@ module.exports = (function() {
return this.dataValues[key].map(function (instance) {
return instance.get({plain: options.plain});
});
} else {
} else if (this.dataValues[key] instanceof Instance) {
return this.dataValues[key].get({plain: options.plain});
} else {
return this.dataValues[key];
}
}
return this.dataValues[key];
......@@ -210,8 +212,10 @@ module.exports = (function() {
values[_key] = this.dataValues[_key].map(function (instance) {
return instance.get({plain: options.plain});
});
} else {
} else if (this.dataValues[_key] instanceof Instance) {
values[_key] = this.dataValues[_key].get({plain: options.plain});
} else {
values[_key] = this.dataValues[_key];
}
} else {
values[_key] = this.dataValues[_key];
......
......@@ -1200,6 +1200,55 @@ describe(Support.getTestDialectTeaser("DAO"), function () {
})
})
it("dont return instance that isn't defined", function ( done ) {
var self = this;
self.Project.create({ lovelyUserId: null })
.then(function ( project ) {
return self.Project.find({
where: {
id: project.id,
},
include: [
{ model: self.User, as: 'LovelyUser' }
]
})
})
.then(function ( project ) {
var json = project.toJSON();
expect( json.LovelyUser ).to.be.equal( null )
})
.done( done )
.catch( done );
});
it("dont return instances that aren't defined", function ( done ) {
var self = this;
self.User.create({ username: 'cuss' })
.then(function ( user ) {
return self.User.find({
where: {
id: user.id,
},
include: [
{ model: self.Project, as: 'Projects' }
]
})
})
.then(function ( user ) {
var json = user.toJSON();
expect( user.Projects ).to.be.instanceof( Array )
expect( user.Projects ).to.be.length( 0 )
})
.done( done )
.catch( done );
});
it('returns an object containing all values', function(done) {
var user = this.User.build({ username: 'test.user', age: 99, isAdmin: true })
expect(user.toJSON()).to.deep.equal({ username: 'test.user', age: 99, isAdmin: true, id: null })
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!