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

Commit 644cb30c by Mick Hansen

include where with hasOne and alias

1 parent bf185d94
...@@ -1338,7 +1338,10 @@ module.exports = (function() { ...@@ -1338,7 +1338,10 @@ module.exports = (function() {
} }
if (include.required === undefined) { if (include.required === undefined) {
include.required = false
if (include.where) {
include.required = true
}
} }
// Validate child includes // Validate child includes
......
...@@ -539,6 +539,10 @@ module.exports = (function() { ...@@ -539,6 +539,10 @@ module.exports = (function() {
joinQueryItem += joinType + self.quoteIdentifier(table) + " AS " + self.quoteIdentifier(as) + " ON " joinQueryItem += joinType + self.quoteIdentifier(table) + " AS " + self.quoteIdentifier(as) + " ON "
joinQueryItem += self.quoteIdentifier(tableLeft) + "." + self.quoteIdentifier(attrLeft) + " = " joinQueryItem += self.quoteIdentifier(tableLeft) + "." + self.quoteIdentifier(attrLeft) + " = "
joinQueryItem += self.quoteIdentifier(tableRight) + "." + self.quoteIdentifier(attrRight) joinQueryItem += self.quoteIdentifier(tableRight) + "." + self.quoteIdentifier(attrRight)
if (include.where) {
joinQueryItem += " AND "+self.getWhereConditions(include.where, as, include.daoFactory, options)
}
} }
if (include.include) { if (include.include) {
......
...@@ -969,7 +969,7 @@ describe(Support.getTestDialectTeaser("Include"), function () { ...@@ -969,7 +969,7 @@ describe(Support.getTestDialectTeaser("Include"), function () {
it('should support a required belongsTo include', function (done) { it('should support a required belongsTo include', function (done) {
var User = this.sequelize.define('User', {}) var User = this.sequelize.define('User', {})
, Group = this.sequelize.define('group', {}) , Group = this.sequelize.define('Group', {})
User.belongsTo(Group) User.belongsTo(Group)
...@@ -1004,6 +1004,100 @@ describe(Support.getTestDialectTeaser("Include"), function () { ...@@ -1004,6 +1004,100 @@ describe(Support.getTestDialectTeaser("Include"), function () {
}) })
}) })
}) })
it('should be possible to extend the on clause with a where option on a belongsTo include', function (done) {
var User = this.sequelize.define('User', {})
, Group = this.sequelize.define('Group', {
name: DataTypes.STRING
})
User.belongsTo(Group)
this.sequelize.sync({force: true}).done(function () {
async.auto({
groups: function (callback) {
Group.bulkCreate([
{name: 'A'},
{name: 'B'}
]).done(function () {
Group.findAll().done(callback)
})
},
users: function (callback) {
User.bulkCreate([{}, {}]).done(function () {
User.findAll().done(callback)
})
},
userGroups: ['users', 'groups', function (callback, results) {
var chainer = new Sequelize.Utils.QueryChainer()
chainer.add(results.users[0].setGroup(results.groups[1]))
chainer.add(results.users[1].setGroup(results.groups[0]))
chainer.run().done(callback)
}]
}, function (err, results) {
expect(err).not.to.be.ok
User.findAll({
include: [
{model: Group, where: {name: 'A'}}
]
}).done(function (err, users) {
expect(err).not.to.be.ok
expect(users.length).to.equal(1)
expect(users[0].group).to.be.ok
expect(users[0].group.name).to.equal('A')
done()
})
})
})
})
it('should be possible to extend the on clause with a where option on a hasOne include', function (done) {
var User = this.sequelize.define('User', {})
, Project = this.sequelize.define('Project', {
title: DataTypes.STRING
})
User.hasOne(Project, {as: 'LeaderOf'})
this.sequelize.sync({force: true}).done(function () {
async.auto({
projects: function (callback) {
Project.bulkCreate([
{title: 'Alpha'},
{title: 'Beta'}
]).done(function () {
Project.findAll().done(callback)
})
},
users: function (callback) {
User.bulkCreate([{}, {}]).done(function () {
User.findAll().done(callback)
})
},
userProjects: ['users', 'projects', function (callback, results) {
var chainer = new Sequelize.Utils.QueryChainer()
chainer.add(results.users[1].setLeaderOf(results.projects[1]))
chainer.add(results.users[0].setLeaderOf(results.projects[0]))
chainer.run().done(callback)
}]
}, function (err, results) {
expect(err).not.to.be.ok
User.findAll({
include: [
{model: Project, as: 'LeaderOf', where: {title: 'Beta'}}
]
}).done(function (err, users) {
expect(err).not.to.be.ok
expect(users.length).to.equal(1)
expect(users[0].leaderOf).to.be.ok
expect(users[0].leaderOf.title).to.equal('Beta')
done()
})
})
})
})
}) })
describe('findAndCountAll', function () { describe('findAndCountAll', function () {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!