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

Commit 1f661612 by Rafael Martins

Adding support for filter through hasMany

1 parent 878e22cc
...@@ -239,20 +239,42 @@ module.exports = (function() { ...@@ -239,20 +239,42 @@ module.exports = (function() {
key.split('.').forEach(function(attribute){ key.split('.').forEach(function(attribute){
var isAssociation = false; var isAssociation = false;
Object.keys(dao.associations).forEach(function(associationName){ Object.keys(dao.associations).forEach(function(associationName){
var accessor = dao.associations[associationName].associationAccessor if(!dao.associations[associationName]) return;
accessor = Utils.singularize(accessor[0].toLowerCase() + accessor.slice(1)); var accessor,
associationType = dao.associations[associationName].associationType;
if(associationType === 'HasMany') {
accessor = dao.associations[associationName].accessors.get.replace('get', '')
accessor = accessor[0].toLowerCase() + accessor.slice(1);
} else {
accessor = dao.associations[associationName].associationAccessor
accessor = Utils.singularize(accessor[0].toLowerCase() + accessor.slice(1))
}
accessor = accessor[0].toLowerCase() + accessor.slice(1);
if(attribute === accessor){ if(attribute === accessor){
var targetTableName = dao.associations[associationName].target.tableName if(associationType === 'BelongsTo') {
, targetColumnName = dao.associations[associationName].target.autoIncrementField var targetTableName = dao.associations[associationName].target.tableName
, sourceTableName = dao.tableName , targetColumnName = dao.associations[associationName].target.autoIncrementField
, sourceColumnName = dao.associations[associationName].identifier , sourceTableName = dao.tableName
, sourceColumnName = dao.associations[associationName].identifier
innerQuery += '\r\n\r\nLEFT JOIN ' + self.quoteIdentifier(targetTableName)
innerQuery += '\r\nON ' + self.quoteIdentifier(targetTableName) + '.' + self.quoteIdentifier(targetColumnName) + ' = ' + self.quoteIdentifier(sourceTableName) + '.' + self.quoteIdentifier(sourceColumnName) innerQuery += '\r\n\r\nLEFT JOIN ' + self.quoteIdentifier(targetTableName)
dao = dao.associations[associationName].target; innerQuery += '\r\nON ' + self.quoteIdentifier(targetTableName) + '.' + self.quoteIdentifier(targetColumnName) + ' = ' + self.quoteIdentifier(sourceTableName) + '.' + self.quoteIdentifier(sourceColumnName)
dao = dao.associations[associationName].target;
} else {
var targetTableName = dao.associations[associationName].target.tableName
, targetColumnName = dao.associations[associationName].identifier
, sourceTableName = dao.tableName
, sourceColumnName = dao.associations[associationName].source.autoIncrementField
innerQuery += '\r\n\r\nLEFT JOIN ' + self.quoteIdentifier(targetTableName)
innerQuery += '\r\nON ' + self.quoteIdentifier(targetTableName) + '.' + self.quoteIdentifier(targetColumnName) + ' = ' + self.quoteIdentifier(sourceTableName) + '.' + self.quoteIdentifier(sourceColumnName)
dao = dao.associations[associationName].target;
}
isAssociation = true; isAssociation = true;
} }
}); });
......
...@@ -7,16 +7,16 @@ var chai = require('chai') ...@@ -7,16 +7,16 @@ var chai = require('chai')
chai.Assertion.includeStack = true chai.Assertion.includeStack = true
describe(Support.getTestDialectTeaser("Multiple Level Filters"), function() { describe(Support.getTestDialectTeaser("Multiple Level Filters"), function() {
it('can filter through relations', function(done) { it('can filter through belongsTo', function(done) {
var User = this.sequelize.define('User', {username: DataTypes.STRING }) var User = this.sequelize.define('User', {username: DataTypes.STRING })
, Task = this.sequelize.define('Task', {title: DataTypes.STRING }) , Task = this.sequelize.define('Task', {title: DataTypes.STRING })
, Project = this.sequelize.define('Project', { title: DataTypes.STRING }) , Project = this.sequelize.define('Project', { title: DataTypes.STRING })
Project.belongsTo(User); Project.belongsTo(User);
//User.hasMany(Project) User.hasMany(Project)
Task.belongsTo(Project); Task.belongsTo(Project);
//Project.hasMany(Task); Project.hasMany(Task);
this.sequelize.sync({ force: true }).success(function() { this.sequelize.sync({ force: true }).success(function() {
...@@ -72,4 +72,69 @@ describe(Support.getTestDialectTeaser("Multiple Level Filters"), function() { ...@@ -72,4 +72,69 @@ describe(Support.getTestDialectTeaser("Multiple Level Filters"), function() {
}); });
}) })
}) })
it('can filter through hasMany', function(done) {
var User = this.sequelize.define('User', {username: DataTypes.STRING })
, Task = this.sequelize.define('Task', {title: DataTypes.STRING })
, Project = this.sequelize.define('Project', { title: DataTypes.STRING })
Project.belongsTo(User);
User.hasMany(Project)
Task.belongsTo(Project);
Project.hasMany(Task);
this.sequelize.sync({ force: true }).success(function() {
User.bulkCreate([{
id: 101,
username: 'leia'
}, {
id: 102,
username: 'vader'
}]).success(function() {
Project.bulkCreate([{
id: 201,
UserId: 101,
title: 'republic'
},{
id: 202,
UserId: 102,
title: 'empire'
}]).success(function() {
Task.bulkCreate([{
id: 301,
ProjectId: 201,
title: 'fight empire'
},{
id: 302,
ProjectId: 201,
title: 'stablish republic'
},{
id: 303,
ProjectId: 202,
title: 'destroy rebel alliance'
},{
id: 304,
ProjectId: 202,
title: 'rule everything'
}]).success(function() {
User.findAll({
where: {
'projects.tasks.title': 'fight empire'
}
}).success(function(users){
try{
expect(users.length).to.be.equal(1);
expect(users[0].username).to.be.equal('leia');
done();
}catch(e){
done(e);
}
})
});
});
});
})
})
}) })
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!