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

Commit defcb043 by Jan Aagaard Meier

cherry picking done

1 parent c76d575d
......@@ -23,12 +23,21 @@ 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)
newAttributes[this.identifier] = { type: DataTypes.INTEGER }
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[self.identifier] = id
, where = Utils.setAttributes({}, self.identifier, obj)
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) })
......@@ -75,4 +78,4 @@ module.exports = (function() {
}
return HasOne
})()
})()
\ No newline at end of file
......@@ -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)
......@@ -127,4 +129,4 @@ module.exports = (function() {
}
return Sequelize
})()
})()
\ No newline at end of file
......@@ -97,9 +97,22 @@ 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
}
}
Utils.CustomEventEmitter = require("./emitters/custom-event-emitter")
Utils.QueryChainer = require("./query-chainer")
Utils.Lingo = require("lingo")
Utils.Lingo = require("lingo")
\ No newline at end of file
......@@ -575,7 +575,7 @@ describe('DAO', function() {
it('returns a response that can be stringified', function() {
var self = this
var User = sequelize.define('User', {
username: Sequelize.STRING, age: Sequelize.INTEGER, isAdmin: Sequelize.BOOLEAN
}, { timestamps: false, logging: false })
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!