alsky
alsky

Reputation: 325

When is data actually transferred in firestore? In the document snapshot? Or not until .data() or .get() is called?

I'm trying to decide how to structure permissions for an app. Much of this comes down to the question in the title. Specifically, if I call:

const tasks = db.collection('tasks').where('canRead', 'array-contains', firebase.auth().currentUser.uid).get() and retrieve an array of document snapshots, is the data actually transferred for each snapshot, or is the data not transferred until I iterate through that array:

for(let taskSS of tasks.docs) { ... and eventually call .data() with let task = taskSS.data() ?

The reason I ask is if I had a task document with a massive array of users who are allowed to get and list that task, I would not want to transfer all of that data on the initial db.collection('tasks').where('canRead', 'array-contains', firebase.auth().currentUser.uid).get() call.

If the data is not actually transferred until we call either .data() or .get() then I could put all the task data in task.public and then just do let task = taskSS.get('public').

Does someone know when the actual data is transferred and put into memory with firestore? Is the data actually "in" the document snapshot or not transferred until you request that data with .data() or .get()?

Upvotes: 0

Views: 59

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598976

The data is transferred from the database to your application when you call get() or onSnapshot(). Each DocumentSnapshot contains all data for the document when you get it. Calling document.data() merely returns a map of the data it already has.

Upvotes: 1

Related Questions