Skip to content

Instantly share code, notes, and snippets.

View pglazkov's full-sized avatar

Pavlo Glazkov pglazkov

View GitHub Profile
@pglazkov
pglazkov / firestore-collection-count-cloud-function.js
Created March 25, 2018 18:32
Firestore cloud function that maintains a document field that contains number of items in a collection
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const db = admin.firestore();
exports.updateMyCollectionCount = functions.firestore
.document(`users/{userId}/my-collection/{itemId}`)
.onWrite(event => {
const isUpdate = event.data.exists && event.data.previous.exists;