Skip to content
Back

Function in Node.JS to monitor events around a specific collection

  • 0
  • Functions
Bagios
13 Dec, 2024, 17:09

Hello everyone. I'm creating my first Node.JS function, but I don't have much experience with node and javascript.

I'm trying to create a function, that monitor events (update/create) around a specific collection. Every time a specific field of every document is updated or created in this collection, I need to update other fields.

However, I am experiencing difficulties in capturing events. I have configured the function on AppWrite Cloud, and when an event of these happens, my function is called, but then I haven't figured out how to capture the event inside node.js.

Also it seems there is no documentation on AppWrite docs about how to monitor events, so every help is much appreciated!

cc @Kenny

TL;DR
Developers are seeking a way to trigger events based on specific attributes being updated in a collection using Node.js in AppWrite. They share a function logic to accomplish this, including code snippet and helpful links to documentation.
Kenny
13 Dec, 2024, 17:16

Hey! So to grab the event data you can do something like this

TypeScript
import { Client, Databases } from 'node-appwrite';

export default async ({ req, res, log, error }) => {
  // The event should send the payload in the body
  const body = req.bodyJson;
  const id = body['id'];

  // To get the information you want out of this you could do something like
  const attributeData = body['attribute'];
  
  // Do whatever calculations you need to set the new attributes derived value.
  const newAttributeData = attributeData + 1;

  // Setup your client
  const client = new Client()
    .setEndpoint(process.env.APPWRITE_FUNCTION_API_ENDPOINT)
    .setProject(process.env.APPWRITE_FUNCTION_PROJECT_ID)
    .setKey(req.headers['x-appwrite-key'] ?? '');
  const database = new Databases(client);

  try {
    // Update the derived attribute.
    const response = await database.updateDocument(DATABASE_ID, COLLECTION_ID, id, {
      newAttribute: newAttributeData
    });
  } catch(err) {
    // Log any errors.
    error(err.message);
  }

  // Return
  return res.empty();
};
Kenny
13 Dec, 2024, 17:16

I didn't create a function and test this out, but this should be the logic you need to accomplish what you want

Bagios
13 Dec, 2024, 17:18

Thanks, Kenny! Let me test it πŸ™‚

Bagios
13 Dec, 2024, 17:24

it works! Amazing

Bagios
13 Dec, 2024, 17:24

Thanks, Kenny 🌈 kudos to you!

Bagios
13 Dec, 2024, 17:25

I was using a completely wrong approach

Bagios
13 Dec, 2024, 17:41

As far as you know, is it possible to trigger an event, if just specific attributes are updated, instead of the whole document @Kenny ?

Kenny
13 Dec, 2024, 17:49

I don’t believe so :(

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more