Skip to content

[FEAT] [WIP] Doc/QuerySnapshot fromJSON. #8960

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 17 commits into
base: ddb-firestore-result-serialization
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions common/api-review/firestore.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,11 @@ export class DocumentSnapshot<AppModelType = DocumentData, DbModelType extends D
protected constructor();
data(options?: SnapshotOptions): AppModelType | undefined;
exists(): this is QueryDocumentSnapshot<AppModelType, DbModelType>;
static fromJSON<AppModelType, DbModelType extends DocumentData = DocumentData>(db: Firestore, json: object, converter?: FirestoreDataConverter<AppModelType, DbModelType> | null): DocumentSnapshot<AppModelType, DbModelType>;
get(fieldPath: string | FieldPath, options?: SnapshotOptions): any;
get id(): string;
readonly metadata: SnapshotMetadata;
get ref(): DocumentReference<AppModelType, DbModelType>;
// (undocumented)
toJSON(): object;
}

Expand Down Expand Up @@ -655,10 +655,11 @@ export class QuerySnapshot<AppModelType = DocumentData, DbModelType extends Docu
get docs(): Array<QueryDocumentSnapshot<AppModelType, DbModelType>>;
get empty(): boolean;
forEach(callback: (result: QueryDocumentSnapshot<AppModelType, DbModelType>) => void, thisArg?: unknown): void;
static fromJSON(db: Firestore, json: object): QuerySnapshot;
static fromJSONUsingConverter<AppModelType, DbModelType extends DocumentData = DocumentData>(db: Firestore, json: object, converter: FirestoreDataConverter<AppModelType, DbModelType>): QuerySnapshot<AppModelType, DbModelType>;
readonly metadata: SnapshotMetadata;
readonly query: Query<AppModelType, DbModelType>;
get size(): number;
// (undocumented)
toJSON(): object;
}

