Skip to content

onFirestoreCreate, onFirestoreDelete, onFirestoreUpdate, onFirestoreWrite

Franklin Chieze edited this page Oct 16, 2020 · 2 revisions
onFirestoreCreate(path: string)
onFirestoreDelete(path: string)
onFirestoreUpdate(path: string)
onFirestoreWrite(path: string)

These decorators specify that a function should handle events in Cloud Firestore.

parameters

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

example

import { func, onFirestoreCreate, onFirestoreDelete, onFirestoreUpdate, onFirestoreWrite } from 'firefuncs';

export class FirestoreFunctions {
    @func()
    @onFirestoreCreate('users/{userId}')
    public async listenForWhenUserIsCreated(snapshot, context) {
        console.log(`New data at users/${context.params.userId} is:`);
        console.log(snapshot.data());
    }

    @func()
    @onFirestoreDelete('users/{userId}')
    public async listenForWhenUserIsDeleted(snapshot, context) {
        console.log(`Deleted data at users/${context.params.userId} is:`);
        console.log(snapshot.data());
    }

    @func()
    @onFirestoreUpdate('users/{userId}')
    public async listenForWhenUserIsUpdated(change, context) {
        console.log(`previous data at users/${context.params.userId} is:`);
        console.log(change.before.data());
        console.log(`New data at users/${context.params.userId} is:`);
        console.log(change.after.data());
    }

    @func()
    @onFirestoreWrite('users/marie')
    public async listenForWhenUserMarieIsWritten(change, context) {
        console.log('previous data at users/marie is:');
        console.log(change.before.data());
        // a write operation could be a create, update or delete
        // check to see if the document still exists
        console.log('New data at users/marie is:');
        console.log(change.after.exists ? change.after.data() : undefined);
    }

    @func()
    @onFirestoreWrite('users/{userId}')
    public async listenForWhenUserIsWritten(change, context) {
        console.log(`previous data at users/${context.params.userId} is:`);
        console.log(change.before.data());
        // a write operation could be a create, update or delete
        // check to see if the document still exists
        console.log(`New data at users/${context.params.userId} is:`);
        console.log(change.after.exists ? change.after.data() : undefined);
    }
}