Bucket and database triggers
Run a function whenever documents are inserted, updated or deleted, either through the bucket APIs or anywhere in the database.
Two triggers react to data changes:
bucket | database | |
|---|---|---|
| Fires on | Writes made through the bucket data API, the panel and realtime | Any change in a database collection, whatever made it |
| Scope | One bucket | Any collection, including system collections |
| Payload | Previous and current document | Current document and changed fields |
| Requires | --bucket-hooks (on by default) | Change streams (MongoDB replica set) |
Use the bucket trigger for business logic around your data. Use the database trigger when you need to see changes that don't go through the bucket API, or changes in system collections such as identities.
Bucket trigger
YAML
triggers:
onPostCreated:
type: bucket
active: true
options:
bucket: 6b0b1968a5611972c29c25f7
type: INSERT| Option | Description |
|---|---|
bucket | Id of the bucket to watch. |
type | INSERT, UPDATE, DELETE or ALL. |
The handler receives a change:
| Field | Description |
|---|---|
bucket | Id of the bucket. |
documentKey | Id of the document. |
previous | The document before the change, for updates and deletes. |
current | The document after the change, for inserts and updates. |
kind | "update" or "delete". |
JavaScript
export async function onPostCreated(change) {
const post = change.current;
console.info("New post", change.documentKey, post.slug);
}Example: keep a counter up to date
JavaScript
import * as Bucket from "@spica-devkit/bucket";
export async function onCommentChanged(change) {
Bucket.initialize({apikey: process.env.SPICA_API_KEY});
const postId = (change.current ?? change.previous).post;
const comments = await Bucket.data.getAll(process.env.COMMENTS_BUCKET_ID, {
queryParams: {filter: `document.post == "${postId}"`, paginate: true, limit: 1}
});
await Bucket.data.patch(process.env.POSTS_BUCKET_ID, postId, {commentCount: comments.meta.total});
}Database trigger
YAML
triggers:
onIdentityChange:
type: database
active: true
options:
collection: identity
type: INSERT| Option | Description |
|---|---|
collection | Collection to watch. Bucket data lives in collections named bucket_<bucketId>. |
type | INSERT, UPDATE, REPLACE or DELETE. |
The handler receives:
| Field | Description |
|---|---|
kind | "insert", "update", "replace" or "delete". |
collection | The collection name. |
documentKey | Id of the changed document. |
document | The current document, when it still exists. |
updateDescription | For updates: updatedFields and removedFields. |
JavaScript
export function onIdentityChange(change) {
console.info(`${change.kind} on ${change.collection}`, change.documentKey);
}The panel lists the collections you can pick. Over the API, GET /function/information returns the allowed values.
Delivery
- Events are processed asynchronously after the write has been committed. The client that made the write doesn't wait for your function.
- Handlers run under the function's timeout. Keep them short, or hand long work off to a queue.
- Make handlers idempotent. Writing the same result twice should be harmless.