BVB Source Codes

waterline Show help-remove-from-collection.js Source code

Return Download waterline: download help-remove-from-collection.js Source code - Download waterline Source code - Type:.js
  1. /**
  2.  * Module dependencies
  3.  */
  4.  
  5. var assert = require('assert');
  6. var _ = require('@sailshq/lodash');
  7. var async = require('async');
  8.  
  9.  
  10.  
  11. /**
  12.  * helpRemoveFromCollection()
  13.  *
  14.  * @param  {Dictionary}   query  [stage 2 query]
  15.  * @param  {Ref}   orm
  16.  * @param  {Function} done
  17.  */
  18.  
  19. module.exports = function helpRemoveFromCollection(query, orm, done) {
  20.  
  21.   // Validate arguments
  22.   if (_.isUndefined(query) || !_.isObject(query)) {
  23.     throw new Error('Consistency violation: Invalid arguments - missing or invalid `query` argument (a stage 2 query).');
  24.   }
  25.  
  26.   if (_.isUndefined(orm) || !_.isObject(orm)) {
  27.     throw new Error('Consistency violation: Invalid arguments - missing or invalid `orm` argument.');
  28.   }
  29.  
  30.   // Get the model being used as the parent
  31.   var WLModel = orm.collections[query.using];
  32.   try { assert.equal(query.using.toLowerCase(), query.using, '`query.using` (identity) should have already been normalized before getting here!  But it was not: '+query.using); } catch (e) { return done(e); }
  33.  
  34.   // Look up the association by name in the schema definition.
  35.   var schemaDef = WLModel.schema[query.collectionAttrName];
  36.  
  37.   // Look up the associated collection using the schema def which should have
  38.   // join tables normalized
  39.   var WLChild = orm.collections[schemaDef.collection];
  40.   try {
  41.     assert.equal(schemaDef.collection.toLowerCase(), schemaDef.collection, '`schemaDef.collection` (identity) should have already been normalized before getting here!  But it was not: '+schemaDef.collection);
  42.     assert.equal(schemaDef.referenceIdentity.toLowerCase(), schemaDef.referenceIdentity, '`schemaDef.referenceIdentity` (identity) should have already been normalized before getting here!  But it was not: '+schemaDef.referenceIdentity);
  43.     assert.equal(Object.getPrototypeOf(WLChild).identity.toLowerCase(), Object.getPrototypeOf(WLChild).identity, '`Object.getPrototypeOf(WLChild).identity` (identity) should have already been normalized before getting here!  But it was not: '+Object.getPrototypeOf(WLChild).identity);
  44.   } catch (e) { return done(e); }
  45.  
  46.   // Flag to determine if the WLChild is a manyToMany relation
  47.   var manyToMany = false;
  48.  
  49.   // Check if the schema references something other than the WLChild
  50.   if (schemaDef.referenceIdentity !== Object.getPrototypeOf(WLChild).identity) {
  51.     manyToMany = true;
  52.     WLChild = orm.collections[schemaDef.referenceIdentity];
  53.   }
  54.  
  55.   // Check if the child is a join table
  56.   if (_.has(Object.getPrototypeOf(WLChild), 'junctionTable') && WLChild.junctionTable) {
  57.     manyToMany = true;
  58.   }
  59.  
  60.   // Check if the child is a through table
  61.   if (_.has(Object.getPrototypeOf(WLChild), 'throughTable') && _.keys(WLChild.throughTable).length) {
  62.     manyToMany = true;
  63.   }
  64.  
  65.   // Ensure the query skips lifecycle callbacks
  66.   query.meta = query.meta || {};
  67.   query.meta.skipAllLifecycleCallbacks = true;
  68.   // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  69.   // TODO: change this b/c we can't safely do that ^^^
  70.   // (it destructively mutates the `meta` QK such that it could change the behavior of other pieces of this query)
  71.   // Instead, we need to do something else-- probably use a shallow clone
  72.   // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  73.  
  74.  
  75.   //   鈻堚枅鈺椻枅鈻堚枅鈺   鈻堚枅鈺      鈻堚枅鈻堚晽   鈻堚枅鈻堚晽鈻堚枅鈺
  76.   //  鈻堚枅鈺斺暆鈻堚枅鈻堚枅鈺  鈻堚枅鈺      鈻堚枅鈻堚枅鈺 鈻堚枅鈻堚枅鈺戔暁鈻堚枅鈺
  77.   //  鈻堚枅鈺 鈻堚枅鈺斺枅鈻堚晽 鈻堚枅鈺      鈻堚枅鈺斺枅鈻堚枅鈻堚晹鈻堚枅鈺 鈻堚枅鈺
  78.   //  鈻堚枅鈺 鈻堚枅鈺戔暁鈻堚枅鈺椻枅鈻堚晳      鈻堚枅鈺戔暁鈻堚枅鈺斺暆鈻堚枅鈺 鈻堚枅鈺
  79.   //  鈺氣枅鈻堚晽鈻堚枅鈺 鈺氣枅鈻堚枅鈻堚晳鈻堚枅鈺椻枅鈻堚晽鈻堚枅鈺 鈺氣晲鈺 鈻堚枅鈺戔枅鈻堚晹鈺
  80.   //   鈺氣晲鈺濃暁鈺愨暆  鈺氣晲鈺愨晲鈺濃暁鈺愨暆鈺氣晲鈺濃暁鈺愨暆     鈺氣晲鈺濃暁鈺愨暆
  81.   //
  82.   //  鈻堚枅鈻堚晽   鈻堚枅鈻堚晽 鈻堚枅鈻堚枅鈻堚晽 鈻堚枅鈻堚晽   鈻堚枅鈺椻枅鈻堚晽   鈻堚枅鈺    鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈺 鈻堚枅鈻堚枅鈻堚枅鈺     鈻堚枅鈻堚晽   鈻堚枅鈻堚晽 鈻堚枅鈻堚枅鈻堚晽 鈻堚枅鈻堚晽   鈻堚枅鈺椻枅鈻堚晽   鈻堚枅鈺
  83.   //  鈻堚枅鈻堚枅鈺 鈻堚枅鈻堚枅鈺戔枅鈻堚晹鈺愨晲鈻堚枅鈺椻枅鈻堚枅鈻堚晽  鈻堚枅鈺戔暁鈻堚枅鈺 鈻堚枅鈺斺暆    鈺氣晲鈺愨枅鈻堚晹鈺愨晲鈺濃枅鈻堚晹鈺愨晲鈺愨枅鈻堚晽    鈻堚枅鈻堚枅鈺 鈻堚枅鈻堚枅鈺戔枅鈻堚晹鈺愨晲鈻堚枅鈺椻枅鈻堚枅鈻堚晽  鈻堚枅鈺戔暁鈻堚枅鈺 鈻堚枅鈺斺暆
  84.   //  鈻堚枅鈺斺枅鈻堚枅鈻堚晹鈻堚枅鈺戔枅鈻堚枅鈻堚枅鈻堚枅鈺戔枅鈻堚晹鈻堚枅鈺 鈻堚枅鈺 鈺氣枅鈻堚枅鈻堚晹鈺        鈻堚枅鈺   鈻堚枅鈺   鈻堚枅鈺    鈻堚枅鈺斺枅鈻堚枅鈻堚晹鈻堚枅鈺戔枅鈻堚枅鈻堚枅鈻堚枅鈺戔枅鈻堚晹鈻堚枅鈺 鈻堚枅鈺 鈺氣枅鈻堚枅鈻堚晹鈺
  85.   //  鈻堚枅鈺戔暁鈻堚枅鈺斺暆鈻堚枅鈺戔枅鈻堚晹鈺愨晲鈻堚枅鈺戔枅鈻堚晳鈺氣枅鈻堚晽鈻堚枅鈺  鈺氣枅鈻堚晹鈺         鈻堚枅鈺   鈻堚枅鈺   鈻堚枅鈺    鈻堚枅鈺戔暁鈻堚枅鈺斺暆鈻堚枅鈺戔枅鈻堚晹鈺愨晲鈻堚枅鈺戔枅鈻堚晳鈺氣枅鈻堚晽鈻堚枅鈺  鈺氣枅鈻堚晹鈺
  86.   //  鈻堚枅鈺 鈺氣晲鈺 鈻堚枅鈺戔枅鈻堚晳  鈻堚枅鈺戔枅鈻堚晳 鈺氣枅鈻堚枅鈻堚晳   鈻堚枅鈺          鈻堚枅鈺   鈺氣枅鈻堚枅鈻堚枅鈻堚晹鈺    鈻堚枅鈺 鈺氣晲鈺 鈻堚枅鈺戔枅鈻堚晳  鈻堚枅鈺戔枅鈻堚晳 鈺氣枅鈻堚枅鈻堚晳   鈻堚枅鈺
  87.   //  鈺氣晲鈺     鈺氣晲鈺濃暁鈺愨暆  鈺氣晲鈺濃暁鈺愨暆  鈺氣晲鈺愨晲鈺   鈺氣晲鈺          鈺氣晲鈺    鈺氣晲鈺愨晲鈺愨晲鈺     鈺氣晲鈺     鈺氣晲鈺濃暁鈺愨暆  鈺氣晲鈺濃暁鈺愨暆  鈺氣晲鈺愨晲鈺   鈺氣晲鈺
  88.   //
  89.   // If the collection uses a join table, build a query that removes the records
  90.   // from the table.
  91.   if (manyToMany) {
  92.  
  93.     //  鈺斺晽 鈺 鈺︹暒鈺  鈺斺暒鈺  鈹攢鈹愨攲鈹€鈹愨攲鈹€鈹愨攲鈹€鈹愨敩鈹€鈹愨攲鈹€鈹愨攲鈹愨攲鈹屸攢鈹愨攲鈹€鈹  鈹屸敩鈹愨攲鈹€鈹愨攲鈹€鈹愨攲鈹€鈹愨敩鈹屸攼鈹屸攲鈹€鈹
  94.     //  鈺犫暕鈺椻晳 鈺戔晳鈺   鈺戔晳  鈹溾敩鈹樷敎鈹 鈹溾敜 鈹溾敜 鈹溾敩鈹樷敎鈹 鈹傗攤鈹傗攤  鈹溾敜   鈹傗攤鈹傗敎鈹€鈹も敎鈹€鈹樷敎鈹€鈹樷攤鈹傗攤鈹傗攤 鈹
  95.     //  鈺氣晲鈺濃暁鈺愨暆鈺┾暕鈺愨暆鈺愨暕鈺  鈹粹敂鈹€鈹斺攢鈹樷敂  鈹斺攢鈹樷敶鈹斺攢鈹斺攢鈹樷敇鈹斺敇鈹斺攢鈹樷敂鈹€鈹  鈹 鈹粹敶 鈹粹敶  鈹  鈹粹敇鈹斺敇鈹斺攢鈹
  96.     //
  97.     // Maps out the parent and child attribute names to use for the query.
  98.     var parentReference;
  99.     var childReference;
  100.  
  101.     // Find the parent reference
  102.     if (_.has(Object.getPrototypeOf(WLChild), 'junctionTable') && WLChild.junctionTable) {
  103.  
  104.       // Assumes the generated junction table will only ever have two foreign key
  105.       // values. Should be safe for now and any changes would need to be made in
  106.       // Waterline-Schema where a map could be formed anyway.
  107.       _.each(WLChild.schema, function(val, key) {
  108.         if (!_.has(val, 'references')) {
  109.           return;
  110.         }
  111.  
  112.         // If this is the piece of the join table, set the parent reference.
  113.         if (_.has(val, 'columnName') && val.columnName === schemaDef.on) {
  114.           parentReference = key;
  115.         }
  116.       });
  117.  
  118.     }
  119.     // If it's a through table, grab the parent and child reference from the
  120.     // through table mapping that was generated by Waterline-Schema.
  121.     else if (_.has(Object.getPrototypeOf(WLChild), 'throughTable')) {
  122.  
  123.       childReference = WLChild.throughTable[WLModel.identity + '.' + query.collectionAttrName];
  124.       _.each(WLChild.throughTable, function(val, key) {
  125.         if (key !== WLModel.identity + '.' + query.collectionAttrName) {
  126.           parentReference = val;
  127.         }
  128.       });
  129.  
  130.     }//>-
  131.  
  132.     // Find the child reference in a junction table
  133.     if (_.has(Object.getPrototypeOf(WLChild), 'junctionTable') && WLChild.junctionTable) {
  134.  
  135.       // Assumes the generated junction table will only ever have two foreign key
  136.       // values. Should be safe for now and any changes would need to be made in
  137.       // Waterline-Schema where a map could be formed anyway.
  138.       _.each(WLChild.schema, function(val, key) {
  139.         if (!_.has(val, 'references')) {
  140.           return;
  141.         }
  142.  
  143.         // If this is the other piece of the join table, set the child reference.
  144.         if (_.has(val, 'columnName') && val.columnName !== schemaDef.on) {
  145.           childReference = key;
  146.         }
  147.       });//</_.each()>
  148.  
  149.     }//>-
  150.  
  151.  
  152.     //  鈺斺晽 鈺 鈺︹暒鈺  鈺斺暒鈺  鈹屸攢鈹 鈹 鈹攲鈹€鈹愨敩鈹€鈹愨敩 鈹
  153.     //  鈺犫暕鈺椻晳 鈺戔晳鈺   鈺戔晳  鈹傗攢鈹尖攼鈹 鈹傗敎鈹 鈹溾敩鈹樷敂鈹敇
  154.     //  鈺氣晲鈺濃暁鈺愨暆鈺┾暕鈺愨暆鈺愨暕鈺  鈹斺攢鈹樷敂鈹斺攢鈹樷敂鈹€鈹樷敶鈹斺攢 鈹 (S)
  155.     //
  156.     // If only a single targetRecordId is used, this can be done in a single
  157.     // query. Otherwise multiple queries will be needed - one for each parent.
  158.     // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  159.     // FUTURE: Combine this bit into one single query using something like:
  160.     // ```
  161.     // { or: [ { and: [{..},{..:{in:[..]}}] }, { and: [{..},{..:{in: [..]}}] }, ... ] }
  162.     // ```
  163.     // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  164.  
  165.     // Build an array to hold `where` clauses for all records being removed.
  166.     // For each target record, build a constraint destroy query for the associated records.
  167.     var joinRecordWhereClauses = [];
  168.     _.each(query.targetRecordIds, function(targetId) {
  169.       var whereClauseForTarget = {};
  170.       whereClauseForTarget[parentReference] = targetId;
  171.       whereClauseForTarget[childReference] = { in: query.associatedIds };
  172.       joinRecordWhereClauses.push(whereClauseForTarget);
  173.     });
  174.  
  175.     //  鈺︹晲鈺椻暒 鈺︹晹鈺椻晹  鈹屸攢鈹 鈹 鈹攲鈹€鈹愨敩鈹€鈹愨敩 鈹
  176.     //  鈺犫暒鈺濃晳 鈺戔晳鈺戔晳  鈹傗攢鈹尖攼鈹 鈹傗敎鈹 鈹溾敩鈹樷敂鈹敇
  177.     //  鈺┾暁鈺愨暁鈺愨暆鈺濃暁鈺  鈹斺攢鈹樷敂鈹斺攢鈹樷敂鈹€鈹樷敶鈹斺攢 鈹
  178.     async.each(joinRecordWhereClauses, function(whereClause, next) {
  179.  
  180.       WLChild.destroy(whereClause, function(err){
  181.         if (err) { return next(err); }
  182.         return next();
  183.       }, query.meta);
  184.  
  185.     },// ~鈭?掳
  186.     function _after(err) {
  187.       if (err) { return done(err); }
  188.       return done();
  189.     });//</ async.each() >
  190.  
  191.     return;
  192.   }//_鈭廮.  </ if this is a n..m (many to many) association >
  193.  
  194.  
  195.   //   鈻堚枅鈺椻枅鈻堚枅鈺   鈻堚枅鈺      鈻堚枅鈺椻枅鈻堚晽
  196.   //  鈻堚枅鈺斺暆鈻堚枅鈻堚枅鈺  鈻堚枅鈺     鈻堚枅鈻堚晳鈺氣枅鈻堚晽
  197.   //  鈻堚枅鈺 鈻堚枅鈺斺枅鈻堚晽 鈻堚枅鈺     鈺氣枅鈻堚晳 鈻堚枅鈺
  198.   //  鈻堚枅鈺 鈻堚枅鈺戔暁鈻堚枅鈺椻枅鈻堚晳      鈻堚枅鈺 鈻堚枅鈺
  199.   //  鈺氣枅鈻堚晽鈻堚枅鈺 鈺氣枅鈻堚枅鈻堚晳鈻堚枅鈺椻枅鈻堚晽鈻堚枅鈺戔枅鈻堚晹鈺
  200.   //   鈺氣晲鈺濃暁鈺愨暆  鈺氣晲鈺愨晲鈺濃暁鈺愨暆鈺氣晲鈺濃暁鈺愨暆鈺氣晲鈺
  201.   //
  202.   //  鈻堚枅鈻堚枅鈻堚枅鈺 鈻堚枅鈻堚枅鈻堚枅鈻堚晽鈻堚枅鈺      鈻堚枅鈻堚枅鈻堚枅鈺 鈻堚枅鈻堚晽   鈻堚枅鈺 鈻堚枅鈻堚枅鈻堚枅鈺 鈻堚枅鈻堚枅鈻堚枅鈻堚晽    鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈺 鈻堚枅鈻堚枅鈻堚枅鈺
  203.   //  鈻堚枅鈺斺晲鈺愨枅鈻堚晽鈻堚枅鈺斺晲鈺愨晲鈺愨暆鈻堚枅鈺     鈻堚枅鈺斺晲鈺愨晲鈻堚枅鈺椻枅鈻堚枅鈻堚晽  鈻堚枅鈺戔枅鈻堚晹鈺愨晲鈺愨晲鈺 鈻堚枅鈺斺晲鈺愨晲鈺愨暆    鈺氣晲鈺愨枅鈻堚晹鈺愨晲鈺濃枅鈻堚晹鈺愨晲鈺愨枅鈻堚晽
  204.   //  鈻堚枅鈻堚枅鈻堚枅鈺斺暆鈻堚枅鈻堚枅鈻堚晽  鈻堚枅鈺     鈻堚枅鈺   鈻堚枅鈺戔枅鈻堚晹鈻堚枅鈺 鈻堚枅鈺戔枅鈻堚晳  鈻堚枅鈻堚晽鈻堚枅鈻堚枅鈻堚枅鈻堚晽       鈻堚枅鈺   鈻堚枅鈺   鈻堚枅鈺
  205.   //  鈻堚枅鈺斺晲鈺愨枅鈻堚晽鈻堚枅鈺斺晲鈺愨暆  鈻堚枅鈺     鈻堚枅鈺   鈻堚枅鈺戔枅鈻堚晳鈺氣枅鈻堚晽鈻堚枅鈺戔枅鈻堚晳   鈻堚枅鈺戔暁鈺愨晲鈺愨晲鈻堚枅鈺       鈻堚枅鈺   鈻堚枅鈺   鈻堚枅鈺
  206.   //  鈻堚枅鈻堚枅鈻堚枅鈺斺暆鈻堚枅鈻堚枅鈻堚枅鈻堚晽鈻堚枅鈻堚枅鈻堚枅鈻堚晽鈺氣枅鈻堚枅鈻堚枅鈻堚晹鈺濃枅鈻堚晳 鈺氣枅鈻堚枅鈻堚晳鈺氣枅鈻堚枅鈻堚枅鈻堚晹鈺濃枅鈻堚枅鈻堚枅鈻堚枅鈺       鈻堚枅鈺   鈺氣枅鈻堚枅鈻堚枅鈻堚晹鈺
  207.   //  鈺氣晲鈺愨晲鈺愨晲鈺 鈺氣晲鈺愨晲鈺愨晲鈺愨暆鈺氣晲鈺愨晲鈺愨晲鈺愨暆 鈺氣晲鈺愨晲鈺愨晲鈺 鈺氣晲鈺  鈺氣晲鈺愨晲鈺 鈺氣晲鈺愨晲鈺愨晲鈺 鈺氣晲鈺愨晲鈺愨晲鈺愨暆       鈺氣晲鈺    鈺氣晲鈺愨晲鈺愨晲鈺
  208.   //
  209.   // Otherwise, this association is exclusive-- so rather than deleting junction records, we'll need
  210.   // to update the child records themselves, nulling out their foreign key value (aka singular, "model", association).
  211.  
  212.  
  213.   //  鈺斺晽 鈺 鈺︹暒鈺  鈺斺暒鈺  鈹屸攢鈹 鈹 鈹攲鈹€鈹愨敩鈹€鈹愨敩 鈹
  214.   //  鈺犫暕鈺椻晳 鈺戔晳鈺   鈺戔晳  鈹傗攢鈹尖攼鈹 鈹傗敎鈹 鈹溾敩鈹樷敂鈹敇
  215.   //  鈺氣晲鈺濃暁鈺愨暆鈺┾暕鈺愨暆鈺愨暕鈺  鈹斺攢鈹樷敂鈹斺攢鈹樷敂鈹€鈹樷敶鈹斺攢 鈹
  216.   //
  217.   // Build up criteria that selects child records.
  218.   var criteria = { where: {} };
  219.   criteria.where[WLChild.primaryKey] = query.associatedIds;
  220.   criteria.where[schemaDef.via] = query.targetRecordIds;
  221.  
  222.   // Build up the values to set (we'll null out the other side).
  223.   var valuesToUpdate = {};
  224.   valuesToUpdate[schemaDef.via] = null;
  225.  
  226.  
  227.   //  鈺︹晲鈺椻暒 鈺︹晹鈺椻晹  鈹屸攢鈹 鈹 鈹攲鈹€鈹愨敩鈹€鈹愨敩 鈹
  228.   //  鈺犫暒鈺濃晳 鈺戔晳鈺戔晳  鈹傗攢鈹尖攼鈹 鈹傗敎鈹 鈹溾敩鈹樷敂鈹敇
  229.   //  鈺┾暁鈺愨暁鈺愨暆鈺濃暁鈺  鈹斺攢鈹樷敂鈹斺攢鈹樷敂鈹€鈹樷敶鈹斺攢 鈹
  230.   WLChild.update(criteria, valuesToUpdate, function(err){
  231.     if (err) { return done(err); }
  232.  
  233.     return done();
  234.  
  235.   }, query.meta);//</.update()>
  236.  
  237. };
  238.  
