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

Commit c8235a54 by Mick Hansen

feat(include): make it possible to filter attributes on the through table

1 parent 23f0460a
...@@ -375,15 +375,16 @@ module.exports = (function() { ...@@ -375,15 +375,16 @@ module.exports = (function() {
attributes: include.originalAttributes attributes: include.originalAttributes
} }
} }
if (include.originalAttributes === undefined || include.originalAttributes.length) {
// downcase the first char
accessor = accessor.slice(0,1).toLowerCase() + accessor.slice(1)
// downcase the first char if (association.isSingleAssociation) {
accessor = accessor.slice(0,1).toLowerCase() + accessor.slice(1) accessor = Utils.singularize(accessor, self.Model.options.language)
self[accessor] = self.dataValues[accessor] = isEmpty ? null : include.model.build(value[0], childOptions)
if (association.isSingleAssociation) { } else {
accessor = Utils.singularize(accessor, self.Model.options.language) self[accessor] = self.dataValues[accessor] = isEmpty ? [] : include.model.bulkBuild(value, childOptions)
self[accessor] = self.dataValues[accessor] = isEmpty ? null : include.model.build(value[0], childOptions) }
} else {
self[accessor] = self.dataValues[accessor] = isEmpty ? [] : include.model.bulkBuild(value, childOptions)
} }
}; };
......
...@@ -1774,7 +1774,9 @@ module.exports = (function() { ...@@ -1774,7 +1774,9 @@ module.exports = (function() {
} }
// pseudo include just needed the attribute logic, return // pseudo include just needed the attribute logic, return
if (include._pseudo) return include if (include._pseudo) {
return include
}
// check if the current Model is actually associated with the passed Model - or it's a pseudo include // check if the current Model is actually associated with the passed Model - or it's a pseudo include
var association = this.getAssociation(include.model, include.as) var association = this.getAssociation(include.model, include.as)
...@@ -1787,14 +1789,14 @@ module.exports = (function() { ...@@ -1787,14 +1789,14 @@ module.exports = (function() {
if (!include.include) include.include = [] if (!include.include) include.include = []
var through = include.association.through var through = include.association.through
include.through = { include.through = Utils._.defaults(include.through || {}, {
model: through, model: through,
as: Utils.singularize(through.tableName, through.options.language), as: Utils.singularize(through.tableName, through.options.language),
association: { association: {
isSingleAssociation: true isSingleAssociation: true
}, },
_pseudo: true _pseudo: true
} })
include.include.push(include.through) include.include.push(include.through)
tableNames[through.tableName] = true tableNames[through.tableName] = true
......
...@@ -1451,6 +1451,28 @@ describe(Support.getTestDialectTeaser("Include"), function () { ...@@ -1451,6 +1451,28 @@ describe(Support.getTestDialectTeaser("Include"), function () {
}) })
}) })
it('should be possible to turn off the attributes for the through table', function (done) {
var self = this
this.fixtureA(function () {
self.models.Product.findAll({
attributes: ['title'],
include: [
{model: self.models.Tag, through: {attributes: []}, required: true}
],
}).done(function (err, products) {
expect(err).not.to.be.ok
products.forEach(function (product) {
expect(product.tags.length).to.be.ok
product.tags.forEach(function (tag) {
expect(tag.get().productTags).not.to.be.ok
})
})
done()
})
})
});
// Test case by @eshell // Test case by @eshell
it('should be possible not to include the main id in the attributes', function (done) { it('should be possible not to include the main id in the attributes', function (done) {
var Member = this.sequelize.define('Member', { var Member = this.sequelize.define('Member', {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!