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
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?*
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
Pls, can you go deeper in your explanation.
Thank you
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
Thanks
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?
Write a script to count it and store the count. Just make sure you paginate
okay... can i use function to loop through users then accredit the count to those user?
would that work?
Sure
thank you
can i also do this for Likes (liking of Post)?
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
Recommended threads
- The current user is not authorized to pe...
I want to create a document associated with user after log in with OAuth. The user were logged in, but Appwrite said user is unauthorized. User is logged in wi...
- My account is blocked so please check an...
My account is blocked so please unblock my account because all the apps are closed due to which it is causing a lot of problems
- Applying free credits on Github Student ...
So this post is kind of related to my old post where i was charged 15usd by mistake. This happens when you are trying to apply free credits you got from somewh...