BVB Source Codes

waterline Show add-to-collection.js Source code

Return Download waterline: download add-to-collection.js Source code - Download waterline Source code - Type:.js
  1. /**
  2.  * Module dependencies
  3.  */
  4.  
  5. var _ = require('@sailshq/lodash');
  6. var flaverr = require('flaverr');
  7. var Deferred = require('../utils/query/deferred');
  8. var forgeStageTwoQuery = require('../utils/query/forge-stage-two-query');
  9. var helpAddToCollection = require('../utils/collection-operations/help-add-to-collection');
  10.  
  11.  
  12. /**
  13.  * addToCollection()
  14.  *
  15.  * Add new child records to the specified collection in each of the target record(s).
  16.  *
  17.  * ```
  18.  * // For users 3 and 4, add pets 99 and 98 to the "pets" collection.
  19.  * // > (if either user record already has one of those pets in its "pets",
  20.  * // > then we just silently skip over it)
  21.  * User.addToCollection([3,4], 'pets', [99,98]).exec(...);
  22.  * ```
  23.  *
  24.  * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  25.  *
  26.  * Usage without deferred object:
  27.  * ================================================
  28.  *
  29.  * @param {Array?|String?|Number?} targetRecordIds
  30.  *
  31.  * @param {String?} collectionAttrName
  32.  *
  33.  * @param {Array?} associatedIds
  34.  *
  35.  * @param {Function?} done
  36.  *        Callback function to run when query has either finished successfully or errored.
  37.  *        (If unspecified, will return a Deferred object instead of actually doing anything.)
  38.  *
  39.  * @param {Ref?} meta
  40.  *     For internal use.
  41.  *
  42.  * @returns {Ref?} Deferred object if no `done` callback was provided
  43.  *
  44.  * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  45.  *
  46.  * The underlying query keys:
  47.  * ==============================
  48.  *
  49.  * @qkey {Array|String|Number} targetRecordIds
  50.  *     The primary key value(s) (i.e. ids) for the parent record(s).
  51.  *     Must be a number or string; e.g. '507f191e810c19729de860ea' or 49
  52.  *     Or an array of numbers or strings; e.g. ['507f191e810c19729de860ea', '14832ace0c179de897'] or [49, 32, 37]
  53.  *     If an empty array (`[]`) is specified, then this is a no-op.
  54.  *
  55.  * @qkey {String} collectionAttrName
  56.  *     The name of the collection association (e.g. "pets")
  57.  *
  58.  * @qkey {Array} associatedIds
  59.  *     The primary key values (i.e. ids) for the child records to add.
  60.  *     Must be an array of numbers or strings; e.g. ['334724948aca33ea0f13', '913303583e0af031358bac931'] or [18, 19]
  61.  *     If an empty array (`[]`) is specified, then this is a no-op.
  62.  *
  63.  * @qkey {Dictionary?} meta
  64.  * @qkey {String} using
  65.  * @qkey {String} method
  66.  *
  67.  * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  68.  */
  69.  
  70. module.exports = function addToCollection(/* targetRecordIds, collectionAttrName, associatedIds?, done?, meta? */) {
  71.  
  72.   // Set up a few, common local vars for convenience / familiarity.
  73.   var WLModel = this;
  74.   var orm = this.waterline;
  75.   var modelIdentity = this.identity;
  76.  
  77.   // Build query w/ initial, universal keys.
  78.   var query = {
  79.     method: 'addToCollection',
  80.     using: modelIdentity
  81.   };
  82.  
  83.   //  鈻堚枅鈺   鈻堚枅鈺 鈻堚枅鈻堚枅鈻堚晽 鈻堚枅鈻堚枅鈻堚枅鈺 鈻堚枅鈺 鈻堚枅鈻堚枅鈻堚晽 鈻堚枅鈻堚枅鈻堚枅鈺 鈻堚枅鈺 鈻堚枅鈻堚枅鈻堚枅鈺椻枅鈻堚枅鈻堚枅鈻堚枅鈺
  84.   //  鈻堚枅鈺   鈻堚枅鈺戔枅鈻堚晹鈺愨晲鈻堚枅鈺椻枅鈻堚晹鈺愨晲鈻堚枅鈺椻枅鈻堚晳鈻堚枅鈺斺晲鈺愨枅鈻堚晽鈻堚枅鈺斺晲鈺愨枅鈻堚晽鈻堚枅鈺戔枅鈻堚晹鈺愨晲鈺愨晲鈺濃枅鈻堚晹鈺愨晲鈺愨晲鈺
  85.   //  鈻堚枅鈺   鈻堚枅鈺戔枅鈻堚枅鈻堚枅鈻堚枅鈺戔枅鈻堚枅鈻堚枅鈻堚晹鈺濃枅鈻堚晳鈻堚枅鈻堚枅鈻堚枅鈻堚晳鈻堚枅鈺  鈻堚枅鈺戔枅鈻堚晳鈻堚枅鈺     鈻堚枅鈻堚枅鈻堚枅鈻堚晽
  86.   //  鈺氣枅鈻堚晽 鈻堚枅鈺斺暆鈻堚枅鈺斺晲鈺愨枅鈻堚晳鈻堚枅鈺斺晲鈺愨枅鈻堚晽鈻堚枅鈺戔枅鈻堚晹鈺愨晲鈻堚枅鈺戔枅鈻堚晳  鈻堚枅鈺戔枅鈻堚晳鈻堚枅鈺     鈺氣晲鈺愨晲鈺愨枅鈻堚晳
  87.   //   鈺氣枅鈻堚枅鈻堚晹鈺 鈻堚枅鈺  鈻堚枅鈺戔枅鈻堚晳  鈻堚枅鈺戔枅鈻堚晳鈻堚枅鈺  鈻堚枅鈺戔枅鈻堚枅鈻堚枅鈻堚晹鈺濃枅鈻堚晳鈺氣枅鈻堚枅鈻堚枅鈻堚晽鈻堚枅鈻堚枅鈻堚枅鈻堚晳
  88.   //    鈺氣晲鈺愨晲鈺  鈺氣晲鈺  鈺氣晲鈺濃暁鈺愨暆  鈺氣晲鈺濃暁鈺愨暆鈺氣晲鈺  鈺氣晲鈺濃暁鈺愨晲鈺愨晲鈺愨暆 鈺氣晲鈺 鈺氣晲鈺愨晲鈺愨晲鈺濃暁鈺愨晲鈺愨晲鈺愨晲鈺
  89.   //
  90.   // Handle the various supported usage possibilities
  91.   // (locate the `done` callback, and extend the `query` dictionary)
  92.  
  93.   // The `done` callback, if one was provided.
  94.   var done;
  95.  
  96.   // Handle the various supported usage possibilities
  97.   // (locate the `done` callback)
  98.   //
  99.   // > Note that we define `args` so that we can insulate access
  100.   // > to the arguments provided to this function.
  101.   var args = arguments;
  102.   (function _handleVariadicUsage(){
  103.  
  104.     // The metadata container, if one was provided.
  105.     var _meta;
  106.  
  107.  
  108.     // Handle first two arguments:
  109.     // (both of which always have exactly one meaning)
  110.     //
  111.     // 鈥 addToCollection(targetRecordIds, collectionAttrName, ...)
  112.     query.targetRecordIds = args[0];
  113.     query.collectionAttrName = args[1];
  114.  
  115.  
  116.     // Handle double meaning of third argument, & then handle the rest:
  117.     //
  118.     // 鈥 addToCollection(____, ____, associatedIds, done, _meta)
  119.     var is3rdArgArray = !_.isUndefined(args[2]);
  120.     if (is3rdArgArray) {
  121.       query.associatedIds = args[2];
  122.       done = args[3];
  123.       _meta = args[4];
  124.     }
  125.     // 鈥 addToCollection(____, ____, done, _meta)
  126.     else {
  127.       done = args[2];
  128.       _meta = args[3];
  129.     }
  130.  
  131.     // Fold in `_meta`, if relevant.
  132.     if (!_.isUndefined(_meta)) {
  133.       query.meta = _meta;
  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.   // If a callback function was not specified, then build a new `Deferred` and bail now.
  160.   //
  161.   // > This method will be called AGAIN automatically when the Deferred is executed.
  162.   // > and next time, it'll have a callback.
  163.   if (!done) {
  164.     return new Deferred(WLModel, addToCollection, query);
  165.   } // --鈥
  166.  
  167.  
  168.   // Otherwise, IWMIH, we know that a callback was specified.
  169.   // So...
  170.   //
  171.   //  鈻堚枅鈻堚枅鈻堚枅鈻堚晽鈻堚枅鈺  鈻堚枅鈺椻枅鈻堚枅鈻堚枅鈻堚枅鈺 鈻堚枅鈻堚枅鈻堚枅鈺椻枅鈻堚晽   鈻堚枅鈺椻枅鈻堚枅鈻堚枅鈻堚枅鈻堚晽鈻堚枅鈻堚枅鈻堚枅鈻堚晽
  172.   //  鈻堚枅鈺斺晲鈺愨晲鈺愨暆鈺氣枅鈻堚晽鈻堚枅鈺斺暆鈻堚枅鈺斺晲鈺愨晲鈺愨暆鈻堚枅鈺斺晲鈺愨晲鈺愨暆鈻堚枅鈺   鈻堚枅鈺戔暁鈺愨晲鈻堚枅鈺斺晲鈺愨暆鈻堚枅鈺斺晲鈺愨晲鈺愨暆
  173.   //  鈻堚枅鈻堚枅鈻堚晽   鈺氣枅鈻堚枅鈺斺暆 鈻堚枅鈻堚枅鈻堚晽  鈻堚枅鈺     鈻堚枅鈺   鈻堚枅鈺   鈻堚枅鈺   鈻堚枅鈻堚枅鈻堚晽
  174.   //  鈻堚枅鈺斺晲鈺愨暆   鈻堚枅鈺斺枅鈻堚晽 鈻堚枅鈺斺晲鈺愨暆  鈻堚枅鈺     鈻堚枅鈺   鈻堚枅鈺   鈻堚枅鈺   鈻堚枅鈺斺晲鈺愨暆
  175.   //  鈻堚枅鈻堚枅鈻堚枅鈻堚晽鈻堚枅鈺斺暆 鈻堚枅鈺椻枅鈻堚枅鈻堚枅鈻堚枅鈺椻暁鈻堚枅鈻堚枅鈻堚枅鈺椻暁鈻堚枅鈻堚枅鈻堚枅鈺斺暆   鈻堚枅鈺   鈻堚枅鈻堚枅鈻堚枅鈻堚晽
  176.   //  鈺氣晲鈺愨晲鈺愨晲鈺愨暆鈺氣晲鈺  鈺氣晲鈺濃暁鈺愨晲鈺愨晲鈺愨晲鈺 鈺氣晲鈺愨晲鈺愨晲鈺 鈺氣晲鈺愨晲鈺愨晲鈺    鈺氣晲鈺   鈺氣晲鈺愨晲鈺愨晲鈺愨暆
  177.  
  178.  
  179.   //  鈺斺晲鈺椻晹鈺愨晽鈺︹晲鈺椻晹鈺愨晽鈺斺晲鈺  鈹屸攢鈹愨攲鈹攼鈹屸攢鈹愨攲鈹€鈹愨攲鈹€鈹  鈹屸敩鈹愨敩 鈹攲鈹€鈹  鈹屸攢鈹 鈹 鈹攲鈹€鈹愨敩鈹€鈹愨敩 鈹
  180.   //  鈺犫暎 鈺 鈺戔暊鈺︹暆鈺 鈺︹晳鈺   鈹斺攢鈹 鈹 鈹溾攢鈹も攤 鈹敎鈹    鈹 鈹傗攤鈹傗攤 鈹  鈹傗攢鈹尖攼鈹 鈹傗敎鈹 鈹溾敩鈹樷敂鈹敇
  181.   //  鈺  鈺氣晲鈺濃暕鈺氣晲鈺氣晲鈺濃暁鈺愨暆  鈹斺攢鈹 鈹 鈹 鈹粹敂鈹€鈹樷敂鈹€鈹   鈹 鈹斺敶鈹樷敂鈹€鈹  鈹斺攢鈹樷敂鈹斺攢鈹樷敂鈹€鈹樷敶鈹斺攢 鈹
  182.   //
  183.   // Forge a stage 2 query (aka logical protostatement)
  184.   try {
  185.     forgeStageTwoQuery(query, orm);
  186.   } catch (e) {
  187.     switch (e.code) {
  188.  
  189.       case 'E_INVALID_TARGET_RECORD_IDS':
  190.         return done(
  191.           flaverr(
  192.             { name: 'UsageError' },
  193.             new Error(
  194.               'The target record ids (i.e. first argument) passed to `.addToCollection()` '+
  195.               'should be the ID (or IDs) of target records whose collection will be modified.\n'+
  196.               'Details:\n'+
  197.               '  ' + e.details + '\n'
  198.             )
  199.           )
  200.         );
  201.  
  202.       case 'E_INVALID_COLLECTION_ATTR_NAME':
  203.         return done(
  204.           flaverr(
  205.             { name: 'UsageError' },
  206.             new Error(
  207.               'The collection attr name (i.e. second argument) to `.addToCollection()` should '+
  208.               'be the name of a collection association from this model.\n'+
  209.               'Details:\n'+
  210.               '  ' + e.details + '\n'
  211.             )
  212.           )
  213.         );
  214.  
  215.       case 'E_INVALID_ASSOCIATED_IDS':
  216.         return done(
  217.           flaverr(
  218.             { name: 'UsageError' },
  219.             new Error(
  220.               'The associated ids (i.e. using `.members()`, or the third argument) passed to `.addToCollection()` should be '+
  221.               'the ID (or IDs) of associated records to add.\n'+
  222.               'Details:\n'+
  223.               '  ' + e.details + '\n'
  224.             )
  225.           )
  226.         );
  227.  
  228.       case 'E_NOOP':
  229.         return done();
  230.         // ^ tolerate no-ops -- i.e. empty array of target record ids or empty array of associated ids (members)
  231.  
  232.       case 'E_INVALID_META':
  233.         return done(e);
  234.         // ^ when the standard usage error is good enough as-is, without any further customization
  235.  
  236.       default:
  237.         return done(e);
  238.         // ^ when an internal, miscellaneous, or unexpected error occurs
  239.  
  240.     }
  241.   } // >-鈥
  242.  
  243.  
  244.   //  鈹屸攼鈹屸攲鈹€鈹愨敩 鈹  鈺斺晲鈺椻晹鈺愨晽鈺斺暒鈺椻暒 鈺︹晹鈺愨晽鈺  鈺 鈺 鈺  鈹屸敩鈹愨攲鈹€鈹愨敩  鈹攲鈹€  鈹屸敩鈹愨攲鈹€鈹  鈹屸敩鈹愨敩 鈹攲鈹€鈹  鈹屸敩鈹愨攲鈹 鈹屸攢鈹
  245.   //  鈹傗攤鈹傗攤 鈹傗攤鈹傗攤  鈺犫晲鈺b晳   鈺 鈺 鈺戔暊鈺愨暎鈺  鈺 鈺氣暒鈺   鈹 鈹溾攢鈹も攤  鈹溾敶鈹   鈹 鈹 鈹   鈹 鈹溾攢鈹も敎鈹    鈹傗攤鈹溾敶鈹愨敂鈹€鈹
  246.   //  鈹樷敂鈹樷敂鈹€鈹樷敂鈹粹敇  鈺 鈺┾暁鈺愨暆 鈺 鈺氣晲鈺濃暕 鈺┾暕鈺愨暆鈺┾晲鈺濃暕    鈹 鈹 鈹粹敶鈹€鈹樷敶 鈹   鈹 鈹斺攢鈹   鈹 鈹 鈹粹敂鈹€鈹  鈹€鈹粹敇鈹斺攢鈹樷敂鈹€鈹
  247.   helpAddToCollection(query, orm, function (err) {
  248.     if (err) { return done(err); }
  249.  
  250.     // IWMIH, everything worked!
  251.     // > Note that we do not send back a result of any kind-- this it to reduce the likelihood
  252.     // > writing userland code that relies undocumented/experimental output.
  253.     return done();
  254.  
  255.   });//</helpAddToCollection>
  256.  
  257. };
  258.  
downloadadd-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
mongo-express - Web-based MongoDB admin interface, written with No... 2017-05-17
XCL-Charts - Android charting libraries (XCL-Charts is a free c... 2017-05-17
scrollMonitor - A simple and fast API to monitor elements as you s... 2017-05-16
XCDYouTubeKit - YouTube video player for iOS, tvOS and macOS 2017-05-16
rails-erd - Generate Entity-Relationship Diagrams for Rails ap... 2017-05-16
np - A better `npm publish` 2017-05-16
android - Smartisan open source code for full build.(repo ma... 2017-05-16
word_cloud - A little word cloud generator in Pytho 2017-05-16
mobileplayer-ios - 2017-05-16
Destroy-Windows-10-Spying - Destroy Windows Spying tool ... 2017-05-15

 Back to top