pocketbase: Collection triggers

This was previously discussed in https://github.com/pocketbase/pocketbase/issues/34, https://github.com/pocketbase/pocketbase/discussions/245 and several other places. Another user (emilydev) also mailed me with very helpful and detailed examples how other platforms tackle this.

Although I still think that for more advanced scenarios the available PocketBase event hooks should be preferred, not everyone is familiar with Go and this ended up being a blocker for a lot of users (the documentation currently is also bit lacking on “How to use as framework” but that will be improved in the near future).

Based on the user feedback, I think we can try to simplify things a little and introduce “Collection Triggers”. This triggers will be fired after a successfull create/update action and initially we will support only 2 types of triggers:

  • Webhook - trigger that will send a single http request to another url. The record data will be available as placeholders (eg. {field1}, {field2.subfield}, etc.) so that users will be able to use them to construct a request body. This trigger could be also used to maintain another collection (there will be an option to authorize as admin when configuring the http request).

  • Mail - (inspired by this Firebase extension) trigger that will send a single email based on the created/updated record data. This trigger will have 3 main configurable fields - to, subject and body. The record data will be available as placeholders (eg. {field1}, {field2.subfield}, etc.), so that users will be able to use them as values in the mail fields.

If you have other ideas or suggestions, please share your feedback below. I plan to implement this feature for v0.7.0 or v0.8.0 release.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 54
  • Comments: 17 (5 by maintainers)

Most upvoted comments

From my point of view, webhooks may be the best fit for PocketBase rather than embedding scripting or a workflow engine. If an application needs custom business logic on the server side, I think they would do better to simply host a separate service that presents a REST endpoint, and/or responds to PocketBase webhook events. One enhancement may be to support API key authentication for server-to-server connections to PocketBase to make this integration easier.

From my own experience maintaining an existing custom workflow engine: I would avoid if possible. Non-developers do not understand them, and developers prefer to write code rather than work within the constraints of the engine.

@Baoqi Thanks for the suggestion.

I’m familiar with openfaas, but for now I’m not planning to add serverless function support to PocketBase because it will be hard to implement while still keeping everything simple and within a single executable.

PocketBase in its core is designed as a Go framework/toolkit and that is one of the main difference (and one of the biggest benefit in my opinion) compared to other similar solutions because this way you can execute any custom code without the constraints that usually comes with cloud functions (access to globals, what packages are you allowed to import, limited control over the request lifecycle, etc.) and still have a single binary at the end by just running go build.

While the goal of this feature request is to help developers not familiar with Go, it is not intended to be a full replacement for the cases when you really need to write custom business logic.

Eventual support in the future for a “Workflow builder” with various plugins/blocks, one of which could be a scripting interpreter like goja, may be added but for now this is out of the scope for v1.0.0.

For anyone interested in this issue, you might want to checkout this project of mine - https://github.com/spinspire/pocketbase-sveltekit-starter/. It is a general purpose starter-kit with pocketbase backend and a static SveltKit frontend.

But the main thing that is relevant to the audience here is the support for “config-driven hooks” that support both command execution and webhook posts tied to specific events (insert/update/delete) on specific tables. Implementation details here

Discussion here and documenation here

@ganigeorgiev some suggested features:

In addition to webhooks and mail …

  • Now that we have a JavaScript VM embedded into PocketBase, may be allow writing triggers in JS? In general, I think JS should be used to do most of the extending of PocketBase. This might cover most use-cases where we are extending via Golang and using PocketBase as a framework.
  • Possibly allow execution of arbitrary programs (such as shell scripts) as triggers? This is probably same as the “run command” idea above by @ktown007. I know this can get out of hand. But if we run these things in a goroutine then it shouldn’t block anything else.

Also, regarding webhook: Most webhooks will require some kind of auth. It would be interesting to see how you provide credentials to the code that posts to webhooks. Also, most of the times you might have to transform the JSON payload before POST’ing it to a webhook. This can be done with a jq-like expression language. But even here JavaScript might come handy.