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

Commit eb77dfb9 by Mick Hansen

Fix validator so it loops on rawAttributes and not dataValues (then we dont need…

… to have the value defined to validate)
1 parent 086d94b9
...@@ -70,9 +70,8 @@ var validateAttributes = function() { ...@@ -70,9 +70,8 @@ var validateAttributes = function() {
var self = this var self = this
, errors = {} , errors = {}
// for each field and value Utils._.each(this.model.rawAttributes, function(rawAttribute, field) {
Utils._.each(this.model.dataValues, function(value, field) { var value = self.model.dataValues[field] || undefined
var rawAttribute = self.model.rawAttributes[field]
, hasAllowedNull = ((rawAttribute === undefined || rawAttribute.allowNull === true) && ((value === null) || (value === undefined))) , hasAllowedNull = ((rawAttribute === undefined || rawAttribute.allowNull === true) && ((value === null) || (value === undefined)))
, isSkipped = self.options.skip.length > 0 && self.options.skip.indexOf(field) === -1 , isSkipped = self.options.skip.length > 0 && self.options.skip.indexOf(field) === -1
......
...@@ -175,20 +175,33 @@ module.exports = (function() { ...@@ -175,20 +175,33 @@ module.exports = (function() {
}, },
bulkInsertQuery: function(tableName, attrValueHashes) { bulkInsertQuery: function(tableName, attrValueHashes) {
var tuples = [] var query = "INSERT INTO <%= table %> (<%= attributes %>) VALUES <%= tuples %>;"
, tuples = []
, allAttributes = []
Utils._.forEach(attrValueHashes, function(attrValueHash, i) {
Utils._.forEach(attrValueHash, function(value, key, hash) {
if (allAttributes.indexOf(key) === -1) allAttributes.push(key)
})
})
Utils._.forEach(attrValueHashes, function(attrValueHash) { Utils._.forEach(attrValueHashes, function(attrValueHash, i) {
tuples.push("(" + tuples.push("(" +
Utils._.values(attrValueHash).map(function(v) { return this.escape(v) }.bind(this)).join(",") + allAttributes.map(function (key) {
return this.escape(attrValueHash[key])
}.bind(this)).join(",") +
")") ")")
}.bind(this)) }.bind(this))
var table = this.quoteIdentifier(tableName) var replacements = {
var attributes = Object.keys(attrValueHashes[0]).map(function(attr){return this.quoteIdentifier(attr)}.bind(this)).join(",") table: this.quoteIdentifier(tableName),
attributes: allAttributes.map(function(attr){
var query = "INSERT INTO " + table + " (" + attributes + ") VALUES " + tuples.join(",") + ";" return this.quoteIdentifier(attr)
}.bind(this)).join(","),
tuples: tuples
}
return query return Utils._.template(query)(replacements)
}, },
updateQuery: function(tableName, attrValueHash, where, options) { updateQuery: function(tableName, attrValueHash, where, options) {
......
...@@ -293,34 +293,37 @@ module.exports = (function() { ...@@ -293,34 +293,37 @@ module.exports = (function() {
}, },
bulkInsertQuery: function(tableName, attrValueHashes) { bulkInsertQuery: function(tableName, attrValueHashes) {
var query = "INSERT INTO <%= table %> (<%= attributes %>) VALUES <%= tuples %> RETURNING *;" var query = "INSERT INTO <%= table %> (<%= attributes %>) VALUES <%= tuples %> RETURNING *;"
, tuples = [] , tuples = []
, serials = [] , serials = []
, allAttributes = []
Utils._.forEach(attrValueHashes, function(attrValueHash, i) { Utils._.forEach(attrValueHashes, function(attrValueHash, i) {
if (i === 0) { Utils._.forEach(attrValueHash, function(value, key, hash) {
Utils._.forEach(attrValueHash, function(value, key, hash) { if (allAttributes.indexOf(key) === -1) allAttributes.push(key)
if (tables[tableName] && tables[tableName][key]) {
if (['bigserial', 'serial'].indexOf(tables[tableName][key]) !== -1) { if (tables[tableName] && tables[tableName][key]) {
serials.push(key) if (['bigserial', 'serial'].indexOf(tables[tableName][key]) !== -1 && serials.indexOf(key) === -1) {
} serials.push(key)
} }
}) }
} })
})
Utils._.forEach(attrValueHashes, function(attrValueHash, i) {
tuples.push("(" + tuples.push("(" +
Utils._.map(attrValueHash, function(value, key){ allAttributes.map(function (key) {
if (serials.indexOf(key) !== -1) { if (serials.indexOf(key) !== -1) {
return value || 'DEFAULT'; return attrValueHash[key] || 'DEFAULT';
} }
return this.escape(value) return this.escape(attrValueHash[key])
}.bind(this)).join(",") + }.bind(this)).join(",") +
")") ")")
}.bind(this)) }.bind(this))
var replacements = { var replacements = {
table: this.quoteIdentifiers(tableName) table: this.quoteIdentifiers(tableName)
, attributes: Object.keys(attrValueHashes[0]).map(function(attr){ , attributes: allAttributes.map(function(attr){
return this.quoteIdentifier(attr) return this.quoteIdentifier(attr)
}.bind(this)).join(",") }.bind(this)).join(",")
, tuples: tuples.join(",") , tuples: tuples.join(",")
......
...@@ -175,18 +175,27 @@ module.exports = (function() { ...@@ -175,18 +175,27 @@ module.exports = (function() {
bulkInsertQuery: function(tableName, attrValueHashes) { bulkInsertQuery: function(tableName, attrValueHashes) {
var query = "INSERT INTO <%= table %> (<%= attributes %>) VALUES <%= tuples %>;" var query = "INSERT INTO <%= table %> (<%= attributes %>) VALUES <%= tuples %>;"
, tuples = [] , tuples = []
, allAttributes = []
Utils._.forEach(attrValueHashes, function(attrValueHash) { Utils._.forEach(attrValueHashes, function(attrValueHash, i) {
Utils._.forEach(attrValueHash, function(value, key, hash) {
if (allAttributes.indexOf(key) === -1) allAttributes.push(key)
})
})
Utils._.forEach(attrValueHashes, function(attrValueHash, i) {
tuples.push("(" + tuples.push("(" +
Utils._.values(attrValueHash).map(function(value){ allAttributes.map(function (key) {
return this.escape(value) return this.escape(attrValueHash[key])
}.bind(this)).join(",") + }.bind(this)).join(",") +
")") ")")
}.bind(this)) }.bind(this))
var replacements = { var replacements = {
table: this.quoteIdentifier(tableName), table: this.quoteIdentifier(tableName),
attributes: Object.keys(attrValueHashes[0]).map(function(attr){return this.quoteIdentifier(attr)}.bind(this)).join(","), attributes: allAttributes.map(function(attr){
return this.quoteIdentifier(attr)
}.bind(this)).join(","),
tuples: tuples tuples: tuples
} }
......
...@@ -700,7 +700,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -700,7 +700,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
}) })
}) })
it.only('properly handles disparate field lists', function(done) { it('properly handles disparate field lists', function(done) {
var self = this var self = this
, data = [{username: 'Peter', secretValue: '42' }, , data = [{username: 'Peter', secretValue: '42' },
{username: 'Paul'}, {username: 'Paul'},
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!