BVB Source Codes

waterline Show create.js Source code

Return Download waterline: download create.js Source code - Download waterline Source code - Type:.js
  1. /**
  2.  * Module Dependencies
  3.  */
  4.  
  5. var util = require('util');
  6. var async = require('async');
  7. var _ = require('@sailshq/lodash');
  8. var flaverr = require('flaverr');
  9. var Deferred = require('../utils/query/deferred');
  10. var forgeStageTwoQuery = require('../utils/query/forge-stage-two-query');
  11. var forgeStageThreeQuery = require('../utils/query/forge-stage-three-query');
  12. var transformUniquenessError = require('../utils/query/transform-uniqueness-error');
  13. var processAllRecords = require('../utils/records/process-all-records');
  14.  
  15.  
  16. /**
  17.  * Create a new record
  18.  *
  19.  * @param {Object || Array} values for single model or array of multiple values
  20.  * @param {Function} callback
  21.  * @return Deferred object if no callback
  22.  */
  23.  
  24. module.exports = function create(values, done, metaContainer) {
  25.  
  26.   // Set up a few, common local vars for convenience / familiarity.
  27.   var WLModel = this;
  28.   var orm = this.waterline;
  29.   var modelIdentity = this.identity;
  30.  
  31.  
  32.   // Build an omen.
  33.   //
  34.   // This Error instance is defined up here in order to grab a stack trace.
  35.   // (used for providing a better experience when viewing the stack trace of errors that come from
  36.   // one or more asynchronous ticks down the line; e.g. uniqueness errors)
  37.   //
  38.   // > Note that this can only be used once.
  39.   var omen = new Error('omen');
  40.   Error.captureStackTrace(omen, create);
  41.   // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  42.   // FUTURE: do something fancier here to keep track of this so that it suppots both sorts of usages
  43.   // and does an even better job of reporting exactly where the error came from in userland code as
  44.   // the very first entry in the stack trace.  e.g.
  45.   // ```
  46.   // Error.captureStackTrace(omen, Deferred.prototype.exec);
  47.   // // ^^ but would need to pass through the original omen or something
  48.   // ```
  49.   // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  50.  
  51.   var query = {
  52.     method: 'create',
  53.     using: modelIdentity,
  54.     newRecord: values,
  55.     meta: metaContainer
  56.   };
  57.  
  58.   //  鈻堚枅鈺   鈻堚枅鈺 鈻堚枅鈻堚枅鈻堚晽 鈻堚枅鈻堚枅鈻堚枅鈺 鈻堚枅鈺 鈻堚枅鈻堚枅鈻堚晽 鈻堚枅鈻堚枅鈻堚枅鈺 鈻堚枅鈺 鈻堚枅鈻堚枅鈻堚枅鈺椻枅鈻堚枅鈻堚枅鈻堚枅鈺
  59.   //  鈻堚枅鈺   鈻堚枅鈺戔枅鈻堚晹鈺愨晲鈻堚枅鈺椻枅鈻堚晹鈺愨晲鈻堚枅鈺椻枅鈻堚晳鈻堚枅鈺斺晲鈺愨枅鈻堚晽鈻堚枅鈺斺晲鈺愨枅鈻堚晽鈻堚枅鈺戔枅鈻堚晹鈺愨晲鈺愨晲鈺濃枅鈻堚晹鈺愨晲鈺愨晲鈺
  60.   //  鈻堚枅鈺   鈻堚枅鈺戔枅鈻堚枅鈻堚枅鈻堚枅鈺戔枅鈻堚枅鈻堚枅鈻堚晹鈺濃枅鈻堚晳鈻堚枅鈻堚枅鈻堚枅鈻堚晳鈻堚枅鈺  鈻堚枅鈺戔枅鈻堚晳鈻堚枅鈺     鈻堚枅鈻堚枅鈻堚枅鈻堚晽
  61.   //  鈺氣枅鈻堚晽 鈻堚枅鈺斺暆鈻堚枅鈺斺晲鈺愨枅鈻堚晳鈻堚枅鈺斺晲鈺愨枅鈻堚晽鈻堚枅鈺戔枅鈻堚晹鈺愨晲鈻堚枅鈺戔枅鈻堚晳  鈻堚枅鈺戔枅鈻堚晳鈻堚枅鈺     鈺氣晲鈺愨晲鈺愨枅鈻堚晳
  62.   //   鈺氣枅鈻堚枅鈻堚晹鈺 鈻堚枅鈺  鈻堚枅鈺戔枅鈻堚晳  鈻堚枅鈺戔枅鈻堚晳鈻堚枅鈺  鈻堚枅鈺戔枅鈻堚枅鈻堚枅鈻堚晹鈺濃枅鈻堚晳鈺氣枅鈻堚枅鈻堚枅鈻堚晽鈻堚枅鈻堚枅鈻堚枅鈻堚晳
  63.   //    鈺氣晲鈺愨晲鈺  鈺氣晲鈺  鈺氣晲鈺濃暁鈺愨暆  鈺氣晲鈺濃暁鈺愨暆鈺氣晲鈺  鈺氣晲鈺濃暁鈺愨晲鈺愨晲鈺愨暆 鈺氣晲鈺 鈺氣晲鈺愨晲鈺愨晲鈺濃暁鈺愨晲鈺愨晲鈺愨晲鈺
  64.   //
  65.   // FUTURE: when time allows, update this to match the "VARIADICS" format
  66.   // used in the other model methods.
  67.  
  68.  
  69.   //  鈻堚枅鈻堚枅鈻堚枅鈺 鈻堚枅鈻堚枅鈻堚枅鈻堚晽鈻堚枅鈻堚枅鈻堚枅鈻堚晽鈻堚枅鈻堚枅鈻堚枅鈻堚晽鈻堚枅鈻堚枅鈻堚枅鈺
  70.   //  鈻堚枅鈺斺晲鈺愨枅鈻堚晽鈻堚枅鈺斺晲鈺愨晲鈺愨暆鈻堚枅鈺斺晲鈺愨晲鈺愨暆鈻堚枅鈺斺晲鈺愨晲鈺愨暆鈻堚枅鈺斺晲鈺愨枅鈻堚晽
  71.   //  鈻堚枅鈺  鈻堚枅鈺戔枅鈻堚枅鈻堚枅鈺  鈻堚枅鈻堚枅鈻堚晽  鈻堚枅鈻堚枅鈻堚晽  鈻堚枅鈻堚枅鈻堚枅鈺斺暆
  72.   //  鈻堚枅鈺  鈻堚枅鈺戔枅鈻堚晹鈺愨晲鈺  鈻堚枅鈺斺晲鈺愨暆  鈻堚枅鈺斺晲鈺愨暆  鈻堚枅鈺斺晲鈺愨枅鈻堚晽
  73.   //  鈻堚枅鈻堚枅鈻堚枅鈺斺暆鈻堚枅鈻堚枅鈻堚枅鈻堚晽鈻堚枅鈺     鈻堚枅鈻堚枅鈻堚枅鈻堚晽鈻堚枅鈺  鈻堚枅鈺
  74.   //  鈺氣晲鈺愨晲鈺愨晲鈺 鈺氣晲鈺愨晲鈺愨晲鈺愨暆鈺氣晲鈺     鈺氣晲鈺愨晲鈺愨晲鈺愨暆鈺氣晲鈺  鈺氣晲鈺
  75.   //
  76.   //   鈻堚枅鈺椻枅鈻堚枅鈺   鈻堚枅鈻堚晽 鈻堚枅鈻堚枅鈻堚晽 鈻堚枅鈺   鈻堚枅鈺椻枅鈻堚枅鈻堚枅鈻堚晽 鈻堚枅鈻堚枅鈻堚枅鈻堚晽鈻堚枅鈺
  77.   //  鈻堚枅鈺斺暆鈻堚枅鈻堚枅鈺 鈻堚枅鈻堚枅鈺戔枅鈻堚晹鈺愨晲鈻堚枅鈺椻暁鈻堚枅鈺 鈻堚枅鈺斺暆鈻堚枅鈺斺晲鈺愨枅鈻堚晽鈻堚枅鈺斺晲鈺愨晲鈺愨暆鈺氣枅鈻堚晽
  78.   //  鈻堚枅鈺 鈻堚枅鈺斺枅鈻堚枅鈻堚晹鈻堚枅鈺戔枅鈻堚枅鈻堚枅鈻堚枅鈺 鈺氣枅鈻堚枅鈻堚晹鈺 鈻堚枅鈻堚枅鈻堚枅鈺斺暆鈻堚枅鈻堚枅鈻堚晽   鈻堚枅鈺
  79.   //  鈻堚枅鈺 鈻堚枅鈺戔暁鈻堚枅鈺斺暆鈻堚枅鈺戔枅鈻堚晹鈺愨晲鈻堚枅鈺  鈺氣枅鈻堚晹鈺  鈻堚枅鈺斺晲鈺愨枅鈻堚晽鈻堚枅鈺斺晲鈺愨暆   鈻堚枅鈺
  80.   //  鈺氣枅鈻堚晽鈻堚枅鈺 鈺氣晲鈺 鈻堚枅鈺戔枅鈻堚晳  鈻堚枅鈺   鈻堚枅鈺   鈻堚枅鈻堚枅鈻堚枅鈺斺暆鈻堚枅鈻堚枅鈻堚枅鈻堚晽鈻堚枅鈺斺暆
  81.   //   鈺氣晲鈺濃暁鈺愨暆     鈺氣晲鈺濃暁鈺愨暆  鈺氣晲鈺   鈺氣晲鈺   鈺氣晲鈺愨晲鈺愨晲鈺 鈺氣晲鈺愨晲鈺愨晲鈺愨暆鈺氣晲鈺
  82.   //
  83.   //  鈹屸攼 鈹 鈹敩鈹  鈹屸敩鈹   鈹   鈹攢鈹愨攲鈹€鈹愨攲鈹攼鈹 鈹敩鈹€鈹愨攲鈹愨攲  鈹屸攼鈹屸攲鈹€鈹愨敩 鈹  鈹屸敩鈹愨攲鈹€鈹愨攲鈹€鈹愨攲鈹€鈹愨敩鈹€鈹愨敩鈹€鈹愨攲鈹€鈹愨攲鈹攼
  84.   //  鈹溾敶鈹愨攤 鈹傗攤鈹   鈹傗攤  鈹屸敿鈹€  鈹溾敩鈹樷敎鈹  鈹 鈹 鈹傗敎鈹敇鈹傗攤鈹  鈹傗攤鈹傗敎鈹 鈹傗攤鈹   鈹傗攤鈹溾敜 鈹溾敜 鈹溾敜 鈹溾敩鈹樷敎鈹敇鈹溾敜  鈹傗攤
  85.   //  鈹斺攢鈹樷敂鈹€鈹樷敶鈹粹攢鈹樷攢鈹粹敇  鈹斺敇   鈹粹敂鈹€鈹斺攢鈹 鈹 鈹斺攢鈹樷敶鈹斺攢鈹樷敂鈹  鈹樷敂鈹樷敂鈹€鈹樷敂鈹粹敇  鈹€鈹粹敇鈹斺攢鈹樷敂  鈹斺攢鈹樷敶鈹斺攢鈹粹敂鈹€鈹斺攢鈹樷攢鈹粹敇
  86.   //  鈹屸攢    鈹攲鈹€鈹  鈹攢鈹愨攲鈹€鈹愨敩  鈹屸攢鈹愨敩  鈹攲鈹€鈹愨攲鈹愨攲鈹屸敩鈹    鈹€鈹
  87.   //  鈹傗攢鈹€鈹€  鈹傗敎鈹   鈹溾敩鈹樷敎鈹 鈹  鈹溾敜 鈹斺攼鈹屸敇鈹溾攢鈹も攤鈹傗攤 鈹   鈹€鈹€鈹€鈹
  88.   //  鈹斺攢    鈹粹敂    鈹粹敂鈹€鈹斺攢鈹樷敶鈹€鈹樷敂鈹€鈹 鈹斺敇 鈹 鈹粹敇鈹斺敇 鈹     鈹€鈹
  89.   // Return Deferred or pass to adapter
  90.   if (typeof done !== 'function') {
  91.     return new Deferred(WLModel, WLModel.create, query);
  92.   }
  93.  
  94.   //  鈻堚枅鈻堚枅鈻堚枅鈻堚晽鈻堚枅鈺  鈻堚枅鈺椻枅鈻堚枅鈻堚枅鈻堚枅鈺 鈻堚枅鈻堚枅鈻堚枅鈺椻枅鈻堚晽   鈻堚枅鈺椻枅鈻堚枅鈻堚枅鈻堚枅鈻堚晽鈻堚枅鈻堚枅鈻堚枅鈻堚晽
  95.   //  鈻堚枅鈺斺晲鈺愨晲鈺愨暆鈺氣枅鈻堚晽鈻堚枅鈺斺暆鈻堚枅鈺斺晲鈺愨晲鈺愨暆鈻堚枅鈺斺晲鈺愨晲鈺愨暆鈻堚枅鈺   鈻堚枅鈺戔暁鈺愨晲鈻堚枅鈺斺晲鈺愨暆鈻堚枅鈺斺晲鈺愨晲鈺愨暆
  96.   //  鈻堚枅鈻堚枅鈻堚晽   鈺氣枅鈻堚枅鈺斺暆 鈻堚枅鈻堚枅鈻堚晽  鈻堚枅鈺     鈻堚枅鈺   鈻堚枅鈺   鈻堚枅鈺   鈻堚枅鈻堚枅鈻堚晽
  97.   //  鈻堚枅鈺斺晲鈺愨暆   鈻堚枅鈺斺枅鈻堚晽 鈻堚枅鈺斺晲鈺愨暆  鈻堚枅鈺     鈻堚枅鈺   鈻堚枅鈺   鈻堚枅鈺   鈻堚枅鈺斺晲鈺愨暆
  98.   //  鈻堚枅鈻堚枅鈻堚枅鈻堚晽鈻堚枅鈺斺暆 鈻堚枅鈺椻枅鈻堚枅鈻堚枅鈻堚枅鈺椻暁鈻堚枅鈻堚枅鈻堚枅鈺椻暁鈻堚枅鈻堚枅鈻堚枅鈺斺暆   鈻堚枅鈺   鈻堚枅鈻堚枅鈻堚枅鈻堚晽
  99.   //  鈺氣晲鈺愨晲鈺愨晲鈺愨暆鈺氣晲鈺  鈺氣晲鈺濃暁鈺愨晲鈺愨晲鈺愨晲鈺 鈺氣晲鈺愨晲鈺愨晲鈺 鈺氣晲鈺愨晲鈺愨晲鈺    鈺氣晲鈺   鈺氣晲鈺愨晲鈺愨晲鈺愨暆
  100.   //
  101.   //  鈺斺晲鈺椻晹鈺愨晽鈺︹晲鈺椻晹鈺愨晽鈺斺晲鈺  鈹屸攢鈹愨攲鈹攼鈹屸攢鈹愨攲鈹€鈹愨攲鈹€鈹  鈹屸敩鈹愨敩 鈹攲鈹€鈹  鈹屸攢鈹 鈹 鈹攲鈹€鈹愨敩鈹€鈹愨敩 鈹
  102.   //  鈺犫暎 鈺 鈺戔暊鈺︹暆鈺 鈺︹晳鈺   鈹斺攢鈹 鈹 鈹溾攢鈹も攤 鈹敎鈹    鈹 鈹傗攤鈹傗攤 鈹  鈹傗攢鈹尖攼鈹 鈹傗敎鈹 鈹溾敩鈹樷敂鈹敇
  103.   //  鈺  鈺氣晲鈺濃暕鈺氣晲鈺氣晲鈺濃暁鈺愨暆  鈹斺攢鈹 鈹 鈹 鈹粹敂鈹€鈹樷敂鈹€鈹   鈹 鈹斺敶鈹樷敂鈹€鈹  鈹斺攢鈹樷敂鈹斺攢鈹樷敂鈹€鈹樷敶鈹斺攢 鈹
  104.   //
  105.   // Forge a stage 2 query (aka logical protostatement)
  106.   // This ensures a normalized format.
  107.   try {
  108.     forgeStageTwoQuery(query, orm);
  109.   } catch (e) {
  110.     switch (e.code) {
  111.       case 'E_INVALID_NEW_RECORD':
  112.         return done(
  113.           flaverr(
  114.             { name: 'UsageError' },
  115.             new Error(
  116.               'Invalid new record(s).\n'+
  117.               'Details:\n'+
  118.               '  '+e.details+'\n'
  119.             )
  120.           )
  121.         );
  122.  
  123.       default:
  124.         return done(e);
  125.     }
  126.   }
  127.  
  128.  
  129.   //  鈺斺晽 鈺斺晲鈺椻晹鈺愨晽鈺斺晲鈺椻暒鈺愨晽鈺斺晲鈺  鈹屸攢鈹愨敩鈹€鈹愨攲鈹€鈹愨攲鈹€鈹愨攲鈹攼鈹屸攢鈹  鈹  鈹攲鈹€鈹愨攲鈹€鈹愨攲鈹€鈹愨敩 鈹攲鈹€鈹愨敩  鈹屸攢鈹  鈹屸攢鈹愨攲鈹€鈹愨敩  鈹  鈹屸攼 鈹屸攢鈹愨攲鈹€鈹愨敩鈹屸攢
  130.   //  鈺犫暕鈺椻晳鈺 鈺犫暎 鈺 鈺戔暊鈺︹暆鈺戔暎   鈹  鈹溾敩鈹樷敎鈹 鈹溾攢鈹 鈹 鈹溾敜   鈹  鈹傗敎鈹 鈹溾敜 鈹  鈹斺敩鈹樷攤  鈹  鈹溾敜   鈹  鈹溾攢鈹も攤  鈹  鈹溾敶鈹愨敎鈹€鈹も攤  鈹溾敶鈹
  131.   //  鈺氣晲鈺濃暁鈺愨暆鈺  鈺氣晲鈺濃暕鈺氣晲鈺氣晲鈺  鈹斺攢鈹樷敶鈹斺攢鈹斺攢鈹樷敶 鈹 鈹 鈹斺攢鈹  鈹粹攢鈹樷敶鈹  鈹斺攢鈹樷敂鈹€鈹 鈹 鈹斺攢鈹樷敶鈹€鈹樷敂鈹€鈹  鈹斺攢鈹樷敶 鈹粹敶鈹€鈹樷敶鈹€鈹樷敂鈹€鈹樷敶 鈹粹敂鈹€鈹樷敶 鈹
  132.   // Determine what to do about running "before" lifecycle callbacks
  133.   (function _maybeRunBeforeLC(proceed){
  134.  
  135.     // If the `skipAllLifecycleCallbacks` meta key was enabled, then don't run this LC.
  136.     if (_.has(query.meta, 'skipAllLifecycleCallbacks') && query.meta.skipAllLifecycleCallbacks) {
  137.       return proceed(undefined, query);
  138.     }//-鈥
  139.  
  140.     // If there is no relevant "before" lifecycle callback, then just proceed.
  141.     if (!_.has(WLModel._callbacks, 'beforeCreate')) {
  142.       return proceed(undefined, query);
  143.     }//-鈥
  144.  
  145.     // IWMIH, run the "before" lifecycle callback.
  146.     WLModel._callbacks.beforeCreate(query.newRecord, function(err){
  147.       if (err) { return proceed(err); }
  148.       return proceed(undefined, query);
  149.     });
  150.  
  151.   })(function _afterPotentiallyRunningBeforeLC(err, query) {
  152.     if (err) {
  153.       return done(err);
  154.     }
  155.  
  156.     //  鈺斺晲鈺椻暒 鈺︹晹鈺愨晽鈺斺晲鈺椻暒鈺斺晲  鈹屸攢鈹愨攲鈹€鈹愨敩鈹€鈹  鈹屸攢鈹愨攲鈹愨攲鈹 鈹
  157.     //  鈺  鈺犫晲鈺b晳鈺 鈺  鈺犫暕鈺  鈹溾敜 鈹 鈹傗敎鈹敇  鈹溾攢鈹も攤鈹傗攤鈹斺敩鈹
  158.     //  鈺氣晲鈺濃暕 鈺┾暁鈺愨暆鈺氣晲鈺濃暕 鈺  鈹  鈹斺攢鈹樷敶鈹斺攢  鈹 鈹粹敇鈹斺敇 鈹
  159.     //  鈹屸攢鈹愨攲鈹€鈹愨敩  鈹  鈹屸攢鈹愨攲鈹€鈹愨攲鈹攼鈹攲鈹€鈹愨攲鈹愨攲  鈹攢鈹愨攲鈹€鈹愨攲鈹€鈹愨攲鈹€鈹愨攲鈹攼鈹屸攢鈹
  160.     //  鈹  鈹 鈹傗攤  鈹  鈹溾敜 鈹   鈹 鈹傗攤 鈹傗攤鈹傗攤  鈹溾敩鈹樷敎鈹 鈹斺攢鈹愨敎鈹  鈹 鈹斺攢鈹
  161.     //  鈹斺攢鈹樷敂鈹€鈹樷敶鈹€鈹樷敶鈹€鈹樷敂鈹€鈹樷敂鈹€鈹 鈹 鈹粹敂鈹€鈹樷敇鈹斺敇  鈹粹敂鈹€鈹斺攢鈹樷敂鈹€鈹樷敂鈹€鈹 鈹 鈹斺攢鈹
  162.     // Also removes them from the newRecord before sending to the adapter.
  163.     var collectionResets = {};
  164.     _.each(WLModel.attributes, function _eachKnownAttrDef(attrDef, attrName) {
  165.       if (attrDef.collection) {
  166.         // Only create a reset if the value isn't an empty array. If the value
  167.         // is an empty array there isn't any resetting to do.
  168.         if (query.newRecord[attrName].length) {
  169.           collectionResets[attrName] = query.newRecord[attrName];
  170.         }
  171.  
  172.         // Remove the collection value from the newRecord because the adapter
  173.         // doesn't need to do anything during the initial create.
  174.         delete query.newRecord[attrName];
  175.       }
  176.     });//</ each known attribute def >
  177.  
  178.     // If any collection resets were specified, force `fetch: true` (meta key)
  179.     // so that we can use it below.
  180.     if (_.keys(collectionResets).length > 0) {
  181.       query.meta = query.meta || {};
  182.       query.meta.fetch = true;
  183.     }//>-
  184.  
  185.  
  186.     //  鈺斺晲鈺椻晹鈺愨晽鈺︹晲鈺椻晹鈺愨晽鈺斺晲鈺  鈹屸攢鈹愨攲鈹攼鈹屸攢鈹愨攲鈹€鈹愨攲鈹€鈹  鈹屸敩鈹愨敩 鈹敩鈹€鈹愨攲鈹€鈹愨攲鈹€鈹  鈹屸攢鈹 鈹 鈹攲鈹€鈹愨敩鈹€鈹愨敩 鈹
  187.     //  鈺犫暎 鈺 鈺戔暊鈺︹暆鈺 鈺︹晳鈺   鈹斺攢鈹 鈹 鈹溾攢鈹も攤 鈹敎鈹    鈹 鈹溾攢鈹も敎鈹敇鈹溾敜 鈹溾敜   鈹傗攢鈹尖攼鈹 鈹傗敎鈹 鈹溾敩鈹樷敂鈹敇
  188.     //  鈺  鈺氣晲鈺濃暕鈺氣晲鈺氣晲鈺濃暁鈺愨暆  鈹斺攢鈹 鈹 鈹 鈹粹敂鈹€鈹樷敂鈹€鈹   鈹 鈹 鈹粹敶鈹斺攢鈹斺攢鈹樷敂鈹€鈹  鈹斺攢鈹樷敂鈹斺攢鈹樷敂鈹€鈹樷敶鈹斺攢 鈹
  189.     // Now, destructively forge this S2Q into a S3Q.
  190.     try {
  191.       query = forgeStageThreeQuery({
  192.         stageTwoQuery: query,
  193.         identity: modelIdentity,
  194.         transformer: WLModel._transformer,
  195.         originalModels: orm.collections
  196.       });
  197.     } catch (e) { return done(e); }
  198.  
  199.  
  200.     //  鈹屸攢鈹愨攲鈹€鈹愨攲鈹愨攲鈹屸敩鈹  鈹屸敩鈹愨攲鈹€鈹  鈺斺晲鈺椻晹鈺︹晽鈺斺晲鈺椻晹鈺愨晽鈺斺暒鈺椻晹鈺愨晽鈺︹晲鈺
  201.     //  鈹斺攢鈹愨敎鈹 鈹傗攤鈹 鈹傗攤   鈹 鈹 鈹  鈺犫晲鈺 鈺戔晳鈺犫晲鈺b暊鈺愨暆 鈺 鈺戔暎 鈺犫暒鈺
  202.     //  鈹斺攢鈹樷敂鈹€鈹樷敇鈹斺敇鈹€鈹粹敇   鈹 鈹斺攢鈹  鈺 鈺┾晲鈺┾暆鈺 鈺┾暕   鈺 鈺氣晲鈺濃暕鈺氣晲
  203.     // Grab the appropriate adapter method and call it.
  204.     var adapter = WLModel._adapter;
  205.     if (!adapter.create) {
  206.       return done(new Error('Cannot complete query: The adapter used by this model (`' + modelIdentity + '`) doesn\'t support the `'+query.method+'` method.'));
  207.     }
  208.  
  209.     // And call the adapter method.
  210.     adapter.create(WLModel.datastore, query, function _afterTalkingToAdapter(err, rawAdapterResult) {
  211.       if (err) {
  212.  
  213.         if (!_.isError(err)) {
  214.           return done(new Error(
  215.             'If an error is sent back from the adapter, it should always be an Error instance.  '+
  216.             'But instead, got: '+util.inspect(err, {depth:5})+''
  217.           ));
  218.         }//-鈥
  219.  
  220.         // Attach the identity of this model (for convenience).
  221.         err.modelIdentity = modelIdentity;
  222.  
  223.         // If this is a standardized, uniqueness constraint violation error, then standardize
  224.         // the error, mapping the `footprint.keys` (~=column names) back to attribute names,
  225.         // attaching toJSON(), adjusting the stack trace, etc.
  226.         if (err.footprint && err.footprint.identity === 'notUnique') {
  227.           err = transformUniquenessError(err, omen, modelIdentity, orm);
  228.         }//>-
  229.  
  230.         return done(err);
  231.       }//-鈥
  232.  
  233.  
  234.       //  鈺斺晲鈺椻晹鈺︹晽鈺斺晲鈺椻晹鈺愨晽  鈺斺晽鈺斺晹鈺愨晽鈺 鈺     鈹 鈹攲鈹愨攲鈹  鈹屸攢鈹愨攲鈹€鈹愨攲鈹€鈹  鈺斺晲鈺椻晹鈺愨晽鈺斺暒鈺椻晹鈺愨晽鈺 鈺  鈹屸敩鈹愨攲鈹€鈹愨攲鈹攼鈹屸攢鈹  鈹攲鈹€鈹屸攢鈹愨敩 鈹
  235.       //  鈺氣晲鈺 鈺 鈺 鈺戔暊鈺愨暆  鈺戔晳鈺戔晳 鈺戔晳鈺戔晳     鈹 鈹傗攤鈹傗攤鈹  鈹溾敜 鈹斺攢鈹愨敂鈹€鈹  鈺犫暎 鈺戔暎  鈺 鈺  鈺犫晲鈺  鈹傗攤鈹傗敎鈹  鈹 鈹溾攢鈹  鈹溾敶鈹愨敎鈹 鈹斺敩鈹
  236.       //  鈺氣晲鈺 鈺 鈺氣晲鈺濃暕    鈺濃暁鈺濃暁鈺愨暆鈺氣暕鈺漮oo  鈹斺攢鈹樷敇鈹斺敇鈹粹攢鈹樷敂鈹€鈹樷敂鈹€鈹樷敂鈹€鈹  鈺  鈺氣晲鈺 鈺 鈺氣晲鈺濃暕 鈺  鈹 鈹粹敂鈹€鈹 鈹 鈹 鈹  鈹 鈹粹敂鈹€鈹 鈹
  237.       //  鈹 鈹攲鈹€鈹愨攲鈹€鈹  鈹屸攢鈹愨攲鈹€鈹愨攲鈹攼  鈹屸敩鈹愨攲鈹€鈹  鈹屸敩鈹愨敩鈹€鈹愨敩 鈹攲鈹€鈹
  238.       //  鈹傗攤鈹傗敎鈹€鈹も敂鈹€鈹  鈹斺攢鈹愨敎鈹  鈹    鈹 鈹 鈹   鈹 鈹溾敩鈹樷攤 鈹傗敎鈹
  239.       //  鈹斺敶鈹樷敶 鈹粹敂鈹€鈹  鈹斺攢鈹樷敂鈹€鈹 鈹    鈹 鈹斺攢鈹   鈹 鈹粹敂鈹€鈹斺攢鈹樷敂鈹€鈹
  240.       // If `fetch` was not enabled, return.
  241.       if (!_.has(query.meta, 'fetch') || query.meta.fetch === false) {
  242.  
  243.         if (!_.isUndefined(rawAdapterResult)) {
  244.           console.warn('\n'+
  245.             'Warning: Unexpected behavior in database adapter:\n'+
  246.             'Since `fetch` is NOT enabled, this adapter (for datastore `'+WLModel.datastore+'`)\n'+
  247.             'should NOT have sent back anything as the 2nd argument when triggering the callback\n'+
  248.             'from its `create` method.  But it did -- which is why this warning is being displayed:\n'+
  249.             'to help avoid confusion and draw attention to the bug.  Specifically, got:\n'+
  250.             util.inspect(rawAdapterResult, {depth:5})+'\n'+
  251.             '(Ignoring it and proceeding anyway...)'+'\n'
  252.           );
  253.         }//>-
  254.  
  255.         return done();
  256.  
  257.       }//-鈥
  258.  
  259.  
  260.       // IWMIH then we know that `fetch: true` meta key was set, and so the
  261.       // adapter should have sent back an array.
  262.  
  263.       // Sanity check:
  264.       if (!_.isObject(rawAdapterResult) || _.isArray(rawAdapterResult) || _.isFunction(rawAdapterResult)) {
  265.         return done(new Error('Consistency violation: expected `create` adapter method to send back the created record b/c `fetch: true` was enabled.  But instead, got: ' + util.inspect(rawAdapterResult, {depth:5})+''));
  266.       }
  267.  
  268.       //  鈺斺暒鈺椻暒鈺愨晽鈺斺晲鈺椻晹鈺椻晹鈺斺晲鈺椻晹鈺愨晽鈺斺晲鈺椻暒鈺愨晽鈺斺暒鈺  鈹屸攢鈹愨攲鈹攼鈹屸攢鈹愨攲鈹€鈹愨攲鈹攼鈹屸攢鈹愨敩鈹€鈹  鈹攢鈹愨攲鈹€鈹愨攲鈹€鈹愨敩 鈹敩 鈹屸敩鈹
  269.       //   鈺 鈺犫暒鈺濃暊鈺愨暎鈺戔晳鈺戔暁鈺愨晽鈺犫暎 鈺 鈺戔暊鈺︹暆鈺戔晳鈺  鈹溾攢鈹 鈹傗攤鈹溾攢鈹も敎鈹€鈹 鈹 鈹溾敜 鈹溾敩鈹  鈹溾敩鈹樷敎鈹 鈹斺攢鈹愨攤 鈹傗攤  鈹
  270.       //   鈺 鈺┾暁鈺愨暕 鈺┾暆鈺氣暆鈺氣晲鈺濃暁  鈺氣晲鈺濃暕鈺氣晲鈺 鈺  鈹 鈹粹攢鈹粹敇鈹 鈹粹敶   鈹 鈹斺攢鈹樷敶鈹斺攢  鈹粹敂鈹€鈹斺攢鈹樷敂鈹€鈹樷敂鈹€鈹樷敶鈹€鈹樷敶
  271.       // Attempt to convert the record's column names to attribute names.
  272.       var transformedRecord;
  273.       try {
  274.         transformedRecord = WLModel._transformer.unserialize(rawAdapterResult);
  275.       } catch (e) { return done(e); }
  276.  
  277.       // Check the record to verify compliance with the adapter spec,
  278.       // as well as any issues related to stale data that might not have been
  279.       // been migrated to keep up with the logical schema (`type`, etc. in
  280.       // attribute definitions).
  281.       try {
  282.         processAllRecords([ transformedRecord ], query.meta, modelIdentity, orm);
  283.       } catch (e) { return done(e); }
  284.  
  285.  
  286.       //  鈹屸攢鈹愨攲鈹€鈹愨敩  鈹    鈺︹晲鈺椻晹鈺愨晽鈺斺晲鈺椻暒  鈺斺晲鈺椻晹鈺愨晽鈺斺晲鈺  鈺斺晲鈺椻晹鈺愨晽鈺  鈺  鈺斺晲鈺椻晹鈺愨晽鈺斺暒鈺椻暒鈺斺晲鈺椻晹鈺椻晹  鈹屸攢鈹愨攲鈹€鈹愨敩鈹€鈹
  287.       //  鈹  鈹溾攢鈹も攤  鈹    鈺犫暒鈺濃晳鈺 鈺犫晲鈺濃晳  鈺犫晲鈺b晳  鈺戔暎   鈺  鈺 鈺戔晳  鈺  鈺戔暎 鈺   鈺 鈺戔晳 鈺戔晳鈺戔晳  鈹溾敜 鈹 鈹傗敎鈹敇
  288.       //  鈹斺攢鈹樷敶 鈹粹敶鈹€鈹樷敶鈹€鈹  鈺┾暁鈺愨暁鈺愨暆鈺  鈺┾晲鈺濃暕 鈺┾暁鈺愨暆鈺氣晲鈺  鈺氣晲鈺濃暁鈺愨暆鈺┾晲鈺濃暕鈺愨暆鈺氣晲鈺濃暁鈺愨暆 鈺 鈺┾暁鈺愨暆鈺濃暁鈺  鈹  鈹斺攢鈹樷敶鈹斺攢
  289.       //  鈹屸攢鈹愨攢鈹 鈹攲鈹€鈹愨敩  鈹攲鈹€鈹愨敩鈹屸敩鈹愨敩 鈹 鈹   鈹屸攢鈹愨攲鈹€鈹愨攲鈹€鈹愨攲鈹€鈹愨敩鈹屸攢鈹愨敩鈹屸攢鈹愨攲鈹攼  鈹屸攢鈹愨攲鈹€鈹愨攲鈹€鈹愨攲鈹€鈹愨攲鈹€鈹愨敩鈹屸攢鈹愨攲鈹攼鈹攲鈹€鈹愨攲鈹愨攲鈹屸攢鈹
  290.       //  鈹溾敜 鈹屸敶鈹敇鈹溾攢鈹樷攤  鈹傗攤  鈹 鈹 鈹 鈹斺敩鈹樷攢鈹€鈹€鈹斺攢鈹愨敎鈹€鈹樷敎鈹 鈹  鈹傗敎鈹 鈹傗敎鈹  鈹傗攤  鈹溾攢鈹も敂鈹€鈹愨敂鈹€鈹愨攤 鈹傗攤  鈹傗敎鈹€鈹 鈹 鈹傗攤 鈹傗攤鈹傗攤鈹斺攢鈹
  291.       //  鈹斺攢鈹樷敶 鈹斺攢鈹  鈹粹攢鈹樷敶鈹斺攢鈹樷敶 鈹 鈹粹攢鈹樷敶    鈹斺攢鈹樷敶  鈹斺攢鈹樷敂鈹€鈹樷敶鈹  鈹粹敂鈹€鈹樷攢鈹粹敇  鈹 鈹粹敂鈹€鈹樷敂鈹€鈹樷敂鈹€鈹樷敂鈹€鈹樷敶鈹 鈹 鈹 鈹粹敂鈹€鈹樷敇鈹斺敇鈹斺攢鈹
  292.       var targetId = transformedRecord[WLModel.primaryKey];
  293.       async.each(_.keys(collectionResets), function _eachReplaceCollectionOp(collectionAttrName, next) {
  294.  
  295.         WLModel.replaceCollection(targetId, collectionAttrName, collectionResets[collectionAttrName], function(err){
  296.           if (err) { return next(err); }
  297.           return next();
  298.         }, query.meta);
  299.  
  300.       },// ~鈭?掳
  301.       function _afterReplacingAllCollections(err) {
  302.         if (err) { return done(err); }
  303.  
  304.         //  鈺斺晲鈺椻晹鈺愨晽鈺斺暒鈺椻晹鈺愨晽鈺︹晲鈺  鈹屸攢鈹愨敩鈹€鈹愨攲鈹€鈹愨攲鈹€鈹愨攲鈹攼鈹屸攢鈹  鈹屸攢鈹愨攲鈹€鈹愨敩  鈹  鈹屸攼 鈹屸攢鈹愨攲鈹€鈹愨敩鈹屸攢
  305.         //  鈺犫晲鈺b暊鈺  鈺 鈺戔暎 鈺犫暒鈺  鈹  鈹溾敩鈹樷敎鈹 鈹溾攢鈹 鈹 鈹溾敜   鈹  鈹溾攢鈹も攤  鈹  鈹溾敶鈹愨敎鈹€鈹も攤  鈹溾敶鈹
  306.         //  鈺 鈺┾暁   鈺 鈺氣晲鈺濃暕鈺氣晲  鈹斺攢鈹樷敶鈹斺攢鈹斺攢鈹樷敶 鈹 鈹 鈹斺攢鈹  鈹斺攢鈹樷敶 鈹粹敶鈹€鈹樷敶鈹€鈹樷敂鈹€鈹樷敶 鈹粹敂鈹€鈹樷敶 鈹
  307.         (function _maybeRunAfterLC(proceed){
  308.  
  309.           // If the `skipAllLifecycleCallbacks` meta flag was set, don't run the LC.
  310.           if (_.has(query.meta, 'skipAllLifecycleCallbacks') && query.meta.skipAllLifecycleCallbacks) {
  311.             return proceed(undefined, transformedRecord);
  312.           }//-鈥
  313.  
  314.           // If no afterCreate callback defined, just proceed.
  315.           if (!_.has(WLModel._callbacks, 'afterCreate')) {
  316.             return proceed(undefined, transformedRecord);
  317.           }//-鈥
  318.  
  319.           // Otherwise, run it.
  320.           return WLModel._callbacks.afterCreate(transformedRecord, function(err) {
  321.             if (err) {
  322.               return proceed(err);
  323.             }
  324.  
  325.             return proceed(undefined, transformedRecord);
  326.           });
  327.  
  328.         })(function _afterPotentiallyRunningAfterLC(err, transformedRecord) {
  329.           if (err) { return done(err); }
  330.  
  331.           // Return the new record.
  332.           return done(undefined, transformedRecord);
  333.  
  334.         });//</ ran "after" lifecycle callback, maybe >
  335.  
  336.       });//</ async.each()  (calling replaceCollection() for each explicitly-specified plural association) >
  337.     });//</ adapter.create() >
  338.   });//</ ran "before" lifecycle callback, maybe >
  339. };
  340.  
downloadcreate.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