Permalink
Browse files

Merge branch 'fracture' of git://github.com/durango/sequelize into du…

…rango-fracture
  • Loading branch information...
2 parents 322e1bb + 01abe94 commit 5a1631eefb42b6fbbd79867cbe42fec5eac21284 @sdepold committed Jan 15, 2013
Showing with 38 additions and 7 deletions.
  1. +14 −4 lib/dao-factory.js
  2. +6 −2 lib/dao.js
  3. +18 −1 spec/dao.spec.js
View
@@ -15,7 +15,8 @@ module.exports = (function() {
freezeTableName: false,
underscored: false,
syncOnAssociation: true,
- paranoid: false
+ paranoid: false,
+ whereCollection: null
}, options || {})
this.name = name
@@ -54,7 +55,7 @@ module.exports = (function() {
this.primaryKeys = {};
Utils._.each(this.attributes, function(dataTypeString, attributeName) {
// If you don't specify a valid data type lets help you debug it
- if( dataTypeString === undefined ) throw new Error("Unrecognized data type for field " + attributeName );
+ if( dataTypeString === undefined ) throw new Error("Unrecognized data type for field " + attributeName );
if((attributeName != 'id') && (dataTypeString.indexOf('PRIMARY KEY') !== -1)) {
self.primaryKeys[attributeName] = dataTypeString
}
@@ -150,7 +151,10 @@ module.exports = (function() {
if (!options.include[daoName]) {
options.include[daoName] = this.getAssociationByAlias(daoName).target
}
- }.bind(this))
+ }.bind(this));
+
+ // whereCollection is used for non-primary key updates
+ this.options.whereCollection = options.where || null;
}
return this.QueryInterface.select(this, this.tableName, options, { type: 'SELECT', hasJoin: hasJoin })
@@ -161,6 +165,9 @@ module.exports = (function() {
var optcpy = Utils._.clone(options)
optcpy.attributes = optcpy.attributes || [Utils.addTicks(this.tableName)+".*"]
+ // whereCollection is used for non-primary key updates
+ this.options.whereCollection = optcpy.where || null;
+
return this.QueryInterface.select(this, [this.tableName, joinTableName], optcpy, { type: 'SELECT' })
}
@@ -210,7 +217,10 @@ module.exports = (function() {
if (!options.include[daoName]) {
options.include[daoName] = this.getAssociationByAlias(daoName).target
}
- }.bind(this))
+ }.bind(this));
+
+ // whereCollection is used for non-primary key updates
+ this.options.whereCollection = options.where || null;
}
options.limit = 1
View
@@ -121,8 +121,12 @@ module.exports = (function() {
if(this.isNewRecord) {
return this.QueryInterface.insert(this, this.__factory.tableName, values)
} else {
- var identifier = this.__options.hasPrimaryKeys ? this.primaryKeyValues : this.id
- , tableName = this.__factory.tableName
+ var identifier = this.__options.hasPrimaryKeys ? this.primaryKeyValues : this.id;
+
+ if (identifier === null && this.__options.whereCollection !== null)
+ identifier = this.__options.whereCollection;
+
+ var tableName = this.__factory.tableName
, query = this.QueryInterface.update(this, tableName, values, identifier)
return query
View
@@ -19,9 +19,17 @@ describe(Helpers.getTestDialectTeaser("DAO"), function() {
touchedAt: { type: DataTypes.DATE, defaultValue: DataTypes.NOW },
aNumber: { type: DataTypes.INTEGER }
})
+
+ self.HistoryLog = sequelize.define('HistoryLog', {
+ someText: { type: DataTypes.STRING },
+ aNumber: { type: DataTypes.INTEGER },
+ aRandomId: { type: DataTypes.INTEGER }
+ })
},
onComplete: function() {
- self.User.sync({ force: true }).success(done)
+ self.User.sync({ force: true }).success(function(){
+ self.HistoryLog.sync({ force: true }).success(done)
+ })
}
})
})
@@ -67,6 +75,15 @@ describe(Helpers.getTestDialectTeaser("DAO"), function() {
done()
})
})
+
+ it('saves a record with no primary key', function(done){
+ this.HistoryLog.create({ someText: 'Some random text', aNumber: 3, aRandomId: 5 }).success(function(log) {
+ log.updateAttributes({ aNumber: 5 }).success(function(newLog){
+ expect(newLog.aNumber).toEqual(5)
+ done()
+ })
+ })
+ })
})
describe('toJSON', function toJSON() {

0 comments on commit 5a1631e

Please sign in to comment.