Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make queries compatible with koa and yield keyword #1859

Closed
capaj opened this issue Jan 4, 2014 · 15 comments
Closed

Make queries compatible with koa and yield keyword #1859

capaj opened this issue Jan 4, 2014 · 15 comments
Labels
enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature

Comments

@capaj
Copy link

capaj commented Jan 4, 2014

According to this article it is not possible to use mongoose with yield keyword to get rid of callbacks. Could this be addressed in Mongoose 4.0.0?

@aheckmann
Copy link
Collaborator

Haven't tried it but I'm guessing that the following works:

yield Model.find(..).exec()

Can you confirm?

@bernhardw
Copy link

I can confirm that it works, at least for find() and create(). But it should work for all methods that return a promise as well.
@capaj You can check the API to see what methods return a promise.

@flockonus
Copy link

what about yield document.save() ?

Maybe I'm missing something, but to make it work i need to do:

var thunk = require('thunkify')
// ...
document.save = thunk(document.save)
document = yield document.save()

@bernhardw
Copy link

@flockonus save() doesn't return a promise yet, but it will in 3.10. See #1431

@bodokaiser
Copy link

The current most "native" workaround:

model.js

var thunkify = require('thunkify');

schema.method('persist', function() {
  return thunkify(this.save.bind(this));
});

routes.js

app.use(function *(next) {
  this.status = 200;

  yield model.persist()();
});

@paglias
Copy link
Contributor

paglias commented May 22, 2014

@bodokaiser did you get your code working? I get an error:Error: yield a function, promise, generator, array, or object

@paglias
Copy link
Contributor

paglias commented May 22, 2014

@bodokaiser no error but it looks like yield is never resolved

@bodokaiser
Copy link

@paglias yeah checkout #1812 (comment)

@paglias
Copy link
Contributor

paglias commented May 22, 2014

@bodokaiser thanks, actually I wasn't running next() in a middleware... sorry

@bodokaiser
Copy link

yeah generators are still somehow experimental. I also got some cases where I get undefined thrown as error and no idea where it comes from (though not from l´koa)

@paglias
Copy link
Contributor

paglias commented May 22, 2014

@bodokaiser it was my fault, totally :) Anyway I'm using

  schema.method('persist', thunkify(function(){
    return this.save.apply(this, arguments);
  }));

To avoid having to call it twice: persist() instead of persist()()

@bodokaiser
Copy link

If you use my plugin as linked than you also do not have to use it twice + no dependences + workable on all models :)

module.exports = function(schema) {
  schema.method('persist', function(body) {
    var model = this;

    return function(callback) {
      model.set(body)
      model.save(callback);
    }
  });
  schema.method('destroy', function() {
    var model = this;

    return function(callback) {
      model.remove(callback);
    }
  });
};

use with mongoose.use(require('./plugin'));

@iliakan
Copy link

iliakan commented Jun 28, 2014

@bodokaiser You have a typo in your last code.
mongoose.use(require('./plugin')); should be mongoose.plugin(require('./plugin'));

Thanks will try that.

@miickel
Copy link

miickel commented Jul 30, 2014

model.create() returns a promise, so it is possible to use that as an alternative as well.

@neverfox
Copy link

Since thunks are on the way out of co, you can always promisify save with e.g. bluebird:

yield Promise.promisify(model.save, model)();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature
Projects
None yet
Development

No branches or pull requests

10 participants