waterline
Advanced tools
Comparing version 0.10.29 to 0.10.30
@@ -1,2 +0,1 @@ | ||
/** | ||
@@ -52,6 +51,5 @@ * Module dependencies | ||
return _obj; | ||
} | ||
// Return a nicer error message than just throwing the json parse message | ||
catch (e) { | ||
} catch (e) { | ||
var err = new Error(); | ||
@@ -77,10 +75,10 @@ err.message = 'There was an error turning the model into an object.'; | ||
if(!this.proto._properties) return; | ||
if(!this.proto._properties.showJoins) return; | ||
if (!this.proto._properties) return; | ||
if (!this.proto._properties.showJoins) return; | ||
// Copy prototype over for attributes | ||
for(var association in this.proto.associations) { | ||
for (var association in this.proto.associations) { | ||
// Handle hasMany attributes | ||
if(hasOwnProperty(this.proto.associations[association], 'value')) { | ||
if (hasOwnProperty(this.proto.associations[association], 'value')) { | ||
@@ -91,3 +89,3 @@ var records = []; | ||
values.forEach(function(record) { | ||
if(typeof record !== 'object') return; | ||
if (typeof record !== 'object') return; | ||
// Since `typeof null` === `"object"`, we should also check for that case: | ||
@@ -108,7 +106,20 @@ if (record === null) return; | ||
var record = this.proto[association]; | ||
var item; | ||
// Check if the association foreign key is a date. If so set the object's | ||
// association and continue. Manual check here is needed because _.isObject | ||
// matches dates and you will end up with a loop that never exits. | ||
if (_.isDate(record)) { | ||
item = new Date(record); | ||
_.extend(item.__proto__ , record.__proto__); | ||
this.object[association] = item; | ||
} | ||
// Is the record is a populated object, create a new object from it. | ||
// _.isObject() does not match null, so we're good here. | ||
if(_.isObject(record) && !Array.isArray(record)) { | ||
else if (_.isObject(record) && !Array.isArray(record)) { | ||
var item = Object.create(record.__proto__); | ||
item = Object.create(record.__proto__); | ||
@@ -120,3 +131,5 @@ Object.keys(record).forEach(function(key) { | ||
this.object[association] = item; | ||
} else if (!_.isUndefined(record)) { | ||
} | ||
else if (!_.isUndefined(record)) { | ||
this.object[association] = record; | ||
@@ -139,3 +152,3 @@ } | ||
Object.keys(this.proto).forEach(function(key) { | ||
if(hasOwnProperty(self.object, key)) return; | ||
if (hasOwnProperty(self.object, key)) return; | ||
self.object[key] = _.cloneDeep(self.proto[key]); | ||
@@ -157,4 +170,4 @@ }); | ||
if(!this.proto._properties) return; | ||
if(!this.proto._properties.showJoins) return; | ||
if (!this.proto._properties) return; | ||
if (!this.proto._properties.showJoins) return; | ||
@@ -165,19 +178,19 @@ // Handle Joins | ||
// Don't run toJSON on records that were not populated | ||
if(!self.proto._properties || !self.proto._properties.joins) return; | ||
if (!self.proto._properties || !self.proto._properties.joins) return; | ||
// Build up a join key name based on the attribute's model/collection name | ||
var joinsName = association; | ||
if(self.context._attributes[association].model) joinsName = self.context._attributes[association].model.toLowerCase(); | ||
if(self.context._attributes[association].collection) joinsName = self.context._attributes[association].collection.toLowerCase(); | ||
if (self.context._attributes[association].model) joinsName = self.context._attributes[association].model.toLowerCase(); | ||
if (self.context._attributes[association].collection) joinsName = self.context._attributes[association].collection.toLowerCase(); | ||
// Check if the join was used | ||
if(self.proto._properties.joins.indexOf(joinsName) < 0 && self.proto._properties.joins.indexOf(association) < 0) return; | ||
if (self.proto._properties.joins.indexOf(joinsName) < 0 && self.proto._properties.joins.indexOf(association) < 0) return; | ||
self.usedJoins.push(association); | ||
// Call toJSON on each associated record | ||
if(Array.isArray(self.object[association])) { | ||
if (Array.isArray(self.object[association])) { | ||
var records = []; | ||
self.object[association].forEach(function(item) { | ||
if(!hasOwnProperty(item.__proto__, 'toJSON')) return; | ||
if (!hasOwnProperty(item.__proto__, 'toJSON')) return; | ||
records.push(item.toJSON()); | ||
@@ -190,3 +203,3 @@ }); | ||
if(!self.object[association]) return; | ||
if (!self.object[association]) return; | ||
@@ -200,3 +213,3 @@ // Association was null or not valid | ||
if(!hasOwnProperty(self.object[association].__proto__, 'toJSON')) return; | ||
if (!hasOwnProperty(self.object[association].__proto__, 'toJSON')) return; | ||
self.object[association] = self.object[association].toJSON(); | ||
@@ -217,7 +230,7 @@ }); | ||
for(var attribute in attributes) { | ||
if(!hasOwnProperty(attributes[attribute], 'model') && !hasOwnProperty(attributes[attribute], 'collection')) continue; | ||
for (var attribute in attributes) { | ||
if (!hasOwnProperty(attributes[attribute], 'model') && !hasOwnProperty(attributes[attribute], 'collection')) continue; | ||
// If no properties and a collection attribute, delete the association and return | ||
if(!properties && hasOwnProperty(attributes[attribute], 'collection')) { | ||
if (!properties && hasOwnProperty(attributes[attribute], 'collection')) { | ||
delete this.object[attribute]; | ||
@@ -228,13 +241,13 @@ continue; | ||
// If showJoins is false remove the association object | ||
if(properties && !properties.showJoins) { | ||
if (properties && !properties.showJoins) { | ||
// Don't delete belongs to keys | ||
if(!attributes[attribute].model) delete this.object[attribute]; | ||
if (!attributes[attribute].model) delete this.object[attribute]; | ||
} | ||
if(properties && properties.joins) { | ||
if(this.usedJoins.indexOf(attribute) < 0) { | ||
if (properties && properties.joins) { | ||
if (this.usedJoins.indexOf(attribute) < 0) { | ||
// Don't delete belongs to keys | ||
if(!attributes[attribute].model) delete this.object[attribute]; | ||
if (!attributes[attribute].model) delete this.object[attribute]; | ||
} | ||
@@ -252,4 +265,4 @@ } | ||
toObject.prototype.filterFunctions = function() { | ||
for(var key in this.object) { | ||
if(typeof this.object[key] === 'function') { | ||
for (var key in this.object) { | ||
if (typeof this.object[key] === 'function') { | ||
delete this.object[key]; | ||
@@ -256,0 +269,0 @@ } |
{ | ||
"name": "waterline", | ||
"description": "An ORM for Node.js and the Sails framework", | ||
"version": "0.10.29", | ||
"version": "0.10.30", | ||
"homepage": "http://github.com/balderdashy/waterline", | ||
@@ -6,0 +6,0 @@ "contributors": [ |
9209
351830
100