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

Commit 6d2295ff by Mick Hansen

feat(model/attributes): attribute.field support for includes

1 parent fde91e4c
......@@ -639,6 +639,8 @@ module.exports = (function() {
if (attr[0] instanceof Utils.fn || attr[0] instanceof Utils.col) {
attr[0] = attr[0].toString(self)
addTable = false
} else {
attr[0] = this.quoteIdentifier(attr[0])
}
attr = [attr[0], this.quoteIdentifier(attr[1])].join(' as ')
} else {
......@@ -648,7 +650,6 @@ module.exports = (function() {
if (options.include && attr.indexOf('.') === -1 && addTable) {
attr = mainTableAs + '.' + attr
}
return attr
}.bind(this))
......@@ -687,7 +688,17 @@ module.exports = (function() {
// includeIgnoreAttributes is used by aggregate functions
if (options.includeIgnoreAttributes !== false) {
attributes = include.attributes.map(function(attr) {
return self.quoteIdentifier(as) + "." + self.quoteIdentifier(attr) + " AS " + self.quoteIdentifier(as + "." + attr)
var attrAs = attr;
if (Array.isArray(attr) && attr.length == 2) {
attr = attr.map(function ($attr) {
return $attr._isSequelizeMethod ? $attr.toString(self) : $attr;
})
attrAs = attr[1];
attr = attr[0];
}
return self.quoteIdentifier(as) + "." + self.quoteIdentifier(attr) + " AS " + self.quoteIdentifier(as + "." + attrAs);
})
if (include.subQuery && subQuery) {
......
......@@ -1695,6 +1695,8 @@ module.exports = (function() {
include.attributes = Object.keys(include.model.attributes)
}
include = mapFieldNames(include, include.model);
// pseudo include just needed the attribute logic, return
if (include._pseudo) {
return include
......
......@@ -2,6 +2,7 @@
/* jshint expr: true */
var chai = require('chai')
, Sequelize = require('../../index')
, Promise = Sequelize.Promise
, expect = chai.expect
, Support = require(__dirname + '/../support')
, DataTypes = require(__dirname + "/../../lib/data-types")
......@@ -25,19 +26,59 @@ describe(Support.getTestDialectTeaser("Model"), function () {
}, {
tableName: 'users',
timestamps: false
})
});
return queryInterface.createTable('users', {
this.Task = this.sequelize.define('task', {
id: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true,
autoIncrement: true
autoIncrement: true,
field: 'taskId'
},
full_name: {
type: DataTypes.STRING
title: {
type: DataTypes.STRING,
field: 'name'
}
}, {
tableName: 'tasks',
timestamps: false
});
this.User.hasMany(this.Task, {
foreignKey: 'user_id'
});
this.Task.belongsTo(this.User, {
foreignKey: 'user_id'
});
return Promise.all([
queryInterface.createTable('users', {
id: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true,
autoIncrement: true
},
full_name: {
type: DataTypes.STRING
}
}),
queryInterface.createTable('tasks', {
taskId: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true,
autoIncrement: true
},
user_id: {
type: DataTypes.INTEGER
},
name: {
type: DataTypes.STRING
}
})
])
});
it('should create, fetch and update with alternative field names from a simple model', function () {
......@@ -63,6 +104,29 @@ describe(Support.getTestDialectTeaser("Model"), function () {
})
});
it('should work with attributes and where on includes', function () {
var self = this;
return this.User.create({
name: 'Foobar'
}).then(function (user) {
return user.createTask({
title: 'DoDat'
});
}).then(function () {
return self.User.findAll({
include: [
{model: self.Task, where: {title: 'DoDat'}}
]
});
}).then(function (users) {
users.forEach(function (user) {
expect(user.get('name')).to.be.ok;
expect(user.get('tasks')[0].get('title')).to.equal('DoDat');
});
});
});
it('should work with a simple where', function () {
var self = this;
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!