Back

Create Collections Using Function

  • 0
  • Databases
  • Functions
  • Flutter
sparkline
12 Mar, 2023, 18:34

How to create collection base on an attribute update in documents using node js function? Collection name should be the data from updated attribute.

TL;DR
Messages: - Deploy a function to see how it works with the `APPWRITE_FUNCTION_EVENT_DATA` variable. - Look for examples of events-based triggers on the Node.js GitHub demo. - Use team and document level permissions to restrict access and ensure privacy. - Consider having a collection of chat messages instead of a collection per room. - Create a function that executes on a document update event. - Pass the updated attribute data to the function to create a collection based on that attribute.
Drake
12 Mar, 2023, 18:35

You want to create a collection if a document is updated?

sparkline
12 Mar, 2023, 18:35

Yes

sparkline
12 Mar, 2023, 18:36

It's a one time setup related function. So two users can use that same collection.

Drake
12 Mar, 2023, 18:37

Sorry I don't understand

sparkline
12 Mar, 2023, 18:38

There is an attribute in User databse called roomName in my project. When two users connect I want that roomName to be their own collection. Where all the chat documents are inside that collection.

Drake
12 Mar, 2023, 18:40

In general, you can have a function execute on an event like document update. The document would be in the event function variable. What part are you stuck on exactly?

Drake
12 Mar, 2023, 18:41

Do you really need a collection per room? Why not just a collection of chat messages?

sparkline
12 Mar, 2023, 18:41

It's a couple's app each couple have their seperate collection. 1-1 communication

Drake
12 Mar, 2023, 18:42

Again, why? You can use a team and document level permissions to restrict access so only the 2 in the team can read those chat messages

sparkline
12 Mar, 2023, 18:43

If I keep all the chat messages in one collection for all the users? Won't that cause privacy concerns and longer to pull data from server with real-time and queries?

Drake
12 Mar, 2023, 18:44

As long as you have the permissions on place, there's no concerns with privacy. Pulling data is negligible

sparkline
12 Mar, 2023, 18:44

Okay.

Drake
12 Mar, 2023, 18:45

Up to you. It typically makes the most sense to have a collection per data type. If you have multiple collections with the same attribute, you should have a good reason to do so

sparkline
12 Mar, 2023, 18:46

Mixing together all the messages of all users in a single collection doesn't sound good.

sparkline
12 Mar, 2023, 18:48

On the function part, are there any examples of events based trigger on demo in Node.js GitHub? And how to get that attribute name in the payload?

Drake
12 Mar, 2023, 18:51

You should just deploy a function to see how it works. The document that was updated would be in the APPWRITE_FUNCTION_EVENT_DATA variable. See https://appwrite.io/docs/functions#functionVariables

Drake
12 Mar, 2023, 18:51

It's pretty normal

sparkline
12 Mar, 2023, 18:51

Okay

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