Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added the ability to update records without a primary key but used a …

…where option when selecting with updateAttribute
  • Loading branch information...
commit 7287f33266c1cf20ef03e26173044bf8f50535ec 1 parent 5fa778e
Daniel Durante authored
Showing with 18 additions and 4 deletions.
  1. +12 −2 lib/dao-factory.js
  2. +6 −2 lib/dao.js
14 lib/dao-factory.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
}
@@ -153,6 +154,9 @@ module.exports = (function() {
}.bind(this))
}
+ // whereCollection is used for non-primary key updates
+ this.options.whereCollection = optcpy.where || undefined;
+
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 || undefined;
+
return this.QueryInterface.select(this, [this.tableName, joinTableName], optcpy, { type: 'SELECT' })
}
@@ -215,6 +222,9 @@ module.exports = (function() {
options.limit = 1
+ // whereCollection is used for non-primary key updates
+ this.options.whereCollection = optcpy.where || undefined;
+
return this.QueryInterface.select(this, this.tableName, options, { plain: true, type: 'SELECT', hasJoin: hasJoin })
}
8 lib/dao.js
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
Please sign in to comment.
Something went wrong with that request. Please try again.