Back

Document Counter

  • 0
  • Databases
  • Flutter
  • Cloud
Zionnite
14 Mar, 2024, 08:09

Hello, please what's the best way to count document for a Particular user,

USE CASE A user created a post, and i would want to count the number of posts that user has made since he joined the platform but here is my code, i'm not too confident on it, and i would love your ideal about what to do

My Controller

TypeScript
  userPostCounter({required uid}) async {
    final document = await _postApi.userPostCounter(uid: uid);
    final data = document.map((post) => PostModel.fromMap(post.data)).toList();

    var counter = 0;
    if (data.isNotEmpty) {
      counter = data.length;
    }
    return counter;
  }```

**My PostApi**

 ``` @override
  Future<List<Document>> userPostCounter({required String uid}) async {
    DocumentList doc;
    doc = await _db.listDocuments(
      databaseId: Common.dataBasesId,
      collectionId: Common.postCollection,
      queries: [
        Query.equal('quickPosting', true),
        Query.equal('uid', uid),
        Query.isNull('repliedTo'),
      ],
    );

    return doc.documents;
  }```

*Please, i'm i on the right track on this?*
TL;DR
Developers want to track the number of posts made by a user. They can create a document for each user containing the post count, updating it whenever a post is created using an Appwrite function to prevent querying all posts. This method ensures scalability and faster performance. **Solution:** 1. Maintain a count for each user that increments with every new post. 2. Use an Appwrite function to update the count when a new post is created. 3. Avoid counting posts each time as it becomes slower with more data. Regarding the specific code shared: 1. The approach seems correct, where the user's post count is calculated based
Steven
14 Mar, 2024, 08:20

You should maintain a count that increments and decrements because counting every time doesn't scale. It will get slower the more data they have

Zionnite
14 Mar, 2024, 08:33

Pls, can you go deeper in your explanation.

Thank you

WhiteWalker72
14 Mar, 2024, 08:39

You could have a document for the user which contains a number with the amount of posts the user has. Whenever a post is created an Appwrite function could trigger that increases the count for the user. This way you don't have to query the whole posts collection for the user to know how many posts there are

Zionnite
14 Mar, 2024, 08:43

Thanks

Zionnite
14 Mar, 2024, 08:44

But I think I have an issue here, Is there a way I can accredit the already post count to the new document for each user that already have post?

Steven
14 Mar, 2024, 08:47

Write a script to count it and store the count. Just make sure you paginate

Zionnite
14 Mar, 2024, 08:50

okay... can i use function to loop through users then accredit the count to those user?

Zionnite
14 Mar, 2024, 08:50

would that work?

Steven
14 Mar, 2024, 08:55

Sure

Zionnite
14 Mar, 2024, 08:57

thank you

Zionnite
14 Mar, 2024, 08:57

can i also do this for Likes (liking of Post)?

Zionnite
14 Mar, 2024, 08:58

because, what i have before is that i create an array attribute for a document, so when user likes the post, i added it to the post array document

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more