BVB Source Codes

waterline Show find-one.js Source code

Return Download waterline: download find-one.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 flaverr = require('flaverr');
  8. var Deferred = require('../utils/query/deferred');
  9. var forgeStageTwoQuery = require('../utils/query/forge-stage-two-query');
  10. var helpFind = require('../utils/query/help-find');
  11. var processAllRecords = require('../utils/records/process-all-records');
  12.  
  13.  
  14. /**
  15.  * findOne()
  16.  *
  17.  * Find the record matching the specified criteria.
  18.  *
  19.  * ```
  20.  * // Look up the bank account with exactly $34,986 in it.
  21.  * BankAccount.findOne().where({
  22.  *   balance: { '>': 34986 }
  23.  * }).exec(function(err, bankAccount) {
  24.  *   // ...
  25.  * });
  26.  * ```
  27.  *
  28.  * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  29.  *
  30.  * Usage without deferred object:
  31.  * ================================================
  32.  *
  33.  * @param {Dictionary?} criteria
  34.  *
  35.  * @param {Dictionary} populates
  36.  *
  37.  * @param {Function?} done
  38.  *        Callback function to run when query has either finished successfully or errored.
  39.  *        (If unspecified, will return a Deferred object instead of actually doing anything.)
  40.  *
  41.  * @param {Ref?} meta
  42.  *     For internal use.
  43.  *
  44.  * @returns {Ref?} Deferred object if no `done` callback was provided
  45.  *
  46.  * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  47.  *
  48.  * The underlying query keys:
  49.  * ==============================
  50.  *
  51.  * @qkey {Dictionary?} criteria
  52.  * @qkey {Dictionary?} populates
  53.  *
  54.  * @qkey {Dictionary?} meta
  55.  * @qkey {String} using
  56.  * @qkey {String} method
  57.  *
  58.  * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  59.  */
  60.  
  61. module.exports = function findOne( /* criteria?, populates?, done?, meta? */ ) {
  62.  
  63.   // Set up a few, common local vars for convenience / familiarity.
  64.   var WLModel = this;
  65.   var orm = this.waterline;
  66.   var modelIdentity = this.identity;
  67.  
  68.   // Build query w/ initial, universal keys.
  69.   var query = {
  70.     method: 'findOne',
  71.     using: modelIdentity
  72.   };
  73.  
  74.  
  75.   //  鈻堚枅鈺   鈻堚枅鈺 鈻堚枅鈻堚枅鈻堚晽 鈻堚枅鈻堚枅鈻堚枅鈺 鈻堚枅鈺 鈻堚枅鈻堚枅鈻堚晽 鈻堚枅鈻堚枅鈻堚枅鈺 鈻堚枅鈺 鈻堚枅鈻堚枅鈻堚枅鈺椻枅鈻堚枅鈻堚枅鈻堚枅鈺
  76.   //  鈻堚枅鈺   鈻堚枅鈺戔枅鈻堚晹鈺愨晲鈻堚枅鈺椻枅鈻堚晹鈺愨晲鈻堚枅鈺椻枅鈻堚晳鈻堚枅鈺斺晲鈺愨枅鈻堚晽鈻堚枅鈺斺晲鈺愨枅鈻堚晽鈻堚枅鈺戔枅鈻堚晹鈺愨晲鈺愨晲鈺濃枅鈻堚晹鈺愨晲鈺愨晲鈺
  77.   //  鈻堚枅鈺   鈻堚枅鈺戔枅鈻堚枅鈻堚枅鈻堚枅鈺戔枅鈻堚枅鈻堚枅鈻堚晹鈺濃枅鈻堚晳鈻堚枅鈻堚枅鈻堚枅鈻堚晳鈻堚枅鈺  鈻堚枅鈺戔枅鈻堚晳鈻堚枅鈺     鈻堚枅鈻堚枅鈻堚枅鈻堚晽
  78.   //  鈺氣枅鈻堚晽 鈻堚枅鈺斺暆鈻堚枅鈺斺晲鈺愨枅鈻堚晳鈻堚枅鈺斺晲鈺愨枅鈻堚晽鈻堚枅鈺戔枅鈻堚晹鈺愨晲鈻堚枅鈺戔枅鈻堚晳  鈻堚枅鈺戔枅鈻堚晳鈻堚枅鈺     鈺氣晲鈺愨晲鈺愨枅鈻堚晳
  79.   //   鈺氣枅鈻堚枅鈻堚晹鈺 鈻堚枅鈺  鈻堚枅鈺戔枅鈻堚晳  鈻堚枅鈺戔枅鈻堚晳鈻堚枅鈺  鈻堚枅鈺戔枅鈻堚枅鈻堚枅鈻堚晹鈺濃枅鈻堚晳鈺氣枅鈻堚枅鈻堚枅鈻堚晽鈻堚枅鈻堚枅鈻堚枅鈻堚晳
  80.   //    鈺氣晲鈺愨晲鈺  鈺氣晲鈺  鈺氣晲鈺濃暁鈺愨暆  鈺氣晲鈺濃暁鈺愨暆鈺氣晲鈺  鈺氣晲鈺濃暁鈺愨晲鈺愨晲鈺愨暆 鈺氣晲鈺 鈺氣晲鈺愨晲鈺愨晲鈺濃暁鈺愨晲鈺愨晲鈺愨晲鈺
  81.   //
  82.  
  83.   // The `done` callback, if one was provided.
  84.   var done;
  85.  
  86.   // Handle the various supported usage possibilities
  87.   // (locate the `done` callback, and extend the `query` dictionary)
  88.   //
  89.   // > Note that we define `args` so that we can insulate access
  90.   // > to the arguments provided to this function.
  91.   var args = arguments;
  92.   (function _handleVariadicUsage() {
  93.     // The metadata container, if one was provided.
  94.     var _meta;
  95.  
  96.  
  97.     // Handle first argument:
  98.     //
  99.     // 鈥 findOne(criteria, ...)
  100.     query.criteria = args[0];
  101.  
  102.  
  103.     // Handle double meaning of second argument:
  104.     //
  105.     // 鈥 findOne(..., populates, done, _meta)
  106.     var is2ndArgDictionary = (_.isObject(args[1]) && !_.isFunction(args[1]) && !_.isArray(args[1]));
  107.     if (is2ndArgDictionary) {
  108.       query.populates = args[1];
  109.       done = args[2];
  110.       _meta = args[3];
  111.     }
  112.     // 鈥 findOne(..., done, _meta)
  113.     else {
  114.       done = args[1];
  115.       _meta = args[2];
  116.     }
  117.  
  118.     // Fold in `_meta`, if relevant.
  119.     if (_meta) {
  120.       query.meta = _meta;
  121.     } // >-
  122.  
  123.   })();
  124.  
  125.  
  126.   //  鈻堚枅鈻堚枅鈻堚枅鈺 鈻堚枅鈻堚枅鈻堚枅鈻堚晽鈻堚枅鈻堚枅鈻堚枅鈻堚晽鈻堚枅鈻堚枅鈻堚枅鈻堚晽鈻堚枅鈻堚枅鈻堚枅鈺
  127.   //  鈻堚枅鈺斺晲鈺愨枅鈻堚晽鈻堚枅鈺斺晲鈺愨晲鈺愨暆鈻堚枅鈺斺晲鈺愨晲鈺愨暆鈻堚枅鈺斺晲鈺愨晲鈺愨暆鈻堚枅鈺斺晲鈺愨枅鈻堚晽
  128.   //  鈻堚枅鈺  鈻堚枅鈺戔枅鈻堚枅鈻堚枅鈺  鈻堚枅鈻堚枅鈻堚晽  鈻堚枅鈻堚枅鈻堚晽  鈻堚枅鈻堚枅鈻堚枅鈺斺暆
  129.   //  鈻堚枅鈺  鈻堚枅鈺戔枅鈻堚晹鈺愨晲鈺  鈻堚枅鈺斺晲鈺愨暆  鈻堚枅鈺斺晲鈺愨暆  鈻堚枅鈺斺晲鈺愨枅鈻堚晽
  130.   //  鈻堚枅鈻堚枅鈻堚枅鈺斺暆鈻堚枅鈻堚枅鈻堚枅鈻堚晽鈻堚枅鈺     鈻堚枅鈻堚枅鈻堚枅鈻堚晽鈻堚枅鈺  鈻堚枅鈺
  131.   //  鈺氣晲鈺愨晲鈺愨晲鈺 鈺氣晲鈺愨晲鈺愨晲鈺愨暆鈺氣晲鈺     鈺氣晲鈺愨晲鈺愨晲鈺愨暆鈺氣晲鈺  鈺氣晲鈺
  132.   //
  133.   //   鈻堚枅鈺椻枅鈻堚枅鈺   鈻堚枅鈻堚晽 鈻堚枅鈻堚枅鈻堚晽 鈻堚枅鈺   鈻堚枅鈺椻枅鈻堚枅鈻堚枅鈻堚晽 鈻堚枅鈻堚枅鈻堚枅鈻堚晽鈻堚枅鈺
  134.   //  鈻堚枅鈺斺暆鈻堚枅鈻堚枅鈺 鈻堚枅鈻堚枅鈺戔枅鈻堚晹鈺愨晲鈻堚枅鈺椻暁鈻堚枅鈺 鈻堚枅鈺斺暆鈻堚枅鈺斺晲鈺愨枅鈻堚晽鈻堚枅鈺斺晲鈺愨晲鈺愨暆鈺氣枅鈻堚晽
  135.   //  鈻堚枅鈺 鈻堚枅鈺斺枅鈻堚枅鈻堚晹鈻堚枅鈺戔枅鈻堚枅鈻堚枅鈻堚枅鈺 鈺氣枅鈻堚枅鈻堚晹鈺 鈻堚枅鈻堚枅鈻堚枅鈺斺暆鈻堚枅鈻堚枅鈻堚晽   鈻堚枅鈺
  136.   //  鈻堚枅鈺 鈻堚枅鈺戔暁鈻堚枅鈺斺暆鈻堚枅鈺戔枅鈻堚晹鈺愨晲鈻堚枅鈺  鈺氣枅鈻堚晹鈺  鈻堚枅鈺斺晲鈺愨枅鈻堚晽鈻堚枅鈺斺晲鈺愨暆   鈻堚枅鈺
  137.   //  鈺氣枅鈻堚晽鈻堚枅鈺 鈺氣晲鈺 鈻堚枅鈺戔枅鈻堚晳  鈻堚枅鈺   鈻堚枅鈺   鈻堚枅鈻堚枅鈻堚枅鈺斺暆鈻堚枅鈻堚枅鈻堚枅鈻堚晽鈻堚枅鈺斺暆
  138.   //   鈺氣晲鈺濃暁鈺愨暆     鈺氣晲鈺濃暁鈺愨暆  鈺氣晲鈺   鈺氣晲鈺   鈺氣晲鈺愨晲鈺愨晲鈺 鈺氣晲鈺愨晲鈺愨晲鈺愨暆鈺氣晲鈺
  139.   //
  140.   //  鈹屸攼 鈹 鈹敩鈹  鈹屸敩鈹   鈹   鈹攢鈹愨攲鈹€鈹愨攲鈹攼鈹 鈹敩鈹€鈹愨攲鈹愨攲  鈹屸攼鈹屸攲鈹€鈹愨敩 鈹  鈹屸敩鈹愨攲鈹€鈹愨攲鈹€鈹愨攲鈹€鈹愨敩鈹€鈹愨敩鈹€鈹愨攲鈹€鈹愨攲鈹攼
  141.   //  鈹溾敶鈹愨攤 鈹傗攤鈹   鈹傗攤  鈹屸敿鈹€  鈹溾敩鈹樷敎鈹  鈹 鈹 鈹傗敎鈹敇鈹傗攤鈹  鈹傗攤鈹傗敎鈹 鈹傗攤鈹   鈹傗攤鈹溾敜 鈹溾敜 鈹溾敜 鈹溾敩鈹樷敎鈹敇鈹溾敜  鈹傗攤
  142.   //  鈹斺攢鈹樷敂鈹€鈹樷敶鈹粹攢鈹樷攢鈹粹敇  鈹斺敇   鈹粹敂鈹€鈹斺攢鈹 鈹 鈹斺攢鈹樷敶鈹斺攢鈹樷敂鈹  鈹樷敂鈹樷敂鈹€鈹樷敂鈹粹敇  鈹€鈹粹敇鈹斺攢鈹樷敂  鈹斺攢鈹樷敶鈹斺攢鈹粹敂鈹€鈹斺攢鈹樷攢鈹粹敇
  143.   //  鈹屸攢    鈹攲鈹€鈹  鈹攢鈹愨攲鈹€鈹愨敩  鈹屸攢鈹愨敩  鈹攲鈹€鈹愨攲鈹愨攲鈹屸敩鈹    鈹€鈹
  144.   //  鈹傗攢鈹€鈹€  鈹傗敎鈹   鈹溾敩鈹樷敎鈹 鈹  鈹溾敜 鈹斺攼鈹屸敇鈹溾攢鈹も攤鈹傗攤 鈹   鈹€鈹€鈹€鈹
  145.   //  鈹斺攢    鈹粹敂    鈹粹敂鈹€鈹斺攢鈹樷敶鈹€鈹樷敂鈹€鈹 鈹斺敇 鈹 鈹粹敇鈹斺敇 鈹     鈹€鈹
  146.   // If a callback function was not specified, then build a new `Deferred` and bail now.
  147.   //
  148.   // > This method will be called AGAIN automatically when the Deferred is executed.
  149.   // > and next time, it'll have a callback.
  150.   if (!done) {
  151.     return new Deferred(WLModel, findOne, query);
  152.   } // --鈥
  153.  
  154.  
  155.   // Otherwise, IWMIH, we know that a callback was specified.
  156.   // So...
  157.   //
  158.   //  鈻堚枅鈻堚枅鈻堚枅鈻堚晽鈻堚枅鈺  鈻堚枅鈺椻枅鈻堚枅鈻堚枅鈻堚枅鈺 鈻堚枅鈻堚枅鈻堚枅鈺椻枅鈻堚晽   鈻堚枅鈺椻枅鈻堚枅鈻堚枅鈻堚枅鈻堚晽鈻堚枅鈻堚枅鈻堚枅鈻堚晽
  159.   //  鈻堚枅鈺斺晲鈺愨晲鈺愨暆鈺氣枅鈻堚晽鈻堚枅鈺斺暆鈻堚枅鈺斺晲鈺愨晲鈺愨暆鈻堚枅鈺斺晲鈺愨晲鈺愨暆鈻堚枅鈺   鈻堚枅鈺戔暁鈺愨晲鈻堚枅鈺斺晲鈺愨暆鈻堚枅鈺斺晲鈺愨晲鈺愨暆
  160.   //  鈻堚枅鈻堚枅鈻堚晽   鈺氣枅鈻堚枅鈺斺暆 鈻堚枅鈻堚枅鈻堚晽  鈻堚枅鈺     鈻堚枅鈺   鈻堚枅鈺   鈻堚枅鈺   鈻堚枅鈻堚枅鈻堚晽
  161.   //  鈻堚枅鈺斺晲鈺愨暆   鈻堚枅鈺斺枅鈻堚晽 鈻堚枅鈺斺晲鈺愨暆  鈻堚枅鈺     鈻堚枅鈺   鈻堚枅鈺   鈻堚枅鈺   鈻堚枅鈺斺晲鈺愨暆
  162.   //  鈻堚枅鈻堚枅鈻堚枅鈻堚晽鈻堚枅鈺斺暆 鈻堚枅鈺椻枅鈻堚枅鈻堚枅鈻堚枅鈺椻暁鈻堚枅鈻堚枅鈻堚枅鈺椻暁鈻堚枅鈻堚枅鈻堚枅鈺斺暆   鈻堚枅鈺   鈻堚枅鈻堚枅鈻堚枅鈻堚晽
  163.   //  鈺氣晲鈺愨晲鈺愨晲鈺愨暆鈺氣晲鈺  鈺氣晲鈺濃暁鈺愨晲鈺愨晲鈺愨晲鈺 鈺氣晲鈺愨晲鈺愨晲鈺 鈺氣晲鈺愨晲鈺愨晲鈺    鈺氣晲鈺   鈺氣晲鈺愨晲鈺愨晲鈺愨暆
  164.  
  165.   //  鈺斺晲鈺椻晹鈺愨晽鈺︹晲鈺椻晹鈺愨晽鈺斺晲鈺  鈹屸攢鈹愨攲鈹攼鈹屸攢鈹愨攲鈹€鈹愨攲鈹€鈹  鈹屸敩鈹愨敩 鈹攲鈹€鈹  鈹屸攢鈹 鈹 鈹攲鈹€鈹愨敩鈹€鈹愨敩 鈹
  166.   //  鈺犫暎 鈺 鈺戔暊鈺︹暆鈺 鈺︹晳鈺   鈹斺攢鈹 鈹 鈹溾攢鈹も攤 鈹敎鈹    鈹 鈹傗攤鈹傗攤 鈹  鈹傗攢鈹尖攼鈹 鈹傗敎鈹 鈹溾敩鈹樷敂鈹敇
  167.   //  鈺  鈺氣晲鈺濃暕鈺氣晲鈺氣晲鈺濃暁鈺愨暆  鈹斺攢鈹 鈹 鈹 鈹粹敂鈹€鈹樷敂鈹€鈹   鈹 鈹斺敶鈹樷敂鈹€鈹  鈹斺攢鈹樷敂鈹斺攢鈹樷敂鈹€鈹樷敶鈹斺攢 鈹
  168.   //
  169.   // Forge a stage 2 query (aka logical protostatement)
  170.   try {
  171.     forgeStageTwoQuery(query, orm);
  172.   } catch (e) {
  173.     switch (e.code) {
  174.  
  175.       case 'E_INVALID_CRITERIA':
  176.         return done(
  177.           flaverr({
  178.               name: 'UsageError'
  179.             },
  180.             new Error(
  181.               'Invalid criteria.\n' +
  182.               'Details:\n' +
  183.               '  ' + e.details + '\n'
  184.             )
  185.           )
  186.         );
  187.  
  188.       case 'E_INVALID_POPULATES':
  189.         return done(
  190.           flaverr({
  191.               name: 'UsageError'
  192.             },
  193.             new Error(
  194.               'Invalid populate(s).\n' +
  195.               'Details:\n' +
  196.               '  ' + e.details + '\n'
  197.             )
  198.           )
  199.         );
  200.  
  201.       case 'E_NOOP':
  202.         return done(undefined, undefined);
  203.  
  204.       default:
  205.         return done(e);
  206.     }
  207.   } // >-鈥
  208.  
  209.  
  210.   //  鈹 鈹攲鈹€鈹愨攲鈹愨攲鈹屸敩鈹愨敩  鈹屸攢鈹  鈺斺晽 鈺斺晲鈺椻晹鈺愨晽鈺斺晲鈺椻暒鈺愨晽鈺斺晲鈺  鈹  鈹攲鈹€鈹愨攲鈹€鈹愨攲鈹€鈹愨敩 鈹攲鈹€鈹愨敩  鈹屸攢鈹  鈹屸攢鈹愨攲鈹€鈹愨敩  鈹  鈹屸攼 鈹屸攢鈹愨攲鈹€鈹愨敩鈹屸攢
  211.   //  鈹溾攢鈹も敎鈹€鈹も攤鈹傗攤 鈹傗攤鈹  鈹溾敜   鈺犫暕鈺椻晳鈺 鈺犫暎 鈺 鈺戔暊鈺︹暆鈺戔暎   鈹  鈹傗敎鈹 鈹溾敜 鈹  鈹斺敩鈹樷攤  鈹  鈹溾敜   鈹  鈹溾攢鈹も攤  鈹  鈹溾敶鈹愨敎鈹€鈹も攤  鈹溾敶鈹
  212.   //  鈹 鈹粹敶 鈹粹敇鈹斺敇鈹€鈹粹敇鈹粹攢鈹樷敂鈹€鈹  鈺氣晲鈺濃暁鈺愨暆鈺  鈺氣晲鈺濃暕鈺氣晲鈺氣晲鈺  鈹粹攢鈹樷敶鈹  鈹斺攢鈹樷敂鈹€鈹 鈹 鈹斺攢鈹樷敶鈹€鈹樷敂鈹€鈹  鈹斺攢鈹樷敶 鈹粹敶鈹€鈹樷敶鈹€鈹樷敂鈹€鈹樷敶 鈹粹敂鈹€鈹樷敶 鈹
  213.   // Determine what to do about running any lifecycle callbacks
  214.   (function _maybeRunBeforeLC(proceed){
  215.  
  216.     // If the `skipAllLifecycleCallbacks` meta key was enabled, then don't run this LC.
  217.     if (_.has(query.meta, 'skipAllLifecycleCallbacks') && query.meta.skipAllLifecycleCallbacks) {
  218.       return proceed(undefined, query);
  219.     }//-鈥
  220.  
  221.     // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  222.     // FUTURE: This is where the `beforeFindOne()` lifecycle callback would go
  223.     // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  224.     return proceed(undefined, query);
  225.  
  226.   })(function _afterPotentiallyRunningBeforeLC(err, query) {
  227.     if (err) {
  228.       return done(err);
  229.     }
  230.  
  231.     // ================================================================================
  232.     // FUTURE: potentially bring this back (but also would need the `omit clause`)
  233.     // ================================================================================
  234.     // // Before we get to forging again, save a copy of the stage 2 query's
  235.     // // `select` clause.  We'll need this later on when processing the resulting
  236.     // // records, and if we don't copy it now, it might be damaged by the forging.
  237.     // //
  238.     // // > Note that we don't need a deep clone.
  239.     // // > (That's because the `select` clause is only 1 level deep.)
  240.     // var s2QSelectClause = _.clone(query.criteria.select);
  241.     // ================================================================================
  242.  
  243.  
  244.     //  鈹屸攢鈹愨攲鈹€鈹愨攲鈹愨攲鈹屸敩鈹  鈹屸敩鈹愨攲鈹€鈹  鈺斺晲鈺椻晹鈺︹晽鈺斺晲鈺椻晹鈺愨晽鈺斺暒鈺椻晹鈺愨晽鈺︹晲鈺
  245.     //  鈹斺攢鈹愨敎鈹 鈹傗攤鈹 鈹傗攤   鈹 鈹 鈹  鈺犫晲鈺 鈺戔晳鈺犫晲鈺b暊鈺愨暆 鈺 鈺戔暎 鈺犫暒鈺
  246.     //  鈹斺攢鈹樷敂鈹€鈹樷敇鈹斺敇鈹€鈹粹敇   鈹 鈹斺攢鈹  鈺 鈺┾晲鈺┾暆鈺 鈺┾暕   鈺 鈺氣晲鈺濃暕鈺氣晲
  247.     // Use `helpFind()` to forge stage 3 quer(y/ies) and then call the appropriate adapters' method(s).
  248.     // > Note: `helpFind` is responsible for running the `transformer`.
  249.     // > (i.e. so that column names are transformed back into attribute names)
  250.     helpFind(WLModel, query, function _afterFetchingRecords(err, populatedRecords) {
  251.       if (err) {
  252.  
  253.         // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  254.         // Note: Normally, in other model methods, we do this `isError` check + addition of the `modelIdentity` prop
  255.         // when we call the adapter method itself.  But since helpFind() is such a beast, both things are currently
  256.         // implemented here for simplicity.  This could change in the future as helpFind() is refined.
  257.         // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  258.         if (!_.isError(err)) {
  259.           return done(new Error(
  260.             'If an error is sent back from the adapter, it should always be an Error instance.  '+
  261.             'But instead, got: '+util.inspect(err, {depth:5})+''
  262.           ));
  263.         }//-鈥
  264.         // Attach the identity of this model (for convenience).
  265.         err.modelIdentity = modelIdentity;
  266.         return done(err);
  267.       }//-鈥
  268.       // console.log('result from operation runner:', record);
  269.  
  270.       // If more than one matching record was found, then consider this an error.
  271.       if (populatedRecords.length > 1) {
  272.         return done(new Error(
  273.           'More than one matching record found for `.findOne()`:\n'+
  274.           '```\n'+
  275.           _.pluck(populatedRecords, WLModel.primaryKey)+'\n'+
  276.           '```\n'+
  277.           '\n'+
  278.           'Criteria used:\n'+
  279.           '```\n'+
  280.           util.inspect(query.criteria,{depth:5})+''+
  281.           '```'
  282.         ));
  283.       }//-鈥
  284.  
  285.       // Check and see if we actually found a record.
  286.       var thePopulatedRecord = _.first(populatedRecords);
  287.  
  288.       // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  289.       // FUTURE: Allow a `mustExist: true` meta key to be specified, probably via the use of a simple new query
  290.       // method-- something like `.mustExist()`.  If set, then if the record is not found, bail with an error.
  291.       // This is just a nicety to simplify some of the more annoyingly repetitive userland code that one needs
  292.       // to write in a Node/Sails app.
  293.       // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  294.  
  295.       // If so...
  296.       if (thePopulatedRecord) {
  297.  
  298.         // Check the record to verify compliance with the adapter spec,
  299.         // as well as any issues related to stale data that might not have been
  300.         // been migrated to keep up with the logical schema (`type`, etc. in
  301.         // attribute definitions).
  302.         try {
  303.           processAllRecords([ thePopulatedRecord ], query.meta, modelIdentity, orm);
  304.         } catch (e) { return done(e); }
  305.  
  306.       }//>-
  307.  
  308.       //  鈹 鈹攲鈹€鈹愨攲鈹愨攲鈹屸敩鈹愨敩  鈹屸攢鈹  鈺斺晲鈺椻晹鈺愨晽鈺斺暒鈺椻晹鈺愨晽鈺︹晲鈺  鈹  鈹攲鈹€鈹愨攲鈹€鈹愨攲鈹€鈹愨敩 鈹攲鈹€鈹愨敩  鈹屸攢鈹  鈹屸攢鈹愨攲鈹€鈹愨敩  鈹  鈹屸攼 鈹屸攢鈹愨攲鈹€鈹愨敩鈹屸攢
  309.       //  鈹溾攢鈹も敎鈹€鈹も攤鈹傗攤 鈹傗攤鈹  鈹溾敜   鈺犫晲鈺b暊鈺  鈺 鈺戔暎 鈺犫暒鈺  鈹  鈹傗敎鈹 鈹溾敜 鈹  鈹斺敩鈹樷攤  鈹  鈹溾敜   鈹  鈹溾攢鈹も攤  鈹  鈹溾敶鈹愨敎鈹€鈹も攤  鈹溾敶鈹
  310.       //  鈹 鈹粹敶 鈹粹敇鈹斺敇鈹€鈹粹敇鈹粹攢鈹樷敂鈹€鈹  鈺 鈺┾暁   鈺 鈺氣晲鈺濃暕鈺氣晲  鈹粹攢鈹樷敶鈹  鈹斺攢鈹樷敂鈹€鈹 鈹 鈹斺攢鈹樷敶鈹€鈹樷敂鈹€鈹  鈹斺攢鈹樷敶 鈹粹敶鈹€鈹樷敶鈹€鈹樷敂鈹€鈹樷敶 鈹粹敂鈹€鈹樷敶 鈹
  311.       (function _maybeRunAfterLC(proceed){
  312.  
  313.         // If the `skipAllLifecycleCallbacks` meta key was enabled, then don't run this LC.
  314.         if (_.has(query.meta, 'skipAllLifecycleCallbacks') && query.meta.skipAllLifecycleCallbacks) {
  315.           return proceed(undefined, thePopulatedRecord);
  316.         }//-鈥
  317.  
  318.         // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  319.         // FUTURE: This is where the `afterFindOne()` lifecycle callback would go
  320.         // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  321.         return proceed(undefined, thePopulatedRecord);
  322.  
  323.       })(function _afterPotentiallyRunningAfterLC(err, thePopulatedRecord){
  324.         if (err) { return done(err); }
  325.  
  326.         // All done.
  327.         return done(undefined, thePopulatedRecord);
  328.  
  329.       });//</ self-calling function to handle "after" lifecycle callback >
  330.     }); //</ helpFind() >
  331.   }); //</ self-calling function to handle "before" lifecycle callback >
  332. };
  333.  
downloadfind-one.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
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top