Expand Down
31 changes: 30 additions & 1 deletion docs-devsite/firestore_.documentsnapshot.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ export declare class DocumentSnapshot<AppModelType = DocumentData, DbModelType e
| --- | --- | --- |
| [data(options)](./firestore_.documentsnapshot.md#documentsnapshotdata) | | Retrieves all fields in the document as an <code>Object</code>. Returns <code>undefined</code> if the document doesn't exist.<!-- -->By default, <code>serverTimestamp()</code> values that have not yet been set to their final value will be returned as <code>null</code>. You can override this by passing an options object. |
| [exists()](./firestore_.documentsnapshot.md#documentsnapshotexists) | | Returns whether or not the data exists. True if the document exists. |
| [fromJSON(db, json, converter)](./firestore_.documentsnapshot.md#documentsnapshotfromjson) | <code>static</code> | Builds a <code>DocumentSnapshot</code> instance from a JSON object created by [DocumentSnapshot.toJSON()](./firestore_.documentsnapshot.md#documentsnapshottojson)<!-- -->. |
| [get(fieldPath, options)](./firestore_.documentsnapshot.md#documentsnapshotget) | | Retrieves the field specified by <code>fieldPath</code>. Returns <code>undefined</code> if the document or field doesn't exist.<!-- -->By default, a <code>serverTimestamp()</code> that has not yet been set to its final value will be returned as <code>null</code>. You can override this by passing an options object. |
| [toJSON()](./firestore_.documentsnapshot.md#documentsnapshottojson) | | |
| [toJSON()](./firestore_.documentsnapshot.md#documentsnapshottojson) | | Returns a JSON-serializable representation of this <code>DocumentSnapshot</code> instance. |

## DocumentSnapshot.(constructor)

Expand Down Expand Up @@ -120,6 +121,30 @@ exists(): this is QueryDocumentSnapshot<AppModelType, DbModelType>;

this is [QueryDocumentSnapshot](./firestore_.querydocumentsnapshot.md#querydocumentsnapshot_class)<!-- -->&lt;AppModelType, DbModelType&gt;

## DocumentSnapshot.fromJSON()

Builds a `DocumentSnapshot` instance from a JSON object created by [DocumentSnapshot.toJSON()](./firestore_.documentsnapshot.md#documentsnapshottojson)<!-- -->.

<b>Signature:</b>

```typescript
static fromJSON<AppModelType, DbModelType extends DocumentData = DocumentData>(db: Firestore, json: object, converter?: FirestoreDataConverter<AppModelType, DbModelType> | null): DocumentSnapshot<AppModelType, DbModelType>;
```

#### Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| db | [Firestore](./firestore_.firestore.md#firestore_class) | |
| json | object | a JSON object represention of a <code>DocumentSnapshot</code> instance. |
| converter | [FirestoreDataConverter](./firestore_.firestoredataconverter.md#firestoredataconverter_interface)<!-- -->&lt;AppModelType, DbModelType&gt; \| null | Converts objects to and from Firestore. |

<b>Returns:</b>

[DocumentSnapshot](./firestore_.documentsnapshot.md#documentsnapshot_class)<!-- -->&lt;AppModelType, DbModelType&gt;

an instance of [DocumentSnapshot](./firestore_.documentsnapshot.md#documentsnapshot_class) if the JSON object could be parsed. Throws a [FirestoreError](./firestore_.firestoreerror.md#firestoreerror_class) if an error occurs.

## DocumentSnapshot.get()

Retrieves the field specified by `fieldPath`<!-- -->. Returns `undefined` if the document or field doesn't exist.
Expand Down Expand Up @@ -147,6 +172,8 @@ The data at the specified field location or undefined if no such field exists in

## DocumentSnapshot.toJSON()

Returns a JSON-serializable representation of this `DocumentSnapshot` instance.

<b>Signature:</b>

```typescript
Expand All @@ -156,3 +183,5 @@ toJSON(): object;

object

a JSON representation of this object.

2 changes: 1 addition & 1 deletion docs-devsite/firestore_.md
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ export declare function onSnapshot<AppModelType, DbModelType extends DocumentDat
| firestore | [Firestore](./firestore_.firestore.md#firestore_class) | The [Firestore](./firestore_.firestore.md#firestore_class) instance to enable persistence for. |
| snapshotJson | object | A JSON object generated by invoking [DocumentSnapshot.toJSON()](./firestore_.documentsnapshot.md#documentsnapshottojson)<!-- -->. |
| onNext | (snapshot: [DocumentSnapshot](./firestore_.documentsnapshot.md#documentsnapshot_class)<!-- -->&lt;AppModelType, DbModelType&gt;) =&gt; void | A callback to be called every time a new <code>DocumentSnapshot</code> is available. |
| onError | (error: [FirestoreError](./firestore_.firestoreerror.md#firestoreerror_class)<!-- -->) =&gt; void | A callback to be called if the listen fails or is cancelled. No fruther callbacks will occur. |
| onError | (error: [FirestoreError](./firestore_.firestoreerror.md#firestoreerror_class)<!-- -->) =&gt; void | A callback to be called if the listen fails or is cancelled. No further callbacks will occur. |
| onCompletion | () =&gt; void | Can be provided, but will not be called since streams are never ending. |
| converter | [FirestoreDataConverter](./firestore_.firestoredataconverter.md#firestoredataconverter_interface)<!-- -->&lt;DbModelType&gt; | An optional object that converts objects from Firestore before the onNext listener is invoked. |

Expand Down
55 changes: 54 additions & 1 deletion docs-devsite/firestore_.querysnapshot.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ export declare class QuerySnapshot<AppModelType = DocumentData, DbModelType exte
| --- | --- | --- |
| [docChanges(options)](./firestore_.querysnapshot.md#querysnapshotdocchanges) | | Returns an array of the documents changes since the last snapshot. If this is the first snapshot, all documents will be in the list as 'added' changes. |
| [forEach(callback, thisArg)](./firestore_.querysnapshot.md#querysnapshotforeach) | | Enumerates all of the documents in the <code>QuerySnapshot</code>. |
| [toJSON()](./firestore_.querysnapshot.md#querysnapshottojson) | | |
| [fromJSON(db, json)](./firestore_.querysnapshot.md#querysnapshotfromjson) | <code>static</code> | Builds a <code>QuerySnapshot</code> instance from a JSON object created by [QuerySnapshot.toJSON()](./firestore_.querysnapshot.md#querysnapshottojson)<!-- -->. |
| [fromJSONUsingConverter(db, json, converter)](./firestore_.querysnapshot.md#querysnapshotfromjsonusingconverter) | <code>static</code> | Builds a <code>QuerySnapshot</code> instance from a JSON object created by [QuerySnapshot.toJSON()](./firestore_.querysnapshot.md#querysnapshottojson)<!-- -->. |
| [toJSON()](./firestore_.querysnapshot.md#querysnapshottojson) | | Returns a JSON-serializable representation of this <code>QuerySnapshot</code> instance. |

## QuerySnapshot.docs

Expand Down Expand Up @@ -127,8 +129,57 @@ forEach(callback: (result: QueryDocumentSnapshot<AppModelType, DbModelType>) =>

void

## QuerySnapshot.fromJSON()

Builds a `QuerySnapshot` instance from a JSON object created by [QuerySnapshot.toJSON()](./firestore_.querysnapshot.md#querysnapshottojson)<!-- -->.

<b>Signature:</b>

```typescript
static fromJSON(db: Firestore, json: object): QuerySnapshot;
```

#### Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| db | [Firestore](./firestore_.firestore.md#firestore_class) | |
| json | object | a JSON object represention of a <code>QuerySnapshot</code> instance. |

<b>Returns:</b>

[QuerySnapshot](./firestore_.querysnapshot.md#querysnapshot_class)

an instance of [QuerySnapshot](./firestore_.querysnapshot.md#querysnapshot_class) if the JSON object could be parsed. Throws a [FirestoreError](./firestore_.firestoreerror.md#firestoreerror_class) if an error occurs.

## QuerySnapshot.fromJSONUsingConverter()

Builds a `QuerySnapshot` instance from a JSON object created by [QuerySnapshot.toJSON()](./firestore_.querysnapshot.md#querysnapshottojson)<!-- -->.

<b>Signature:</b>

```typescript
static fromJSONUsingConverter<AppModelType, DbModelType extends DocumentData = DocumentData>(db: Firestore, json: object, converter: FirestoreDataConverter<AppModelType, DbModelType>): QuerySnapshot<AppModelType, DbModelType>;
```

#### Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| db | [Firestore](./firestore_.firestore.md#firestore_class) | |
| json | object | a JSON object represention of a <code>QuerySnapshot</code> instance. |
| converter | [FirestoreDataConverter](./firestore_.firestoredataconverter.md#firestoredataconverter_interface)<!-- -->&lt;AppModelType, DbModelType&gt; | Converts objects to and from Firestore. |

<b>Returns:</b>

[QuerySnapshot](./firestore_.querysnapshot.md#querysnapshot_class)<!-- -->&lt;AppModelType, DbModelType&gt;

an instance of [QuerySnapshot](./firestore_.querysnapshot.md#querysnapshot_class) if the JSON object could be parsed. Throws a [FirestoreError](./firestore_.firestoreerror.md#firestoreerror_class) if an error occurs.

## QuerySnapshot.toJSON()

Returns a JSON-serializable representation of this `QuerySnapshot` instance.

<b>Signature:</b>

```typescript
Expand All @@ -138,3 +189,5 @@ toJSON(): object;

object

a JSON representation of this object.

4 changes: 2 additions & 2 deletions docs-devsite/firestore_.timestamp.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export declare class Timestamp
| Method | Modifiers | Description |
| --- | --- | --- |
| [fromDate(date)](./firestore_.timestamp.md#timestampfromdate) | <code>static</code> | Creates a new timestamp from the given date. |
| [fromJSON(json)](./firestore_.timestamp.md#timestampfromjson) | <code>static</code> | Builds a <code>Timestamp</code> instance from a JSON serialized version of <code>Bytes</code>. |
| [fromJSON(json)](./firestore_.timestamp.md#timestampfromjson) | <code>static</code> | Builds a <code>Timestamp</code> instance from a JSON object created by [Timestamp.toJSON()](./firestore_.timestamp.md#timestamptojson)<!-- -->. |
| [fromMillis(milliseconds)](./firestore_.timestamp.md#timestampfrommillis) | <code>static</code> | Creates a new timestamp from the given number of milliseconds. |
| [isEqual(other)](./firestore_.timestamp.md#timestampisequal) | | Returns true if this <code>Timestamp</code> is equal to the provided one. |
| [now()](./firestore_.timestamp.md#timestampnow) | <code>static</code> | Creates a new timestamp with the current date, with millisecond precision. |
Expand Down Expand Up @@ -113,7 +113,7 @@ A new `Timestamp` representing the same point in time as the given date.

## Timestamp.fromJSON()

Builds a `Timestamp` instance from a JSON serialized version of `Bytes`<!-- -->.
Builds a `Timestamp` instance from a JSON object created by [Timestamp.toJSON()](./firestore_.timestamp.md#timestamptojson)<!-- -->.

<b>Signature:</b>

Expand Down
2 changes: 1 addition & 1 deletion docs-devsite/firestore_.vectorvalue.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ static fromJSON(json: object): VectorValue;

| Parameter | Type | Description |
| --- | --- | --- |
| json | object | a JSON object represention of a <code>VectorValue</code> instance |
| json | object | a JSON object represention of a <code>VectorValue</code> instance. |

<b>Returns:</b>

Expand Down
4 changes: 2 additions & 2 deletions docs-devsite/firestore_lite.timestamp.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export declare class Timestamp
| Method | Modifiers | Description |
| --- | --- | --- |
| [fromDate(date)](./firestore_lite.timestamp.md#timestampfromdate) | <code>static</code> | Creates a new timestamp from the given date. |
| [fromJSON(json)](./firestore_lite.timestamp.md#timestampfromjson) | <code>static</code> | Builds a <code>Timestamp</code> instance from a JSON serialized version of <code>Bytes</code>. |
| [fromJSON(json)](./firestore_lite.timestamp.md#timestampfromjson) | <code>static</code> | Builds a <code>Timestamp</code> instance from a JSON object created by [Timestamp.toJSON()](./firestore_.timestamp.md#timestamptojson)<!-- -->. |
| [fromMillis(milliseconds)](./firestore_lite.timestamp.md#timestampfrommillis) | <code>static</code> | Creates a new timestamp from the given number of milliseconds. |
| [isEqual(other)](./firestore_lite.timestamp.md#timestampisequal) | | Returns true if this <code>Timestamp</code> is equal to the provided one. |
| [now()](./firestore_lite.timestamp.md#timestampnow) | <code>static</code> | Creates a new timestamp with the current date, with millisecond precision. |
Expand Down Expand Up @@ -113,7 +113,7 @@ A new `Timestamp` representing the same point in time as the given date.

## Timestamp.fromJSON()

Builds a `Timestamp` instance from a JSON serialized version of `Bytes`<!-- -->.
Builds a `Timestamp` instance from a JSON object created by [Timestamp.toJSON()](./firestore_.timestamp.md#timestamptojson)<!-- -->.

<b>Signature:</b>

Expand Down
2 changes: 1 addition & 1 deletion docs-devsite/firestore_lite.vectorvalue.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ static fromJSON(json: object): VectorValue;

| Parameter | Type | Description |
| --- | --- | --- |
| json | object | a JSON object represention of a <code>VectorValue</code> instance |
| json | object | a JSON object represention of a <code>VectorValue</code> instance. |

<b>Returns:</b>

Expand Down
2 changes: 1 addition & 1 deletion packages/firestore/src/api/reference_impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ export function onSnapshot<AppModelType, DbModelType extends DocumentData>(
* @param firestore - The {@link Firestore} instance to enable persistence for.
* @param snapshotJson - A JSON object generated by invoking {@link DocumentSnapshot.toJSON}.
* @param onNext - A callback to be called every time a new `DocumentSnapshot` is available.
* @param onError - A callback to be called if the listen fails or is cancelled. No fruther
* @param onError - A callback to be called if the listen fails or is cancelled. No further
* callbacks will occur.
* @param onCompletion - Can be provided, but will not be called since streams are
* never ending.
Expand Down
Loading
Loading