BVB Source Codes

waterline Show create-each.js Source code

Return Download waterline: download create-each.js Source code - Download waterline Source code - Type:.js
  1. /**
  2.  * Module Dependencies
  3.  */
  4.  
  5. var util = require('util');
  6. var _ = require('@sailshq/lodash');
  7. var async = require('async');
  8. var forgeStageTwoQuery = require('../utils/query/forge-stage-two-query');
  9. var forgeStageThreeQuery = require('../utils/query/forge-stage-three-query');
  10. var Deferred = require('../utils/query/deferred');
  11. var transformUniquenessError = require('../utils/query/transform-uniqueness-error');
  12. var processAllRecords = require('../utils/records/process-all-records');
  13.  
  14.  
  15. /**
  16.  * createEach()
  17.  *
  18.  * Create a set of records in the database.
  19.  *
  20.  * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  21.  *
  22.  * Usage without deferred object:
  23.  * ================================================
  24.  *
  25.  * @param {Array?} newRecords
  26.  *
  27.  * @param {Function?} done
  28.  *        Callback function to run when query has either finished successfully or errored.
  29.  *        (If unspecified, will return a Deferred object instead of actually doing anything.)
  30.  *
  31.  * @param {Ref?} meta
  32.  *     For internal use.
  33.  *
  34.  * @returns {Ref?} Deferred object if no `done` callback was provided
  35.  *
  36.  * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  37.  */
  38.  
  39. module.exports = function createEach( /* newRecords?, done?, meta? */ ) {
  40.  
  41.   // Set up a few, common local vars for convenience / familiarity.
  42.   var WLModel = this;
  43.   var orm = this.waterline;
  44.   var modelIdentity = this.identity;
  45.  
  46.  
  47.   // Build an omen.
  48.   //
  49.   // This Error instance is defined up here in order to grab a stack trace.
  50.   // (used for providing a better experience when viewing the stack trace of errors that come from
  51.   // one or more asynchronous ticks down the line; e.g. uniqueness errors)
  52.   //
  53.   // > Note that this can only be used once.
  54.   var omen = new Error('omen');
  55.   Error.captureStackTrace(omen, createEach);
  56.   // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  57.   // FUTURE: do something fancier here to keep track of this so that it suppots both sorts of usages
  58.   // and does an even better job of reporting exactly where the error came from in userland code as
  59.   // the very first entry in the stack trace.  e.g.
  60.   // ```
  61.   // Error.captureStackTrace(omen, Deferred.prototype.exec);
  62.   // // ^^ but would need to pass through the original omen or something
  63.   // ```
  64.   // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  65.  
  66.  
  67.   // Build query w/ initial, universal keys.
  68.   var query = {
  69.     method: 'createEach',
  70.     using: modelIdentity
  71.   };
  72.  
  73.  
  74.   //  鈻堚枅鈺   鈻堚枅鈺 鈻堚枅鈻堚枅鈻堚晽 鈻堚枅鈻堚枅鈻堚枅鈺 鈻堚枅鈺 鈻堚枅鈻堚枅鈻堚晽 鈻堚枅鈻堚枅鈻堚枅鈺 鈻堚枅鈺 鈻堚枅鈻堚枅鈻堚枅鈺椻枅鈻堚枅鈻堚枅鈻堚枅鈺
  75.   //  鈻堚枅鈺   鈻堚枅鈺戔枅鈻堚晹鈺愨晲鈻堚枅鈺椻枅鈻堚晹鈺愨晲鈻堚枅鈺椻枅鈻堚晳鈻堚枅鈺斺晲鈺愨枅鈻堚晽鈻堚枅鈺斺晲鈺愨枅鈻堚晽鈻堚枅鈺戔枅鈻堚晹鈺愨晲鈺愨晲鈺濃枅鈻堚晹鈺愨晲鈺愨晲鈺
  76.   //  鈻堚枅鈺   鈻堚枅鈺戔枅鈻堚枅鈻堚枅鈻堚枅鈺戔枅鈻堚枅鈻堚枅鈻堚晹鈺濃枅鈻堚晳鈻堚枅鈻堚枅鈻堚枅鈻堚晳鈻堚枅鈺  鈻堚枅鈺戔枅鈻堚晳鈻堚枅鈺     鈻堚枅鈻堚枅鈻堚枅鈻堚晽
  77.   //  鈺氣枅鈻堚晽 鈻堚枅鈺斺暆鈻堚枅鈺斺晲鈺愨枅鈻堚晳鈻堚枅鈺斺晲鈺愨枅鈻堚晽鈻堚枅鈺戔枅鈻堚晹鈺愨晲鈻堚枅鈺戔枅鈻堚晳  鈻堚枅鈺戔枅鈻堚晳鈻堚枅鈺     鈺氣晲鈺愨晲鈺愨枅鈻堚晳
  78.   //   鈺氣枅鈻堚枅鈻堚晹鈺 鈻堚枅鈺  鈻堚枅鈺戔枅鈻堚晳  鈻堚枅鈺戔枅鈻堚晳鈻堚枅鈺  鈻堚枅鈺戔枅鈻堚枅鈻堚枅鈻堚晹鈺濃枅鈻堚晳鈺氣枅鈻堚枅鈻堚枅鈻堚晽鈻堚枅鈻堚枅鈻堚枅鈻堚晳
  79.   //    鈺氣晲鈺愨晲鈺  鈺氣晲鈺  鈺氣晲鈺濃暁鈺愨暆  鈺氣晲鈺濃暁鈺愨暆鈺氣晲鈺  鈺氣晲鈺濃暁鈺愨晲鈺愨晲鈺愨暆 鈺氣晲鈺 鈺氣晲鈺愨晲鈺愨晲鈺濃暁鈺愨晲鈺愨晲鈺愨晲鈺
  80.   //
  81.  
  82.   // The `done` callback, if one was provided.
  83.   var done;
  84.  
  85.   // Handle the various supported usage possibilities
  86.   // (locate the `done` callback)
  87.   //
  88.   // > Note that we define `args` so that we can insulate access
  89.   // > to the arguments provided to this function.
  90.   var args = arguments;
  91.   (function _handleVariadicUsage(){
  92.  
  93.     // The metadata container, if one was provided.
  94.     var _meta;
  95.  
  96.  
  97.     // First argument always means one thing: the array of new records.
  98.     //
  99.     // 鈥 createEach(newRecords, ...)
  100.  
  101.     // 鈥 createEach(..., done, _meta)
  102.     query.newRecords = args[0];
  103.     done = args[1];
  104.     _meta = args[2];
  105.  
  106.     // Fold in `_meta`, if relevant.
  107.     if (!_.isUndefined(_meta)) {
  108.       query.meta = _meta;
  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.   // If a callback function was not specified, then build a new `Deferred` and bail now.
  135.   //
  136.   // > This method will be called AGAIN automatically when the Deferred is executed.
  137.   // > and next time, it'll have a callback.
  138.   if (!done) {
  139.     return new Deferred(WLModel, createEach, query);
  140.   } // --鈥
  141.  
  142.  
  143.   // Otherwise, IWMIH, we know that a callback was specified.
  144.   // So...
  145.   //
  146.   //  鈻堚枅鈻堚枅鈻堚枅鈻堚晽鈻堚枅鈺  鈻堚枅鈺椻枅鈻堚枅鈻堚枅鈻堚枅鈺 鈻堚枅鈻堚枅鈻堚枅鈺椻枅鈻堚晽   鈻堚枅鈺椻枅鈻堚枅鈻堚枅鈻堚枅鈻堚晽鈻堚枅鈻堚枅鈻堚枅鈻堚晽
  147.   //  鈻堚枅鈺斺晲鈺愨晲鈺愨暆鈺氣枅鈻堚晽鈻堚枅鈺斺暆鈻堚枅鈺斺晲鈺愨晲鈺愨暆鈻堚枅鈺斺晲鈺愨晲鈺愨暆鈻堚枅鈺   鈻堚枅鈺戔暁鈺愨晲鈻堚枅鈺斺晲鈺愨暆鈻堚枅鈺斺晲鈺愨晲鈺愨暆
  148.   //  鈻堚枅鈻堚枅鈻堚晽   鈺氣枅鈻堚枅鈺斺暆 鈻堚枅鈻堚枅鈻堚晽  鈻堚枅鈺     鈻堚枅鈺   鈻堚枅鈺   鈻堚枅鈺   鈻堚枅鈻堚枅鈻堚晽
  149.   //  鈻堚枅鈺斺晲鈺愨暆   鈻堚枅鈺斺枅鈻堚晽 鈻堚枅鈺斺晲鈺愨暆  鈻堚枅鈺     鈻堚枅鈺   鈻堚枅鈺   鈻堚枅鈺   鈻堚枅鈺斺晲鈺愨暆
  150.   //  鈻堚枅鈻堚枅鈻堚枅鈻堚晽鈻堚枅鈺斺暆 鈻堚枅鈺椻枅鈻堚枅鈻堚枅鈻堚枅鈺椻暁鈻堚枅鈻堚枅鈻堚枅鈺椻暁鈻堚枅鈻堚枅鈻堚枅鈺斺暆   鈻堚枅鈺   鈻堚枅鈻堚枅鈻堚枅鈻堚晽
  151.   //  鈺氣晲鈺愨晲鈺愨晲鈺愨暆鈺氣晲鈺  鈺氣晲鈺濃暁鈺愨晲鈺愨晲鈺愨晲鈺 鈺氣晲鈺愨晲鈺愨晲鈺 鈺氣晲鈺愨晲鈺愨晲鈺    鈺氣晲鈺   鈺氣晲鈺愨晲鈺愨晲鈺愨暆
  152.  
  153.   //  鈺斺晲鈺椻晹鈺愨晽鈺︹晲鈺椻晹鈺愨晽鈺斺晲鈺  鈹屸攢鈹愨攲鈹攼鈹屸攢鈹愨攲鈹€鈹愨攲鈹€鈹  鈹屸敩鈹愨敩 鈹攲鈹€鈹  鈹屸攢鈹 鈹 鈹攲鈹€鈹愨敩鈹€鈹愨敩 鈹
  154.   //  鈺犫暎 鈺 鈺戔暊鈺︹暆鈺 鈺︹晳鈺   鈹斺攢鈹 鈹 鈹溾攢鈹も攤 鈹敎鈹    鈹 鈹傗攤鈹傗攤 鈹  鈹傗攢鈹尖攼鈹 鈹傗敎鈹 鈹溾敩鈹樷敂鈹敇
  155.   //  鈺  鈺氣晲鈺濃暕鈺氣晲鈺氣晲鈺濃暁鈺愨暆  鈹斺攢鈹 鈹 鈹 鈹粹敂鈹€鈹樷敂鈹€鈹   鈹 鈹斺敶鈹樷敂鈹€鈹  鈹斺攢鈹樷敂鈹斺攢鈹樷敂鈹€鈹樷敶鈹斺攢 鈹
  156.   //
  157.   // Forge a stage 2 query (aka logical protostatement)
  158.   try {
  159.     forgeStageTwoQuery(query, orm);
  160.   } catch (e) {
  161.     switch (e.code) {
  162.  
  163.       case 'E_INVALID_NEW_RECORDS':
  164.       case 'E_INVALID_META':
  165.         return done(e);
  166.         // ^ when the standard usage error is good enough as-is, without any further customization
  167.  
  168.       default:
  169.         return done(e);
  170.         // ^ when an internal, miscellaneous, or unexpected error occurs
  171.     }
  172.   } // >-鈥
  173.  
  174.   // - - - - -
  175.   // FUTURE: beforeCreateEach lifecycle callback?
  176.  
  177.  
  178.   //  鈺斺晲鈺椻暒 鈺︹晹鈺愨晽鈺斺晲鈺椻暒鈺斺晲  鈹屸攢鈹愨攲鈹€鈹愨敩鈹€鈹  鈹屸攢鈹愨攲鈹愨攲鈹 鈹
  179.   //  鈺  鈺犫晲鈺b晳鈺 鈺  鈺犫暕鈺  鈹溾敜 鈹 鈹傗敎鈹敇  鈹溾攢鈹も攤鈹傗攤鈹斺敩鈹
  180.   //  鈺氣晲鈺濃暕 鈺┾暁鈺愨暆鈺氣晲鈺濃暕 鈺  鈹  鈹斺攢鈹樷敶鈹斺攢  鈹 鈹粹敇鈹斺敇 鈹
  181.   //  鈹屸攢鈹愨攲鈹€鈹愨敩  鈹  鈹屸攢鈹愨攲鈹€鈹愨攲鈹攼鈹攲鈹€鈹愨攲鈹愨攲  鈹攢鈹愨攲鈹€鈹愨攲鈹€鈹愨攲鈹€鈹愨攲鈹攼鈹屸攢鈹
  182.   //  鈹  鈹 鈹傗攤  鈹  鈹溾敜 鈹   鈹 鈹傗攤 鈹傗攤鈹傗攤  鈹溾敩鈹樷敎鈹 鈹斺攢鈹愨敎鈹  鈹 鈹斺攢鈹
  183.   //  鈹斺攢鈹樷敂鈹€鈹樷敶鈹€鈹樷敶鈹€鈹樷敂鈹€鈹樷敂鈹€鈹 鈹 鈹粹敂鈹€鈹樷敇鈹斺敇  鈹粹敂鈹€鈹斺攢鈹樷敂鈹€鈹樷敂鈹€鈹 鈹 鈹斺攢鈹
  184.   // Also removes them from the newRecords before sending to the adapter.
  185.   var allCollectionResets = [];
  186.  
  187.   _.each(query.newRecords, function _eachRecord(record) {
  188.     // Hold the individual resets
  189.     var reset = {};
  190.  
  191.     _.each(WLModel.attributes, function _eachKnownAttrDef(attrDef, attrName) {
  192.  
  193.       if (attrDef.collection) {
  194.         // Only create a reset if the value isn't an empty array. If the value
  195.         // is an empty array there isn't any resetting to do.
  196.         if (record[attrName].length) {
  197.           reset[attrName] = record[attrName];
  198.         }
  199.  
  200.         // Remove the collection value from the newRecord because the adapter
  201.         // doesn't need to do anything during the initial create.
  202.         delete record[attrName];
  203.       }
  204.     });//</ each known attr def >
  205.  
  206.     allCollectionResets.push(reset);
  207.   });//</ each record >
  208.  
  209.  
  210.   // If any collection resets were specified, force `fetch: true` (meta key)
  211.   // so that the adapter will send back the records and we can use them below
  212.   // in order to call `resetCollection()`.
  213.   var anyActualCollectionResets = _.any(allCollectionResets, function (reset){
  214.     return _.keys(reset).length > 0;
  215.   });
  216.   if (anyActualCollectionResets) {
  217.     query.meta = query.meta || {};
  218.     query.meta.fetch = true;
  219.   }//>-
  220.  
  221.  
  222.   //  鈺斺晲鈺椻晹鈺愨晽鈺︹晲鈺椻晹鈺愨晽鈺斺晲鈺  鈹屸攢鈹愨攲鈹攼鈹屸攢鈹愨攲鈹€鈹愨攲鈹€鈹  鈹屸敩鈹愨敩 鈹敩鈹€鈹愨攲鈹€鈹愨攲鈹€鈹  鈹屸攢鈹 鈹 鈹攲鈹€鈹愨敩鈹€鈹愨敩 鈹
  223.   //  鈺犫暎 鈺 鈺戔暊鈺︹暆鈺 鈺︹晳鈺   鈹斺攢鈹 鈹 鈹溾攢鈹も攤 鈹敎鈹    鈹 鈹溾攢鈹も敎鈹敇鈹溾敜 鈹溾敜   鈹傗攢鈹尖攼鈹 鈹傗敎鈹 鈹溾敩鈹樷敂鈹敇
  224.   //  鈺  鈺氣晲鈺濃暕鈺氣晲鈺氣晲鈺濃暁鈺愨暆  鈹斺攢鈹 鈹 鈹 鈹粹敂鈹€鈹樷敂鈹€鈹   鈹 鈹 鈹粹敶鈹斺攢鈹斺攢鈹樷敂鈹€鈹  鈹斺攢鈹樷敂鈹斺攢鈹樷敂鈹€鈹樷敶鈹斺攢 鈹
  225.   // Now, destructively forge this S2Q into a S3Q.
  226.   try {
  227.     query = forgeStageThreeQuery({
  228.       stageTwoQuery: query,
  229.       identity: modelIdentity,
  230.       transformer: WLModel._transformer,
  231.       originalModels: orm.collections
  232.     });
  233.   } catch (e) { return done(e); }
  234.  
  235.   //  鈹屸攢鈹愨攲鈹€鈹愨攲鈹愨攲鈹屸敩鈹  鈹屸敩鈹愨攲鈹€鈹  鈺斺晲鈺椻晹鈺︹晽鈺斺晲鈺椻晹鈺愨晽鈺斺暒鈺椻晹鈺愨晽鈺︹晲鈺
  236.   //  鈹斺攢鈹愨敎鈹 鈹傗攤鈹 鈹傗攤   鈹 鈹 鈹  鈺犫晲鈺 鈺戔晳鈺犫晲鈺b暊鈺愨暆 鈺 鈺戔暎 鈺犫暒鈺
  237.   //  鈹斺攢鈹樷敂鈹€鈹樷敇鈹斺敇鈹€鈹粹敇   鈹 鈹斺攢鈹  鈺 鈺┾晲鈺┾暆鈺 鈺┾暕   鈺 鈺氣晲鈺濃暕鈺氣晲
  238.   // Grab the appropriate adapter method and call it.
  239.   var adapter = WLModel._adapter;
  240.   if (!adapter.createEach) {
  241.     return done(new Error('Cannot complete query: The adapter used by this model (`' + modelIdentity + '`) doesn\'t support the `'+query.method+'` method.'));
  242.   }
  243.  
  244.   adapter.createEach(WLModel.datastore, query, function(err, rawAdapterResult) {
  245.     if (err) {
  246.  
  247.       if (!_.isError(err)) {
  248.         return done(new Error(
  249.           'If an error is sent back from the adapter, it should always be an Error instance.  '+
  250.           'But instead, got: '+util.inspect(err, {depth:5})+''
  251.         ));
  252.       }//-鈥
  253.  
  254.       // Attach the identity of this model (for convenience).
  255.       err.modelIdentity = modelIdentity;
  256.  
  257.       // If this is a standardized, uniqueness constraint violation error, then standardize
  258.       // the error, mapping the `footprint.keys` (~=column names) back to attribute names,
  259.       // attaching toJSON(), adjusting the stack trace, etc.
  260.       if (err.footprint && err.footprint.identity === 'notUnique') {
  261.         err = transformUniquenessError(err, omen, modelIdentity, orm);
  262.       }//>-
  263.  
  264.       // Send back the adapter error and bail.
  265.       // (We're done!  At this point, IWMIH, the query must have failed.)
  266.       return done(err);
  267.     }//-鈥
  268.  
  269.     //  鈺斺晲鈺椻晹鈺︹晽鈺斺晲鈺椻晹鈺愨晽  鈺斺晽鈺斺晹鈺愨晽鈺 鈺     鈹 鈹攲鈹愨攲鈹  鈹屸攢鈹愨攲鈹€鈹愨攲鈹€鈹  鈺斺晲鈺椻晹鈺愨晽鈺斺暒鈺椻晹鈺愨晽鈺 鈺  鈹屸敩鈹愨攲鈹€鈹愨攲鈹攼鈹屸攢鈹  鈹攲鈹€鈹屸攢鈹愨敩 鈹
  270.     //  鈺氣晲鈺 鈺 鈺 鈺戔暊鈺愨暆  鈺戔晳鈺戔晳 鈺戔晳鈺戔晳     鈹 鈹傗攤鈹傗攤鈹  鈹溾敜 鈹斺攢鈹愨敂鈹€鈹  鈺犫暎 鈺戔暎  鈺 鈺  鈺犫晲鈺  鈹傗攤鈹傗敎鈹  鈹 鈹溾攢鈹  鈹溾敶鈹愨敎鈹 鈹斺敩鈹
  271.     //  鈺氣晲鈺 鈺 鈺氣晲鈺濃暕    鈺濃暁鈺濃暁鈺愨暆鈺氣暕鈺漮oo  鈹斺攢鈹樷敇鈹斺敇鈹粹攢鈹樷敂鈹€鈹樷敂鈹€鈹樷敂鈹€鈹  鈺  鈺氣晲鈺 鈺 鈺氣晲鈺濃暕 鈺  鈹 鈹粹敂鈹€鈹 鈹 鈹 鈹  鈹 鈹粹敂鈹€鈹 鈹
  272.     //  鈹 鈹攲鈹€鈹愨攲鈹€鈹  鈹屸攢鈹愨攲鈹€鈹愨攲鈹攼  鈹屸敩鈹愨攲鈹€鈹  鈹屸敩鈹愨敩鈹€鈹愨敩 鈹攲鈹€鈹
  273.     //  鈹傗攤鈹傗敎鈹€鈹も敂鈹€鈹  鈹斺攢鈹愨敎鈹  鈹    鈹 鈹 鈹   鈹 鈹溾敩鈹樷攤 鈹傗敎鈹
  274.     //  鈹斺敶鈹樷敶 鈹粹敂鈹€鈹  鈹斺攢鈹樷敂鈹€鈹 鈹    鈹 鈹斺攢鈹   鈹 鈹粹敂鈹€鈹斺攢鈹樷敂鈹€鈹
  275.     // If `fetch` was not enabled, return.
  276.     if (!_.has(query.meta, 'fetch') || query.meta.fetch === false) {
  277.  
  278.       if (!_.isUndefined(rawAdapterResult)) {
  279.         console.warn('\n'+
  280.           'Warning: Unexpected behavior in database adapter:\n'+
  281.           'Since `fetch` is NOT enabled, this adapter (for datastore `'+WLModel.datastore+'`)\n'+
  282.           'should NOT have sent back anything as the 2nd argument when triggering the callback\n'+
  283.           'from its `createEach` method.  But it did -- which is why this warning is being displayed:\n'+
  284.           'to help avoid confusion and draw attention to the bug.  Specifically, got:\n'+
  285.           util.inspect(rawAdapterResult, {depth:5})+'\n'+
  286.           '(Ignoring it and proceeding anyway...)'+'\n'
  287.         );
  288.       }//>-
  289.  
  290.       return done();
  291.  
  292.     }//-鈥
  293.  
  294.  
  295.     // IWMIH then we know that `fetch: true` meta key was set, and so the
  296.     // adapter should have sent back an array.
  297.  
  298.     //  鈺斺暒鈺椻暒鈺愨晽鈺斺晲鈺椻晹鈺椻晹鈺斺晲鈺椻晹鈺愨晽鈺斺晲鈺椻暒鈺愨晽鈺斺暒鈺  鈹屸攢鈹愨攲鈹攼鈹屸攢鈹愨攲鈹€鈹愨攲鈹攼鈹屸攢鈹愨敩鈹€鈹  鈹攢鈹愨攲鈹€鈹愨攲鈹€鈹愨敩 鈹敩 鈹屸敩鈹
  299.     //   鈺 鈺犫暒鈺濃暊鈺愨暎鈺戔晳鈺戔暁鈺愨晽鈺犫暎 鈺 鈺戔暊鈺︹暆鈺戔晳鈺  鈹溾攢鈹 鈹傗攤鈹溾攢鈹も敎鈹€鈹 鈹 鈹溾敜 鈹溾敩鈹  鈹溾敩鈹樷敎鈹 鈹斺攢鈹愨攤 鈹傗攤  鈹
  300.     //   鈺 鈺┾暁鈺愨暕 鈺┾暆鈺氣暆鈺氣晲鈺濃暁  鈺氣晲鈺濃暕鈺氣晲鈺 鈺  鈹 鈹粹攢鈹粹敇鈹 鈹粹敶   鈹 鈹斺攢鈹樷敶鈹斺攢  鈹粹敂鈹€鈹斺攢鈹樷敂鈹€鈹樷敂鈹€鈹樷敶鈹€鈹樷敶
  301.     // Attempt to convert the records' column names to attribute names.
  302.     var transformationErrors = [];
  303.     var transformedRecords = [];
  304.     _.each(rawAdapterResult, function(record) {
  305.       var transformedRecord;
  306.       try {
  307.         transformedRecord = WLModel._transformer.unserialize(record);
  308.       } catch (e) {
  309.         transformationErrors.push(e);
  310.       }
  311.  
  312.       transformedRecords.push(transformedRecord);
  313.     });
  314.  
  315.     if (transformationErrors.length > 0) {
  316.       return done(new Error(
  317.         'Encountered '+transformationErrors.length+' error(s) processing the record(s) sent back '+
  318.         'from the adapter-- specifically, when converting column names back to attribute names.  '+
  319.         'Details: '+
  320.         util.inspect(transformationErrors,{depth:5})+''
  321.       ));
  322.     }//-鈥
  323.  
  324.     // Check the record to verify compliance with the adapter spec,
  325.     // as well as any issues related to stale data that might not have been
  326.     // been migrated to keep up with the logical schema (`type`, etc. in
  327.     // attribute definitions).
  328.     try {
  329.       processAllRecords(transformedRecords, query.meta, WLModel.identity, orm);
  330.     } catch (e) { return done(e); }
  331.  
  332.  
  333.     //  鈹屸攢鈹愨攲鈹€鈹愨敩  鈹    鈺︹晲鈺椻晹鈺愨晽鈺斺晲鈺椻暒  鈺斺晲鈺椻晹鈺愨晽鈺斺晲鈺  鈺斺晲鈺椻晹鈺愨晽鈺  鈺  鈺斺晲鈺椻晹鈺愨晽鈺斺暒鈺椻暒鈺斺晲鈺椻晹鈺椻晹  鈹屸攢鈹愨攲鈹€鈹愨敩鈹€鈹
  334.     //  鈹  鈹溾攢鈹も攤  鈹    鈺犫暒鈺濃晳鈺 鈺犫晲鈺濃晳  鈺犫晲鈺b晳  鈺戔暎   鈺  鈺 鈺戔晳  鈺  鈺戔暎 鈺   鈺 鈺戔晳 鈺戔晳鈺戔晳  鈹溾敜 鈹 鈹傗敎鈹敇
  335.     //  鈹斺攢鈹樷敶 鈹粹敶鈹€鈹樷敶鈹€鈹  鈺┾暁鈺愨暁鈺愨暆鈺  鈺┾晲鈺濃暕 鈺┾暁鈺愨暆鈺氣晲鈺  鈺氣晲鈺濃暁鈺愨暆鈺┾晲鈺濃暕鈺愨暆鈺氣晲鈺濃暁鈺愨暆 鈺 鈺┾暁鈺愨暆鈺濃暁鈺  鈹  鈹斺攢鈹樷敶鈹斺攢
  336.     //  鈹屸攢鈹愨攢鈹 鈹攲鈹€鈹愨敩  鈹攲鈹€鈹愨敩鈹屸敩鈹愨敩 鈹 鈹   鈹屸攢鈹愨攲鈹€鈹愨攲鈹€鈹愨攲鈹€鈹愨敩鈹屸攢鈹愨敩鈹屸攢鈹愨攲鈹攼  鈹屸攢鈹愨攲鈹€鈹愨攲鈹€鈹愨攲鈹€鈹愨攲鈹€鈹愨敩鈹屸攢鈹愨攲鈹攼鈹攲鈹€鈹愨攲鈹愨攲鈹屸攢鈹
  337.     //  鈹溾敜 鈹屸敶鈹敇鈹溾攢鈹樷攤  鈹傗攤  鈹 鈹 鈹 鈹斺敩鈹樷攢鈹€鈹€鈹斺攢鈹愨敎鈹€鈹樷敎鈹 鈹  鈹傗敎鈹 鈹傗敎鈹  鈹傗攤  鈹溾攢鈹も敂鈹€鈹愨敂鈹€鈹愨攤 鈹傗攤  鈹傗敎鈹€鈹 鈹 鈹傗攤 鈹傗攤鈹傗攤鈹斺攢鈹
  338.     //  鈹斺攢鈹樷敶 鈹斺攢鈹  鈹粹攢鈹樷敶鈹斺攢鈹樷敶 鈹 鈹粹攢鈹樷敶    鈹斺攢鈹樷敶  鈹斺攢鈹樷敂鈹€鈹樷敶鈹  鈹粹敂鈹€鈹樷攢鈹粹敇  鈹 鈹粹敂鈹€鈹樷敂鈹€鈹樷敂鈹€鈹樷敂鈹€鈹樷敶鈹 鈹 鈹 鈹粹敂鈹€鈹樷敇鈹斺敇鈹斺攢鈹
  339.     var argsForEachReplaceOp = [];
  340.     _.each(transformedRecords, function (record, idx) {
  341.  
  342.       // Grab the dictionary of collection resets corresponding to this record.
  343.       var reset = allCollectionResets[idx];
  344.  
  345.       // If there are no resets, then there's no need to build up a replaceCollection() query.
  346.       if (_.keys(reset).length === 0) {
  347.         return;
  348.       }//-鈥
  349.  
  350.       // Otherwise, build an array of arrays, where each sub-array contains
  351.       // the first three arguments that need to be passed in to `replaceCollection()`.
  352.       var targetIds = [ record[WLModel.primaryKey] ];
  353.       _.each(_.keys(reset), function (collectionAttrName) {
  354.  
  355.         // (targetId(s), collectionAttrName, associatedPrimaryKeys)
  356.         argsForEachReplaceOp.push([
  357.           targetIds,
  358.           collectionAttrName,
  359.           reset[collectionAttrName]
  360.         ]);
  361.  
  362.       });// </ each key in "reset" >
  363.     });//</ each record>
  364.  
  365.     async.each(argsForEachReplaceOp, function _eachReplaceCollectionOp(argsForReplace, next) {
  366.  
  367.       // Note that, by using the same `meta`, we use same db connection
  368.       // (if one was explicitly passed in, anyway)
  369.       WLModel.replaceCollection(argsForReplace[0], argsForReplace[1], argsForReplace[2], function(err) {
  370.         if (err) { return next(err); }
  371.         return next();
  372.       }, query.meta);
  373.  
  374.     },// ~鈭?掳
  375.     function _afterReplacingAllCollections(err) {
  376.       if (err) {
  377.         return done(err);
  378.       }
  379.  
  380.       // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  381.       // FUTURE: `afterCreateEach` lifecycle callback?
  382.       // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  383.  
  384.       return done(undefined, transformedRecords);
  385.  
  386.     });//</async.each()>
  387.   });//</adapter.createEach()>
  388. };
  389.  
downloadcreate-each.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
GoSublime - A Golang plugin collection for SublimeText **3**, ... 2017-02-19
awesome-emacs - A community driven list of useful Emacs packages, ... 2017-02-19
RKNotificationHub - Make any UIView a full fledged notification center 2017-02-19
vimr - Project VimR - Refined Neovim experience for macOS... 2017-02-19
vue-admin - Vue Admin Panel Framework, Powered by Vue 2.0 and ... 2017-02-19
dev-setup - Mac OS X development environment setup: Easy-to-un... 2017-02-19
CMPopTipView - Custom UIView for iOS that pops up an animated "bu... 2017-02-19
git-recipes - Git for recipes in Chinese. The high quality of Gi... 2017-02-19
CLNDR - 2017-02-19
OptiKey - OptiKey - Full computer control and speech with yo... 2017-02-19

 Back to top