googleapis / googleapis/google-cloud-node

Allow converters to return Promises

Open
#7,471 3 comments 0 reactions 1 assignee Claimed by @wu-hui 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

**Is your feature request related to a problem? Please describe.**

I have some data that needs to be processed with async APIs when read or written to the database. I'd like to put the processing in a converter to ensure it's done consistently, but I can't because converters can't be async.

In this case the processing is compression/decompression. Without async converters I'm forced to choose between doing the processing at every read and write or tying up the main thread during compression.

**Describe the solution you'd like**

Converters should have an interface that allows for async methods:

```ts
export interface FirestoreDataConverter {
toFirestore(modelObject: WithFieldValue): DocumentData | Promise;

toFirestore(
modelObject: PartialWithFieldValue,
options: SetOptions
): DocumentData | Promise;

fromFirestore(snapshot: QueryDocumentSnapshot): T | Promise;
}
```

We would need a new async method for reading data from snapshots and the existing `.data()` method would have to throw if an async converter is used:

```ts
// Throws if the converter returns a Promise
data(): T | undefined;
dataAsync(): T | Promise | undefined;
```

DocumentReference write methods `set()` and `update()` would need to support Promise fields, but they are already async.

Transaction write methods aren't async, but are obviously performing async work without blocking already.

**Describe alternatives you've considered**

Not using FirestoreDataConverters.

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.