BVB Source Codes

waterline Show deferred.js Source code

Return Download waterline: download deferred.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 Promise = require('bluebird');
  8. var normalizeCallback = require('./private/normalize-callback');
  9.  
  10.  
  11. /**
  12.  * Module constants
  13.  */
  14.  
  15. var RECOGNIZED_S2Q_CRITERIA_CLAUSE_NAMES = ['where', 'limit', 'skip', 'sort', 'select', 'omit'];
  16.  
  17.  
  18.  
  19.  
  20. /**
  21.  * Deferred Object
  22.  *
  23.  * Used for building up a Query
  24.  */
  25. var Deferred = module.exports = function(context, method, wlQueryInfo) {
  26.  
  27.   if (!context) {
  28.     throw new Error('Must supply a context to a new Deferred object. Usage: new Deferred(context, fn, wlQueryInfo)');
  29.   }
  30.  
  31.   if (!method) {
  32.     throw new Error('Must supply a method to a new Deferred object. Usage: new Deferred(context, fn, wlQueryInfo)');
  33.   }
  34.  
  35.   if (!wlQueryInfo) {
  36.     throw new Error('Must supply a third arg (`wlQueryInfo`) to a new Deferred object. Usage: new Deferred(context, fn, wlQueryInfo)');
  37.   }
  38.   if (!_.isObject(wlQueryInfo)) {
  39.     throw new Error('Third arg (`wlQueryInfo`) must be a valid dictionary. Usage: new Deferred(context, fn, wlQueryInfo)');
  40.   }
  41.  
  42.  
  43.   this._context = context;
  44.   this._method = method;
  45.  
  46.   // Make sure `_wlQueryInfo` is always a dictionary.
  47.   this._wlQueryInfo = wlQueryInfo || {};
  48.  
  49.   // Make sure `._wlQueryInfo.valuesToSet` is `null`, rather than simply undefined or any other falsey thing..
  50.   // (This is just for backwards compatibility.  Should be removed as soon as it's proven that it's safe to do so.)
  51.   this._wlQueryInfo.valuesToSet = this._wlQueryInfo.valuesToSet || null;
  52.  
  53.   // Make sure `_wlQueryInfo.criteria` is always a dictionary
  54.   // (just in case one of the chainable query methods gets used)
  55.   this._wlQueryInfo.criteria = this._wlQueryInfo.criteria || {};
  56.  
  57.   // Handle implicit `where` clause:
  58.   //
  59.   // If the provided criteria dictionary DOES NOT contain the names of ANY known
  60.   // criteria clauses (like `where`, `limit`, etc.) as properties, then we can
  61.   // safely assume that it is relying on shorthand: i.e. simply specifying what
  62.   // would normally be the `where` clause, but at the top level.
  63.   var recognizedClauses = _.intersection(_.keys(this._wlQueryInfo.criteria), RECOGNIZED_S2Q_CRITERIA_CLAUSE_NAMES);
  64.   if (recognizedClauses.length === 0) {
  65.     this._wlQueryInfo.criteria = {
  66.       where: this._wlQueryInfo.criteria
  67.     };
  68.   }//>-
  69.  
  70.  
  71.   // Initialize `_deferred` to `null`.
  72.   // (this is used for promises)
  73.   this._deferred = null;
  74.  
  75.   return this;
  76. };
  77.  
  78.  
  79.  
  80. //   鈻堚枅鈻堚枅鈻堚枅鈺 鈻堚枅鈺   鈻堚枅鈺椻枅鈻堚枅鈻堚枅鈻堚枅鈺椻枅鈻堚枅鈻堚枅鈻堚晽 鈻堚枅鈺   鈻堚枅鈺
  81. //  鈻堚枅鈺斺晲鈺愨晲鈻堚枅鈺椻枅鈻堚晳   鈻堚枅鈺戔枅鈻堚晹鈺愨晲鈺愨晲鈺濃枅鈻堚晹鈺愨晲鈻堚枅鈺椻暁鈻堚枅鈺 鈻堚枅鈺斺暆
  82. //  鈻堚枅鈺   鈻堚枅鈺戔枅鈻堚晳   鈻堚枅鈺戔枅鈻堚枅鈻堚枅鈺  鈻堚枅鈻堚枅鈻堚枅鈺斺暆 鈺氣枅鈻堚枅鈻堚晹鈺
  83. //  鈻堚枅鈺戔杽鈻 鈻堚枅鈺戔枅鈻堚晳   鈻堚枅鈺戔枅鈻堚晹鈺愨晲鈺  鈻堚枅鈺斺晲鈺愨枅鈻堚晽  鈺氣枅鈻堚晹鈺
  84. //  鈺氣枅鈻堚枅鈻堚枅鈻堚晹鈺濃暁鈻堚枅鈻堚枅鈻堚枅鈺斺暆鈻堚枅鈻堚枅鈻堚枅鈻堚晽鈻堚枅鈺  鈻堚枅鈺   鈻堚枅鈺
  85. //   鈺氣晲鈺愨杸鈻€鈺愨暆  鈺氣晲鈺愨晲鈺愨晲鈺 鈺氣晲鈺愨晲鈺愨晲鈺愨暆鈺氣晲鈺  鈺氣晲鈺   鈺氣晲鈺
  86. //
  87. //  鈻堚枅鈻堚晽   鈻堚枅鈻堚晽 鈻堚枅鈻堚枅鈻堚枅鈺 鈻堚枅鈻堚枅鈻堚枅鈺 鈻堚枅鈺椻枅鈻堚枅鈻堚枅鈻堚枅鈺椻枅鈻堚晽鈻堚枅鈻堚枅鈻堚枅鈻堚晽鈻堚枅鈻堚枅鈻堚枅鈺
  88. //  鈻堚枅鈻堚枅鈺 鈻堚枅鈻堚枅鈺戔枅鈻堚晹鈺愨晲鈺愨枅鈻堚晽鈻堚枅鈺斺晲鈺愨枅鈻堚晽鈻堚枅鈺戔枅鈻堚晹鈺愨晲鈺愨晲鈺濃枅鈻堚晳鈻堚枅鈺斺晲鈺愨晲鈺愨暆鈻堚枅鈺斺晲鈺愨枅鈻堚晽
  89. //  鈻堚枅鈺斺枅鈻堚枅鈻堚晹鈻堚枅鈺戔枅鈻堚晳   鈻堚枅鈺戔枅鈻堚晳  鈻堚枅鈺戔枅鈻堚晳鈻堚枅鈻堚枅鈻堚晽  鈻堚枅鈺戔枅鈻堚枅鈻堚枅鈺  鈻堚枅鈻堚枅鈻堚枅鈺斺暆
  90. //  鈻堚枅鈺戔暁鈻堚枅鈺斺暆鈻堚枅鈺戔枅鈻堚晳   鈻堚枅鈺戔枅鈻堚晳  鈻堚枅鈺戔枅鈻堚晳鈻堚枅鈺斺晲鈺愨暆  鈻堚枅鈺戔枅鈻堚晹鈺愨晲鈺  鈻堚枅鈺斺晲鈺愨枅鈻堚晽
  91. //  鈻堚枅鈺 鈺氣晲鈺 鈻堚枅鈺戔暁鈻堚枅鈻堚枅鈻堚枅鈺斺暆鈻堚枅鈻堚枅鈻堚枅鈺斺暆鈻堚枅鈺戔枅鈻堚晳     鈻堚枅鈺戔枅鈻堚枅鈻堚枅鈻堚枅鈺椻枅鈻堚晳  鈻堚枅鈺
  92. //  鈺氣晲鈺     鈺氣晲鈺 鈺氣晲鈺愨晲鈺愨晲鈺 鈺氣晲鈺愨晲鈺愨晲鈺 鈺氣晲鈺濃暁鈺愨暆     鈺氣晲鈺濃暁鈺愨晲鈺愨晲鈺愨晲鈺濃暁鈺愨暆  鈺氣晲鈺
  93. //
  94. //  鈻堚枅鈻堚晽   鈻堚枅鈻堚晽鈻堚枅鈻堚枅鈻堚枅鈻堚晽鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈺椻枅鈻堚晽  鈻堚枅鈺 鈻堚枅鈻堚枅鈻堚枅鈺 鈻堚枅鈻堚枅鈻堚枅鈺 鈻堚枅鈻堚枅鈻堚枅鈻堚晽
  95. //  鈻堚枅鈻堚枅鈺 鈻堚枅鈻堚枅鈺戔枅鈻堚晹鈺愨晲鈺愨晲鈺濃暁鈺愨晲鈻堚枅鈺斺晲鈺愨暆鈻堚枅鈺  鈻堚枅鈺戔枅鈻堚晹鈺愨晲鈺愨枅鈻堚晽鈻堚枅鈺斺晲鈺愨枅鈻堚晽鈻堚枅鈺斺晲鈺愨晲鈺愨暆
  96. //  鈻堚枅鈺斺枅鈻堚枅鈻堚晹鈻堚枅鈺戔枅鈻堚枅鈻堚枅鈺     鈻堚枅鈺   鈻堚枅鈻堚枅鈻堚枅鈻堚晳鈻堚枅鈺   鈻堚枅鈺戔枅鈻堚晳  鈻堚枅鈺戔枅鈻堚枅鈻堚枅鈻堚枅鈺
  97. //  鈻堚枅鈺戔暁鈻堚枅鈺斺暆鈻堚枅鈺戔枅鈻堚晹鈺愨晲鈺     鈻堚枅鈺   鈻堚枅鈺斺晲鈺愨枅鈻堚晳鈻堚枅鈺   鈻堚枅鈺戔枅鈻堚晳  鈻堚枅鈺戔暁鈺愨晲鈺愨晲鈻堚枅鈺
  98. //  鈻堚枅鈺 鈺氣晲鈺 鈻堚枅鈺戔枅鈻堚枅鈻堚枅鈻堚枅鈺   鈻堚枅鈺   鈻堚枅鈺  鈻堚枅鈺戔暁鈻堚枅鈻堚枅鈻堚枅鈺斺暆鈻堚枅鈻堚枅鈻堚枅鈺斺暆鈻堚枅鈻堚枅鈻堚枅鈻堚晳
  99. //  鈺氣晲鈺     鈺氣晲鈺濃暁鈺愨晲鈺愨晲鈺愨晲鈺   鈺氣晲鈺   鈺氣晲鈺  鈺氣晲鈺 鈺氣晲鈺愨晲鈺愨晲鈺 鈺氣晲鈺愨晲鈺愨晲鈺 鈺氣晲鈺愨晲鈺愨晲鈺愨暆
  100. //
  101.  
  102. /**
  103.  * Modify this query so that it populates all associations (singular and plural).
  104.  *
  105.  * @returns {Query}
  106.  */
  107. Deferred.prototype.populateAll = function() {
  108.   var pleaseDoNotUseThis = arguments[0];
  109.  
  110.   if (!_.isUndefined(pleaseDoNotUseThis)) {
  111.     console.warn(
  112.       'Deprecation warning: Passing in an argument to `.populateAll()` is no longer supported.\n'+
  113.       '(But interpreting this usage the original way for you this time...)\n'+
  114.       'Note: If you really want to use the _exact same_ criteria for simultaneously populating multiple\n'+
  115.       'different plural ("collection") associations, please use separate calls to `.populate()` instead.\n'+
  116.       'Or, alternatively, instead of using `.populate()`, you can choose to call `.find()`, `.findOne()`,\n'+
  117.       'or `.stream()` with a dictionary (plain JS object) as the second argument, where each key is the\n'+
  118.       'name of an association, and each value is either:\n'+
  119.       ' 鈥 true  (for singular aka "model" associations), or\n'+
  120.       ' 鈥 a criteria dictionary (for plural aka "collection" associations)\n'
  121.     );
  122.   }//>-
  123.  
  124.   var self = this;
  125.   this._context.associations.forEach(function (associationInfo) {
  126.     self.populate(associationInfo.alias, pleaseDoNotUseThis);
  127.   });
  128.   return this;
  129. };
  130.  
  131. /**
  132.  * .populate()
  133.  *
  134.  * Set the `populates` key for this query.
  135.  *
  136.  * > Used for populating associations.
  137.  *
  138.  * @param {String|Array} key, the key to populate or array of string keys
  139.  * @returns {Query}
  140.  */
  141.  
  142. Deferred.prototype.populate = function(keyName, criteria) {
  143.   var self = this;
  144.  
  145.   // Adds support for arrays into keyName so that a list of
  146.   // populates can be passed
  147.   if (_.isArray(keyName)) {
  148.     console.warn(
  149.       'Deprecation warning: `.populate()` no longer accepts an array as its first argument.\n'+
  150.       'Please use separate calls to `.populate()` instead.  Or, alternatively, instead of\n'+
  151.       'using `.populate()`, you can choose to call `.find()`, `.findOne()` or `.stream()`\n'+
  152.       'with a dictionary (plain JS object) as the second argument, where each key is the\n'+
  153.       'name of an association, and each value is either:\n'+
  154.       ' 鈥 true  (for singular aka "model" associations), or\n'+
  155.       ' 鈥 a criteria dictionary (for plural aka "collection" associations)\n'+
  156.       '(Interpreting this usage the original way for you this time...)\n'
  157.     );
  158.     _.each(keyName, function(populate) {
  159.       self.populate(populate, criteria);
  160.     });
  161.     return this;
  162.   }//-鈥
  163.  
  164.   // If this is the first time, make the `populates` query key an empty dictionary.
  165.   if (_.isUndefined(this._wlQueryInfo.populates)) {
  166.     this._wlQueryInfo.populates = {};
  167.   }
  168.  
  169.   // Then, if criteria was specified, use it.
  170.   if (!_.isUndefined(criteria)){
  171.     this._wlQueryInfo.populates[keyName] = criteria;
  172.   }
  173.   else {
  174.     // (Note: even though we set {} regardless, even when it should really be `true`
  175.     // if it's a singular association, that's ok because it gets silently normalized
  176.     // in FS2Q.)
  177.     this._wlQueryInfo.populates[keyName] = {};
  178.   }
  179.  
  180.   return this;
  181. };
  182.  
  183.  
  184.  
  185.  
  186. /**
  187.  * Add associated IDs to the query
  188.  *
  189.  * @param {Array} associatedIds
  190.  * @returns {Query}
  191.  */
  192.  
  193. Deferred.prototype.members = function(associatedIds) {
  194.   this._wlQueryInfo.associatedIds = associatedIds;
  195.   return this;
  196. };
  197.  
  198.  
  199. /**
  200.  * Add an iteratee to the query
  201.  *
  202.  * @param {Function} iteratee
  203.  * @returns {Query}
  204.  */
  205.  
  206. Deferred.prototype.eachRecord = function(iteratee) {
  207.   this._wlQueryInfo.eachRecordFn = iteratee;
  208.   return this;
  209. };
  210.  
  211. Deferred.prototype.eachBatch = function(iteratee) {
  212.   this._wlQueryInfo.eachBatchFn = iteratee;
  213.   return this;
  214. };
  215.  
  216.  
  217. /**
  218.  * Add projections to the query
  219.  *
  220.  * @param {Array} attributes to select
  221.  * @returns {Query}
  222.  */
  223.  
  224. Deferred.prototype.select = function(selectAttributes) {
  225.   this._wlQueryInfo.criteria.select = selectAttributes;
  226.   return this;
  227. };
  228.  
  229. /**
  230.  * Add an omit clause to the query's criteria.
  231.  *
  232.  * @param {Array} attributes to select
  233.  * @returns {Query}
  234.  */
  235. Deferred.prototype.omit = function(omitAttributes) {
  236.   this._wlQueryInfo.criteria.omit = omitAttributes;
  237.   return this;
  238. };
  239.  
  240. /**
  241.  * Add a `where` clause to the query's criteria.
  242.  *
  243.  * @param {Dictionary} criteria to append
  244.  * @returns {Query}
  245.  */
  246.  
  247. Deferred.prototype.where = function(whereCriteria) {
  248.   this._wlQueryInfo.criteria.where = whereCriteria;
  249.   return this;
  250. };
  251.  
  252. /**
  253.  * Add a `limit` clause to the query's criteria.
  254.  *
  255.  * @param {Number} number to limit
  256.  * @returns {Query}
  257.  */
  258.  
  259. Deferred.prototype.limit = function(limit) {
  260.   this._wlQueryInfo.criteria.limit = limit;
  261.   return this;
  262. };
  263.  
  264. /**
  265.  * Add a `skip` clause to the query's criteria.
  266.  *
  267.  * @param {Number} number to skip
  268.  * @returns {Query}
  269.  */
  270.  
  271. Deferred.prototype.skip = function(skip) {
  272.   this._wlQueryInfo.criteria.skip = skip;
  273.   return this;
  274. };
  275.  
  276.  
  277. /**
  278.  * .paginate()
  279.  *
  280.  * Add a `skip`+`limit` clause to the query's criteria
  281.  * based on the specified page number (and optionally,
  282.  * the page size, which defaults to 30 otherwise.)
  283.  *
  284.  * > This method is really just a little dollop of syntactic sugar.
  285.  *
  286.  * ```
  287.  * Show.find({ category: 'home-and-garden' })
  288.  * .paginate(0)
  289.  * .exec(...)
  290.  * ```
  291.  *
  292.  * -OR- (for backwards compat.)
  293.  * ```
  294.  * Show.find({ category: 'home-and-garden' })
  295.  * .paginate({ page: 0, limit: 30 })
  296.  * .exec(...)
  297.  * ```
  298.  * - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  299.  * @param {Number} pageNumOrOpts
  300.  * @param {Number?} pageSize
  301.  *
  302.  * -OR-
  303.  *
  304.  * @param {Number|Dictionary} pageNumOrOpts
  305.  *     @property {Number} page    [the page num. (backwards compat.)]
  306.  *     @property {Number?} limit  [the page size (backwards compat.)]
  307.  * - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  308.  * @returns {Query}
  309.  * - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  310.  */
  311. Deferred.prototype.paginate = function(pageNumOrOpts, pageSize) {
  312.  
  313.   // Interpret page number.
  314.   var pageNum;
  315.   // If not specified...
  316.   if (_.isUndefined(pageNumOrOpts)) {
  317.     console.warn(
  318.       'Please always specify a `page` when calling .paginate() -- for example:\n'+
  319.       '```\n'+
  320.       'Boat.find().sort(\'wetness DESC\')\n'+
  321.       '.paginate(0, 30)\n'+
  322.       '.exec(function (err, first30Boats){\n'+
  323.       '  \n'+
  324.       '});\n'+
  325.       '```\n'+
  326.       '(In the mean time, assuming the first page (#0)...)'
  327.     );
  328.     pageNum = 0;
  329.   }
  330.   // If dictionary... (temporary backwards-compat.)
  331.   else if (_.isObject(pageNumOrOpts)) {
  332.     pageNum = pageNumOrOpts.page || 0;
  333.     console.warn(
  334.       'Deprecation warning: Passing in a dictionary (plain JS object) to .paginate()\n'+
  335.       'is no longer supported -- instead, please use:\n'+
  336.       '```\n'+
  337.       '.paginate(pageNum, pageSize)\n'+
  338.       '```\n'+
  339.       '(In the mean time, interpreting this as page #'+pageNum+'...)'
  340.     );
  341.   }
  342.   // Otherwise, assume it's the proper usage.
  343.   else {
  344.     pageNum = pageNumOrOpts;
  345.   }
  346.  
  347.  
  348.   // Interpret the page size (number of records per page).
  349.   if (!_.isUndefined(pageSize)) {
  350.     if (!_.isNumber(pageSize)) {
  351.       console.warn(
  352.         'Unrecognized usage for .paginate() -- if specified, 2nd argument (page size)\n'+
  353.         'should be a number like 10 (otherwise, it defaults to 30).\n'+
  354.         '(Ignoring this and switching to a page size of 30 automatically...)'
  355.       );
  356.       pageSize = 30;
  357.     }
  358.   }
  359.   else if (_.isObject(pageNumOrOpts) && !_.isUndefined(pageNumOrOpts.limit)) {
  360.     // Note: IWMIH, then we must have already logged a deprecation warning above--
  361.     // so no need to do it again.
  362.     pageSize = pageNumOrOpts.limit || 30;
  363.   }
  364.   else {
  365.     // Note that this default is the same as the default batch size used by `.stream()`.
  366.     pageSize = 30;
  367.   }
  368.  
  369.   // Now, apply the page size as the limit, and compute & apply the appropriate `skip`.
  370.   // (REMEMBER: pages are now zero-indexed!)
  371.   this
  372.   .skip(pageNum * pageSize)
  373.   .limit(pageSize);
  374.  
  375.   return this;
  376. };
  377.  
  378.  
  379. /**
  380.  * Add a `sort` clause to the criteria object
  381.  *
  382.  * @param {Ref} sortClause
  383.  * @returns {Query}
  384.  */
  385.  
  386. Deferred.prototype.sort = function(sortClause) {
  387.   this._wlQueryInfo.criteria.sort = sortClause;
  388.   return this;
  389. };
  390.  
  391.  
  392.  
  393.  
  394. /**
  395.  * Add values to be used in update or create query
  396.  *
  397.  * @param {Object, Array} values
  398.  * @returns {Query}
  399.  */
  400.  
  401. Deferred.prototype.set = function(values) {
  402.  
  403.   if (this._wlQueryInfo.method === 'create') {
  404.     console.warn(
  405.       'Deprecation warning: In future versions of Waterline, the use of .set() with .create()\n'+
  406.       'will no longer be supported.  In the past, you could use .set() to provide the initial\n'+
  407.       'skeleton of a new record to create (like `.create().set({})`)-- but really .set() should\n'+
  408.       'only be used with .update().  So instead, please change this code so that it just passes in\n'+
  409.       'the initial new record as the first argument to `.create().`'
  410.     );
  411.     this._wlQueryInfo.newRecord = values;
  412.   }
  413.   else if (this._wlQueryInfo.method === 'createEach') {
  414.     console.warn(
  415.       'Deprecation warning: In future versions of Waterline, the use of .set() with .createEach()\n'+
  416.       'will no longer be supported.  In the past, you could use .set() to provide an array of\n'+
  417.       'new records to create (like `.createEach().set([{}, {}])`)-- but really .set() was designed\n'+
  418.       'to be used with .update() only. So instead, please change this code so that it just\n'+
  419.       'passes in the initial new record as the first argument to `.createEach().`'
  420.     );
  421.     this._wlQueryInfo.newRecords = values;
  422.   }
  423.   else {
  424.     this._wlQueryInfo.valuesToSet = values;
  425.   }
  426.  
  427.   return this;
  428.  
  429. };
  430.  
  431. /**
  432.  * Pass metadata down to the adapter that won't be processed or touched by Waterline.
  433.  *
  434.  * > Note that we use `._meta` internally because we're already using `.meta` as a method!
  435.  * > In an actual S2Q, this key becomes `meta` instead (see the impl of .exec() to trace this)
  436.  */
  437.  
  438. Deferred.prototype.meta = function(data) {
  439.   this._meta = data;
  440.   return this;
  441. };
  442.  
  443. /**
  444.  * Pass an active connection down to the query.
  445.  */
  446.  
  447. Deferred.prototype.usingConnection = function(leasedConnection) {
  448.   this._meta = this._meta || {};
  449.   this._meta.leasedConnection = leasedConnection;
  450.   return this;
  451. };
  452.  
  453.  
  454. //     鈻堚枅鈻堚枅鈻堚枅鈻堚晽鈻堚枅鈺  鈻堚枅鈺椻枅鈻堚枅鈻堚枅鈻堚枅鈺 鈻堚枅鈻堚枅鈻堚枅鈺 鈻堚枅鈺椻枅鈻堚晽        鈻堚枅鈺
  455. //     鈻堚枅鈺斺晲鈺愨晲鈺愨暆鈺氣枅鈻堚晽鈻堚枅鈺斺暆鈻堚枅鈺斺晲鈺愨晲鈺愨暆鈻堚枅鈺斺晲鈺愨晲鈺愨暆鈻堚枅鈺斺暆鈺氣枅鈻堚晽       鈻堚枅鈺
  456. //     鈻堚枅鈻堚枅鈻堚晽   鈺氣枅鈻堚枅鈺斺暆 鈻堚枅鈻堚枅鈻堚晽  鈻堚枅鈺     鈻堚枅鈺  鈻堚枅鈺    鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈺
  457. //     鈻堚枅鈺斺晲鈺愨暆   鈻堚枅鈺斺枅鈻堚晽 鈻堚枅鈺斺晲鈺愨暆  鈻堚枅鈺     鈻堚枅鈺  鈻堚枅鈺    鈻堚枅鈺斺晲鈻堚枅鈺斺晲鈺
  458. //  鈻堚枅鈺椻枅鈻堚枅鈻堚枅鈻堚枅鈺椻枅鈻堚晹鈺 鈻堚枅鈺椻枅鈻堚枅鈻堚枅鈻堚枅鈺椻暁鈻堚枅鈻堚枅鈻堚枅鈺椻暁鈻堚枅鈺椻枅鈻堚晹鈺    鈻堚枅鈻堚枅鈻堚枅鈺
  459. //  鈺氣晲鈺濃暁鈺愨晲鈺愨晲鈺愨晲鈺濃暁鈺愨暆  鈺氣晲鈺濃暁鈺愨晲鈺愨晲鈺愨晲鈺 鈺氣晲鈺愨晲鈺愨晲鈺 鈺氣晲鈺濃暁鈺愨暆     鈺氣晲鈺愨晲鈺愨晲鈺
  460. //
  461. //  鈻堚枅鈻堚枅鈻堚枅鈺 鈻堚枅鈻堚枅鈻堚枅鈺  鈻堚枅鈻堚枅鈻堚枅鈺 鈻堚枅鈻堚晽   鈻堚枅鈻堚晽鈻堚枅鈺椻枅鈻堚枅鈻堚枅鈻堚枅鈺椻枅鈻堚枅鈻堚枅鈻堚枅鈺
  462. //  鈻堚枅鈺斺晲鈺愨枅鈻堚晽鈻堚枅鈺斺晲鈺愨枅鈻堚晽鈻堚枅鈺斺晲鈺愨晲鈻堚枅鈺椻枅鈻堚枅鈻堚晽 鈻堚枅鈻堚枅鈺戔枅鈻堚晳鈻堚枅鈺斺晲鈺愨晲鈺愨暆鈻堚枅鈺斺晲鈺愨晲鈺愨暆
  463. //  鈻堚枅鈻堚枅鈻堚枅鈺斺暆鈻堚枅鈻堚枅鈻堚枅鈺斺暆鈻堚枅鈺   鈻堚枅鈺戔枅鈻堚晹鈻堚枅鈻堚枅鈺斺枅鈻堚晳鈻堚枅鈺戔枅鈻堚枅鈻堚枅鈻堚枅鈺椻枅鈻堚枅鈻堚枅鈺
  464. //  鈻堚枅鈺斺晲鈺愨晲鈺 鈻堚枅鈺斺晲鈺愨枅鈻堚晽鈻堚枅鈺   鈻堚枅鈺戔枅鈻堚晳鈺氣枅鈻堚晹鈺濃枅鈻堚晳鈻堚枅鈺戔暁鈺愨晲鈺愨晲鈻堚枅鈺戔枅鈻堚晹鈺愨晲鈺
  465. //  鈻堚枅鈺     鈻堚枅鈺  鈻堚枅鈺戔暁鈻堚枅鈻堚枅鈻堚枅鈺斺暆鈻堚枅鈺 鈺氣晲鈺 鈻堚枅鈺戔枅鈻堚晳鈻堚枅鈻堚枅鈻堚枅鈻堚晳鈻堚枅鈻堚枅鈻堚枅鈻堚晽
  466. //  鈺氣晲鈺     鈺氣晲鈺  鈺氣晲鈺 鈺氣晲鈺愨晲鈺愨晲鈺 鈺氣晲鈺     鈺氣晲鈺濃暁鈺愨暆鈺氣晲鈺愨晲鈺愨晲鈺愨暆鈺氣晲鈺愨晲鈺愨晲鈺愨暆
  467. //
  468. //  鈻堚枅鈻堚晽   鈻堚枅鈻堚晽鈻堚枅鈻堚枅鈻堚枅鈻堚晽鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈺椻枅鈻堚晽  鈻堚枅鈺 鈻堚枅鈻堚枅鈻堚枅鈺 鈻堚枅鈻堚枅鈻堚枅鈺 鈻堚枅鈻堚枅鈻堚枅鈻堚晽
  469. //  鈻堚枅鈻堚枅鈺 鈻堚枅鈻堚枅鈺戔枅鈻堚晹鈺愨晲鈺愨晲鈺濃暁鈺愨晲鈻堚枅鈺斺晲鈺愨暆鈻堚枅鈺  鈻堚枅鈺戔枅鈻堚晹鈺愨晲鈺愨枅鈻堚晽鈻堚枅鈺斺晲鈺愨枅鈻堚晽鈻堚枅鈺斺晲鈺愨晲鈺愨暆
  470. //  鈻堚枅鈺斺枅鈻堚枅鈻堚晹鈻堚枅鈺戔枅鈻堚枅鈻堚枅鈺     鈻堚枅鈺   鈻堚枅鈻堚枅鈻堚枅鈻堚晳鈻堚枅鈺   鈻堚枅鈺戔枅鈻堚晳  鈻堚枅鈺戔枅鈻堚枅鈻堚枅鈻堚枅鈺
  471. //  鈻堚枅鈺戔暁鈻堚枅鈺斺暆鈻堚枅鈺戔枅鈻堚晹鈺愨晲鈺     鈻堚枅鈺   鈻堚枅鈺斺晲鈺愨枅鈻堚晳鈻堚枅鈺   鈻堚枅鈺戔枅鈻堚晳  鈻堚枅鈺戔暁鈺愨晲鈺愨晲鈻堚枅鈺
  472. //  鈻堚枅鈺 鈺氣晲鈺 鈻堚枅鈺戔枅鈻堚枅鈻堚枅鈻堚枅鈺   鈻堚枅鈺   鈻堚枅鈺  鈻堚枅鈺戔暁鈻堚枅鈻堚枅鈻堚枅鈺斺暆鈻堚枅鈻堚枅鈻堚枅鈺斺暆鈻堚枅鈻堚枅鈻堚枅鈻堚晳
  473. //  鈺氣晲鈺     鈺氣晲鈺濃暁鈺愨晲鈺愨晲鈺愨晲鈺   鈺氣晲鈺   鈺氣晲鈺  鈺氣晲鈺 鈺氣晲鈺愨晲鈺愨晲鈺 鈺氣晲鈺愨晲鈺愨晲鈺 鈺氣晲鈺愨晲鈺愨晲鈺愨暆
  474. //
  475.  
  476. /**
  477.  * Execute a Query using the method passed into the
  478.  * constuctor.
  479.  *
  480.  * @param {Function} callback
  481.  * @return callback with parameters (err, results)
  482.  */
  483.  
  484. Deferred.prototype.exec = function(cb) {
  485.   if (_.isUndefined(cb)) {
  486.     console.log(
  487.       'Error: No callback supplied. Please define a callback function when executing a query.  '+
  488.       'See http://sailsjs.com/docs/reference/waterline-orm/queries/exec for help.'
  489.     );
  490.     return;
  491.   }
  492.  
  493.   var isValidCb = _.isFunction(cb) || (_.isObject(cb) && !_.isArray(cb));
  494.   if (!isValidCb) {
  495.     console.log(
  496.       'Error: Sorry, `.exec()` doesn\'t know how to handle a callback like that:\n'+
  497.       util.inspect(cb, {depth: 1})+'\n'+
  498.       'Instead, please provide a callback function when executing a query.  '+
  499.       'See http://sailsjs.com/docs/reference/waterline-orm/queries/exec for help.'
  500.     );
  501.     return;
  502.   }
  503.  
  504.   // Otherwise, the provided callback function is pretty cool, and all is right and well.
  505.  
  506.   // Normalize callback/switchback
  507.   cb = normalizeCallback(cb);
  508.  
  509.   // Build up the arguments based on the method
  510.   var args;
  511.   var query = this._wlQueryInfo;
  512.  
  513.   // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  514.   // FUTURE: Rely on something like an `._isExecuting` flag here and just call
  515.   // the underlying model method with no arguments. (i.e. this way, the variadic
  516.   // stuff won't have to be quite as complex, and it will be less brittle when
  517.   // changed)
  518.   // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  519.  
  520.   // Deterine what arguments to send based on the method
  521.   switch (query.method) {
  522.  
  523.     case 'find':
  524.     case 'findOne':
  525.       args = [query.criteria, query.populates || {}, cb, this._meta];
  526.       break;
  527.  
  528.     case 'stream':
  529.       args = [query.criteria, {
  530.         eachRecordFn: query.eachRecordFn,
  531.         eachBatchFn: query.eachBatchFn,
  532.         populates: query.populates
  533.       }, cb, this._meta];
  534.       break;
  535.  
  536.     case 'avg':
  537.     case 'sum':
  538.       args = [query.numericAttrName, query.criteria, cb, this._meta];
  539.       break;
  540.  
  541.     case 'count':
  542.       args = [query.criteria, cb, this._meta];
  543.       break;
  544.  
  545.     case 'findOrCreate':
  546.       args = [query.criteria, query.newRecord, cb, this._meta];
  547.       break;
  548.  
  549.     case 'create':
  550.       args = [query.newRecord, cb, this._meta];
  551.       break;
  552.  
  553.     case 'createEach':
  554.       args = [query.newRecords, cb, this._meta];
  555.       break;
  556.  
  557.     case 'update':
  558.       args = [query.criteria, query.valuesToSet, cb, this._meta];
  559.       break;
  560.  
  561.     case 'destroy':
  562.       args = [query.criteria, cb, this._meta];
  563.       break;
  564.  
  565.  
  566.     case 'addToCollection':
  567.     case 'removeFromCollection':
  568.     case 'replaceCollection':
  569.       args = [query.targetRecordIds, query.collectionAttrName, query.associatedIds, cb, this._meta];
  570.       break;
  571.  
  572.     default:
  573.       throw new Error('Cannot .exec() unrecognized query method: `'+query.method+'`');
  574.   }
  575.  
  576.   // Pass control back to the method with the appropriate arguments.
  577.   this._method.apply(this._context, args);
  578. };
  579.  
  580. /**
  581.  * Executes a Query, and returns a promise
  582.  */
  583.  
  584. Deferred.prototype.toPromise = function() {
  585.   if (!this._deferred) {
  586.     this._deferred = Promise.promisify(this.exec).bind(this)();
  587.   }
  588.   return this._deferred;
  589. };
  590.  
  591. /**
  592.  * Executes a Query, and returns a promise that applies cb/ec to the
  593.  * result/error.
  594.  */
  595.  
  596. Deferred.prototype.then = function(cb, ec) {
  597.   return this.toPromise().then(cb, ec);
  598. };
  599.  
  600. /**
  601.  * Applies results to function fn.apply, and returns a promise
  602.  */
  603.  
  604. Deferred.prototype.spread = function(cb) {
  605.   return this.toPromise().spread(cb);
  606. };
  607.  
  608. /**
  609.  * returns a promise and gets resolved with error
  610.  */
  611.  
  612. Deferred.prototype.catch = function(cb) {
  613.   return this.toPromise().catch(cb);
  614. };
  615.  
  616.  
  617.  
  618.  
  619.  
  620.  
  621.  
  622. //  鈻堚枅鈺   鈻堚枅鈺椻枅鈻堚枅鈺   鈻堚枅鈺椻枅鈻堚枅鈻堚枅鈻堚枅鈺椻枅鈻堚晽   鈻堚枅鈺椻枅鈻堚枅鈻堚枅鈻堚晽 鈻堚枅鈻堚枅鈻堚枅鈺  鈻堚枅鈻堚枅鈻堚枅鈺 鈻堚枅鈻堚枅鈻堚枅鈺 鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈺椻枅鈻堚枅鈻堚枅鈻堚枅鈺椻枅鈻堚枅鈻堚枅鈻堚晽
  623. //  鈻堚枅鈺   鈻堚枅鈺戔枅鈻堚枅鈻堚晽  鈻堚枅鈺戔枅鈻堚晹鈺愨晲鈺愨晲鈺濃枅鈻堚晳   鈻堚枅鈺戔枅鈻堚晹鈺愨晲鈻堚枅鈺椻枅鈻堚晹鈺愨晲鈻堚枅鈺椻枅鈻堚晹鈺愨晲鈺愨枅鈻堚晽鈻堚枅鈺斺晲鈺愨枅鈻堚晽鈺氣晲鈺愨枅鈻堚晹鈺愨晲鈺濃枅鈻堚晹鈺愨晲鈺愨晲鈺濃枅鈻堚晹鈺愨晲鈻堚枅鈺
  624. //  鈻堚枅鈺   鈻堚枅鈺戔枅鈻堚晹鈻堚枅鈺 鈻堚枅鈺戔枅鈻堚枅鈻堚枅鈻堚枅鈺椻枅鈻堚晳   鈻堚枅鈺戔枅鈻堚枅鈻堚枅鈻堚晹鈺濃枅鈻堚枅鈻堚枅鈻堚晹鈺濃枅鈻堚晳   鈻堚枅鈺戔枅鈻堚枅鈻堚枅鈻堚晹鈺   鈻堚枅鈺   鈻堚枅鈻堚枅鈻堚晽  鈻堚枅鈺  鈻堚枅鈺
  625. //  鈻堚枅鈺   鈻堚枅鈺戔枅鈻堚晳鈺氣枅鈻堚晽鈻堚枅鈺戔暁鈺愨晲鈺愨晲鈻堚枅鈺戔枅鈻堚晳   鈻堚枅鈺戔枅鈻堚晹鈺愨晲鈺愨暆 鈻堚枅鈺斺晲鈺愨晲鈺 鈻堚枅鈺   鈻堚枅鈺戔枅鈻堚晹鈺愨晲鈻堚枅鈺   鈻堚枅鈺   鈻堚枅鈺斺晲鈺愨暆  鈻堚枅鈺  鈻堚枅鈺
  626. //  鈺氣枅鈻堚枅鈻堚枅鈻堚晹鈺濃枅鈻堚晳 鈺氣枅鈻堚枅鈻堚晳鈻堚枅鈻堚枅鈻堚枅鈻堚晳鈺氣枅鈻堚枅鈻堚枅鈻堚晹鈺濃枅鈻堚晳     鈻堚枅鈺     鈺氣枅鈻堚枅鈻堚枅鈻堚晹鈺濃枅鈻堚晳  鈻堚枅鈺   鈻堚枅鈺   鈻堚枅鈻堚枅鈻堚枅鈻堚晽鈻堚枅鈻堚枅鈻堚枅鈺斺暆
  627. //   鈺氣晲鈺愨晲鈺愨晲鈺 鈺氣晲鈺  鈺氣晲鈺愨晲鈺濃暁鈺愨晲鈺愨晲鈺愨晲鈺 鈺氣晲鈺愨晲鈺愨晲鈺 鈺氣晲鈺     鈺氣晲鈺      鈺氣晲鈺愨晲鈺愨晲鈺 鈺氣晲鈺  鈺氣晲鈺   鈺氣晲鈺   鈺氣晲鈺愨晲鈺愨晲鈺愨暆鈺氣晲鈺愨晲鈺愨晲鈺
  628. //
  629. //  鈻堚枅鈻堚晽   鈻堚枅鈻堚晽鈻堚枅鈻堚枅鈻堚枅鈻堚晽鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈺椻枅鈻堚晽  鈻堚枅鈺 鈻堚枅鈻堚枅鈻堚枅鈺 鈻堚枅鈻堚枅鈻堚枅鈺 鈻堚枅鈻堚枅鈻堚枅鈻堚晽
  630. //  鈻堚枅鈻堚枅鈺 鈻堚枅鈻堚枅鈺戔枅鈻堚晹鈺愨晲鈺愨晲鈺濃暁鈺愨晲鈻堚枅鈺斺晲鈺愨暆鈻堚枅鈺  鈻堚枅鈺戔枅鈻堚晹鈺愨晲鈺愨枅鈻堚晽鈻堚枅鈺斺晲鈺愨枅鈻堚晽鈻堚枅鈺斺晲鈺愨晲鈺愨暆
  631. //  鈻堚枅鈺斺枅鈻堚枅鈻堚晹鈻堚枅鈺戔枅鈻堚枅鈻堚枅鈺     鈻堚枅鈺   鈻堚枅鈻堚枅鈻堚枅鈻堚晳鈻堚枅鈺   鈻堚枅鈺戔枅鈻堚晳  鈻堚枅鈺戔枅鈻堚枅鈻堚枅鈻堚枅鈺
  632. //  鈻堚枅鈺戔暁鈻堚枅鈺斺暆鈻堚枅鈺戔枅鈻堚晹鈺愨晲鈺     鈻堚枅鈺   鈻堚枅鈺斺晲鈺愨枅鈻堚晳鈻堚枅鈺   鈻堚枅鈺戔枅鈻堚晳  鈻堚枅鈺戔暁鈺愨晲鈺愨晲鈻堚枅鈺
  633. //  鈻堚枅鈺 鈺氣晲鈺 鈻堚枅鈺戔枅鈻堚枅鈻堚枅鈻堚枅鈺   鈻堚枅鈺   鈻堚枅鈺  鈻堚枅鈺戔暁鈻堚枅鈻堚枅鈻堚枅鈺斺暆鈻堚枅鈻堚枅鈻堚枅鈺斺暆鈻堚枅鈻堚枅鈻堚枅鈻堚晳
  634. //  鈺氣晲鈺     鈺氣晲鈺濃暁鈺愨晲鈺愨晲鈺愨晲鈺   鈺氣晲鈺   鈺氣晲鈺  鈺氣晲鈺 鈺氣晲鈺愨晲鈺愨晲鈺 鈺氣晲鈺愨晲鈺愨晲鈺 鈺氣晲鈺愨晲鈺愨晲鈺愨暆
  635. //
  636.  
  637. /**
  638.  * Add the (NO LONGER SUPPORTED) `sum` clause to the criteria.
  639.  *
  640.  * > This is allowed through purposely, in order to trigger
  641.  * > the proper query error in FS2Q.
  642.  *
  643.  * @returns {Query}
  644.  */
  645. Deferred.prototype.sum = function() {
  646.   this._wlQueryInfo.sum = arguments[0];
  647.   return this;
  648. };
  649.  
  650. /**
  651.  * Add the (NO LONGER SUPPORTED) `avg` clause to the criteria.
  652.  *
  653.  * > This is allowed through purposely, in order to trigger
  654.  * > the proper query error in FS2Q.
  655.  *
  656.  * @returns {Query}
  657.  */
  658. Deferred.prototype.avg = function() {
  659.   this._wlQueryInfo.avg = arguments[0];
  660.   return this;
  661. };
  662.  
  663.  
  664. /**
  665.  * Add the (NO LONGER SUPPORTED) `min` clause to the criteria.
  666.  *
  667.  * > This is allowed through purposely, in order to trigger
  668.  * > the proper query error in FS2Q.
  669.  *
  670.  * @returns {Query}
  671.  */
  672. Deferred.prototype.min = function() {
  673.   this._wlQueryInfo.min = arguments[0];
  674.   return this;
  675. };
  676.  
  677. /**
  678.  * Add the (NO LONGER SUPPORTED) `max` clause to the criteria.
  679.  *
  680.  * > This is allowed through purposely, in order to trigger
  681.  * > the proper query error in FS2Q.
  682.  *
  683.  * @returns {Query}
  684.  */
  685. Deferred.prototype.max = function() {
  686.   this._wlQueryInfo.max = arguments[0];
  687.   return this;
  688. };
  689.  
  690. /**
  691.  * Add the (NO LONGER SUPPORTED) `groupBy` clause to the criteria.
  692.  *
  693.  * > This is allowed through purposely, in order to trigger
  694.  * > the proper query error in FS2Q.
  695.  */
  696. Deferred.prototype.groupBy = function() {
  697.   this._wlQueryInfo.groupBy = arguments[0];
  698.   return this;
  699. };
  700.  
  701.  
downloaddeferred.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