BVB Source Codes

waterline Show replace-collection.js Source code

Return Download waterline: download replace-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 helpReplaceCollection = require('../utils/collection-operations/help-replace-collection');
  10.  
  11.  
  12. /**
  13.  * replaceCollection()
  14.  *
  15.  * Replace all members of the specified collection in each of the target record(s).
  16.  *
  17.  * ```
  18.  * // For users 3 and 4, change their "pets" collection to contain ONLY pets 99 and 98.
  19.  * User.replaceCollection([3,4], 'pets', [99,98]).exec(...);
  20.  * ```
  21.  *
  22.  * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  23.  *
  24.  * Usage without deferred object:
  25.  * ================================================
  26.  *
  27.  * @param {Array?|String?|Number?} targetRecordIds
  28.  *
  29.  * @param {String?} collectionAttrName
  30.  *
  31.  * @param {Array?} associatedIds
  32.  *
  33.  * @param {Function?} done
  34.  *        Callback function to run when query has either finished successfully or errored.
  35.  *        (If unspecified, will return a Deferred object instead of actually doing anything.)
  36.  *
  37.  * @param {Ref?} meta
  38.  *     For internal use.
  39.  *
  40.  * @returns {Ref?} Deferred object if no `done` callback was provided
  41.  *
  42.  * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  43.  *
  44.  * The underlying query keys:
  45.  * ==============================
  46.  *
  47.  * @qkey {Array|String|Number} targetRecordIds
  48.  *     The primary key value(s) (i.e. ids) for the parent record(s).
  49.  *     Must be a number or string; e.g. '507f191e810c19729de860ea' or 49
  50.  *     Or an array of numbers or strings; e.g. ['507f191e810c19729de860ea', '14832ace0c179de897'] or [49, 32, 37]
  51.  *     If an empty array (`[]`) is specified, then this is a no-op.
  52.  *
  53.  * @qkey {String} collectionAttrName
  54.  *     The name of the collection association (e.g. "pets")
  55.  *
  56.  * @qkey {Array} associatedIds
  57.  *     The primary key values (i.e. ids) for the child records that will be the new members of the association.
  58.  *     Must be an array of numbers or strings; e.g. ['334724948aca33ea0f13', '913303583e0af031358bac931'] or [18, 19]
  59.  *     Specify an empty array (`[]`) to completely wipe out the collection's contents.
  60.  *
  61.  * @qkey {Dictionary?} meta
  62.  * @qkey {String} using
  63.  * @qkey {String} method
  64.  *
  65.  * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  66.  */
  67.  
  68. module.exports = function replaceCollection(/* targetRecordIds?, collectionAttrName?, associatedIds?, done?, meta? */) {
  69.  
  70.   // Set up a few, common local vars for convenience / familiarity.
  71.   var WLModel = this;
  72.   var orm = this.waterline;
  73.   var modelIdentity = this.identity;
  74.  
  75.  
  76.   // Build query w/ initial, universal keys.
  77.   var query = {
  78.     method: 'replaceCollection',
  79.     using: modelIdentity
  80.   };
  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.     // 鈥 replaceCollection(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.     // 鈥 replaceCollection(____, ____, 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.     // 鈥 replaceCollection(____, ____, 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.   //  鈹斺攢    鈹粹敂    鈹粹敂鈹€鈹斺攢鈹樷敶鈹€鈹樷敂鈹€鈹 鈹斺敇 鈹 鈹粹敇鈹斺敇 鈹     鈹€鈹
  160.   // If a callback function was not specified, then build a new `Deferred` and bail now.
  161.   //
  162.   // > This method will be called AGAIN automatically when the Deferred is executed.
  163.   // > and next time, it'll have a callback.
  164.   if (!done) {
  165.     return new Deferred(WLModel, replaceCollection, query);
  166.   } // --鈥
  167.  
  168.  
  169.   // Otherwise, IWMIH, we know that a callback was specified.
  170.   // So...
  171.   //
  172.   //  鈻堚枅鈻堚枅鈻堚枅鈻堚晽鈻堚枅鈺  鈻堚枅鈺椻枅鈻堚枅鈻堚枅鈻堚枅鈺 鈻堚枅鈻堚枅鈻堚枅鈺椻枅鈻堚晽   鈻堚枅鈺椻枅鈻堚枅鈻堚枅鈻堚枅鈻堚晽鈻堚枅鈻堚枅鈻堚枅鈻堚晽
  173.   //  鈻堚枅鈺斺晲鈺愨晲鈺愨暆鈺氣枅鈻堚晽鈻堚枅鈺斺暆鈻堚枅鈺斺晲鈺愨晲鈺愨暆鈻堚枅鈺斺晲鈺愨晲鈺愨暆鈻堚枅鈺   鈻堚枅鈺戔暁鈺愨晲鈻堚枅鈺斺晲鈺愨暆鈻堚枅鈺斺晲鈺愨晲鈺愨暆
  174.   //  鈻堚枅鈻堚枅鈻堚晽   鈺氣枅鈻堚枅鈺斺暆 鈻堚枅鈻堚枅鈻堚晽  鈻堚枅鈺     鈻堚枅鈺   鈻堚枅鈺   鈻堚枅鈺   鈻堚枅鈻堚枅鈻堚晽
  175.   //  鈻堚枅鈺斺晲鈺愨暆   鈻堚枅鈺斺枅鈻堚晽 鈻堚枅鈺斺晲鈺愨暆  鈻堚枅鈺     鈻堚枅鈺   鈻堚枅鈺   鈻堚枅鈺   鈻堚枅鈺斺晲鈺愨暆
  176.   //  鈻堚枅鈻堚枅鈻堚枅鈻堚晽鈻堚枅鈺斺暆 鈻堚枅鈺椻枅鈻堚枅鈻堚枅鈻堚枅鈺椻暁鈻堚枅鈻堚枅鈻堚枅鈺椻暁鈻堚枅鈻堚枅鈻堚枅鈺斺暆   鈻堚枅鈺   鈻堚枅鈻堚枅鈻堚枅鈻堚晽
  177.   //  鈺氣晲鈺愨晲鈺愨晲鈺愨暆鈺氣晲鈺  鈺氣晲鈺濃暁鈺愨晲鈺愨晲鈺愨晲鈺 鈺氣晲鈺愨晲鈺愨晲鈺 鈺氣晲鈺愨晲鈺愨晲鈺    鈺氣晲鈺   鈺氣晲鈺愨晲鈺愨晲鈺愨暆
  178.  
  179.  
  180.   //  鈺斺晲鈺椻晹鈺愨晽鈺︹晲鈺椻晹鈺愨晽鈺斺晲鈺  鈹屸攢鈹愨攲鈹攼鈹屸攢鈹愨攲鈹€鈹愨攲鈹€鈹  鈹屸敩鈹愨敩 鈹攲鈹€鈹  鈹屸攢鈹 鈹 鈹攲鈹€鈹愨敩鈹€鈹愨敩 鈹
  181.   //  鈺犫暎 鈺 鈺戔暊鈺︹暆鈺 鈺︹晳鈺   鈹斺攢鈹 鈹 鈹溾攢鈹も攤 鈹敎鈹    鈹 鈹傗攤鈹傗攤 鈹  鈹傗攢鈹尖攼鈹 鈹傗敎鈹 鈹溾敩鈹樷敂鈹敇
  182.   //  鈺  鈺氣晲鈺濃暕鈺氣晲鈺氣晲鈺濃暁鈺愨暆  鈹斺攢鈹 鈹 鈹 鈹粹敂鈹€鈹樷敂鈹€鈹   鈹 鈹斺敶鈹樷敂鈹€鈹  鈹斺攢鈹樷敂鈹斺攢鈹樷敂鈹€鈹樷敶鈹斺攢 鈹
  183.   //
  184.   // Forge a stage 2 query (aka logical protostatement)
  185.   try {
  186.     forgeStageTwoQuery(query, orm);
  187.   } catch (e) {
  188.     switch (e.code) {
  189.  
  190.       case 'E_INVALID_TARGET_RECORD_IDS':
  191.         return done(
  192.           flaverr(
  193.             { name: 'UsageError' },
  194.             new Error(
  195.               'The target record ids (i.e. first argument) passed to `.replaceCollection()` '+
  196.               'should be the ID (or IDs) of target records whose collection will be modified.\n'+
  197.               'Details:\n'+
  198.               '  ' + e.details + '\n'
  199.             )
  200.           )
  201.         );
  202.  
  203.       case 'E_INVALID_COLLECTION_ATTR_NAME':
  204.         return done(
  205.           flaverr(
  206.             { name: 'UsageError' },
  207.             new Error(
  208.               'The collection attr name (i.e. second argument) to `.replaceCollection()` should '+
  209.               'be the name of a collection association from this model.\n'+
  210.               'Details:\n'+
  211.               '  ' + e.details + '\n'
  212.             )
  213.           )
  214.         );
  215.  
  216.       case 'E_INVALID_ASSOCIATED_IDS':
  217.         return done(
  218.           flaverr(
  219.             { name: 'UsageError' },
  220.             new Error(
  221.               'The associated ids (i.e. using `.members()`, or the third argument) passed to `.replaceCollection()` should be '+
  222.               'the ID (or IDs) of associated records to use.\n'+
  223.               'Details:\n'+
  224.               '  ' + e.details + '\n'
  225.             )
  226.           )
  227.         );
  228.  
  229.       case 'E_NOOP':
  230.         return done();
  231.         // ^ tolerate no-ops -- i.e. empty array of target record ids
  232.  
  233.       case 'E_INVALID_META':
  234.         return done(e);
  235.         // ^ when the standard usage error is good enough as-is, without any further customization
  236.  
  237.       default:
  238.         return done(e);
  239.         // ^ when an internal, miscellaneous, or unexpected error occurs
  240.  
  241.     }
  242.   } // >-鈥
  243.  
  244.  
  245.   //  鈹屸攼鈹屸攲鈹€鈹愨敩 鈹  鈺斺晲鈺椻晹鈺愨晽鈺斺暒鈺椻暒 鈺︹晹鈺愨晽鈺  鈺 鈺 鈺  鈹屸敩鈹愨攲鈹€鈹愨敩  鈹攲鈹€  鈹屸敩鈹愨攲鈹€鈹  鈹屸敩鈹愨敩 鈹攲鈹€鈹  鈹屸敩鈹愨攲鈹 鈹屸攢鈹
  246.   //  鈹傗攤鈹傗攤 鈹傗攤鈹傗攤  鈺犫晲鈺b晳   鈺 鈺 鈺戔暊鈺愨暎鈺  鈺 鈺氣暒鈺   鈹 鈹溾攢鈹も攤  鈹溾敶鈹   鈹 鈹 鈹   鈹 鈹溾攢鈹も敎鈹    鈹傗攤鈹溾敶鈹愨敂鈹€鈹
  247.   //  鈹樷敂鈹樷敂鈹€鈹樷敂鈹粹敇  鈺 鈺┾暁鈺愨暆 鈺 鈺氣晲鈺濃暕 鈺┾暕鈺愨暆鈺┾晲鈺濃暕    鈹 鈹 鈹粹敶鈹€鈹樷敶 鈹   鈹 鈹斺攢鈹   鈹 鈹 鈹粹敂鈹€鈹  鈹€鈹粹敇鈹斺攢鈹樷敂鈹€鈹
  248.   helpReplaceCollection(query, orm, function (err) {
  249.     if (err) { return done(err); }
  250.  
  251.     // IWMIH, everything worked!
  252.     // > Note that we do not send back a result of any kind-- this it to reduce the likelihood
  253.     // > writing userland code that relies undocumented/experimental output.
  254.     return done();
  255.  
  256.   });//</helpReplaceCollection>
  257.  
  258. };
  259.  
downloadreplace-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