Home
> Firestore Data Model: Threaded Comments on Several Pages
📧 Subscribe to our Newsletter for course and post updates!

Firestore Data Model: Threaded Comments on Several Pages

2 min read

Jonathan Gamble

jdgamble555 on Friday, December 31, 2021 (last modified on Sunday, December 17, 2023)

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


Related Posts

🔥 Authentication

🔥 Counting Documents

🔥 Data Modeling

🔥 Framework Setup

🔥Under Construction

Newsletter

📧 Get our course and blog post updates!

© 2024 Code.Build