all files / lib/waterline/model/lib/associationMethods/ remove.js

81% Statements 81/100
67.39% Branches 31/46
92.31% Functions 12/13
83.33% Lines 80/96
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295                             144× 144× 144× 144×   144× 144×   144×   144×         144×                     144×                     150×   150× 426× 150× 150×         150×   150×                     144×   144× 17×       144×     136×                         17×       17× 17× 17× 17×     17× 23×                             23×     23×   23× 10×             10×       13× 13×     13×                                                                         23×     23×     23×   23×                                                                                                                                    
var _ = require('lodash');
var async = require('async');
var utils = require('../../../utils/helpers');
var hasOwnProperty = utils.object.hasOwnProperty;
 
/**
 * Remove associations from a model.
 *
 * Accepts a primary key value of an associated record that already exists in the database.
 *
 *
 * @param {Object} collection
 * @param {Object} proto
 * @param {Object} records
 * @param {Function} callback
 */
 
var Remove = module.exports = function(collection, proto, records, cb) {
 
  this.collection = collection;
  this.proto = proto;
  this.failedTransactions = [];
  this.primaryKey = null;
 
  var values = proto.toObject();
  var attributes = collection.waterline.schema[collection.identity].attributes;
 
  this.primaryKey = this.findPrimaryKey(attributes, values);
 
  Iif (!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.'));
  }
 
  Iif (!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.'));
  }
 
  // Loop through each of the associations on this model and remove any associations
  // that have been specified. Do this in series and limit the actual saves to 10
  // at a time so that connection pools are not exhausted.
  //
  // In the future when transactions are available this will all be done on a single
  // connection and can be re-written.
  this.removeCollectionAssociations(records, cb);
};
 
/**
 * Find Primary Key
 *
 * @param {Object} attributes
 * @param {Object} values
 * @api private
 */
 
Remove.prototype.findPrimaryKey = function(attributes, values) {
  var primaryKey = null;
 
  for (var attribute in attributes) {
    if (hasOwnProperty(attributes[attribute], 'primaryKey') && attributes[attribute].primaryKey) {
      primaryKey = attribute;
      break;
    }
  }
 
  // If no primary key check for an ID property
  Iif (!primaryKey && hasOwnProperty(values, 'id')) primaryKey = 'id';
 
  return primaryKey;
};
 
/**
 * Remove Collection Associations
 *
 * @param {Object} records
 * @param {Function} callback
 * @api private
 */
 
Remove.prototype.removeCollectionAssociations = function(records, cb) {
  var self = this;
 
  async.eachSeries(_.keys(records), function(associationKey, next) {
    self.removeAssociations(associationKey, records[associationKey], next);
  },
 
  function(err) {
    if (err || self.failedTransactions.length > 0) {
      return cb(null, self.failedTransactions);
    }
 
    cb();
  });
};
 
/**
 * Remove Associations
 *
 * @param {String} key
 * @param {Array} records
 * @param {Function} callback
 * @api private
 */
 
Remove.prototype.removeAssociations = function(key, records, cb) {
  var self = this;
 
  // Grab the collection the attribute references
  // this allows us to make a query on it
  var attribute = this.collection._attributes[key];
  var collectionName = attribute.collection.toLowerCase();
  var associatedCollection = this.collection.waterline.collections[collectionName];
  var schema = this.collection.waterline.schema[this.collection.identity].attributes[key];
 
  // Limit Removes to 10 at a time to prevent the connection pool from being exhausted
  async.eachLimit(records, 10, function(associationId, next) {
    self.removeRecord(associatedCollection, schema, associationId, key, next);
  }, cb);
 
};
 
/**
 * Remove A Single Record
 *
 * @param {Object} collection
 * @param {Object} attribute
 * @param {Object} values
 * @param {Function} callback
 * @api private
 */
 
