Skip to content

Instantly share code, notes, and snippets.

@danahartweg
Created December 22, 2019 04:00
Show Gist options
  • Save danahartweg/4969fb5a66307036d59de95f3946dc5d to your computer and use it in GitHub Desktop.
Save danahartweg/4969fb5a66307036d59de95f3946dc5d to your computer and use it in GitHub Desktop.
Creating homestead indices - Efficiently managing large Cloud Firestore lists
import { firestore } from 'firebase-functions';
import { getFirestore } from '../admin';
import { IndexMeta } from '../types';
/**
* When a new homestead is created we want to create
* the needed indices so they can be immediately accessed.
*/
export const createIndicesForHomestead = firestore
.document('homesteads/{homesteadId}')
.onCreate(async (_, context) => {
const db = getFirestore();
const batch = db.batch();
const indicesCollection = db.collection('indices');
batch.set(indicesCollection.doc(), {
indexName: 'plant-varieties',
isFull: false,
parentId: context.params.homesteadId,
} as IndexMeta);
/** additional indices to be added later */
return batch.commit();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment