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

Commit c7bee173 by Jisoo Park

fix #334 ('include' places a "null" element to the result if no data is related)

1 parent c05f7acc
......@@ -284,12 +284,14 @@ module.exports = (function() {
associationData.forEach(function(data) {
var daoInstance = associatedDaoFactory.build(data, { isNewRecord: false })
, identifier = associatedDaoFactory.hasPrimaryKeys ? Utils.firstKeyOfHash(associatedDaoFactory.primaryKeys) : 'id'
if (['BelongsTo', 'HasOne'].indexOf(association.associationType) > -1) {
accessor = Utils.singularize(accessor)
dao[accessor] = daoInstance
dao[accessor] = daoInstance[identifier] ? daoInstance : null
} else {
dao[accessor] = dao[accessor] || []
if (daoInstance[identifier])
dao[accessor].push(daoInstance)
}
})
......
......@@ -149,6 +149,14 @@ var Utils = module.exports = {
}
},
firstKeyOfHash: function(obj) {
for (var key in obj) {
if (obj.hasOwnProperty(key))
return key
}
return null
},
inherit: function(subClass, superClass) {
if (superClass.constructor == Function) {
// Normal Inheritance
......@@ -164,6 +172,7 @@ var Utils = module.exports = {
return subClass;
}
}
Utils.CustomEventEmitter = require("./emitters/custom-event-emitter")
......
......@@ -500,6 +500,25 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
}.bind(this)) //- sequelize.sync
})
it('fetches no associated object if none is set (1st direction)', function(done) {
this.User.hasOne(this.Task)
this.Task.belongsTo(this.User)
this.sequelize.sync({ force: true }).success(function() {
this.User.create({ name: 'barfooz' }).success(function(user) {
this.Task.create({ title: 'task' }).success(function(task) {
this.User.find({
where: { 'UserWithNames.id': 1 },
include: [ 'Task' ]
}).success(function(user) {
expect(user.task).toEqual(null)
done()
})
}.bind(this)) //- Task.create
}.bind(this)) //- User.create
}.bind(this)) //- sequelize.sync
})
it('fetches associated objects via "as" param (1st direction)', function(done) {
this.User.hasOne(this.Task, { as: 'Homework' })
this.Task.belongsTo(this.User)
......@@ -546,6 +565,27 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
}.bind(this)) //- sequelize.sync
})
it('fetches no associated object if none is set (2nd direction)', function(done) {
this.User.hasOne(this.Task)
this.Task.belongsTo(this.User)
this.sequelize.sync({ force: true }).success(function() {
this.User.create({ name: 'barfooz' }).success(function(user) {
this.User.create({ name: 'another user' }).success(function(another_user) {
this.Task.create({ title: 'task' }).success(function(task) {
this.Task.find({
where: { 'Tasks.id': 1 },
include: [ 'UserWithName' ]
}).success(function(task) {
expect(task.userWithName).toEqual(null)
done()
})
}.bind(this)) //- Task.create
}.bind(this)) //- User.create
}.bind(this)) //- User.create
}.bind(this)) //- sequelize.sync
})
it('fetches associated object via "as" param (2nd direction)', function(done) {
this.User.hasOne(this.Task)
this.Task.belongsTo(this.User, { as: 'Owner' })
......@@ -627,6 +667,25 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
}.bind(this)) //- sequelize.sync
})
it('fetches no associated objects for 1:N associations if none are set (1st direction)', function(done) {
this.User.hasMany(this.Task)
this.Task.belongsTo(this.User)
this.sequelize.sync({ force: true }).success(function() {
this.User.create({ name: 'barfooz' }).success(function(user) {
this.Task.create({ title: 'task1' }).success(function(task1) {
this.User.find({
where: { 'UserWithNames.id': 1 },
include: [ 'Task' ]
}).success(function(user) {
expect(user.tasks.length).toEqual(0)
done()
})
}.bind(this)) //- Task.create
}.bind(this)) //- User.create
}.bind(this)) //- sequelize.sync
})
it('fetches associated objects for 1:N associations (2nd direction)', function(done) {
this.User.hasMany(this.Task)
this.Task.belongsTo(this.User)
......@@ -705,6 +764,29 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
}.bind(this)) //- sequelize.sync
})
it('fetches no associated objects for N:M associations if none are set (1st direction)', function(done) {
this.User.hasMany(this.Task)
this.Task.hasMany(this.User)
this.sequelize.sync({ force: true }).success(function() {
this.User.create({ name: 'barfooz' }).success(function(user1) {
this.Task.create({ title: 'task1' }).success(function(task1) {
this.Task.create({ title: 'task2' }).success(function(task2) {
this.User.find({
where: { 'UserWithNames.id': user1.id },
include: [ 'Task' ]
}).success(function(user) {
expect(user.tasks.length).toEqual(0)
done()
})
}.bind(this)) //- Task.create
}.bind(this)) //- Task.create
}.bind(this)) //- User.create
}.bind(this)) //- sequelize.sync
})
it('fetches associated objects via "as" param for N:M associations (1st direction)', function(done) {
this.User.hasMany(this.Task, { as: 'Homeworks' })
this.Task.hasMany(this.User, { as: 'Owners' })
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!