dfahlander / dexie-relationships

Dexie relationship plugin
MIT License
89 stars 17 forks source link

Does not work for multiple indexes #21

Open agkovalev opened 7 years ago

agkovalev commented 7 years ago

Hello! Thank you very much for this helpful and useful addon and for the Dexie! It would be even better, if it would working with multiple indexes. For example "*groupsIds -> group.id"

May be I have some problem with architecture of my app's DB, but I need that for example one item could be a member of 2 or more groups. (user is admin and buyer at same time, or MacBook is notebook and PC and topseller at the same time).

getForeignKeys() method doesn't cut off prefixes of fields before searching by index. But may be the problem is some deeper in core...

Thanks

dfahlander commented 7 years ago

Thanks for the request. Indeed support for many-to-many would be a nice feature. Part of the problem is how it parses indexes, but I believe that wouldn't solve it all. Anyone who have the time to fix this, please send a PR.

douglasg14b commented 6 years ago

@dfahlander

I'm interested in this, could you provide a bit of info or references to how current multi index columns are handled?

dfahlander commented 6 years ago

multiEntry indexes generally in indexedDB works on array properties rather than ordinary properties in such way that you can put an arbitrary number of keys into the array and a query that matches any of those keys will match. A simple example is a 'tags' based model, where you can tag the same row with multiple tags.

var db = new Dexie ('mydb');
db.version(1).stores ({
  books: 'id, author, name, *categories'
});
db.books.put({
  id: 1,
  name: 'Under the Dome', 
  author: 'Stephen King',
  categories: ['sci-fi', 'thriller']
});

function queryBookByCategory(category) {
  return db.books
    .where('categories').equals(category)
    .toArray ();
} 

If dexie-relationships would support putting foreign keys into multiEntry indexed arrays, we would have support for many-to-many relations without the need of a junction table.