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

Commit 0691ced4 by Mick Hansen

Make sure eager loading validater can correctly identify associations after setting default alias

1 parent 5816f037
...@@ -4,11 +4,12 @@ var Utils = require("./../utils") ...@@ -4,11 +4,12 @@ var Utils = require("./../utils")
module.exports = (function() { module.exports = (function() {
var BelongsTo = function(source, target, options) { var BelongsTo = function(source, target, options) {
this.associationType = 'BelongsTo' this.associationType = 'BelongsTo'
this.source = source this.source = source
this.target = target this.target = target
this.options = options this.options = options
this.isSelfAssociation = (this.source.tableName == this.target.tableName) this.isSingleAssociation = true
this.isSelfAssociation = (this.source.tableName == this.target.tableName)
if (this.isSelfAssociation && !this.options.foreignKey && !!this.options.as) { if (this.isSelfAssociation && !this.options.foreignKey && !!this.options.as) {
this.options.foreignKey = Utils._.underscoredIf(Utils.singularize(this.options.as, this.source.options.language) + "Id", this.source.options.underscored) this.options.foreignKey = Utils._.underscoredIf(Utils.singularize(this.options.as, this.source.options.language) + "Id", this.source.options.underscored)
......
...@@ -17,6 +17,7 @@ module.exports = (function() { ...@@ -17,6 +17,7 @@ module.exports = (function() {
this.options = options this.options = options
this.sequelize = source.daoFactoryManager.sequelize this.sequelize = source.daoFactoryManager.sequelize
this.through = options.through this.through = options.through
this.isMultiAssociation = true
this.isSelfAssociation = (this.source.tableName === this.target.tableName) this.isSelfAssociation = (this.source.tableName === this.target.tableName)
this.doubleLinked = false this.doubleLinked = false
this.combinedTableName = Utils.combineTableNames( this.combinedTableName = Utils.combineTableNames(
......
...@@ -4,11 +4,12 @@ var Utils = require("./../utils") ...@@ -4,11 +4,12 @@ var Utils = require("./../utils")
module.exports = (function() { module.exports = (function() {
var HasOne = function(srcDAO, targetDAO, options) { var HasOne = function(srcDAO, targetDAO, options) {
this.associationType = 'HasOne' this.associationType = 'HasOne'
this.source = srcDAO this.source = srcDAO
this.target = targetDAO this.target = targetDAO
this.options = options this.options = options
this.isSelfAssociation = (this.source.tableName == this.target.tableName) this.isSingleAssociation = true
this.isSelfAssociation = (this.source.tableName == this.target.tableName)
if (this.isSelfAssociation && !this.options.foreignKey && !!this.options.as) { if (this.isSelfAssociation && !this.options.foreignKey && !!this.options.as) {
this.options.foreignKey = Utils._.underscoredIf(Utils.singularize(this.options.as, this.target.options.language) + "Id", this.options.underscored) this.options.foreignKey = Utils._.underscoredIf(Utils.singularize(this.options.as, this.target.options.language) + "Id", this.options.underscored)
......
...@@ -561,8 +561,9 @@ module.exports = (function() { ...@@ -561,8 +561,9 @@ module.exports = (function() {
if (options.hasOwnProperty('include') && (!options.includeValidated || !options.includeNames)) { if (options.hasOwnProperty('include') && (!options.includeValidated || !options.includeNames)) {
options.includeNames = [] options.includeNames = []
options.include = options.include.map(function(include) { options.include = options.include.map(function(include) {
include = validateIncludedElement.call(this, include)
options.includeNames.push(include.as) options.includeNames.push(include.as)
return validateIncludedElement.call(this, include) return include
}.bind(this)) }.bind(this))
} }
...@@ -1223,6 +1224,11 @@ module.exports = (function() { ...@@ -1223,6 +1224,11 @@ module.exports = (function() {
var usesAlias = (include.as !== include.daoFactory.tableName) var usesAlias = (include.as !== include.daoFactory.tableName)
, association = (usesAlias ? this.getAssociationByAlias(include.as) : this.getAssociation(include.daoFactory)) , association = (usesAlias ? this.getAssociationByAlias(include.as) : this.getAssociation(include.daoFactory))
// If single (1:1) association, we singularize the alias, so it will match the automatically generated alias of belongsTo/HasOne
if (association && !usesAlias && association.isSingleAssociation) {
include.as = Utils.singularize(include.daoFactory.tableName, include.daoFactory.options.language)
}
// check if the current daoFactory is actually associated with the passed daoFactory // check if the current daoFactory is actually associated with the passed daoFactory
if (!!association && (!association.options.as || (association.options.as === include.as))) { if (!!association && (!association.options.as || (association.options.as === include.as))) {
include.association = association include.association = association
......
...@@ -530,7 +530,7 @@ module.exports = (function() { ...@@ -530,7 +530,7 @@ module.exports = (function() {
for (key in attrs) { for (key in attrs) {
if (options.include && options.includeNames.indexOf(key) !== -1) { if (options.include && options.includeNames.indexOf(key) !== -1) {
var include = _.find(options.include, function (include) { var include = _.find(options.include, function (include) {
return include.as === key return include.as === key || (include.as.slice(0,1).toLowerCase() + include.as.slice(1)) === key
}) })
var association = include.association var association = include.association
, self = this , self = this
...@@ -544,7 +544,7 @@ module.exports = (function() { ...@@ -544,7 +544,7 @@ module.exports = (function() {
var daoInstance = include.daoFactory.build(data, { isNewRecord: false, isDirty: false }) var daoInstance = include.daoFactory.build(data, { isNewRecord: false, isDirty: false })
, isEmpty = !Utils.firstValueOfHash(daoInstance.identifiers) , isEmpty = !Utils.firstValueOfHash(daoInstance.identifiers)
if (['BelongsTo', 'HasOne'].indexOf(association.associationType) > -1) { if (association.isSingleAssociation) {
accessor = Utils.singularize(accessor, self.sequelize.language) accessor = Utils.singularize(accessor, self.sequelize.language)
self.dataValues[accessor] = isEmpty ? null : daoInstance self.dataValues[accessor] = isEmpty ? null : daoInstance
self[accessor] = self.dataValues[accessor] self[accessor] = self.dataValues[accessor]
......
...@@ -368,6 +368,49 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -368,6 +368,49 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
expect(p.price2).to.equal(20) expect(p.price2).to.equal(20)
done() done()
}) })
/*describe('include', function () {
it('should support basic includes', function () {
var Product = this.sequelize.define('Product', {
title: Sequelize.STRING
})
var Tag = this.sequelize.define('Tag', {
name: Sequelize.STRING
})
var User = this.sequelize.define('User', {
first_name: Sequelize.STRING,
last_name: Sequelize.STRING
})
Product.hasMany(Tag)
Product.belongsTo(User)
var product = Product.build({
id: 1,
title: 'Chair',
tags: [
{id: 1, name: 'Alpha'},
{id: 2, name: 'Beta'}
],
user: {
id: 1,
first_name: 'Mick',
last_name: 'Hansen'
}
}, {
include: [
User,
Tag
]
})
expect(product.tags).to.be.ok
expect(product.tags.length).to.equal(2)
expect(product.tags[0].Model).to.equal(Tag)
expect(product.user).to.be.ok
expect(product.user.Model).to.equal(User)
})
})*/
}) })
describe('findOrInitialize', function() { describe('findOrInitialize', function() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!