BVB Source Codes

waterline Show help-add-to-collection.js Source code

Return Download waterline: download help-add-to-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.  
  8.  
  9.  
  10. /**
  11.  * helpAddToCollection()
  12.  *
  13.  * @param  {Dictionary}   query  [stage 2 query]
  14.  * @param  {Ref}   orm
  15.  * @param  {Function} done
  16.  */
  17.  
  18. module.exports = function helpAddToCollection(query, orm, cb) {
  19.  
  20.   // Validate arguments
  21.   if (_.isUndefined(query) || !_.isPlainObject(query)) {
  22.     throw new Error('Consistency violation: Invalid arguments - missing `stageTwoQuery` argument.');
  23.   }
  24.  
  25.   if (_.isUndefined(orm) || !_.isPlainObject(orm)) {
  26.     throw new Error('Consistency violation: Invalid arguments - missing `orm` argument.');
  27.   }
  28.  
  29.  
  30.  
  31.   // Get the model being used as the parent
  32.   var WLModel = orm.collections[query.using];
  33.   assert.equal(query.using.toLowerCase(), query.using, '`query.using` (identity) should have already been normalized before getting here!  But it was not: '+query.using);
  34.  
  35.   // Look up the association by name in the schema definition.
  36.   var schemaDef = WLModel.schema[query.collectionAttrName];
  37.  
  38.   // Look up the associated collection using the schema def which should have
  39.   // join tables normalized
  40.   var WLChild = orm.collections[schemaDef.collection];
  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.  
  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.  
  70.   //  鈻堚枅鈻堚晽   鈻堚枅鈻堚晽 鈻堚枅鈻堚枅鈻堚晽 鈻堚枅鈻堚晽   鈻堚枅鈺椻枅鈻堚晽   鈻堚枅鈺    鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈺 鈻堚枅鈻堚枅鈻堚枅鈺     鈻堚枅鈻堚晽   鈻堚枅鈻堚晽 鈻堚枅鈻堚枅鈻堚晽 鈻堚枅鈻堚晽   鈻堚枅鈺椻枅鈻堚晽   鈻堚枅鈺
  71.   //  鈻堚枅鈻堚枅鈺 鈻堚枅鈻堚枅鈺戔枅鈻堚晹鈺愨晲鈻堚枅鈺椻枅鈻堚枅鈻堚晽  鈻堚枅鈺戔暁鈻堚枅鈺 鈻堚枅鈺斺暆    鈺氣晲鈺愨枅鈻堚晹鈺愨晲鈺濃枅鈻堚晹鈺愨晲鈺愨枅鈻堚晽    鈻堚枅鈻堚枅鈺 鈻堚枅鈻堚枅鈺戔枅鈻堚晹鈺愨晲鈻堚枅鈺椻枅鈻堚枅鈻堚晽  鈻堚枅鈺戔暁鈻堚枅鈺 鈻堚枅鈺斺暆
  72.   //  鈻堚枅鈺斺枅鈻堚枅鈻堚晹鈻堚枅鈺戔枅鈻堚枅鈻堚枅鈻堚枅鈺戔枅鈻堚晹鈻堚枅鈺 鈻堚枅鈺 鈺氣枅鈻堚枅鈻堚晹鈺        鈻堚枅鈺   鈻堚枅鈺   鈻堚枅鈺    鈻堚枅鈺斺枅鈻堚枅鈻堚晹鈻堚枅鈺戔枅鈻堚枅鈻堚枅鈻堚枅鈺戔枅鈻堚晹鈻堚枅鈺 鈻堚枅鈺 鈺氣枅鈻堚枅鈻堚晹鈺
  73.   //  鈻堚枅鈺戔暁鈻堚枅鈺斺暆鈻堚枅鈺戔枅鈻堚晹鈺愨晲鈻堚枅鈺戔枅鈻堚晳鈺氣枅鈻堚晽鈻堚枅鈺  鈺氣枅鈻堚晹鈺         鈻堚枅鈺   鈻堚枅鈺   鈻堚枅鈺    鈻堚枅鈺戔暁鈻堚枅鈺斺暆鈻堚枅鈺戔枅鈻堚晹鈺愨晲鈻堚枅鈺戔枅鈻堚晳鈺氣枅鈻堚晽鈻堚枅鈺  鈺氣枅鈻堚晹鈺
  74.   //  鈻堚枅鈺 鈺氣晲鈺 鈻堚枅鈺戔枅鈻堚晳  鈻堚枅鈺戔枅鈻堚晳 鈺氣枅鈻堚枅鈻堚晳   鈻堚枅鈺          鈻堚枅鈺   鈺氣枅鈻堚枅鈻堚枅鈻堚晹鈺    鈻堚枅鈺 鈺氣晲鈺 鈻堚枅鈺戔枅鈻堚晳  鈻堚枅鈺戔枅鈻堚晳 鈺氣枅鈻堚枅鈻堚晳   鈻堚枅鈺
  75.   //  鈺氣晲鈺     鈺氣晲鈺濃暁鈺愨暆  鈺氣晲鈺濃暁鈺愨暆  鈺氣晲鈺愨晲鈺   鈺氣晲鈺          鈺氣晲鈺    鈺氣晲鈺愨晲鈺愨晲鈺     鈺氣晲鈺     鈺氣晲鈺濃暁鈺愨暆  鈺氣晲鈺濃暁鈺愨暆  鈺氣晲鈺愨晲鈺   鈺氣晲鈺
  76.   //
  77.   // If the collection uses a join table, build a query that inserts the records
  78.   // into the table.
  79.   if (manyToMany) {
  80.  
  81.     //  鈺斺晽 鈺 鈺︹暒鈺  鈺斺暒鈺  鈹攢鈹愨攲鈹€鈹愨攲鈹€鈹愨攲鈹€鈹愨敩鈹€鈹愨攲鈹€鈹愨攲鈹愨攲鈹屸攢鈹愨攲鈹€鈹  鈹屸敩鈹愨攲鈹€鈹愨攲鈹€鈹愨攲鈹€鈹愨敩鈹屸攼鈹屸攲鈹€鈹
  82.     //  鈺犫暕鈺椻晳 鈺戔晳鈺   鈺戔晳  鈹溾敩鈹樷敎鈹 鈹溾敜 鈹溾敜 鈹溾敩鈹樷敎鈹 鈹傗攤鈹傗攤  鈹溾敜   鈹傗攤鈹傗敎鈹€鈹も敎鈹€鈹樷敎鈹€鈹樷攤鈹傗攤鈹傗攤 鈹
  83.     //  鈺氣晲鈺濃暁鈺愨暆鈺┾暕鈺愨暆鈺愨暕鈺  鈹粹敂鈹€鈹斺攢鈹樷敂  鈹斺攢鈹樷敶鈹斺攢鈹斺攢鈹樷敇鈹斺敇鈹斺攢鈹樷敂鈹€鈹  鈹 鈹粹敶 鈹粹敶  鈹  鈹粹敇鈹斺敇鈹斺攢鈹
  84.     //
  85.     // Maps out the parent and child attribute names to use for the query.
  86.     var parentReference;
  87.     var childReference;
  88.  
  89.     // Find the parent reference
  90.     if (_.has(Object.getPrototypeOf(WLChild), 'junctionTable') && WLChild.junctionTable) {
  91.       // Assumes the generated junction table will only ever have two foreign key
  92.       // values. Should be safe for now and any changes would need to be made in
  93.       // Waterline-Schema where a map could be formed anyway.
  94.       _.each(WLChild.schema, function(val, key) {
  95.         if (!_.has(val, 'references')) {
  96.           return;
  97.         }
  98.  
  99.         // If this is the piece of the join table, set the parent reference.
  100.         if (_.has(val, 'columnName') && val.columnName === schemaDef.on) {
  101.           parentReference = key;
  102.         }
  103.       });
  104.     }
  105.  
  106.     // If it's a through table, grab the parent and child reference from the
  107.     // through table mapping that was generated by Waterline-Schema.
  108.     else if (_.has(Object.getPrototypeOf(WLChild), 'throughTable')) {
  109.       childReference = WLChild.throughTable[WLModel.identity + '.' + query.collectionAttrName];
  110.       _.each(WLChild.throughTable, function(val, key) {
  111.         if (key !== WLModel.identity + '.' + query.collectionAttrName) {
  112.           parentReference = val;
  113.         }
  114.       });
  115.     }
  116.  
  117.     // Find the child reference in a junction table
  118.     if (_.has(Object.getPrototypeOf(WLChild), 'junctionTable') && WLChild.junctionTable) {
  119.       // Assumes the generated junction table will only ever have two foreign key
  120.       // values. Should be safe for now and any changes would need to be made in
  121.       // Waterline-Schema where a map could be formed anyway.
  122.       _.each(WLChild.schema, function(val, key) {
  123.         if (!_.has(val, 'references')) {
  124.           return;
  125.         }
  126.  
  127.         // If this is the other piece of the join table, set the child reference.
  128.         if (_.has(val, 'columnName') && val.columnName !== schemaDef.on) {
  129.           childReference = key;
  130.         }
  131.       });
  132.     }
  133.  
  134.  
  135.     //  鈺斺晽 鈺 鈺︹暒鈺  鈺斺暒鈺  鈹屸攢鈹 鈹 鈹攲鈹€鈹愨敩鈹€鈹愨敩 鈹
  136.     //  鈺犫暕鈺椻晳 鈺戔晳鈺   鈺戔晳  鈹傗攢鈹尖攼鈹 鈹傗敎鈹 鈹溾敩鈹樷敂鈹敇
  137.     //  鈺氣晲鈺濃暁鈺愨暆鈺┾暕鈺愨暆鈺愨暕鈺  鈹斺攢鈹樷敂鈹斺攢鈹樷敂鈹€鈹樷敶鈹斺攢 鈹
  138.  
  139.     // Build an array to hold all the records being inserted
  140.     var joinRecords = [];
  141.  
  142.     // For each target record, build an insert query for the associated records.
  143.     _.each(query.targetRecordIds, function(targetId) {
  144.       _.each(query.associatedIds, function(associatedId) {
  145.         var record = {};
  146.         record[parentReference] = targetId;
  147.         record[childReference] = associatedId;
  148.         joinRecords.push(record);
  149.       });
  150.     });
  151.  
  152.  
  153.     //  鈺︹晲鈺椻暒 鈺︹晹鈺椻晹  鈹屸攢鈹 鈹 鈹攲鈹€鈹愨敩鈹€鈹愨敩 鈹
  154.     //  鈺犫暒鈺濃晳 鈺戔晳鈺戔晳  鈹傗攢鈹尖攼鈹 鈹傗敎鈹 鈹溾敩鈹樷敂鈹敇
  155.     //  鈺┾暁鈺愨暁鈺愨暆鈺濃暁鈺  鈹斺攢鈹樷敂鈹斺攢鈹樷敂鈹€鈹樷敶鈹斺攢 鈹
  156.     WLChild.createEach(joinRecords, cb, query.meta);
  157.  
  158.     return;
  159.   }//-鈥
  160.  
  161.  
  162.   //  鈻堚枅鈻堚枅鈻堚枅鈺 鈻堚枅鈻堚枅鈻堚枅鈻堚晽鈻堚枅鈺      鈻堚枅鈻堚枅鈻堚枅鈺 鈻堚枅鈻堚晽   鈻堚枅鈺 鈻堚枅鈻堚枅鈻堚枅鈺 鈻堚枅鈻堚枅鈻堚枅鈻堚晽    鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈺 鈻堚枅鈻堚枅鈻堚枅鈺
  163.   //  鈻堚枅鈺斺晲鈺愨枅鈻堚晽鈻堚枅鈺斺晲鈺愨晲鈺愨暆鈻堚枅鈺     鈻堚枅鈺斺晲鈺愨晲鈻堚枅鈺椻枅鈻堚枅鈻堚晽  鈻堚枅鈺戔枅鈻堚晹鈺愨晲鈺愨晲鈺 鈻堚枅鈺斺晲鈺愨晲鈺愨暆    鈺氣晲鈺愨枅鈻堚晹鈺愨晲鈺濃枅鈻堚晹鈺愨晲鈺愨枅鈻堚晽
  164.   //  鈻堚枅鈻堚枅鈻堚枅鈺斺暆鈻堚枅鈻堚枅鈻堚晽  鈻堚枅鈺     鈻堚枅鈺   鈻堚枅鈺戔枅鈻堚晹鈻堚枅鈺 鈻堚枅鈺戔枅鈻堚晳  鈻堚枅鈻堚晽鈻堚枅鈻堚枅鈻堚枅鈻堚晽       鈻堚枅鈺   鈻堚枅鈺   鈻堚枅鈺
  165.   //  鈻堚枅鈺斺晲鈺愨枅鈻堚晽鈻堚枅鈺斺晲鈺愨暆  鈻堚枅鈺     鈻堚枅鈺   鈻堚枅鈺戔枅鈻堚晳鈺氣枅鈻堚晽鈻堚枅鈺戔枅鈻堚晳   鈻堚枅鈺戔暁鈺愨晲鈺愨晲鈻堚枅鈺       鈻堚枅鈺   鈻堚枅鈺   鈻堚枅鈺
  166.   //  鈻堚枅鈻堚枅鈻堚枅鈺斺暆鈻堚枅鈻堚枅鈻堚枅鈻堚晽鈻堚枅鈻堚枅鈻堚枅鈻堚晽鈺氣枅鈻堚枅鈻堚枅鈻堚晹鈺濃枅鈻堚晳 鈺氣枅鈻堚枅鈻堚晳鈺氣枅鈻堚枅鈻堚枅鈻堚晹鈺濃枅鈻堚枅鈻堚枅鈻堚枅鈺       鈻堚枅鈺   鈺氣枅鈻堚枅鈻堚枅鈻堚晹鈺
  167.   //  鈺氣晲鈺愨晲鈺愨晲鈺 鈺氣晲鈺愨晲鈺愨晲鈺愨暆鈺氣晲鈺愨晲鈺愨晲鈺愨暆 鈺氣晲鈺愨晲鈺愨晲鈺 鈺氣晲鈺  鈺氣晲鈺愨晲鈺 鈺氣晲鈺愨晲鈺愨晲鈺 鈺氣晲鈺愨晲鈺愨晲鈺愨暆       鈺氣晲鈺    鈺氣晲鈺愨晲鈺愨晲鈺
  168.   //
  169.   // Otherwise the child records need to be updated to reflect the new foreign
  170.   // key value. Because in this case the targetRecordIds **should** only be a
  171.   // single value, just an update here should do the trick.
  172.  
  173.  
  174.   //  鈺斺晽 鈺 鈺︹暒鈺  鈺斺暒鈺  鈹屸攢鈹 鈹 鈹攲鈹€鈹愨敩鈹€鈹愨敩 鈹
  175.   //  鈺犫暕鈺椻晳 鈺戔晳鈺   鈺戔晳  鈹傗攢鈹尖攼鈹 鈹傗敎鈹 鈹溾敩鈹樷敂鈹敇
  176.   //  鈺氣晲鈺濃暁鈺愨暆鈺┾暕鈺愨暆鈺愨暕鈺  鈹斺攢鈹樷敂鈹斺攢鈹樷敂鈹€鈹樷敶鈹斺攢 鈹
  177.  
  178.  
  179.   // Build up a search criteria
  180.   var criteria = {
  181.     where: {}
  182.   };
  183.  
  184.   criteria.where[WLChild.primaryKey] = query.associatedIds;
  185.  
  186.   // Build up the values to update
  187.   var valuesToUpdate = {};
  188.   valuesToUpdate[schemaDef.via] = _.first(query.targetRecordIds);
  189.  
  190.  
  191.   //  鈺︹晲鈺椻暒 鈺︹晹鈺椻晹  鈹屸攢鈹 鈹 鈹攲鈹€鈹愨敩鈹€鈹愨敩 鈹
  192.   //  鈺犫暒鈺濃晳 鈺戔晳鈺戔晳  鈹傗攢鈹尖攼鈹 鈹傗敎鈹 鈹溾敩鈹樷敂鈹敇
  193.   //  鈺┾暁鈺愨暁鈺愨暆鈺濃暁鈺  鈹斺攢鈹樷敂鈹斺攢鈹樷敂鈹€鈹樷敶鈹斺攢 鈹
  194.  
  195.   WLChild.update(criteria, valuesToUpdate, cb, query.meta);
  196.  
  197. };
  198.  
downloadhelp-add-to-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