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

Commit 471c4fac by Mick Hansen

should support an include with multiple different association types

1 parent 8c100802
...@@ -191,7 +191,7 @@ module.exports = (function() { ...@@ -191,7 +191,7 @@ module.exports = (function() {
} }
var isSelectQuery = function() { var isSelectQuery = function() {
return this.options.type === 'SELECT'; return this.options.type === 'SELECT'
} }
var isUpdateQuery = function() { var isUpdateQuery = function() {
...@@ -342,7 +342,7 @@ module.exports = (function() { ...@@ -342,7 +342,7 @@ module.exports = (function() {
child = Object(row[attrName]) === row[attrName] && options.includeMap && options.includeMap[attrName] child = Object(row[attrName]) === row[attrName] && options.includeMap && options.includeMap[attrName]
if (child) { if (child) {
if (!existingResult.__children) existingResult.__children = {} if (!existingResult.__children) existingResult.__children = {}
if (!existingResult.__children[attrName]) existingResult.__children[attrName] = []; if (!existingResult.__children[attrName]) existingResult.__children[attrName] = []
existingResult.__children[attrName].push(row[attrName]) existingResult.__children[attrName].push(row[attrName])
} }
......
...@@ -296,5 +296,157 @@ describe(Support.getTestDialectTeaser("Include"), function () { ...@@ -296,5 +296,157 @@ describe(Support.getTestDialectTeaser("Include"), function () {
}) })
}) })
}) })
it('should support an include with multiple different association types', function (done) {
var User = this.sequelize.define('User', {})
, Product = this.sequelize.define('Product', {
title: DataTypes.STRING
})
, Tag = this.sequelize.define('Tag', {
name: DataTypes.STRING
})
, Price = this.sequelize.define('Price', {
value: DataTypes.FLOAT
})
, Customer = this.sequelize.define('Customer', {
name: DataTypes.STRING
})
, Group = this.sequelize.define('Group', {
name: DataTypes.STRING
})
, GroupMember = this.sequelize.define('GroupMember', {
})
, Rank = this.sequelize.define('Rank', {
name: DataTypes.STRING,
canInvite: {
type: DataTypes.INTEGER,
defaultValue: 0
},
canRemove: {
type: DataTypes.INTEGER,
defaultValue: 0
}
})
User.hasMany(Product)
Product.belongsTo(User)
Product.hasMany(Tag)
Tag.hasMany(Product)
Product.belongsTo(Tag, {as: 'Category'})
Product.hasMany(Price)
Price.belongsTo(Product)
User.hasMany(GroupMember, {as: 'Memberships'})
GroupMember.belongsTo(User)
GroupMember.belongsTo(Rank)
GroupMember.belongsTo(Group)
Group.hasMany(GroupMember, {as: 'Memberships'})
this.sequelize.sync({force: true}).done(function () {
async.auto({
user: function (callback) {
User.create().done(callback)
},
groups: function(callback) {
Group.bulkCreate([
{name: 'Developers'},
{name: 'Designers'}
]).done(function () {
Group.findAll().done(callback)
})
},
ranks: function(callback) {
Rank.bulkCreate([
{name: 'Admin', canInvite: 1, canRemove: 1},
{name: 'Member', canInvite: 1}
]).done(function () {
Rank.findAll().done(callback)
})
},
memberships: ['user', 'groups', 'ranks', function (callback, results) {
GroupMember.bulkCreate([
{UserId: results.user.id, GroupId: results.groups[0].id, RankId: results.ranks[0].id},
{UserId: results.user.id, GroupId: results.groups[1].id, RankId: results.ranks[1].id}
]).done(callback)
}],
products: function (callback) {
Product.bulkCreate([
{title: 'Chair'},
{title: 'Desk'}
]).done(function () {
Product.findAll().done(callback)
})
},
tags: function(callback) {
Tag.bulkCreate([
{name: 'A'},
{name: 'B'},
{name: 'C'}
]).done(function () {
Tag.findAll().done(callback)
})
},
userProducts: ['user', 'products', function (callback, results) {
results.user.setProducts(results.products).done(callback)
}],
productTags: ['products', 'tags', function (callback, results) {
var chainer = new Sequelize.Utils.QueryChainer()
chainer.add(results.products[0].setTags([results.tags[0], results.tags[2]]))
chainer.add(results.products[1].setTags([results.tags[1]]))
chainer.add(results.products[0].setCategory(results.tags[1]))
chainer.run().done(callback)
}],
prices: ['products', function (callback, results) {
Price.bulkCreate([
{ProductId: results.products[0].id, value: 5},
{ProductId: results.products[0].id, value: 10},
{ProductId: results.products[1].id, value: 5},
{ProductId: results.products[1].id, value: 10},
{ProductId: results.products[1].id, value: 15},
{ProductId: results.products[1].id, value: 20}
]).done(callback)
}]
}, function (err, results) {
expect(err).not.to.be.ok
User.find({
where: {id: results.user.id},
include: [
{model: GroupMember, as: 'Memberships', include: [
Group,
Rank
]},
{model: Product, include: [
Tag,
{model: Tag, as: 'Category'},
Price
]}
]
}).done(function (err, user) {
expect(user.memberships.length).to.equal(2)
expect(user.memberships[0].group.name).to.equal('Developers')
expect(user.memberships[0].rank.canRemove).to.equal(1)
expect(user.memberships[1].group.name).to.equal('Designers')
expect(user.memberships[1].rank.canRemove).to.equal(0)
expect(user.products.length).to.equal(2)
expect(user.products[0].tags.length).to.equal(2)
expect(user.products[1].tags.length).to.equal(1)
expect(user.products[0].category).to.be.ok
expect(user.products[1].category).not.to.be.ok
expect(user.products[0].prices.length).to.equal(2)
expect(user.products[1].prices.length).to.equal(4)
done()
})
})
})
})
}) })
}) })
\ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!