Firestore Data Model: Threaded Comments on Several Pages
2 min read

Here is how I would model a comment thread if you have X amount of posts that might each have separate comment threads.

getComments

getComments(parentID: string, postID: string) {
  return db.collection(`posts/${postID}/comments`)
    .where('parent', '==', parentID)
    .orderBy('createdAt', 'desc');
}

posts/postID/comments

commentDoc - root

{
  text: 'comment contents',
  uid: 293slek2l2s,
  parent: 'root',
  createdAt: serverTimestamp()
}

commentDoc - child

{
  text: 'comment contents',
  uid: 293slek2l2s,
  parent: 'parentID',
  createdAt: serverTimestamp()
}

post component

<comment parent="root', post="54232k21">

comment component

<div class="container tab">
  <if comments=getComments(parent, post)>
    <forEach comments as comment>
      {{ comment.text }}
      user: {{ comment.uid }}
      <comment parent="comment.id", post="54232k21">
    </comment>
  <endIf>
</div>

This HTML code is highly dependent on your framework, but I wanted to give a general idea of how I would model this.

Hopefully this works for you.

I will probably implement it in my Angular Project eventually.

J

firebase
cloud-firestore
data-modeling


Related Posts

7 min read


6 min read



© 2023 Code.Build