Firestore Data Model: Threaded Comments on Several Pages

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** ```typescript getComments(parentID: string, postID: string) { return db.collection(`posts/${postID}/comments`) .where('parent', '==', parentID) .orderBy('createdAt', 'desc'); } ``` `posts/postID/comments` **commentDoc - root** ```typescript { text: 'comment contents', uid: 293slek2l2s, parent: 'root', createdAt: serverTimestamp() } ``` **commentDoc - child** ```typescript { text: 'comment contents', uid: 293slek2l2s, parent: 'parentID', createdAt: serverTimestamp() } ``` **post component** ```html ``` **comment component** ```html
{{ comment.text }} user: {{ comment.uid }}
``` 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](https://dev.to/jdgamble555/a-free-fully-functional-blog-in-angular-and-firebase-e4j) eventually. J

datamodeling
manytomany