find.fixture.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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.fixture.js
  10. *
  11. * A simplified mock of Waterline's `find()` model method.
  12. *
  13. * @param {Dictionary?} criteria
  14. * @param {Function} explicitCb
  15. *
  16. * @returns {Deferred} If no callback specified
  17. */
  18. module.exports = function find( /* variadic */ ){
  19. var metadata = {};
  20. var explicitCb;
  21. // Handle variadic usage:
  22. // ===========================================================================
  23. if (!_.isUndefined(arguments[0])) {
  24. if (_.isFunction(arguments[0])) {
  25. explicitCb = arguments[0];
  26. }
  27. else {
  28. metadata.criteria = arguments[0];
  29. }
  30. }//>-
  31. if (!_.isUndefined(arguments[1])) {
  32. explicitCb = arguments[1];
  33. }//>-
  34. // ===========================================================================
  35. // This deferred may or may not actually need to get built.
  36. // (but in case it does, we define it out here so we can unambiguously
  37. // return it below)
  38. //
  39. // > If an explicit callback was specified, then go ahead
  40. // > and proceed to where the real action is at.
  41. // > Otherwise, no callback was specified explicitly,
  42. // > so we'll build and return a Deferred instead.
  43. var deferred = parley(function (deferredCb){
  44. helpFind(undefined, metadata, deferredCb);
  45. }, explicitCb);
  46. // If we ended up building a Deferred above, we would have done so synchronously.
  47. // In other words, if there's going to be a Deferred, we have it here.
  48. //
  49. // So if we DON'T have a Deferred, then we know that we must have already went ahead
  50. // and performed our business logic. So we'll just return undefined.
  51. if (!deferred) {
  52. return;
  53. }//-•
  54. // IWMIH, then we know we have a Deferred.
  55. // (and thus we haven't actually done anything yet.)
  56. // At this point, we might opt to attach some methods to our Deferred.
  57. _.extend(deferred, {
  58. where: function(clause) {
  59. metadata.criteria = metadata.criteria || {};
  60. metadata.criteria.where = clause;
  61. return deferred;
  62. }
  63. });
  64. // When we're confident that our Deferred is ready for primetime,
  65. // we finish up by returning it.
  66. return deferred;
  67. };