superfeedr / indexeddb-backbonejs-adapter

An indexedDB adapter for Backbonejs
http://blog.superfeedr.com/indexeddb-backbonejs-adapter/
MIT License
248 stars 61 forks source link

Compound indices #47

Closed amiuhle closed 10 years ago

amiuhle commented 10 years ago

Added support for compound indices when fetching models. The index that matches most properties is chosen:

store.createIndex("titleIndex", "title", { unique: false });
store.createIndex("formatIndex", "format", { unique: false });
store.createIndex("titleAndFormat", ["title", "format"], { unique: false });

new Movie({ title: 'foo'}).fetch() will use titleIndex, whereas new Movie({ title: 'foo', format: 'bar'}) will use titleAndFormat. If there is no titleAndFormat index, but you supply both properties, the behavior is unchanged, eg the first matching index is chosen. If you supply additional properties, for example new Movie({ title: 'foo', format: 'bar', 'rating': 4.5 }), titleAndFormat will be chosen because it matches more properties than either titleIndex or formatIndex.

I also added an option to supply index information in the options:

var movie = new Movie();
movie.fetch({
  index: {
    name: 'titleAndFormat',
    value: ['Django Unchained', 'blueray']
  }
});
julien51 commented 10 years ago

Thanks a lot!

amiuhle commented 10 years ago

No problem. I might be doing the same thing for Collections, depending on whether I need it. Thanks for the quick merge! :)