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

Commit f798e806 by Sascha Depold

association prefetching is now also working with aliased associations

1 parent 27f89487
...@@ -39,16 +39,34 @@ Mixin.hasMany = function(associatedDAO, options) { ...@@ -39,16 +39,34 @@ Mixin.hasMany = function(associatedDAO, options) {
return this return this
} }
Mixin.getAssociation = function(target, options) { Mixin.getAssociation = function(target) {
var result = null var result = null
for (var associationName in this.associations) { for (var associationName in this.associations) {
if (this.associations.hasOwnProperty(associationName)) {
var association = this.associations[associationName] var association = this.associations[associationName]
if (!result && (association.target === target)) { if (!result && (association.target === target)) {
result = association result = association
} }
} }
}
return result
}
Mixin.getAssociationByAlias = function(alias) {
var result = null
for (var associationName in this.associations) {
if (this.associations.hasOwnProperty(associationName)) {
var association = this.associations[associationName]
if (!result && (association.options.as === alias)) {
result = association
}
}
}
return result return result
} }
......
...@@ -196,6 +196,10 @@ module.exports = (function() { ...@@ -196,6 +196,10 @@ module.exports = (function() {
includes.forEach(function(daoName) { includes.forEach(function(daoName) {
options.include[daoName] = this.daoFactoryManager.getDAO(daoName) options.include[daoName] = this.daoFactoryManager.getDAO(daoName)
if (!options.include[daoName]) {
options.include[daoName] = this.getAssociationByAlias(daoName).target
}
}.bind(this)) }.bind(this))
} }
......
...@@ -133,14 +133,33 @@ module.exports = (function() { ...@@ -133,14 +133,33 @@ module.exports = (function() {
// private // // private //
///////////// /////////////
/**
* Iterate over all known tables and search their names inside the sql query.
* This method will also check association aliases ('as' option).
*
* @param {String} attribute An attribute of a SQL query. (?)
* @return {String} The found tableName / alias.
*/
var findTableNameInAttribute = function(attribute) { var findTableNameInAttribute = function(attribute) {
var tableName = null var tableName = null
this.sequelize.daoFactoryManager.daos.forEach(function(daoFactory) { this.sequelize.daoFactoryManager.daos.forEach(function(daoFactory) {
if (!!tableName) { if (!!tableName) {
return return
} else if (attribute.indexOf(daoFactory.tableName + ".") === 0) {
tableName = daoFactory.tableName
} else if (attribute.indexOf(Utils.singularize(daoFactory.tableName) + ".") === 0) {
tableName = Utils.singularize(daoFactory.tableName)
} else { } else {
if (attribute.indexOf(daoFactory.tableName+".") === 0) tableName = daoFactory.tableName; for (var associationName in daoFactory.associations) {
if (daoFactory.associations.hasOwnProperty(associationName)) {
var association = daoFactory.associations[associationName]
if (attribute.indexOf(association.options.as + ".") === 0) {
tableName = association.options.as
}
}
}
} }
}) })
...@@ -242,15 +261,29 @@ module.exports = (function() { ...@@ -242,15 +261,29 @@ module.exports = (function() {
} }
var buildAssociatedDaoInstances = function(tableName, associationData, dao) { var buildAssociatedDaoInstances = function(tableName, associationData, dao) {
var associatedDao = this.sequelize.daoFactoryManager.getDAO(tableName, { attribute: 'tableName' }) var associatedDaoFactory = this.sequelize.daoFactoryManager.getDAO(tableName, { attribute: 'tableName' })
, association = this.callee.getAssociation(associatedDao) , association = null
, accessor = Utils._.camelize(associatedDao.tableName)
if (!!associatedDaoFactory) {
association = this.callee.getAssociation(associatedDaoFactory)
} else {
associatedDaoFactory = this.sequelize.daoFactoryManager.getDAO(Utils.pluralize(tableName), { attribute: 'tableName' })
if (!!associatedDaoFactory) {
association = this.callee.getAssociation(associatedDaoFactory)
} else {
association = this.callee.getAssociationByAlias(tableName)
associatedDaoFactory = association.target
}
}
var accessor = Utils._.camelize(tableName)
// downcase the first char // downcase the first char
accessor = accessor.slice(0,1).toLowerCase() + accessor.slice(1) accessor = accessor.slice(0,1).toLowerCase() + accessor.slice(1)
associationData.forEach(function(data) { associationData.forEach(function(data) {
var daoInstance = associatedDao.build(data, { isNewRecord: false }) var daoInstance = associatedDaoFactory.build(data, { isNewRecord: false })
if (['BelongsTo', 'HasOne'].indexOf(association.associationType) > -1) { if (['BelongsTo', 'HasOne'].indexOf(association.associationType) > -1) {
accessor = Utils.singularize(accessor) accessor = Utils.singularize(accessor)
......
...@@ -161,10 +161,15 @@ module.exports = (function() { ...@@ -161,10 +161,15 @@ module.exports = (function() {
query += Utils.addTicks(association.associationType === 'BelongsTo' ? tableName : dao.tableName) + '.' + Utils.addTicks('id') query += Utils.addTicks(association.associationType === 'BelongsTo' ? tableName : dao.tableName) + '.' + Utils.addTicks('id')
} }
var aliasAssoc = daoFactory.getAssociationByAlias(daoName)
, aliasName = !!aliasAssoc ? Utils.addTicks(daoName) : _tableName
optAttributes = optAttributes.concat( optAttributes = optAttributes.concat(
Utils._.keys(dao.attributes).map(function(attr) { Utils._.keys(dao.attributes).map(function(attr) {
var identifier = [_tableName, Utils.addTicks(attr)] return '' +
return identifier.join('.') + ' AS ' + Utils.addTicks(identifier.join('.')) [_tableName, Utils.addTicks(attr)].join('.') +
' AS ' +
Utils.addTicks([aliasName, attr].join('.'))
}) })
) )
} }
......
...@@ -236,10 +236,15 @@ module.exports = (function() { ...@@ -236,10 +236,15 @@ module.exports = (function() {
query += addQuotes(association.associationType === 'BelongsTo' ? tableName : dao.tableName) + '.' + addQuotes('id') query += addQuotes(association.associationType === 'BelongsTo' ? tableName : dao.tableName) + '.' + addQuotes('id')
} }
var aliasAssoc = daoFactory.getAssociationByAlias(daoName)
, aliasName = !!aliasAssoc ? addQuotes(daoName) : _tableName
optAttributes = optAttributes.concat( optAttributes = optAttributes.concat(
Utils._.keys(dao.attributes).map(function(attr) { Utils._.keys(dao.attributes).map(function(attr) {
var identifier = [_tableName, addQuotes(attr)] return '' +
return identifier.join('.') + ' AS "' + removeQuotes(identifier.join('.')) + '"' [_tableName, addQuotes(attr)].join('.') +
' AS "' +
removeQuotes([aliasName, attr].join('.')) + '"'
}) })
) )
} }
......
...@@ -450,7 +450,8 @@ describe("[" + Helpers.getTestDialectTeaser() + "] DAOFactory", function() { ...@@ -450,7 +450,8 @@ describe("[" + Helpers.getTestDialectTeaser() + "] DAOFactory", function() {
}) })
}) })
it('fetches associated objects for 1:1 associations (1st direction)', function(done) { describe('1:1 associations', function() {
it('fetches associated objects (1st direction)', function(done) {
this.User.hasOne(this.Task) this.User.hasOne(this.Task)
this.Task.belongsTo(this.User) this.Task.belongsTo(this.User)
...@@ -472,7 +473,29 @@ describe("[" + Helpers.getTestDialectTeaser() + "] DAOFactory", function() { ...@@ -472,7 +473,29 @@ describe("[" + Helpers.getTestDialectTeaser() + "] DAOFactory", function() {
}.bind(this)) //- sequelize.sync }.bind(this)) //- sequelize.sync
}) })
it('fetches associated objects for 1:1 associations (2nd direction)', function(done) { it('fetches associated objects via "as" param (1st direction)', function(done) {
this.User.hasOne(this.Task, { as: 'Homework' })
this.Task.belongsTo(this.User)
this.sequelize.sync({ force: true }).success(function() {
this.User.create({ name: 'barfooz' }).success(function(user) {
this.Task.create({ title: 'task' }).success(function(task) {
user.setHomework(task).success(function() {
this.User.find({
where: { 'UserWithNames.id': 1 },
include: [ 'Homework' ]
}).success(function(user) {
expect(user.homework).toBeDefined()
expect(user.homework.id).toEqual(task.id)
done()
})
}.bind(this)) //- setTask
}.bind(this)) //- Task.create
}.bind(this)) //- User.create
}.bind(this)) //- sequelize.sync
})
it('fetches associated object (2nd direction)', function(done) {
this.User.hasOne(this.Task) this.User.hasOne(this.Task)
this.Task.belongsTo(this.User) this.Task.belongsTo(this.User)
...@@ -495,6 +518,11 @@ describe("[" + Helpers.getTestDialectTeaser() + "] DAOFactory", function() { ...@@ -495,6 +518,11 @@ describe("[" + Helpers.getTestDialectTeaser() + "] DAOFactory", function() {
}.bind(this)) //- User.create }.bind(this)) //- User.create
}.bind(this)) //- sequelize.sync }.bind(this)) //- sequelize.sync
}) })
})
it('fetches associated objects for 1:N associations (1st direction)', function(done) { it('fetches associated objects for 1:N associations (1st direction)', function(done) {
this.User.hasMany(this.Task) this.User.hasMany(this.Task)
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!