As I understand it appwrite doesn't support transactions. I'm attempting to make a document in three separate collections at the same time when a user registers. If one fails I want all to fail. This is my attempt at that. Is it wrong?
async ({
email,
password,
firstName,
lastName,
buildings,
units,
roles,
}) => {
const { account, databases } = createAdminClient()
const user = await account.create(ID.unique(), email, password)
const userDetails = await databases.createDocument(
import.meta.env.DATABASE_ID,
import.meta.env.USER_DETAILS_ID,
user.$id,
{
firstName: firstName,
lastName: lastName,
},
)
if (!userDetails) {
await account.deleteIdentity(user.$id)
}
const unitUsers = await Promise.all(
buildings.map((building, index) => {
databases.createDocument(
import.meta.env.DATABASE_ID,
import.meta.env.UNIT_USERS_ID,
ID.unique(),
{
unitId: building + units[index],
userId: user.$id,
role: roles[index],
},
)
}),
)
if (!unitUsers) {
await account.deleteIdentity(user.$id)
await databases.deleteDocument(
import.meta.env.DATABASE_ID,
import.meta.env.USER_DETAILS_ID,
userDetails.$id,
)
}
if (user && userDetails && unitUsers) return { registered: true }
},
}),
This looks fine but you might want to add some sort of error handling so when something goes wrong it's easy to find what happened, and you can give a useful message to the user.
OK I'm going to be testing it very soon!
Recommended threads
- Cloud function deploy stucks in processi...
Been trying for the last hours to deploy my function but for whatever reason, alwasy stuck on processing!
- One-time Cloud migration blocked by data...
Hi, I’m blocked on a one-time migration from Appwrite Cloud to my self-hosted Appwrite instance. We already fixed the region issue, and the migration now corre...
- All My Project is Gone
Hello everyone, please help. Why have all my projects suddenly disappeared? I received a warning via email about one of my projects being paused. When I clicked...