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

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() {
var self = this
, errors = {}
// for each field and value
Utils._.each(this.model.dataValues, function(value, field) {
var rawAttribute = self.model.rawAttributes[field]
Utils._.each(this.model.rawAttributes, function(rawAttribute, field) {
var value = self.model.dataValues[field] || undefined
, hasAllowedNull = ((rawAttribute === undefined || rawAttribute.allowNull === true) && ((value === null) || (value === undefined)))
, isSkipped = self.options.skip.length > 0 && self.options.skip.indexOf(field) === -1
......
......@@ -175,20 +175,33 @@ module.exports = (function() {
},
bulkInsertQuery: function(tableName, attrValueHashes) {
var tuples = []
var query = "INSERT INTO <%= table %> (<%= attributes %>) VALUES <%= 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("(" +
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))
var table = this.quoteIdentifier(tableName)
var attributes = Object.keys(attrValueHashes[0]).map(function(attr){return this.quoteIdentifier(attr)}.bind(this)).join(",")
var query = "INSERT INTO " + table + " (" + attributes + ") VALUES " + tuples.join(",") + ";"
var replacements = {
table: this.quoteIdentifier(tableName),
attributes: allAttributes.map(function(attr){
return this.quoteIdentifier(attr)
}.bind(this)).join(","),
tuples: tuples
}
return query
return Utils._.template(query)(replacements)
},
updateQuery: function(tableName, attrValueHash, where, options) {
......
......@@ -296,31 +296,34 @@ module.exports = (function() {
var query = "INSERT INTO <%= table %> (<%= attributes %>) VALUES <%= tuples %> RETURNING *;"
, tuples = []
, serials = []
, allAttributes = []
Utils._.forEach(attrValueHashes, function(attrValueHash, i) {
if (i === 0) {
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 (['bigserial', 'serial'].indexOf(tables[tableName][key]) !== -1 && serials.indexOf(key) === -1) {
serials.push(key)
}
}
})
}
})
Utils._.forEach(attrValueHashes, function(attrValueHash, i) {
tuples.push("(" +
Utils._.map(attrValueHash, function(value, key){
allAttributes.map(function (key) {
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))
var replacements = {
table: this.quoteIdentifiers(tableName)
, attributes: Object.keys(attrValueHashes[0]).map(function(attr){
, attributes: allAttributes.map(function(attr){
return this.quoteIdentifier(attr)
}.bind(this)).join(",")
, tuples: tuples.join(",")
......
......@@ -175,18 +175,27 @@ module.exports = (function() {
bulkInsertQuery: function(tableName, attrValueHashes) {
var query = "INSERT INTO <%= table %> (<%= attributes %>) VALUES <%= 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("(" +
Utils._.values(attrValueHash).map(function(value){
return this.escape(value)
allAttributes.map(function (key) {
return this.escape(attrValueHash[key])
}.bind(this)).join(",") +
")")
}.bind(this))
var replacements = {
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
}
......
......@@ -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
, data = [{username: 'Peter', secretValue: '42' },
{username: 'Paul'},
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!