Replies: 1 comment
|
Appwrite database events are post-write events; they are notifications, not transactional before-create hooks. A Function triggered by databases..collections..documents.*.create therefore cannot add required fields before the original create is validated/committed. Use a server-side command boundary instead:
Pseudo-code: export default async ({ req, res, error }) => {
const input = JSON.parse(req.body || '{}')
// Validate req.headers / JWT and input here.
const document = await databases.createDocument(
DATABASE_ID,
COLLECTION_ID,
ID.unique(),
{
...input,
calculatedField: calculate(input),
createdBy: req.headers['x-appwrite-user-id'],
},
)
return res.json(document)
}Do not create first and then patch from an event if the calculated fields are required for validity: clients can observe the intermediate state, the event can fail/retry, and the two writes are not one transaction. If the field is only enrichment and eventual consistency is acceptable, an after-create event plus idempotent update is fine. For required/calculated input, the Function/backend-before-write pattern is the reliable design. If this resolves the architecture question, please mark it as accepted. |
Uh oh!
There was an error while loading. Please reload this page.
Hello everyone,
I recently transitioned from Firebase to AppWrite and I'm trying to find a way to trigger a function before a document is created in a collection. Specifically, I want to add certain properties to a new document before it's actually created in the collection. Is there a mechanism in AppWrite for achieving this?
I have already reviewed the documentation and found the
databases.*.collections.*.createevent, but it triggers after the document has been created. I need to add required, calculated properties to ensure successful write operations.Thank you for your assistance!
All reactions