find-but-with-final-after-exec-lc.fixture.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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-final-after-exec-lc.fixture.js
  10. *
  11. * A simplified/fake mock of Waterline's `find()` model method --
  12. * but using a lifecycle callback to do some weird stuff.
  13. *
  14. * > See `find.fixture.js` for more info. Many comments were removed from the code below
  15. * > to avoid unnecessary duplication and reduce the chances of things getting weird.
  16. *
  17. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  18. * @param {Dictionary?} criteria
  19. * @param {Function} explicitCb
  20. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  21. * @returns {Deferred} If no callback specified
  22. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  23. */
  24. module.exports = function findButWithFinalAfterExecLC( /* variadic */ ){
  25. var metadata = {};
  26. var explicitCb;
  27. // Handle variadic usage:
  28. // ===========================================================================
  29. if (!_.isUndefined(arguments[0])) {
  30. if (_.isFunction(arguments[0])) {
  31. explicitCb = arguments[0];
  32. }
  33. else {
  34. metadata.criteria = arguments[0];
  35. }
  36. }//>-
  37. if (!_.isUndefined(arguments[1])) {
  38. explicitCb = arguments[1];
  39. }//>-
  40. // ===========================================================================
  41. return parley(function (done){
  42. helpFind(undefined, metadata, done);
  43. }, explicitCb, {
  44. where: function(clause) {
  45. metadata.criteria = metadata.criteria || {};
  46. metadata.criteria.where = clause;
  47. return this;
  48. }
  49. }, undefined, undefined, function (err, result){
  50. // console.log('* * * * running intercept');
  51. if (err) {
  52. if (err.code !== 'E_SOME_ERROR') {
  53. err = flaverr('E_SOME_UNRECOGNIZED_ERROR', new Error(err.message));
  54. // console.log('* * MUTATED ERROR!');
  55. return err;
  56. }//-•
  57. return err;
  58. }//-•
  59. // Unless criteria is `true`, simulate a case where we'd want to change the result.
  60. // > Note that we could mutate result and return that, or just return the result.
  61. // > It shouldn't matter! Same thing for the error above.
  62. if (metadata.criteria !== true) {
  63. result.push({ fake: true });
  64. // console.log('* * MUTATED OUTPUT!');
  65. return result;
  66. }//-•
  67. return result;
  68. });
  69. };