
I have a Collection with a list of items and our stock level for them. When someone makes an order, I need to decrement stock, and when new stock is added, I need to increment stock.
The simplest way would be to getDocument
, then updateDocument
with stock-1, but that would be an issue if there are two orders at the same time.
The only method I can think of to deal with this is:
Create a new record in a collection order_updates
.
Check order_updates
for any records created before the one I just created.
If there are, wait 0.5 seconds and check again.
Once there are no more records before my new one, get the current stock level and update it.
Delete the order_updates
record.
This seems a bit roundabout, though - does anyone have any better ways to deal with this? TIA!

One way to create a lock is to create a locks collection. A process that wants to obtain a lock will create a document with id == id of the resource. If it can create the document successfully, it has obtained the lock. If any other document tries to obtain a lock, they'll get an error because the document already exists with the same id. When the process that obtained the lock is done, it can delete the lock document to release the lock

Iβm not sure how thatβs really any more efficient than my method (although it is tidier)

Iβd really prefer if there were a way to just directly increment/decrement a number in a collection, but I donβt really expect that to happen

You can π the related feature request: https://github.com/appwrite/appwrite/issues/3905

It's on our roadmap, but I'm not sure when it'll be completed

Oh cool, and itβs got a PR already too!

Eh I ran into complications π

Following

Here might be relevant information: https://discord.com/channels/564160730845151244/1169995985922031728/1170035273837596672
Recommended threads
- Permissions - Design Patterns
Hey Appwriters. I'm keen to tap into your database expertise. In my app I have nested collections: Canvas (think of it like Trello) Cards (just like Trello ca...
- Reset/Recalculate Dashboard Values
Is there a way to reset/recalc this value on the dash? I had issues importing, and after my second import this was how it ended, I've since added some documents...
- Sites_: 500 Server Error when connecting...
I am trying to set up Sites on my self-hosted Appwrite instance. I have a GitHub Organization with a repo I want access to. I had previously installed the appwr...
