googleapis / googleapis/google-cloud-node

feat: Support preconditions in `set()`

Open
#7,452 7 comments 0 reactions 1 assignee Claimed by @cherylEnkidu View on GitHub
api: firestore library: nodejs-firestore priority: p3 type: feature request
Dominant language
TypeScript
Stars
3.2k
Forks
712
Avg merge
2d 3h
Merged PRs (30d)
99

Description

There is a bit of a gap in functionality between the `update()` and `set()` APIs.
- With `set()`, we can control merging or full replacement of the document, but cannot control preconditions.
- With `update()` we can control preconditions, but cannot control merging (it always does a shallow merge).

However, what if we want to to a complete replacement of the document (i.e. `set()` without merge) but enforce a precondition that the document must already exist?

If one makes extensive use of `undefined` values and wants a `exists: true` precondition, then one has no other choice but to use `update()` and then must be very careful to replace all instances of `undefined` with `FieldValue.delete()`.

I would propose including the `lastUpdateTime` and `exists` precondition fields in the `SetOptions` so that we can apply preconditions on a set operation, e.g.:

```ts
export type SetOptions =
| ({ merge?: boolean } & Precondition)
| ({ mergeFields?: Array } & Precondition);
```

Subsequently, a minor win of then using `set(..., { exists: true })` instead of `update(...)` is saving the bandwidth and overhead of calculating and transmitting the field mask with the request to the backend.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.