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:

bucketdatabase
Fires onWrites made through the bucket data API, the panel and realtimeAny change in a database collection, whatever made it
ScopeOne bucketAny collection, including system collections
PayloadPrevious and current documentCurrent 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
OptionDescription
bucketId of the bucket to watch.
typeINSERT, UPDATE, DELETE or ALL.

The handler receives a change:

FieldDescription
bucketId of the bucket.
documentKeyId of the document.
previousThe document before the change, for updates and deletes.
currentThe 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
OptionDescription
collectionCollection to watch. Bucket data lives in collections named bucket_<bucketId>.
typeINSERT, UPDATE, REPLACE or DELETE.

The handler receives:

FieldDescription
kind"insert", "update", "replace" or "delete".
collectionThe collection name.
documentKeyId of the changed document.
documentThe current document, when it still exists.
updateDescriptionFor 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.