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
- Having issues with Goggle Authentication...
My google auth is not redirecting me to my failureurl. I think it might be a session issue because it's working in my laptop but when I try it in another laptop...
- I'm getting error Invalid `url` param: I...
``` 2025-10-26T12:52:02.292Z [error] AppwriteException: Invalid `url` param: Invalid URI. Register your new client (vercel.com) as a new Web platform on your pr...
- Unable to create records with other user...
are we able to create records in collections with permissions of different user than the caller of this request? (with document security on) I have backend func...