downloadhelp-remove-from-collection.js Source code - Download waterline Source code
Related Source Codes/Software:
notepad-plus-plus - Notepad++ official repository h... 2017-01-10
che - Eclipse Che: Next-generation Eclipse IDE. Open sou... 2017-01-10
Gource - oftware version control visualization ... 2017-01-10
FDFullscreenPopGesture - A UINavigationController's category to enable full... 2017-01-10
node-style-guide - A guide for styling your node.js / JavaScript code... 2017-01-09
Workerman - An asynchronous event driven PHP framework for eas... 2017-01-10
structor - An advanced visual editor for React components ... 2017-01-10
golearn - Machine Learning for Go 2017-01-10
poisontap - Exploits locked/password protected computers over ... 2017-01-10
kcptun - A Simple UDP Tunnel Based On KCP 2017-01-11
discover-flask - Full Stack Web Development with Flask. ... 2017-01-12
spring-mvc-showcase - Demonstrates the features of the Spring MVC web fr... 2017-01-12
tushare - TuShare is a utility for crawling historical data ... 2017-01-12
raml-spec - RAML Specification http://raml.... 2017-01-12
android-stackblur - Android StackBlur is a library that can perform a ... 2017-01-12
sound-redux - A Soundcloud client built with React / Redux ... 2017-01-12
httpstat - curl statistics made simple 2017-01-12
vim - Vim configuration file and plug-ins 2017-01-12
appframework - The definitive HTML5 mobile javascript framework ... 2017-01-12
BaiduExporter - Assistant for Baidu to export download links to ar... 2017-01-11

 Back to top