
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
- I am facing this error: type 'Null' is ...
When attempting to fetch areas from the area collection, the application throws an error: "type 'Null' is not a subtype of type 'int.'" This issue originates in...
- Adding Domain to Sites [Self Hosted]
I am struggling to get this working. I stood-up a new server and deployed appwrite 1.7.4. I added update .env file _APP_DOMAIN=appwrite.mydomain.com _APP_DOMAI...
- Adding custom domain to Appwrite project
My app is hosted on Vercel on domain todo.velleb.com. On PC, the OAuth logins like GitHub, Discord and Google work and create the account and log in. On mobile ...
