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
- execution failed
When executing an appwrite function, I'm getting a 500 error, but I don't see it in executions. This issue appeared today. Here's the appwrite function ID: 68b4...
- Internal server Error when trying to exe...
When executing the function locally it works fine, but when the function is deployed I get this error: ```requests.exceptions.HTTPError: 500 Server Error: Inter...
- Deploying Function fails after several d...
I get the below issue on deploying to self hosted appwrite 1.8.0 functions, restarting the docker compose, or waiting a while, fixes it but is annoying. Any ide...