Hello ! Can you tell me if the function I wrote is well optimized or not ? It have to generate Unique ID like "AZ45DF" for documents when inserting. Thank you for your returns.
const payload = JSON.parse(req.variables["APPWRITE_FUNCTION_EVENT_DATA"]);
const {
$id,
$collectionId
} = payload;
if (!payload.hasOwnProperty("id")) return console.error("No ID attribute in this collection");
let uniqueId = "";
while (true) {
for (let i = 0; i < 6; i++) {
const randomIndex = Math.floor(Math.random() * characters.length);
uniqueId += characters[randomIndex];
}
const idQuery = [
sdk.Query.equal("id", uniqueId),
];
const requestIds = await databases.listDocuments(
req.variables["USERS_DATABASE_ID"],
$collectionId,
idQuery
);
const responseIds = requestIds.documents;
if (responseIds.length === 0) {
break;
} else {
uniqueId = ""
}
}
const json = {
id: uniqueId
};
await databases.updateDocument(req.variables["USERS_DATABASE_ID"], $collectionId, $id, json)
res.json({
success: true,
});
}
You need to do this because you need the ID to be 6 chars in length?
Yes !
I'm not sure how this collection is used and where you need the speed...some things to consider:
getDocument()is faster thanlistDocuments()becausegetDocument()uses the cache- I would make sure to have a unqiue index to ensure uniqueness at the database level
It's for all collections that have an ID attribute. I need all users, tickets and reservations to have a 6 characters long ID, so it's easily readable and shareable. But getDocument() need the document Id in parameters, so I can't ? And yes I have added an index on the ID attribute but I don't really understand how indexes work...
It's for all collections that have an ID attribute. I need all users, tickets and reservations to have a 6 characters long ID, so it's easily readable and shareable. But getDocument() need the document Id in parameters, so I can't ?
Keep in mind, you can use a custom 6 character ID for the document id. Then you can use getDocument() on that 6 character ID.
I have added an index on the ID attribute but I don't really understand how indexes work...
They just work behind the scenes. You don't really do anything. They speed up queries and provide restrictions (if it's a unique index)
how can I use a custom 6 characters ID as the document ID ? With createDocument()#documentId parameter ?
yes, you pass ID.custom(yourId) instead of ID.unique()
Oh okay I thought It needed to be formatted like default ID.unique(). Thank you very much. So I can add an index on the documentId ?
there's no need to add an index on the document ID
How do I know if the document was found please ?
In nodeJS
with then() catch() ?
ty
Steven, can I change a document ID in a function ?
I can't handle making 2 requests in my apps just to check if the ID already exists before creating a document
I think I need to do this with a function
No, you can't change a document id
I mean...you can just try to create it...if it throws an error saying it's a duplicate, try again
ty
Recommended threads
- IP / CIDR For Appwrite Cloud Functions
[I saw this message](https://discord.com/channels/564160730845151244/1330335737954046067) that went unanswered. I have the same question: what IP addresses / CI...
- Cloudflare output appearing in function ...
Hello everyone! My function execution failed and it showing Cloudflare output in the execution log. It leaks the IP (DigitalOcean) as well. It's not the first ...
- How to access oldValue payload in functi...
I want to record old and new values of data changes as audits. But when i try to read row in function. The value is new already even before grt updated