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 Tag
s and one Tag
can be used by many Blog
s.
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:
- have a
Blog.tags
which would be an array ofBlogTag
objects which holdsblogId
andtagId
(and would also be in the store for retrieval) or - have a
Blog.tags
which would be an array oftagId
s that could be used to look up theTag
s.
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 Tag
s. This seems simplest but then I couldn't query for Tag
s 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?