I'm building an application with Google Firebase and Firestore that has its structure defined in the DB. This structure is not very big and doesn't change all that often. This structure is stored in different collections based on hierarchy going something like domain > chapter > unit, where each item has a list of child Ids defined.

To prevent making a ton of DB calls for a structure that doesn't change all that often I was thinking to keep every entity retrieved from the DB in the browser's localStorage as well as a version for the current application structure. At the start, it would compare the local version with the DB version and if they don't match, discard the local version.

I'm thinking to make a collection called 'system' or something like that with (for now) a single record called something like 'composition version'. Each time the structure changes, a cloud function should update this record.

So my question is: does this make sense or are there better ways of doing this in Firebase?


Turns out this is pretty much build into Firebase already so there seems to be no need to write this yourself in local storage.

It seems I can check the DB version with one call and then globally switch the source to cache for the relevant collections.

let snapshot = await documentRef.get({ source: 'cache' }
if (!snapshot.exists) {
    snapshot = await documentRef.get({ source: 'server' })

Article on Medium about this

I will try to update this answer with more details once implemented.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.