
how to store auto increment ID in appwrite PK

Yeah, that’s not supported at the moment. You’ll have to check yourself what the last entry is an increment yourself

any recommendation

When you’re creating a new Document, first listDocuments, limit 1, ordered newest first, to get the last record created, and then create your new document with that+1

Although if it’s possible that multiple records will be created at the same time, you’ll have to have a way to handle race conditions

please also suggest the concurrency creation. if you don mind


Hey there 👋 You could create Appwrite Function to implement this logic. You could make getId
function, which would return to you a next available number, starting at 1.
This function could use locking mechanism to ensure 2 concurrent calls dont do DB queries, which prevents giving same number to 2 requests.
For example, you could have collection counter
and in there 1 document with attribute value
set to 1
.
So in theory you could simply get this document, do +1, save document, and return incremented value.
To introduce locking, make collection couterLock
, which could have just 1 boolean attribue locked
. If there is document, couter is locked. If not, not locked. You also need to create document here with custom ID. No matter what ID but you need to make sure it's same always. That way unique index wont let you create 2 locks.
With that in mind, you could do something along lines:
locked = true
# Wait to be unlocked, and lock
while locked
try
get couterLock document
sleep 1 second
catch
# Document not found - not locked
try
create couterLock document
locked = false
catch
# Cannot create lock doc, someone else already made it
sleep 1 second
# Code protected against race condition with concurrency
get counter document
increment value by 1
update counter document
# Unlock
delete couterLock document
Hope this helps 🙌

very handy approach. I am just courious, is the https://github.com/appwrite/appwrite/issues/3905 coming out soon

any sample function of this

I have seem implementation of numeric incremental and decremental for both integers and floats in our underlying database library. I ahve not yet seen any PRs in Appwrite API itself. Most likely it wont be coming in next version release, unless it's last-minute implementation.

I havent written any yet, and didnt see anyome implement it. But I did very similar approach in Appwrite when I needed to ensure locking for OAuth endpoints to get GitHub user information. That makes me confident about the approach.

okay, I will try to check this out.
Recommended threads
- Selfhosting problem
I'm migrating from cloud to self-hosted. I tried using 'Export to self-hosted instance' and use my free Ngrok domain as Endpoint self-hosted instance, but I got...
- How to reduce DB Reads?
I just noticed that I hit the 500k db reads limit on my very small next js app with the most data being present in one collection having around 50 documents. ...
- Getting issue while migrating from Self ...
i try to migrating my project but when do this error come and dont allow to crate that migration
