Back

Need Guidance: Database Approach For Lack Of "Triggers"

  • 0
  • Databases
hellosendpie
31 Aug, 2023, 03:06

Hello!

I'm working on migrating a project from Airtable to Appwrite, but I am a little confused about how to handle my schema.

Background: My site is a public listicle, and un-authenticated users fill up various forms to enquire about various properties on the site.

Currently:

  • I have one table, which we can call 'Form Submissions' (containing user email, and info about the properties they enquiring about)
  • I have a second table, which we can call 'User Callback Requests' (again, containing user mail, and info specific to their call back requests)

Problem:

  • I am required to create a new table this time, called "User Activity". The purpose of this table is to be a unique list of user emails, user activity info (incremental count of form Submissions table, as incremental count from Callback Requests table).
  • The above logic should be triggered automatically update at every document inserted into the two other tables mentioned above.
  • I know with Postgresql i could've made triggers to handle this, but how do I do this in App Write?
TL;DR
The user is seeking guidance on how to handle schema migration from Airtable to Appwrite. They have three tables: 'Form Submissions', 'User Callback Requests', and a new table called 'User Activity'. They want the 'User Activity' table to be automatically updated whenever a document is inserted into the other two tables. They mention that they are familiar with using triggers in PostgreSQL, but unsure of how to accomplish this in Appwrite. Solution: To achieve this functionality in Appwrite, you can create an Appwrite function that triggers on document creation. Within the function, you can pull the newly updated document and update the 'User Activity
Drake
31 Aug, 2023, 03:11

You can create an Appwrite function to trigger on document creation.

In depth guide on functions: https://appwrite.io/docs/functions

hellosendpie
31 Aug, 2023, 03:15

Ah! Thanks Stephen!

So I'd basically need to maintain a separate codebase in a sense, to handle this. Got it.

Within the function, Is there a way I can ascertain which document was JUST updated (that triggered this function)? Or do I just need to pull all documents that were created in the last X minutes and ascertain which one triggered the function?

Drake
31 Aug, 2023, 03:17

The APPWRITE_FUNCTION_EVENT_DATA variable will have the newly updated document (for an update document event)

hellosendpie
31 Aug, 2023, 03:36

Thank you so much! Would this also trigger when a document is created?

I can't find a documentation page on this, if you could point me to that it'll be super helpful.

Thanks @Steven!

Drake
31 Aug, 2023, 03:39

Yes document creation is an event too. The link to all the events is in the first paragraph

hellosendpie
31 Aug, 2023, 03:40

You're the best, Steven! Thank you

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