Created
December 22, 2019 04:17
-
-
Save danahartweg/55cce69f9bbc2c6dfa8c5edc0062d68d to your computer and use it in GitHub Desktop.
Updating index meta information - Efficiently managing large Cloud Firestore lists
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { firestore } from 'firebase-functions'; | |
// @ts-ignore there is no declaration file for this module | |
import * as sizeOf from 'firestore-size'; | |
import { getFirestore } from '../admin'; | |
import { IndexMeta } from '../types'; | |
// 1 MiB * 1024 kB * 1024 B * margin of error | |
const MAX_DOCUMENT_SIZE = 1 * 1024 * 1024 * 0.95; | |
/** | |
* When indices are updated we need to ensure they can still accept more fields. | |
* If the index is full, we need to create a new index document to accept more entries. | |
*/ | |
export const updateIndexMeta = firestore | |
.document('indices/{indexId}') | |
.onUpdate(async (change, context) => { | |
const indexData: IndexMeta = change.after.data() as IndexMeta; | |
if (indexData.isFull) { | |
return null; | |
} | |
const currentDocumentSize = sizeOf(indexData); | |
if (currentDocumentSize < MAX_DOCUMENT_SIZE) { | |
return null; | |
} | |
const db = getFirestore(); | |
const batch = db.batch(); | |
const { indexName, parentId } = indexData; | |
batch.set(db.collection('indices').doc(), { | |
isFull: false, | |
indexName, | |
parentId, | |
} as IndexMeta); | |
batch.update(db.collection('indices').doc(context.params.indexId), { | |
isFull: true, | |
}); | |
return batch.commit(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment