Bookshelf: collection.fetch() vs model.fetchAll()

Created on 24 Jan 2015  路  4Comments  路  Source: bookshelf/bookshelf

Assuming I have this:

var Ad = base.Model.extend({
  tableName: 'ads'
});

var Ads = base.Collection.extend({
    model: Ad
});

module.exports = {
    Ad: base.model('Ad', Ad),
    Ads: base.collection('Ads', Ads)
};

and I need to return a collection of ads matching certain criteria. I can do it two ways:

// Option 1: via collection
Ads
  .query({
    where: {
      classified_id: query.classId, section_id: query.secId, category_id: query.catId
    }
  })
  .fetch()
  .then(function(results) {
    //  ... ... ...
  });
// Option 2: via a Model and fetchAll()
Ad
  .where({
    classified_id: query.classId, section_id: query.secId, category_id: query.catId
  })
  .fetchAll()
  .then(function(results) {
    //  ... ... ...
  });

Are these both pretty much the same, or is one way preferred than the other?

Most helpful comment

Stick to the latter. We're looking to pull the collections API ASAP. It's too heavy and as you've noticed the shift towards exposing Active Record style methods on models has already started.

That was over a year ago. Whats the situation now?

All 4 comments

Stick to the latter. We're looking to pull the collections API ASAP. It's too heavy and as you've noticed the shift towards exposing Active Record style methods on models has already started.

:+1: I love this shift. Thank you!

Stick to the latter. We're looking to pull the collections API ASAP. It's too heavy and as you've noticed the shift towards exposing Active Record style methods on models has already started.

That was over a year ago. Whats the situation now?

That was over a year ago. Whats the situation now?

I'd like to know that too. Should I still keep using only Model?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MarkHerhold picture MarkHerhold  路  3Comments

XavierGeerinck picture XavierGeerinck  路  3Comments

josdotso picture josdotso  路  3Comments

Oxyrus picture Oxyrus  路  4Comments

cuteboi picture cuteboi  路  4Comments