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
- Invalid document structure: missing requ...
I just pick up my code that's working a week ago, and now I got this error: ``` code: 400, type: 'document_invalid_structure', response: { message: 'Inv...
- custom domain with CloudFlare
Hi all, it seems that CloudFlare has blocked cross-domain CNAME link which made my app hostname which is in CloudFlare, unable to create a CNAME pointing to clo...
- Type Mismatch in AppwriteException
There is a discrepancy in the TypeScript type definitions for AppwriteException. The response property is defined as a string in the type definitions, but in pr...