森口万太郎
森口万太郎

Reputation: 895

What is 'Pending write' in the document of Firestore's Security rules

Use existing data in Firebase Security Rules:

When writing data, you may want to compare incoming data to existing data. This lets you do things like ensure a field hasn't changed, that a field has only incremented by one, or that the new value is at least a week in the future. In this case, if your ruleset allows the pending write, the request.resource variable contains the future state of the document. For update operations that only modify a subset of the document fields, the request.resource variable will contain the pending document state after the operation. You can check the field values in request.resource to prevent unwanted or inconsistent data updates:

I don't understand the meaning of "pending" and "pending write" in the above explanation. I can't imagine it.

Is it possible to suspend writes by security rules?

After the 'update operation' is done, the document is updated, so I don't know what the word "pending document state" means.

Upvotes: 0

Views: 329

Answers (1)

Rafael Lemos
Rafael Lemos

Reputation: 5829

Pending write in this case means the actual write that you are currently making and that the firebase rules are checking if it is allowed or not. As mentioned in the documentation this is made to guarantee data consistency.

I think the best way to understand this is by looking at the example shared in the documentation:

match /cities/{city} {
    allow update: if request.resource.data.population > 0
                  && request.resource.data.name == resource.data.name;
}

In this case, the idea is to not allow an incoming request (which is the said pending write), to make the population field have a value lower than 1 or the name value being changed.

Upvotes: 1

Related Questions