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

Commit defcb043 by Jan Aagaard Meier

cherry picking done

1 parent c76d575d
...@@ -23,12 +23,21 @@ module.exports = (function() { ...@@ -23,12 +23,21 @@ module.exports = (function() {
// the id is in the target table // the id is in the target table
HasOne.prototype.injectAttributes = function() { 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) 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) Utils._.extend(this.target.rawAttributes, newAttributes)
return this return this
} }
...@@ -37,9 +46,8 @@ module.exports = (function() { ...@@ -37,9 +46,8 @@ module.exports = (function() {
obj[this.accessors.get] = function() { obj[this.accessors.get] = function() {
var id = obj.id var id = obj.id
, where = {} , where = Utils.setAttributes({}, self.identifier, obj)
where[self.identifier] = id
return self.target.find({where: where}) return self.target.find({where: where})
} }
...@@ -53,16 +61,11 @@ module.exports = (function() { ...@@ -53,16 +61,11 @@ module.exports = (function() {
var customEventEmitter = new Utils.CustomEventEmitter(function() { var customEventEmitter = new Utils.CustomEventEmitter(function() {
obj[self.accessors.get]().success(function(oldObj) { obj[self.accessors.get]().success(function(oldObj) {
if(oldObj) { if(oldObj) {
<<<<<<< HEAD oldObj = Utils.setAttributes(oldObj, self.identifier, self.options.omitNull ? '' : null)
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.save() oldObj.save()
} }
associatedObject[self.identifier] = obj.id associatedObject = Utils.setAttributes(associatedObject, self.identifier, obj)
associatedObject.save() associatedObject.save()
.success(function() { customEventEmitter.emit('success', associatedObject) }) .success(function() { customEventEmitter.emit('success', associatedObject) })
.error(function(err) { customEventEmitter.emit('failure', err) }) .error(function(err) { customEventEmitter.emit('failure', err) })
...@@ -75,4 +78,4 @@ module.exports = (function() { ...@@ -75,4 +78,4 @@ module.exports = (function() {
} }
return HasOne return HasOne
})() })()
\ No newline at end of file
...@@ -19,7 +19,8 @@ module.exports = (function() { ...@@ -19,7 +19,8 @@ module.exports = (function() {
define: {}, define: {},
query: {}, query: {},
sync: {}, sync: {},
logging: console.log logging: console.log,
omitNull: false
}, options || {}) }, options || {})
if(this.options.logging === true) { if(this.options.logging === true) {
...@@ -47,7 +48,7 @@ module.exports = (function() { ...@@ -47,7 +48,7 @@ module.exports = (function() {
Sequelize.Utils._.map(DataTypes, function(sql, accessor) { Sequelize[accessor] = sql}) Sequelize.Utils._.map(DataTypes, function(sql, accessor) { Sequelize[accessor] = sql})
Sequelize.prototype.getQueryInterface = function() { Sequelize.prototype.getQueryInterface = function() {
this.queryInterface = this.queryInterface || new QueryInterface(this) this.queryInterface = this.queryInterface || new QueryInterface(this)
return this.queryInterface return this.queryInterface
} }
...@@ -65,6 +66,7 @@ module.exports = (function() { ...@@ -65,6 +66,7 @@ module.exports = (function() {
if(this.options.define) if(this.options.define)
options = Sequelize.Utils.merge(options, this.options.define) options = Sequelize.Utils.merge(options, this.options.define)
options.omitNull = this.options.omitNull
var factory = new DAOFactory(daoName, attributes, options) var factory = new DAOFactory(daoName, attributes, options)
...@@ -127,4 +129,4 @@ module.exports = (function() { ...@@ -127,4 +129,4 @@ module.exports = (function() {
} }
return Sequelize return Sequelize
})() })()
\ No newline at end of file
...@@ -97,9 +97,22 @@ var Utils = module.exports = { ...@@ -97,9 +97,22 @@ var Utils = module.exports = {
toDefaultValue: function(value) { toDefaultValue: function(value) {
return (value == DataTypes.NOW) ? new Date() : 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.CustomEventEmitter = require("./emitters/custom-event-emitter")
Utils.QueryChainer = require("./query-chainer") 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() { ...@@ -575,7 +575,7 @@ describe('DAO', function() {
it('returns a response that can be stringified', function() { it('returns a response that can be stringified', function() {
var self = this var self = this
var User = sequelize.define('User', { var User = sequelize.define('User', {
username: Sequelize.STRING, age: Sequelize.INTEGER, isAdmin: Sequelize.BOOLEAN username: Sequelize.STRING, age: Sequelize.INTEGER, isAdmin: Sequelize.BOOLEAN
}, { timestamps: false, logging: false }) }, { 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!