Remove.prototype.removeRecord = function(collection, attribute, associationId, key, cb) {
  var self = this;
 
  // Validate `values` is a correct primary key format
  var validAssociationKey = this.validatePrimaryKey(associationId);
 
  if (!validAssociationKey) {
    this.failedTransactions.push({
      type: 'remove',
      collection: collection.identity,
      values: associationId,
      err: new Error('Remove association only accepts a single primary key value')
    });
 
    return cb();
  }
 
  // Check if this is a many-to-many by looking at the junctionTable flag
  var schema = this.collection.waterline.schema[attribute.collection.toLowerCase()];
  var junctionTable = schema.junctionTable || schema.throughTable;
 
  // If so build out the criteria and remove a record from the junction table
  if (junctionTable) {
    var joinCollection = this.collection.waterline.collections[attribute.collection.toLowerCase()];
    return this.removeManyToMany(joinCollection, attribute, associationId, key, cb);
  }
 
  // Grab the associated collection's primaryKey
  var attributes = this.collection.waterline.schema[collection.identity].attributes;
  var associationKey = this.findPrimaryKey(attributes, attributes);
 
  Iif (!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.'));
  }
 
  // Build up criteria and updated values used to update the record
  var criteria = {};
  var _values = {};
 
  criteria[associationKey] = associationId;
  _values[attribute.on] = null;
 
  collection.update(criteria, _values, function(err) {
 
    Iif (err) {
      self.failedTransactions.push({
        type: 'update',
        collection: collection.identity,
        criteria: criteria,
        values: _values,
        err: err
      });
    }
 
    cb();
  });
};
 
/**
 * Validate A Primary Key
 *
 * Only support primary keys being passed in to the remove function. Check if it's a mongo
 * id or anything that has a toString method.
 *
 * @param {Integer|String} key
 * @return {Boolean}
 * @api private
 */
 
Remove.prototype.validatePrimaryKey = function(key) {
  var validAssociation = false;
 
  // Attempt to see if the value is an ID and resembles a MongoID
  Iif (_.isString(key) && utils.matchMongoId(key)) validAssociation = true;
 
  // Check it can be turned into a string
  if (key && key.toString() !== '[object Object]') validAssociation = true;
 
  return validAssociation;
};
 
/**
 * Remove A Many To Many Join Table Record
 *
 * @param {Object} collection
 * @param {Object} attribute
 * @param {Object} values
 * @param {Function} callback
 * @api private
 */
 
Remove.prototype.removeManyToMany = function(collection, attribute, pk, key, cb) {
  var self = this;
 
  // Grab the associated collection's primaryKey
  var collectionAttributes = this.collection.waterline.schema[attribute.collection.toLowerCase()];
  var associationKey = collectionAttributes.attributes[attribute.on].via;
 
  // 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];
    Iif (!cacheKey) {
      return cb(new Error('Unable to find the proper cache key in the through table definition'));
    }
 
    associationKey = cacheKey;
  }
 
  Iif (!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
  var criteria = {};
  criteria[associationKey] = pk;
  criteria[attribute.on] = this.proto[this.primaryKey];
 
  // Run a destroy on the join table record
  collection.destroy(criteria, function(err) {
 
    Iif (err) {
      self.failedTransactions.push({
        type: 'destroy',
        collection: collection.identity,
        criteria: criteria,
        err: err
      });
    }
 
    cb();
  });
};
 
/**
 * Find Association Key
 *
 * @param {Object} collection
 * @return {String}
 * @api private
 */
 
Remove.prototype.findAssociationKey = function(collection) {
  var associationKey = null;
 
  for (var attribute in collection.attributes) {
    var attr = collection.attributes[attribute];
    var identity = this.collection.identity;
 
    if (!hasOwnProperty(attr, 'references')) continue;
    var attrCollection = attr.references.toLowerCase();
 
    if (attrCollection !== identity) {
      associationKey = attr.columnName;
    }
  }
 
  return associationKey;
};