sails-disk
Advanced tools
Comparing version 0.10.5 to 0.10.6
@@ -444,3 +444,12 @@ /** | ||
// If uniqueness constraints were violated, send back a validation error. | ||
var violations = self.enforceUniqueness(collectionName, values); | ||
var pkAttrName = getPrimaryKey(this.schema[collectionName]); | ||
// console.log('pkAttrName',pkAttrName); | ||
var pkValueOfFirstRecordInResultSet = (function (){ | ||
// console.log('resultSet',resultSet.results); | ||
if (resultSet.results && resultSet.results[0] && resultSet.results[0][pkAttrName]) { | ||
return resultSet.results[0][pkAttrName]; | ||
} | ||
return undefined; | ||
})(); | ||
var violations = self.enforceUniqueness(collectionName, values, pkValueOfFirstRecordInResultSet); | ||
if (violations.length) { | ||
@@ -591,2 +600,3 @@ return cb(new UniquenessError(violations)); | ||
* @param {Object} values - attribute values for a single record | ||
* @param {*} pkValueOfFirstRecordInResultSet - (optional) the primary key value of the record we're updating | ||
* @return {Object} | ||
@@ -596,3 +606,3 @@ * @api private | ||
Database.prototype.enforceUniqueness = function(collectionName, values) { | ||
Database.prototype.enforceUniqueness = function(collectionName, values, pkValueOfFirstRecordInResultSet) { | ||
@@ -610,2 +620,3 @@ var errors = []; | ||
// Loop through each record in our database | ||
for (var index in this.data[collectionName]) { | ||
@@ -617,2 +628,24 @@ | ||
// If `pkValueOfFirstRecordInResultSet` was provided, this must be an update, | ||
// and we're updating the record with this pk value. | ||
// e.g. if I'm updating record w/ id=3 to have email='foo@foo.com', | ||
// but it already has an email='foo@foo.com', then this is not a | ||
// uniqueness violation. | ||
// | ||
// (note that if I'm updating multiple records but providing only one value for | ||
// a unique attribute, this will inevitably fail anyways-- this is why we only care | ||
// about the first record in the result set) | ||
if (pkValueOfFirstRecordInResultSet) { | ||
var existingRecord = _.find(this.data[collectionName], function (record){ | ||
return record[pkAttrName] === pkValueOfFirstRecordInResultSet; | ||
}); | ||
if (existingRecord) { | ||
// It isn't actually a uniquness violation if the record | ||
// we're checking is the same as the record we're updating/creating | ||
if (values[attrName] === existingRecord[attrName]){ | ||
continue; | ||
} | ||
} | ||
} | ||
// Does it look like a "uniqueness violation"? | ||
@@ -619,0 +652,0 @@ if (values[attrName] === this.data[collectionName][index][attrName]) { |
{ | ||
"name": "sails-disk", | ||
"version": "0.10.5", | ||
"version": "0.10.6", | ||
"description": "Persistent local-disk adapter for Sails.js / Waterline", | ||
@@ -5,0 +5,0 @@ "main": "lib/adapter.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
32954
897