New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

waterline

Package Overview
Dependencies
Maintainers
3
Versions
165
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

waterline - npm Package Compare versions

Comparing version 0.10.27 to 0.10.28

110

lib/waterline/model/lib/associationMethods/add.js

@@ -36,7 +36,11 @@ /**

if(!this.primaryKey) return cb(new Error('No Primary Key set to associate the record with! ' +
if (!this.primaryKey) {
return cb(new Error('No Primary Key set to associate the record with! ' +
'Try setting an attribute as a primary key or include an ID property.'));
}
if(!proto.toObject()[this.primaryKey]) return cb(new Error('No Primary Key set to associate the record with! ' +
if (!proto.toObject()[this.primaryKey]) {
return cb(new Error('No Primary Key set to associate the record with! ' +
'Primary Key must have a value, it can\'t be an optional value.'));
}

@@ -64,4 +68,4 @@ // Loop through each of the associations on this model and add any associations

for(var attribute in attributes) {
if(hasOwnProperty(attributes[attribute], 'primaryKey') && attributes[attribute].primaryKey) {
for (var attribute in attributes) {
if (hasOwnProperty(attributes[attribute], 'primaryKey') && attributes[attribute].primaryKey) {
primaryKey = attribute;

@@ -73,3 +77,3 @@ break;

// If no primary key check for an ID property
if(!primaryKey && hasOwnProperty(values, 'id')) primaryKey = 'id';
if (!primaryKey && hasOwnProperty(values, 'id')) primaryKey = 'id';

@@ -95,3 +99,3 @@ return primaryKey;

function(err) {
if(err || self.failedTransactions.length > 0) {
if (err || self.failedTransactions.length > 0) {
return cb(null, self.failedTransactions);

@@ -121,3 +125,3 @@ }

var associatedCollection = this.collection.waterline.collections[collectionName];
var relatedPK = _.find(associatedCollection.attributes, { primaryKey: true });
var relatedPK = _.find(associatedCollection.attributes, { primaryKey: true });
var schema = this.collection.waterline.schema[this.collection.identity].attributes[key];

@@ -130,3 +134,3 @@

// This allows new records to be created through the association interface
if(association !== null && typeof association === 'object' && Object.keys(association).length > 0) {
if (association !== null && typeof association === 'object' && Object.keys(association).length > 0) {

@@ -136,13 +140,13 @@ // If a custom PK was used on the associated collection and it's not

// creates to work when custom PK's are used.
if(!relatedPK || !relatedPK.autoIncrement && !associatedCollection.autoPK) {
return self.createNewRecord(associatedCollection, schema, association, next);
if (!relatedPK || !relatedPK.autoIncrement && !associatedCollection.autoPK) {
return self.createNewRecord(associatedCollection, schema, association, key, next);
}
// Check if the record contains a primary key, if so just link the values
if(hasOwnProperty(association, associatedCollection.primaryKey)) {
if (hasOwnProperty(association, associatedCollection.primaryKey)) {
var pk = associatedCollection.primaryKey;
return self.updateRecord(associatedCollection, schema, association[pk], next);
return self.updateRecord(associatedCollection, schema, association[pk], key, next);
}
return self.createNewRecord(associatedCollection, schema, association, next);
return self.createNewRecord(associatedCollection, schema, association, key, next);
}

@@ -153,3 +157,3 @@

// with another collection.
self.updateRecord(associatedCollection, schema, association, next);
self.updateRecord(associatedCollection, schema, association, key, next);

@@ -169,3 +173,3 @@ }, cb);

Add.prototype.createNewRecord = function(collection, attribute, values, cb) {
Add.prototype.createNewRecord = function(collection, attribute, values, key, cb) {
var self = this;

@@ -175,6 +179,6 @@

var schema = this.collection.waterline.schema[attribute.collection.toLowerCase()];
var junctionTable = schema.junctionTable || false;
var junctionTable = schema.junctionTable || schema.throughTable;
// If this isn't a many-to-many then add the foreign key in to the values
if(!junctionTable) {
if (!junctionTable) {
values[attribute.onKey] = this.proto[this.primaryKey];

@@ -184,7 +188,7 @@ }

collection.create(values, function(err, record) {
if(err) {
if (err) {
// If no via was specified and the insert failed on a one-to-many build up an error message that
// properly reflects the error.
if(!junctionTable && !hasOwnProperty(attribute, 'via')) {
if (!junctionTable && !hasOwnProperty(attribute, 'via')) {
err = new Error('You attempted to create a has many relationship but didn\'t link the two ' +

@@ -203,6 +207,6 @@ 'atttributes together. Please setup a link using the via keyword.');

// if no junction table then return
if(!junctionTable) return cb();
if (!junctionTable) return cb();
// if junction table but there was an error don't try and link the records
if(err) return cb();
if (err) return cb();

@@ -212,3 +216,3 @@ // Find the collection's Primary Key value

if(!primaryKey) {
if (!primaryKey) {
self.failedTransactions.push({

@@ -227,3 +231,3 @@ type: 'insert',

// needs to be created to link the two records
self.createManyToMany(joinCollection, attribute, record[primaryKey], cb);
self.createManyToMany(joinCollection, attribute, record[primaryKey], key, cb);
});

@@ -242,3 +246,3 @@ };

Add.prototype.updateRecord = function(collection, attribute, values, cb) {
Add.prototype.updateRecord = function(collection, attribute, pk, key, cb) {
var self = this;

@@ -248,8 +252,8 @@

var schema = this.collection.waterline.schema[attribute.collection.toLowerCase()];
var junctionTable = schema.junctionTable || false;
var junctionTable = schema.junctionTable || schema.throughTable;
// If so build out the criteria and create a new record in the junction table
if(junctionTable) {
if (junctionTable) {
var joinCollection = this.collection.waterline.collections[attribute.collection.toLowerCase()];
return this.createManyToMany(joinCollection, attribute, values, cb);
return this.createManyToMany(joinCollection, attribute, pk, key, cb);
}

@@ -261,5 +265,7 @@

if(!associationKey) return cb(new Error('No Primary Key defined on the child record you ' +
'are trying to associate the record with! Try setting an attribute as a primary key or ' +
'include an ID property.'));
if (!associationKey) {
return cb(new Error('No Primary Key defined on the child record you ' +
'are trying to associate the record with! Try setting an attribute as a primary key or ' +
'include an ID property.'));
}

@@ -270,3 +276,3 @@ // Build up criteria and updated values used to update the record

criteria[associationKey] = values;
criteria[associationKey] = pk;
_values[attribute.onKey] = this.proto[this.primaryKey];

@@ -276,3 +282,3 @@

if(err) {
if (err) {
self.failedTransactions.push({

@@ -301,3 +307,3 @@ type: 'update',

Add.prototype.createManyToMany = function(collection, attribute, pk, cb) {
Add.prototype.createManyToMany = function(collection, attribute, pk, key, cb) {
var self = this;

@@ -309,6 +315,18 @@

if(!associationKey) return cb(new Error('No Primary Key set on the child record you ' +
'are trying to associate the record with! Try setting an attribute as a primary key or ' +
'include an ID property.'));
// If this is a throughTable, look into the meta data cache for what key to use
if (collectionAttributes.throughTable) {
var cacheKey = collectionAttributes.throughTable[attribute.on + '.' + key];
if (!cacheKey) {
return cb(new Error('Unable to find the proper cache key in the through table definition'));
}
associationKey = cacheKey;
}
if (!associationKey) {
return cb(new Error('No Primary Key set on the child record you ' +
'are trying to associate the record with! Try setting an attribute as a primary key or ' +
'include an ID property.'));
}
// Build up criteria and updated values used to create the record

@@ -332,5 +350,7 @@ var criteria = {};

associatedCollection.findOne(_criteria, function(err, record) {
if(err) return next(err);
if(!record) return next(new Error("Associated Record For " + associatedCollectionName +
" with " + primaryKey + " = " + pk + " No Longer Exists"));
if (err) return next(err);
if (!record) {
return next(new Error('Associated Record For ' + associatedCollectionName +
' with ' + primaryKey + ' = ' + pk + ' No Longer Exists'));
}

@@ -345,4 +365,4 @@ next();

collection.findOne(criteria, function(err, val) {
if(err || val) {
return next(new Error("Trying to '.add()' an instance which already exists!"));
if (err || val) {
return next(new Error('Trying to \'.add()\' an instance which already exists!'));
}

@@ -358,3 +378,3 @@ next();

}, function(err) {
if(err) {
if (err) {
self.failedTransactions.push({

@@ -385,10 +405,10 @@ type: 'insert',

for(var attribute in collection.attributes) {
for (var attribute in collection.attributes) {
var attr = collection.attributes[attribute];
var identity = this.collection.identity;
if(!hasOwnProperty(attr, 'references')) continue;
if (!hasOwnProperty(attr, 'references')) continue;
var attrCollection = attr.references;
if(attrCollection !== identity) {
if (attrCollection !== identity) {
associationKey = attr.columnName;

@@ -395,0 +415,0 @@ }

@@ -30,7 +30,11 @@ var _ = require('lodash');

if(!this.primaryKey) return cb(new Error('No Primary Key set to associate the record with! ' +
if (!this.primaryKey) {
return cb(new Error('No Primary Key set to associate the record with! ' +
'Try setting an attribute as a primary key or include an ID property.'));
}
if(!proto.toObject()[this.primaryKey]) return cb(new Error('No Primary Key set to associate ' +
if (!proto.toObject()[this.primaryKey]) {
return cb(new Error('No Primary Key set to associate ' +
'the record with! Primary Key must have a value, it can\'t be an optional value.'));
}

@@ -57,4 +61,4 @@ // Loop through each of the associations on this model and remove any associations

for(var attribute in attributes) {
if(hasOwnProperty(attributes[attribute], 'primaryKey') && attributes[attribute].primaryKey) {
for (var attribute in attributes) {
if (hasOwnProperty(attributes[attribute], 'primaryKey') && attributes[attribute].primaryKey) {
primaryKey = attribute;

@@ -66,3 +70,3 @@ break;

// If no primary key check for an ID property
if(!primaryKey && hasOwnProperty(values, 'id')) primaryKey = 'id';
if (!primaryKey && hasOwnProperty(values, 'id')) primaryKey = 'id';

@@ -83,3 +87,3 @@ return primaryKey;

async.eachSeries(Object.keys(records), function(associationKey, next) {
async.eachSeries(_.keys(records), function(associationKey, next) {
self.removeAssociations(associationKey, records[associationKey], next);

@@ -89,3 +93,3 @@ },

function(err) {
if(err || self.failedTransactions.length > 0) {
if (err || self.failedTransactions.length > 0) {
return cb(null, self.failedTransactions);

@@ -118,4 +122,4 @@ }

// Limit Removes to 10 at a time to prevent the connection pool from being exhausted
async.eachLimit(records, 10, function(association, next) {
self.removeRecord(associatedCollection, schema, association, next);
async.eachLimit(records, 10, function(associationId, next) {
self.removeRecord(associatedCollection, schema, associationId, key, next);
}, cb);

@@ -135,13 +139,13 @@

Remove.prototype.removeRecord = function(collection, attribute, values, cb) {
Remove.prototype.removeRecord = function(collection, attribute, associationId, key, cb) {
var self = this;
// Validate `values` is a correct primary key format
var validAssociationKey = this.validatePrimaryKey(values);
var validAssociationKey = this.validatePrimaryKey(associationId);
if(!validAssociationKey) {
if (!validAssociationKey) {
this.failedTransactions.push({
type: 'remove',
collection: collection.identity,
values: values,
values: associationId,
err: new Error('Remove association only accepts a single primary key value')

@@ -155,8 +159,8 @@ });

var schema = this.collection.waterline.schema[attribute.collection.toLowerCase()];
var junctionTable = schema.junctionTable || false;
var junctionTable = schema.junctionTable || schema.throughTable;
// If so build out the criteria and remove a record from the junction table
if(junctionTable) {
if (junctionTable) {
var joinCollection = this.collection.waterline.collections[attribute.collection.toLowerCase()];
return this.removeManyToMany(joinCollection, attribute, values, cb);
return this.removeManyToMany(joinCollection, attribute, associationId, key, cb);
}

@@ -168,5 +172,7 @@

if(!associationKey) return cb(new Error('No Primary Key defined on the child record you ' +
'are trying to un-associate the record with! Try setting an attribute as a primary key or ' +
'include an ID property.'));
if (!associationKey) {
return cb(new Error('No Primary Key defined on the child record you ' +
'are trying to un-associate the record with! Try setting an attribute as a primary key or ' +
'include an ID property.'));
}

@@ -177,3 +183,3 @@ // Build up criteria and updated values used to update the record

criteria[associationKey] = values;
criteria[associationKey] = associationId;
_values[attribute.on] = null;

@@ -183,3 +189,3 @@

if(err) {
if (err) {
self.failedTransactions.push({

@@ -213,6 +219,6 @@ type: 'update',

// Attempt to see if the value is an ID and resembles a MongoID
if(_.isString(key) && utils.matchMongoId(key)) validAssociation = true;
if (_.isString(key) && utils.matchMongoId(key)) validAssociation = true;
// Check it can be turned into a string
if(key && key.toString() !== '[object Object]') validAssociation = true;
if (key && key.toString() !== '[object Object]') validAssociation = true;

@@ -232,3 +238,3 @@ return validAssociation;

Remove.prototype.removeManyToMany = function(collection, attribute, pk, cb) {
Remove.prototype.removeManyToMany = function(collection, attribute, pk, key, cb) {
var self = this;

@@ -240,6 +246,18 @@

if(!associationKey) return cb(new Error('No Primary Key set on the child record you ' +
'are trying to associate the record with! Try setting an attribute as a primary key or ' +
'include an ID property.'));
// If this is a throughTable, look into the meta data cache for what key to use
if (collectionAttributes.throughTable) {
var cacheKey = collectionAttributes.throughTable[attribute.on + '.' + key];
if (!cacheKey) {
return cb(new Error('Unable to find the proper cache key in the through table definition'));
}
associationKey = cacheKey;
}
if (!associationKey) {
return cb(new Error('No Primary Key set on the child record you ' +
'are trying to associate the record with! Try setting an attribute as a primary key or ' +
'include an ID property.'));
}
// Build up criteria and updated values used to create the record

@@ -253,3 +271,3 @@ var criteria = {};

if(err) {
if (err) {
self.failedTransactions.push({

@@ -278,10 +296,10 @@ type: 'destroy',

for(var attribute in collection.attributes) {
for (var attribute in collection.attributes) {
var attr = collection.attributes[attribute];
var identity = this.collection.identity;
if(!hasOwnProperty(attr, 'references')) continue;
if (!hasOwnProperty(attr, 'references')) continue;
var attrCollection = attr.references.toLowerCase();
if(attrCollection !== identity) {
if (attrCollection !== identity) {
associationKey = attr.columnName;

@@ -288,0 +306,0 @@ }

@@ -8,8 +8,8 @@ /**

var util = require('util');
var Promise = require('bluebird'),
_ = require('lodash'),
normalize = require('../utils/normalize'),
utils = require('../utils/helpers'),
acyclicTraversal = require('../utils/acyclicTraversal'),
hasOwnProperty = utils.object.hasOwnProperty;
var Promise = require('bluebird');
var _ = require('lodash');
var normalize = require('../utils/normalize');
var utils = require('../utils/helpers');
var acyclicTraversal = require('../utils/acyclicTraversal');
var hasOwnProperty = utils.object.hasOwnProperty;

@@ -22,4 +22,4 @@ // Alias "catch" as "fail", for backwards compatibility with projects

if(!context) return new Error('Must supply a context to a new Deferred object. Usage: new Deferred(context, method, criteria)');
if(!method) return new Error('Must supply a method to a new Deferred object. Usage: new Deferred(context, method, criteria)');
if (!context) return new Error('Must supply a context to a new Deferred object. Usage: new Deferred(context, method, criteria)');
if (!method) return new Error('Must supply a method to a new Deferred object. Usage: new Deferred(context, method, criteria)');

@@ -37,3 +37,2 @@ this._context = context;

/**

@@ -52,3 +51,3 @@ * Add join clause(s) to the criteria object to populate

*/
Deferred.prototype.populateDeep = function ( keyName, criteria ) {
Deferred.prototype.populateDeep = function(keyName, criteria) {

@@ -118,12 +117,11 @@ // The identity of the initial model

// Except make sure `where` exists
criteria.where = criteria.where===false?false:(criteria.where||{});
criteria.where = criteria.where === false ? false : (criteria.where || {});
////////////////////////////////////////////////////////////////////////
}
catch (e) {
} catch (e) {
throw new Error(
'Could not parse sub-criteria passed to '+
util.format('`.populate("%s")`', keyName)+
'\nSub-criteria:\n'+ util.inspect(criteria, false, null)+
'\nDetails:\n'+util.inspect(e,false, null)
'Could not parse sub-criteria passed to ' +
util.format('`.populate("%s")`', keyName) +
'\nSub-criteria:\n' + util.inspect(criteria, false, null) +
'\nDetails:\n' + util.inspect(e, false, null)
);

@@ -139,3 +137,3 @@ }

Object.keys(this._context._attributes).forEach(function(key) {
if(hasOwnProperty(self._context._attributes[key], 'primaryKey') && self._context._attributes[key].primaryKey) {
if (hasOwnProperty(self._context._attributes[key], 'primaryKey') && self._context._attributes[key].primaryKey) {
pk = self._context._attributes[key].columnName || key;

@@ -145,5 +143,5 @@ }

if(!attr) {
if (!attr) {
throw new Error(
'In '+util.format('`.populate("%s")`', keyName)+
'In ' + util.format('`.populate("%s")`', keyName) +
', attempting to populate an attribute that doesn\'t exist'

@@ -154,4 +152,4 @@ );

//////////////////////////////////////////////////////////////////////
///(there has been significant progress made towards both of these ///
/// goals-- contact @mikermcneil if you want to help) ////////////////
// (there has been significant progress made towards both of these ///
// goals-- contact @mikermcneil if you want to help) /////////////////
//////////////////////////////////////////////////////////////////////

@@ -194,5 +192,5 @@ // TODO:

alias: keyName,
removeParentKey: parentKey.model ? true : false,
model: hasOwnProperty(parentKey, 'model') ? true : false,
collection: hasOwnProperty(parentKey, 'collection') ? true : false
removeParentKey: !!parentKey.model,
model: !!hasOwnProperty(parentKey, 'model'),
collection: !!hasOwnProperty(parentKey, 'collection')
};

@@ -204,3 +202,3 @@

var obj = self._context.waterline.schema[attr.references].attributes[key];
if(!hasOwnProperty(obj, 'columnName')) {
if (!hasOwnProperty(obj, 'columnName')) {
select.push(key);

@@ -215,4 +213,15 @@ return;

var schema = this._context.waterline.schema[attr.references];
var reference = null;
// If linking to a junction table the attributes shouldn't be included in the return value
if(this._context.waterline.schema[attr.references].junctionTable) join.select = false;
if (schema.junctionTable) {
join.select = false;
reference = _.find(schema.attributes, function(attribute) {
return attribute.references && attribute.columnName !== attr.on;
});
} else if (schema.throughTable && schema.throughTable[self._context.identity + '.' + keyName]) {
join.select = false;
reference = schema.attributes[schema.throughTable[self._context.identity + '.' + keyName]];
}

@@ -222,61 +231,30 @@ joins.push(join);

// If a junction table is used add an additional join to get the data
if(this._context.waterline.schema[attr.references].junctionTable && hasOwnProperty(attr, 'on')) {
// clone the reference attribute so we can mutate it
var reference = _.clone(this._context.waterline.schema[attr.references].attributes);
// Find the other key in the junction table
Object.keys(reference).forEach(function(key) {
var attribute = reference[key];
if(!hasOwnProperty(attribute, 'references')) {
delete reference[key];
return;
}
if(hasOwnProperty(attribute, 'columnName') && attribute.columnName === attr.on) {
delete reference[key];
return;
}
if(hasOwnProperty(attribute, 'columnName') && attribute.columnName !== attr.on) {
return;
}
if(key !== attr.on) delete reference[key];
if (reference && hasOwnProperty(attr, 'on')) {
// Build out the second join object that will link a junction table with the
// values being populated
var selects = _.map(_.keys(this._context.waterline.schema[reference.references].attributes), function(attr) {
var expandedAttr = self._context.waterline.schema[reference.references].attributes[attr];
return expandedAttr.columnName || attr;
});
// Get the only remaining key left
var ref = Object.keys(reference)[0];
join = {
parent: attr.references,
parentKey: reference.columnName,
child: reference.references,
childKey: reference.on,
select: selects,
alias: keyName,
junctionTable: true,
removeParentKey: !!parentKey.model,
model: false,
collection: true
};
if(ref) {
// Build out the second join object that will link a junction table with the
// values being populated
var selects = _.map(_.keys(this._context.waterline.schema[reference[ref].references].attributes), function(attr) {
var expandedAttr = self._context.waterline.schema[reference[ref].references].attributes[attr];
return expandedAttr.columnName || attr;
});
join = {
parent: attr.references,
parentKey: reference[ref].columnName,
child: reference[ref].references,
childKey: reference[ref].on,
select: selects,
alias: keyName,
junctionTable: true,
removeParentKey: parentKey.model ? true : false,
model: false,
collection: true
};
joins.push(join);
}
joins.push(join);
}
// Append the criteria to the correct join if available
if(criteria && joins.length > 1) {
if (criteria && joins.length > 1) {
joins[1].criteria = criteria;
} else if(criteria) {
} else if (criteria) {
joins[0].criteria = criteria;

@@ -289,9 +267,8 @@ }

return this;
}
catch (e) {
} catch (e) {
throw new Error(
'Encountered unexpected error while building join instructions for '+
util.format('`.populate("%s")`', keyName)+
'\nDetails:\n'+
util.inspect(e,false, null)
'Encountered unexpected error while building join instructions for ' +
util.format('`.populate("%s")`', keyName) +
'\nDetails:\n' +
util.inspect(e, false, null)
);

@@ -310,3 +287,3 @@ }

if(!criteria) return this;
if (!criteria) return this;

@@ -323,9 +300,9 @@ // If the criteria is an array of objects, wrap it in an "or"

// (since neither could match anything)
if (criteria === false){
if (criteria === false) {
this._criteria = false;
}
if(!criteria || !criteria.where) return this;
if (!criteria || !criteria.where) return this;
if(!this._criteria) this._criteria = {};
if (!this._criteria) this._criteria = {};
var where = this._criteria.where || {};

@@ -381,10 +358,10 @@

if(options === undefined) options = { page: 0, limit: defaultLimit };
if (options === undefined) options = { page: 0, limit: defaultLimit };
var page = options.page || 0,
limit = options.limit || defaultLimit,
skip = 0;
var page = options.page || 0;
var limit = options.limit || defaultLimit;
var skip = 0;
if (page > 0 && limit === 0) skip = page - 1;
if (page > 0 && limit > 0) skip = (page * limit) - limit;
if (page > 0 && limit > 0) skip = (page * limit) - limit;

@@ -419,3 +396,3 @@ this

if(!criteria) return this;
if (!criteria) return this;

@@ -481,3 +458,2 @@ // Normalize criteria

/**

@@ -506,4 +482,4 @@ * Add values to be used in update or create query

if(!cb) {
console.log( new Error('Error: No Callback supplied, you must define a callback.').message );
if (!cb) {
console.log(new Error('Error: No Callback supplied, you must define a callback.').message);
return;

@@ -517,3 +493,3 @@ }

var args = [this._criteria, cb];
if(this._values) args.splice(1, 0, this._values);
if (this._values) args.splice(1, 0, this._values);

@@ -520,0 +496,0 @@ // Pass control to the adapter with the appropriate arguments.

{
"name": "waterline",
"description": "An ORM for Node.js and the Sails framework",
"version": "0.10.27",
"version": "0.10.28",
"homepage": "http://github.com/balderdashy/waterline",

@@ -29,3 +29,3 @@ "contributors": [

"waterline-criteria": "~0.11.1",
"waterline-schema": "~0.1.17"
"waterline-schema": "~0.1.19"
},

@@ -32,0 +32,0 @@ "devDependencies": {

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc