Why not provide database "triggers"?

You already have database-side logic (UDFs) and subscriptions, why not allow me to create a pure-database trigger that calls a UDF based on events on a set or document within the set.

My biggest use case is for caching data that is otherwise troublesome to query dynamically.

For example, I have a MessageRequests collection and a MessageOutboundEmail collection, where the outbound email references the messageRequest that triggered it.

Every minute, I check for unfulfilled MessageRequests and call a 3rd party API to send an email, then I record the successful API request as an OutboundEmail.

The query to find “unfulfilled message requests” looks like Difference(messageRequestsRequiringEmails, messageRequestsWithSentEmails), to get a list of “message requests that require emails but we haven’t sent them yet”. Currently this query is timing out my serverless functions due to taking > 60 seconds. !!! I see that it is unwieldy, but I believe this is the appropriate relationship. Maybe someone can help me with a better way to structure it. But it’s kind of unfortunate if I need to “hack” my schema to make Fauna happy.

Anyway, if I had such a “trigger” that I’m describing here, I could have a UDF that writes a property to the corresponding MessageRequest every time an OutboundEmail doc is created/updated/deleted. Then I could write emailCount: 0, emailCount: 1, etc. Or, better yet, undeliveredEmailCount: 1. THEN, it would be super fast and easy to index MessageRequests on undeliveredEmailCount and pull all with undeliveredEmailCount {after: 1} or something like that.

Another solution (still based on internal triggers) might be to create OutboundEmailRequest docs in a separate queue collection, poll for them every minute, then delete them when I’ve fulfilled them. Then I don’t even need to get indexing involved.

Maybe the “correct” way to do this according to current Fauna design is to wrap the associated database capabilities in a UDF. So I block access to Create(Collection('outbound_emails'),...) in favor of Call(Function('createOutboundEmail'), ...), which then performs the desired updates. Either setting emailFulfilled: True on the corresponding MessageRequest, or deleting some corresponding OutboundEmailQueue document.

Just to be certain, did you see the feature request? Add internal & external triggers/hooks, I would personally vote up that request and add your insights there.

1 Like