Skip to content

onDatabaseCreate, onDatabaseDelete, onDatabaseUpdate, onDatabaseWrite

Franklin Chieze edited this page Oct 16, 2020 · 3 revisions
onDatabaseCreate(path: string)
onDatabaseDelete(path: string)
onDatabaseUpdate(path: string)
onDatabaseWrite(path: string)

These decorators specify that a function should handle events in Firebase Realtime Database.

parameters

  • path
    This is a string parameter that represents the path to the document that is being listened to.

auxiliary decorators

instance
instance(databaseInstance: string)

This decorator specifies the database instance that can trigger the associated function.

example

import { func, instance, onDatabaseCreate, onDatabaseDelete, onDatabaseUpdate, onDatabaseWrite } from 'firefuncs';

export class FirestoreFunctions {
    @func()
    @onDatabaseCreate('/messages/{pushId}/original')
    public async makeUppercase(snapshot, context) {
        const original = snapshot.val();
        console.log('Uppercasing', context.params.pushId, original);
        const uppercase = original.toUpperCase();
        return snapshot.ref.parent.child('uppercase').set(uppercase);
    }

    @func()
    @instance('database1')
    @onDatabaseWrite('/messages/{pushId}/original')
    public async makeUppercase2(change, context) {
        if (change.before.exists()) {
            return null;
        }
        // Exit when the data is deleted.
        if (!change.after.exists()) {
            return null;
        }
        const original = change.after.val();
        const uppercase = original.toUpperCase();
        return change.after.ref.parent.child('uppercase').set(uppercase);
    }
}