find-but-with-timeout.fixture.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /**
  2. * Module dependencies
  3. */
  4. var _ = require('@sailshq/lodash');
  5. var flaverr = require('flaverr');
  6. var parley = require('../../');
  7. var helpFind = require('./private/help-find.util');
  8. /**
  9. * find-but-with-timeout.fixture.js
  10. *
  11. * A simplified mock of Waterline's `find()` model method -- but with an extremely short timeout.
  12. *
  13. * > See `find.fixture.js` for more info. Many comments were removed from the code below
  14. * > to avoid unnecessary duplication and reduce the chances of things getting weird.
  15. *
  16. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  17. * @param {Dictionary?} criteria
  18. * @param {Function} explicitCb
  19. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  20. * @returns {Deferred} If no callback specified
  21. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  22. */
  23. module.exports = function findButWithTimeout( /* variadic */ ){
  24. var metadata = {};
  25. var explicitCb;
  26. // Handle variadic usage:
  27. // ===========================================================================
  28. if (!_.isUndefined(arguments[0])) {
  29. if (_.isFunction(arguments[0])) {
  30. explicitCb = arguments[0];
  31. }
  32. else {
  33. metadata.criteria = arguments[0];
  34. }
  35. }//>-
  36. if (!_.isUndefined(arguments[1])) {
  37. explicitCb = arguments[1];
  38. }//>-
  39. // ===========================================================================
  40. return parley(function (done){
  41. helpFind(undefined, metadata, done);
  42. }, explicitCb, {
  43. where: function(clause) {
  44. metadata.criteria = metadata.criteria || {};
  45. metadata.criteria.where = clause;
  46. return this;
  47. }
  48. }, 2);
  49. };