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

Commit defcb043 by Jan Aagaard Meier

cherry picking done

1 parent c76d575d
......@@ -23,10 +23,19 @@ module.exports = (function() {
// the id is in the target table
HasOne.prototype.injectAttributes = function() {
var newAttributes = {}
var newAttributes = {},
self = this
this.identifier = this.options.foreignKey || Utils._.underscoredIf(Utils.singularize(this.source.tableName) + "Id", this.options.underscored)
if(Utils.isHash(this.identifier)) {
Utils._.each(this.identifier, function(elem, key) {
newAttributes[key] = self.identifier[key].type || DataTypes.INTEGER
})
} else {
newAttributes[this.identifier] = { type: DataTypes.INTEGER }
}
Utils._.extend(this.target.rawAttributes, newAttributes)
return this
......@@ -37,9 +46,8 @@ module.exports = (function() {
obj[this.accessors.get] = function() {
var id = obj.id
, where = {}
, where = Utils.setAttributes({}, self.identifier, obj)
where[self.identifier] = id
return self.target.find({where: where})
}
......@@ -53,16 +61,11 @@ module.exports = (function() {
var customEventEmitter = new Utils.CustomEventEmitter(function() {
obj[self.accessors.get]().success(function(oldObj) {
if(oldObj) {
<<<<<<< HEAD
oldObj[self.identifier] = null
=======
console.log(self.options);
oldObj = Utils.setAttributes(oldObj, self.identifier, null)
>>>>>>> 03b8bfa... option renamed to omitNull. Ignores both null and undefined. Tested and working for postgres and sqlite
oldObj = Utils.setAttributes(oldObj, self.identifier, self.options.omitNull ? '' : null)
oldObj.save()
}
associatedObject[self.identifier] = obj.id
associatedObject = Utils.setAttributes(associatedObject, self.identifier, obj)
associatedObject.save()
.success(function() { customEventEmitter.emit('success', associatedObject) })
.error(function(err) { customEventEmitter.emit('failure', err) })
......
......@@ -19,7 +19,8 @@ module.exports = (function() {
define: {},
query: {},
sync: {},
logging: console.log
logging: console.log,
omitNull: false
}, options || {})
if(this.options.logging === true) {
......@@ -47,7 +48,7 @@ module.exports = (function() {
Sequelize.Utils._.map(DataTypes, function(sql, accessor) { Sequelize[accessor] = sql})
Sequelize.prototype.getQueryInterface = function() {
this.queryInterface = this.queryInterface || new QueryInterface(this)
this.queryInterface = this.queryInterface || new QueryInterface(this)
return this.queryInterface
}
......@@ -65,6 +66,7 @@ module.exports = (function() {
if(this.options.define)
options = Sequelize.Utils.merge(options, this.options.define)
options.omitNull = this.options.omitNull
var factory = new DAOFactory(daoName, attributes, options)
......
......@@ -97,6 +97,19 @@ var Utils = module.exports = {
toDefaultValue: function(value) {
return (value == DataTypes.NOW) ? new Date() : value
},
setAttributes: function(hash, identifier, instance, prefix) {
prefix = prefix || ''
if (this.isHash(identifier)) {
this._.each(identifier, function(elem, key) {
hash[prefix + key] = Utils._.isString(instance) ? instance : Utils._.isObject(instance) ? instance[elem.key || elem] : null
})
} else {
hash[prefix + identifier] = Utils._.isString(instance) ? instance : Utils._.isObject(instance) ? instance.id : null
}
return hash
}
}
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!