Back

How to limit amount of documents in a collection?

  • 0
  • Self Hosted
  • Databases
  • Web
BloodThermic
18 May, 2023, 01:55

This there a way to limit the amount of documents in a collection?

I am trying to make a scheduling system. So I need when 20 people sign up to a date the box with disappear or gray out. And I was planning on doing that with collections and documents

TL;DR
To limit the number of documents in a collection, you can create a function that triggers whenever a document is added. Once the number of documents reaches a certain limit (e.g., 20), you can update the collection permissions to prevent further document creation. This can be achieved using the `updateCollection` function provided by the appwrite.io API. Additionally, you can list the documents each time someone views the page and change the appearance (e.g., grayed out) if the document count exceeds the limit.
Binyamin
18 May, 2023, 01:57

I think your best option would be to create a function that will trigger each time someone is adding a document to a collection. Then when the number of documents reach to your limit let's say 20 then you should update the collection permission so no one will have permission to create any new documents.

Binyamin
18 May, 2023, 01:57

You can run the collection update using the updateCollection function https://appwrite.io/docs/server/databases?sdk=nodejs-default#databasesUpdateCollection

BloodThermic
18 May, 2023, 02:03

could I do a thing were I would list the documents everytime someone views the page. it checks the number of documents in a collection then changes it to grayed out if it is 20?

Binyamin
18 May, 2023, 02:05

You can

But doing so will suffer from a few problems

  • You calling all the documents each time, that can a burden on the server
  • As long as your logic is only in the client side, then you won't be able to block it, so in regular use case your solution will work but if a user will send a request behind the scene then you won't be able to stop it.

Although you can mix this two approaches together, so blocking from the server side (function) and give the user feedback in the client side

BloodThermic
18 May, 2023, 04:05

I am doing both server and client side

BloodThermic
18 May, 2023, 04:07

How would you get the number of documents?

Drake
18 May, 2023, 17:38

Best to keep a count, but in 1.3 you can set a high limit when making a list document API call

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