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

Commit a0512041 by Mick Hansen

non-integer/non-id primary key support for has one

1 parent 8b036963
......@@ -21,7 +21,6 @@ module.exports = (function() {
}
if (!this.options.foreignKey) {
console.log(this.as);
this.options.foreignKey = Utils._.camelizeIf(
[this.as, this.target.primaryKeyAttribute].join("_"),
!this.source.options.underscored
......@@ -60,9 +59,15 @@ module.exports = (function() {
var association = this
instancePrototype[this.accessors.get] = function(params) {
var where = {}
params = params || {}
params.where = params.where || {}
params.where[association.targetIdentifier] = this.get(association.identifier)
params.where = [params.where] || []
where[association.targetIdentifier] = this.get(association.identifier)
params.where.push(where)
params.where = new Utils.and(params.where)
return association.target.find(params)
}
......@@ -74,8 +79,8 @@ module.exports = (function() {
BelongsTo.prototype.injectSetter = function(instancePrototype) {
var association = this
instancePrototype[this.accessors.set] = function(instance, options) {
this.set(association.identifier, instance ? instance[association.targetIdentifier] : null)
instancePrototype[this.accessors.set] = function(associatedInstance, options) {
this.set(association.identifier, associatedInstance ? associatedInstance[association.targetIdentifier] : null)
options = Utils._.extend({
fields: [ association.identifier ],
......
......@@ -10,12 +10,8 @@ module.exports = (function() {
this.target = targetDAO
this.options = options
this.isSingleAssociation = true
this.isSelfAssociation = (this.source.name == this.target.name)
this.as = this.options.as
if (this.isSelfAssociation && !this.options.foreignKey && !!this.as) {
this.options.foreignKey = Utils._.underscoredIf(Utils.singularize(this.as, this.target.options.language) + "Id", this.target.options.underscored)
}
this.isSelfAssociation = (this.source == this.target)
this.as = this.options.as
if (this.as) {
this.isAliased = true
......@@ -23,10 +19,16 @@ module.exports = (function() {
this.as = Utils.singularize(this.target.name, this.target.options.language)
}
this.associationAccessor = this.isSelfAssociation
? Utils.combineTableNames(this.target.name, this.as)
: this.as
if (!this.options.foreignKey) {
this.options.foreignKey = Utils._.camelizeIf(
[this.source.name, this.source.primaryKeyAttribute].join("_"),
!this.source.options.underscored
)
}
this.identifier = this.options.foreignKey
this.sourceIdentifier = this.source.primaryKeyAttribute
this.associationAccessor = this.as
this.options.useHooks = options.useHooks
this.accessors = {
......@@ -39,10 +41,8 @@ module.exports = (function() {
// the id is in the target table
HasOne.prototype.injectAttributes = function() {
var newAttributes = {}
, sourceKeys = Object.keys(this.source.primaryKeys)
, keyType = ((this.source.hasPrimaryKeys && sourceKeys.length === 1) ? this.source.rawAttributes[sourceKeys[0]].type : DataTypes.INTEGER)
, keyType = this.source.rawAttributes[this.sourceIdentifier].type
this.identifier = this.options.foreignKey || Utils._.underscoredIf(Utils.singularize(this.source.name, this.source.options.language) + "Id", this.source.options.underscored)
newAttributes[this.identifier] = { type: this.options.keyType || keyType }
Utils._.defaults(this.target.rawAttributes, newAttributes)
Helpers.addForeignKeyConstraints(this.target.rawAttributes[this.identifier], this.source, this.target, this.options)
......@@ -52,81 +52,54 @@ module.exports = (function() {
return this
}
HasOne.prototype.injectGetter = function(obj) {
var self = this
, smart
HasOne.prototype.injectGetter = function(instancePrototype) {
var association = this
obj[this.accessors.get] = function(params) {
var primaryKeys = Object.keys(this.Model.primaryKeys)
, primaryKey = primaryKeys.length === 1 ? primaryKeys[0] : 'id'
, where = {}
, id = this[primaryKey] || this.id
instancePrototype[this.accessors.get] = function(params) {
var where = {}
where[self.identifier] = id
params = params || {}
params.where = [params.where] || []
if (!Utils._.isUndefined(params)) {
if (!Utils._.isUndefined(params.attributes)) {
params = Utils._.extend({where: where}, params)
}
} else {
params = {where: where}
}
where[association.identifier] = this.get(association.sourceIdentifier)
params.where.push(where)
smart = Utils.smartWhere(params.where || [], self.target.daoFactoryManager.sequelize.options.dialect)
smart = Utils.compileSmartWhere.call(self.target, smart, self.target.daoFactoryManager.sequelize.options.dialect)
if (smart.length > 0) {
params.where = smart
}
params.where = new Utils.and(params.where)
var options = {}
if (params.transaction) {
options.transaction = params.transaction;
delete params.transaction;
}
return self.target.find(params, options)
return association.target.find(params)
}
return this
}
HasOne.prototype.injectSetter = function(obj) {
var self = this
HasOne.prototype.injectSetter = function(instancePrototype) {
var association = this
obj[this.accessors.set] = function(associatedObject, options) {
instancePrototype[this.accessors.set] = function(associatedInstance, options) {
var instance = this
, instanceKeys = Object.keys(instance.Model.primaryKeys)
, instanceKey = instanceKeys.length === 1 ? instanceKeys[0] : 'id'
return new Utils.CustomEventEmitter(function(emitter) {
instance[self.accessors.get]().success(function(oldObj) {
if (oldObj) {
oldObj[self.identifier] = null
oldObj
.save(
Utils._.extend({}, options, {
fields: [self.identifier],
allowNull: [self.identifier],
association: true
})
)
instance[association.accessors.get]().success(function(oldInstance) {
if (oldInstance) {
oldInstance[association.identifier] = null
oldInstance
.save(Utils._.extend({}, options, {
fields: [association.identifier],
allowNull: [association.identifier],
association: true
}))
.success(function() {
if (associatedObject) {
associatedObject[self.identifier] = instance[instanceKey]
associatedObject
.save(options)
.success(function() { emitter.emit('success', associatedObject) })
.error(function(err) { emitter.emit('error', err) })
if (associatedInstance) {
associatedInstance.set(association.identifier, instance.get(association.sourceIdentifier))
associatedInstance.save(options).proxy(emitter)
} else {
emitter.emit('success', null)
}
})
} else {
if (associatedObject) {
associatedObject[self.identifier] = instance[instanceKey]
associatedObject
.save(options)
.success(function() { emitter.emit('success', associatedObject) })
.error(function(err) { emitter.emit('error', err) })
if (associatedInstance) {
associatedInstance.set(association.identifier, instance.get(association.sourceIdentifier))
associatedInstance.save(options).proxy(emitter)
} else {
emitter.emit('success', null)
}
......@@ -138,10 +111,10 @@ module.exports = (function() {
return this
}
HasOne.prototype.injectCreator = function(obj) {
var self = this
HasOne.prototype.injectCreator = function(instancePrototype) {
var association = this
obj[this.accessors.create] = function(values, fieldsOrOptions) {
instancePrototype[this.accessors.create] = function(values, fieldsOrOptions) {
var instance = this
, options = {}
......@@ -150,11 +123,11 @@ module.exports = (function() {
}
return new Utils.CustomEventEmitter(function(emitter) {
self.target
association.target
.create(values, fieldsOrOptions)
.proxy(emitter, { events: ['error', 'sql'] })
.success(function(newAssociatedObject) {
instance[self.accessors.set](newAssociatedObject, options)
.success(function(associationInstance) {
instance[association.accessors.set](associationInstance, options)
.proxy(emitter)
})
}).run()
......
......@@ -3,6 +3,7 @@ var chai = require('chai')
, expect = chai.expect
, Support = require(__dirname + '/../support')
, DataTypes = require(__dirname + "/../../lib/data-types")
, Sequelize = require('../../index')
chai.Assertion.includeStack = true
......@@ -51,6 +52,29 @@ describe(Support.getTestDialectTeaser("BelongsTo"), function() {
})
})
})
it('should be able to handle a where object that\'s a first class citizen.', function(done) {
var User = this.sequelize.define('UserXYZ', { username: Sequelize.STRING, gender: Sequelize.STRING })
, Task = this.sequelize.define('TaskXYZ', { title: Sequelize.STRING, status: Sequelize.STRING })
Task.belongsTo(User)
User.sync({ force: true }).success(function() {
Task.sync({ force: true }).success(function() {
User.create({ username: 'foo', gender: 'male' }).success(function(user) {
User.create({ username: 'bar', gender: 'female' }).success(function(falsePositiveCheck) {
Task.create({ title: 'task', status: 'inactive' }).success(function(task) {
task.setUserXYZ(user).success(function() {
task.getUserXYZ({where: ['gender = ?', 'female']}).success(function(user) {
expect(user).to.be.null
done()
})
})
})
})
})
})
})
})
})
describe('setAssociation', function() {
......
......@@ -370,7 +370,7 @@ describe(Support.getTestDialectTeaser("HasOne"), function() {
Group.hasOne(User)
self.sequelize.sync({ force: true }).success(function() {
expect(User.rawAttributes.GroupPKBTId.type.toString()).to.equal(Sequelize.STRING.toString())
expect(User.rawAttributes.GroupPKBTName.type.toString()).to.equal(Sequelize.STRING.toString())
done()
})
})
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!