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

using with KOA, Generators, Yield #2177

Closed
babnik63 opened this issue Jul 5, 2014 · 14 comments
Closed

using with KOA, Generators, Yield #2177

babnik63 opened this issue Jul 5, 2014 · 14 comments
Milestone

Comments

@babnik63
Copy link

babnik63 commented Jul 5, 2014

Hi,
KOA is spreading fast, but we can not use mongoose with it, because lack of generator support in mongoose, do we have to wait for next versions, is it planned? or any solution?

Thanks

@iliakan
Copy link

iliakan commented Jul 7, 2014

I heard it's planned for 4.0

I'm using mongoose 3.8 with a small plugin to make save/remove work with yield.

@vkarpov15
Copy link
Collaborator

So 3.9, which is unstable, right now has support for yield .save(), .validate(). Once 4.0 comes out that'll be stable :)

@vkarpov15 vkarpov15 added this to the 4.0 milestone Jul 11, 2014
@babnik63
Copy link
Author

Hi,
would you please give me some samples?
Thanks

On Mon, Jul 7, 2014 at 1:48 PM, Ilya Kantor notifications@github.com
wrote:

I heard it's planned for 4.0

I'm using mongoose 3.8 with a small plugin to make save/remove work with
yield.


Reply to this email directly or view it on GitHub
#2177 (comment)
.

Sincerely yours
Babak Niknia
Next Generation Solutions Co.
Chief R&D Manager
www.NEGSO.com

@vkarpov15 vkarpov15 modified the milestones: 4.0, 3.9.2 Aug 18, 2014
@paglias
Copy link
Contributor

paglias commented Sep 1, 2014

@vkarpov15 do you think is/will be possible to use generators in hooks?

@vkarpov15
Copy link
Collaborator

@paglias can you provide an example of what you're looking to do?

@paglias
Copy link
Contributor

paglias commented Sep 3, 2014

@vkarpov15 I think I've read somewhere that you plan to change the hook system in 4.0 so maybe it can be fixed there but now in order to use a generator inside an hook I use something like that:

userSchema.pre('save', function(next){
  co(function *(){
    try{
      yield something here....
      next();
    }catch(err){
      next(err);
    }
  }).call(this);
});

Which isn't really nice. I would like the ability to supply a generator directly to the hook instead of having to call co in order to wrap it but it would probably mean rewriting the whole library so feel free to ignore my comment.

Also not exectly related to this but I've noticed that if I don't add an empty listener to mongoose.connection if a middleware ends with an error mongoose throws it instead of sending it up the app's middlewares where at the top I've my own error handler, maybe I don't remember correctly but that didn't seems to happen with Express and plain callback.

Anyway just adding something like that is enough:

mongoose.connection.on('error', function(){});

Thanks for the help!

@paglias
Copy link
Contributor

paglias commented Sep 3, 2014

@vkarpov15 I think the error propagation is due to:

The fn callback is optional. If no fn is passed and validation fails, the validation error will be emitted on the connection used to create this model.

And using exec() instead of the callback it passes the error to the connection

@vkarpov15
Copy link
Collaborator

@paglias that's a good suggestion, but I don't really foresee that happening in the near future, at least until ES6 is way more widely used than it is today. I don't really see a way to do that without breaking compatibility with 0.10.x.

@xpepermint
Copy link

+1 for koa support ;)...

@vkarpov15 vkarpov15 modified the milestones: 3.9.3, 3.9.2 Sep 8, 2014
@cncolder
Copy link

Maybe bluebird is a simple way for 3.8.

    var Promise = require('bluebird');
    var mongoose = Promise.promisifyAll(require('mongoose'));

    app.use(function * () {
      var user = yield User.findOneAsync();
      this.body = user.email;
    });

@xpepermint
Copy link

Thanks @cncolder.
Btw, I think that the support for generators/yields is a must. I don't know a nodejs developer that wouldn't complain about the slow adoption of that new cool features. Once you try e.g. koajs you simply can't go back and you try to write all your code with yields. The code becomes sexy, elegant, readable like you would write a rails app - I love beautiful code.
There is very little nodejs ODM/ORM modules and I believe that mongoose is one of the winners here thus when mongoose is upgraded to support generators the whole community will benefit - new era.

@cncolder
Copy link

@xpepermint bluebird is only a solution for mongoose 3.8.x

I love new feature too. In fact. I had trying traceur in a small personal project. Write nearly all es6 style code. Even async function in es7.
But traceur has a big runtime. Make server run slower.
I'm playing koa now. Learning koa philosophy from koala code.

mongoose 3.9.2 has browserify bundle-able. Nobody care this new feature?

@paglias
Copy link
Contributor

paglias commented Sep 14, 2014

Using Mongoose 3.9.x you have save, remove returning a Promise and for the queries you just need to add .exec() to have a promise too, like Model.find({query}).exec()

I'm using it in a Koa application and it works very well

@vkarpov15 vkarpov15 modified the milestones: 3.9.4, 3.9.3 Oct 1, 2014
@ngduc
Copy link

ngduc commented Oct 9, 2014

What a coincidence! I've just finished a Mongoose DAO class which support promise (Q):
https://github.com/ngduc/mystik/blob/master/app/lib/mkMongoEngine.js
I'll try koa yield with that one soon.

Updated: with that implementation, now I can do like this:

app.get('/users', function *(next) {
    var users = yield Users.findAll();
    this.body = users;
});

@vkarpov15 vkarpov15 modified the milestones: 3.9.5, 3.9.4 Oct 22, 2014
@vkarpov15 vkarpov15 modified the milestones: 3.9.6, 3.9.5 Nov 10, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants