سؤال

How are you all handling many-to-many relationships in IndexedDB?

For example, say I have a Blog object to hold a blog post and a Tag object for a tag/label of the blog post. One Blog can have many Tags and one Tag can be used by many Blogs.

I would create a blog store and tag store (though I'm open to suggestions) to house the two types of objects:

// ...
var blogStore = db.createObjectStore("blog", {keyPath: "blogId", autoIncrement: true});
blogStore.createIndex("title", "title", {unique: true});
var tagStore = db.createObjectStore("tag", {keyPath: "tagId", autoIncrement: true});
tagStore.createIndex("label", "label", {unique: true});

Off hand I can think of two ways to link the two:

  1. have a Blog.tags which would be an array of BlogTag objects which holds blogId and tagId (and would also be in the store for retrieval) or
  2. have a Blog.tags which would be an array of tagIds that could be used to look up the Tags.

The first way seems longer-winded but is how this would be tackled in SQL. Is that just SQL-baggage that I should leave behind?

I suppose a 3rd way would be to have Blog.tags be an array of Tags. This seems simplest but then I couldn't query for Tags or reuse tags across blogs (or could I?).

Has anyone else handled such a situation with indexedDB? If so, what did you end up doing? What were some pitfalls?

لا يوجد حل صحيح

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top