geddy / model

Datastore-agnostic ORM in JavaScript
265 stars 55 forks source link

Is there a co wrapper for model? #152

Open marcelklehr opened 10 years ago

marcelklehr commented 10 years ago

Hey,

I was wondering if it's possible to put some sugar on the callback wrinkles using TJ's visionmedia/co. In any case, a module that exposes model's API using promises (which are also yieldable with co) would be great!

mde commented 10 years ago

I assume we're talking about the query API here, yes? I don't object to Promises in principle. Right now in addition to callbacks, a query returns an EventEmitter which allows streaming datasets, so it might be a bit fiddly to overload that with yet a different, Promisey API. But it's definitely possible, and if there's sufficient interest in it, I'd be happy to merge support for it.

der-On commented 9 years ago

@mde so basically we are not able to return Promises, because we already return pseudo streams?

In my apps I started to wrap model into promises, but this is not very elegant, as I cannot do things like:

model.User.first({ email: 'foo@bar.com' })
  .then(function (user) {
    ....
  });

But must do something like:

model.User.firstPromise({ email: 'foo@bar.com' })
  .then(function (user) {
    ....
  });

This is still okay for loading promises, but get's worse when saving stuff:

Instead of just doing:

user.save(user)
  .then(function (user) {
    ....
  });

I need to call a static method to promisely save my data:

model.User.savePromise(user)
  .then(function (user) {
    ....
  });

It should be fairly simple to add load and save methods that return promises, not EventEmitters.

Today everyone expects promises everywhere. I can take on this task, but we first have to find a good convention on how to integrate it.

My current code to "promisify" model is looking like this

On top of that I'm also "improving" the association methods using this

However I need to "improve" the associations in each instance during runtime using afterCreate:

this.afterCreate = function() {
    improveAssocs(this);
};