Skip to content

[FEAT] 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

Merged
merged 28 commits into from
May 16, 2025
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
fdaa937
Draft of synchronized bundle reader
DellaBitta Apr 18, 2025
a888667
Sync bundle reader stands alone
DellaBitta Apr 20, 2025
ab90498
docsnapshot.FromJSON prototype
DellaBitta Apr 22, 2025
5df33ec
Lint fixes.
DellaBitta Apr 22, 2025
7c8ef87
Put more logic into bundle_impl.
DellaBitta Apr 23, 2025
f0d3304
Templated converter DocSnapshot.fromJSON
DellaBitta Apr 23, 2025
f80c49d
Update bundleReader to be multi use utility
DellaBitta Apr 30, 2025
be5aee8
Update sync engine use of bundle loader.
DellaBitta Apr 30, 2025
ad2f0ac
typo fix
DellaBitta Apr 30, 2025
5ea4ea5
initial QuerySnapshot fromJSON impl
DellaBitta Apr 30, 2025
95b69f6
docgen
DellaBitta Apr 30, 2025
011ac37
Merge branch 'ddb-firestore-result-serialization' into ddb-sync-bundl…
DellaBitta Apr 30, 2025
fccd633
QuerySnapshot jsonSchema
DellaBitta Apr 30, 2025
51bc4bc
Use jsonSchemas
DellaBitta Apr 30, 2025
019aa4d
Remove need for toDocumentSnapshotData impl
DellaBitta Apr 30, 2025
52bc217
Revised comments.
DellaBitta May 1, 2025
90ade11
Add QuerySnapshot.fromJSON converter variant.
DellaBitta May 2, 2025
7ec7a0c
DocumentSet fix.
DellaBitta May 6, 2025
d97009e
Unit tests.
DellaBitta May 6, 2025
8a942f1
Non templated DocumentSnapshot.fromJSON
DellaBitta May 6, 2025
78626ef
fromJSON integration tests
DellaBitta May 6, 2025
5bf5767
remove fromJSONUsingConverter, user override.
DellaBitta May 6, 2025
4f27dc4
DocumentReference fromJSON overload
DellaBitta May 7, 2025
2840bb3
Created onSnapshotResume variant.
DellaBitta May 12, 2025
96a49f7
Move doc / querysnapshot fromJSON to func impl.
DellaBitta May 12, 2025
8359b95
converter feedback
DellaBitta May 15, 2025
2fb5df3
Test for specific exception text.
DellaBitta May 15, 2025
04620bc
format
DellaBitta May 15, 2025
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
3 changes: 2 additions & 1 deletion common/api-review/firestore-lite.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ export function documentId(): FieldPath;
export class DocumentReference<AppModelType = DocumentData, DbModelType extends DocumentData = DocumentData> {
readonly converter: FirestoreDataConverter<AppModelType, DbModelType> | null;
readonly firestore: Firestore;
static fromJSON<NewAppModelType = DocumentData, NewDbModelType extends DocumentData = DocumentData>(firestore: Firestore, json: object, converter?: FirestoreDataConverter<NewAppModelType, NewDbModelType>): DocumentReference<NewAppModelType, NewDbModelType>;
static fromJSON(firestore: Firestore, json: object): DocumentReference;
static fromJSON<NewAppModelType = DocumentData, NewDbModelType extends DocumentData = DocumentData>(firestore: Firestore, json: object, converter: FirestoreDataConverter<NewAppModelType, NewDbModelType>): DocumentReference<NewAppModelType, NewDbModelType>;
get id(): string;
get parent(): CollectionReference<AppModelType, DbModelType>;
get path(): string;
Expand Down
33 changes: 22 additions & 11 deletions common/api-review/firestore.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ export function documentId(): FieldPath;
export class DocumentReference<AppModelType = DocumentData, DbModelType extends DocumentData = DocumentData> {
readonly converter: FirestoreDataConverter<AppModelType, DbModelType> | null;
readonly firestore: Firestore;
static fromJSON<NewAppModelType = DocumentData, NewDbModelType extends DocumentData = DocumentData>(firestore: Firestore, json: object, converter?: FirestoreDataConverter<NewAppModelType, NewDbModelType>): DocumentReference<NewAppModelType, NewDbModelType>;
static fromJSON(firestore: Firestore, json: object): DocumentReference;
static fromJSON<NewAppModelType = DocumentData, NewDbModelType extends DocumentData = DocumentData>(firestore: Firestore, json: object, converter: FirestoreDataConverter<NewAppModelType, NewDbModelType>): DocumentReference<NewAppModelType, NewDbModelType>;
get id(): string;
get parent(): CollectionReference<AppModelType, DbModelType>;
get path(): string;
Expand All @@ -182,10 +183,15 @@ export class DocumentSnapshot<AppModelType = DocumentData, DbModelType extends D
get id(): string;
readonly metadata: SnapshotMetadata;
get ref(): DocumentReference<AppModelType, DbModelType>;
// (undocumented)
toJSON(): object;
}

// @public
export function documentSnapshotFromJSON(db: Firestore, json: object): DocumentSnapshot;

// @public
export function documentSnapshotFromJSON<AppModelType, DbModelType extends DocumentData = DocumentData>(db: Firestore, json: object, converter: FirestoreDataConverter<AppModelType, DbModelType>): DocumentSnapshot<AppModelType, DbModelType>;

export { EmulatorMockTokenOptions }

// @public @deprecated
Expand Down Expand Up @@ -468,40 +474,40 @@ export function onSnapshot<AppModelType, DbModelType extends DocumentData>(query
export function onSnapshot<AppModelType, DbModelType extends DocumentData>(query: Query<AppModelType, DbModelType>, options: SnapshotListenOptions, onNext: (snapshot: QuerySnapshot<AppModelType, DbModelType>) => void, onError?: (error: FirestoreError) => void, onCompletion?: () => void): Unsubscribe;

// @public
export function onSnapshot<AppModelType, DbModelType extends DocumentData>(firestore: Firestore, snapshotJson: object, onNext: (snapshot: QuerySnapshot<AppModelType, DbModelType>) => void, onError?: (error: FirestoreError) => void, onCompletion?: () => void, converter?: FirestoreDataConverter<DbModelType>): Unsubscribe;
export function onSnapshotResume<AppModelType, DbModelType extends DocumentData>(firestore: Firestore, snapshotJson: object, onNext: (snapshot: QuerySnapshot<AppModelType, DbModelType>) => void, onError?: (error: FirestoreError) => void, onCompletion?: () => void, converter?: FirestoreDataConverter<DbModelType>): Unsubscribe;

// @public
export function onSnapshot<AppModelType, DbModelType extends DocumentData>(firestore: Firestore, snapshotJson: object, onNext: (snapshot: DocumentSnapshot<AppModelType, DbModelType>) => void, onError?: (error: FirestoreError) => void, onCompletion?: () => void, converter?: FirestoreDataConverter<DbModelType>): Unsubscribe;
export function onSnapshotResume<AppModelType, DbModelType extends DocumentData>(firestore: Firestore, snapshotJson: object, onNext: (snapshot: DocumentSnapshot<AppModelType, DbModelType>) => void, onError?: (error: FirestoreError) => void, onCompletion?: () => void, converter?: FirestoreDataConverter<DbModelType>): Unsubscribe;

// @public
export function onSnapshot<AppModelType, DbModelType extends DocumentData>(firestore: Firestore, snapshotJson: object, options: SnapshotListenOptions, onNext: (snapshot: QuerySnapshot<AppModelType, DbModelType>) => void, onError?: (error: FirestoreError) => void, onCompletion?: () => void, converter?: FirestoreDataConverter<DbModelType>): Unsubscribe;
export function onSnapshotResume<AppModelType, DbModelType extends DocumentData>(firestore: Firestore, snapshotJson: object, options: SnapshotListenOptions, onNext: (snapshot: QuerySnapshot<AppModelType, DbModelType>) => void, onError?: (error: FirestoreError) => void, onCompletion?: () => void, converter?: FirestoreDataConverter<DbModelType>): Unsubscribe;

// @public
export function onSnapshot<AppModelType, DbModelType extends DocumentData>(firestore: Firestore, snapshotJson: object, options: SnapshotListenOptions, onNext: (snapshot: DocumentSnapshot<AppModelType, DbModelType>) => void, onError?: (error: FirestoreError) => void, onCompletion?: () => void, converter?: FirestoreDataConverter<DbModelType>): Unsubscribe;
export function onSnapshotResume<AppModelType, DbModelType extends DocumentData>(firestore: Firestore, snapshotJson: object, options: SnapshotListenOptions, onNext: (snapshot: DocumentSnapshot<AppModelType, DbModelType>) => void, onError?: (error: FirestoreError) => void, onCompletion?: () => void, converter?: FirestoreDataConverter<DbModelType>): Unsubscribe;

// @public
export function onSnapshot<AppModelType, DbModelType extends DocumentData>(firestore: Firestore, snapshotJson: object, observer: {
export function onSnapshotResume<AppModelType, DbModelType extends DocumentData>(firestore: Firestore, snapshotJson: object, observer: {
next: (snapshot: QuerySnapshot<AppModelType, DbModelType>) => void;
error?: (error: FirestoreError) => void;
complete?: () => void;
}, converter?: FirestoreDataConverter<DbModelType>): Unsubscribe;

// @public
export function onSnapshot<AppModelType, DbModelType extends DocumentData>(firestore: Firestore, snapshotJson: object, observer: {
export function onSnapshotResume<AppModelType, DbModelType extends DocumentData>(firestore: Firestore, snapshotJson: object, observer: {
next: (snapshot: DocumentSnapshot<AppModelType, DbModelType>) => void;
error?: (error: FirestoreError) => void;
complete?: () => void;
}, converter?: FirestoreDataConverter<DbModelType>): Unsubscribe;

// @public
export function onSnapshot<AppModelType, DbModelType extends DocumentData>(firestore: Firestore, snapshotJson: object, options: SnapshotListenOptions, observer: {
export function onSnapshotResume<AppModelType, DbModelType extends DocumentData>(firestore: Firestore, snapshotJson: object, options: SnapshotListenOptions, observer: {
next: (snapshot: QuerySnapshot<AppModelType, DbModelType>) => void;
error?: (error: FirestoreError) => void;
complete?: () => void;
}, converter?: FirestoreDataConverter<DbModelType>): Unsubscribe;

// @public
export function onSnapshot<AppModelType, DbModelType extends DocumentData>(firestore: Firestore, snapshotJson: object, options: SnapshotListenOptions, observer: {
export function onSnapshotResume<AppModelType, DbModelType extends DocumentData>(firestore: Firestore, snapshotJson: object, options: SnapshotListenOptions, observer: {
next: (snapshot: DocumentSnapshot<AppModelType, DbModelType>) => void;
error?: (error: FirestoreError) => void;
complete?: () => void;
Expand Down Expand Up @@ -658,10 +664,15 @@ export class QuerySnapshot<AppModelType = DocumentData, DbModelType extends Docu
readonly metadata: SnapshotMetadata;
readonly query: Query<AppModelType, DbModelType>;
get size(): number;
// (undocumented)
toJSON(): object;
}

// @public
export function querySnapshotFromJSON(db: Firestore, json: object): QuerySnapshot;

// @public
export function querySnapshotFromJSON<AppModelType, DbModelType extends DocumentData = DocumentData>(db: Firestore, json: object, converter: FirestoreDataConverter<AppModelType, DbModelType>): QuerySnapshot<AppModelType, DbModelType>;

// @public
export class QueryStartAtConstraint extends QueryConstraint {
readonly type: 'startAt' | 'startAfter';
Expand Down
30 changes: 27 additions & 3 deletions docs-devsite/firestore_.documentreference.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export declare class DocumentReference<AppModelType = DocumentData, DbModelType

| Method | Modifiers | Description |
| --- | --- | --- |
| [fromJSON(firestore, json)](./firestore_.documentreference.md#documentreferencefromjson) | <code>static</code> | Builds a <code>DocumentReference</code> instance from a JSON object created by [DocumentReference.toJSON()](./firestore_.documentreference.md#documentreferencetojson)<!-- -->. |
| [fromJSON(firestore, json, converter)](./firestore_.documentreference.md#documentreferencefromjson) | <code>static</code> | Builds a <code>DocumentReference</code> instance from a JSON object created by [DocumentReference.toJSON()](./firestore_.documentreference.md#documentreferencetojson)<!-- -->. |
| [toJSON()](./firestore_.documentreference.md#documentreferencetojson) | | Returns a JSON-serializable representation of this <code>DocumentReference</code> instance. |
| [withConverter(converter)](./firestore_.documentreference.md#documentreferencewithconverter) | | Applies a custom data converter to this <code>DocumentReference</code>, allowing you to use your own custom model objects with Firestore. When you call [setDoc()](./firestore_lite.md#setdoc_ee215ad)<!-- -->, [getDoc()](./firestore_lite.md#getdoc_4569087)<!-- -->, etc. with the returned <code>DocumentReference</code> instance, the provided converter will convert between Firestore data of type <code>NewDbModelType</code> and your custom type <code>NewAppModelType</code>. |
Expand Down Expand Up @@ -105,16 +106,39 @@ Builds a `DocumentReference` instance from a JSON object created by [DocumentRef
<b>Signature:</b>

```typescript
static fromJSON<NewAppModelType = DocumentData, NewDbModelType extends DocumentData = DocumentData>(firestore: Firestore, json: object, converter?: FirestoreDataConverter<NewAppModelType, NewDbModelType>): DocumentReference<NewAppModelType, NewDbModelType>;
static fromJSON(firestore: Firestore, json: object): DocumentReference;
```

#### Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| firestore | [Firestore](./firestore_.firestore.md#firestore_class) | |
| firestore | [Firestore](./firestore_.firestore.md#firestore_class) | The [Firestore](./firestore_.firestore.md#firestore_class) instance the snapshot should be loaded for. |
| json | object | a JSON object represention of a <code>DocumentReference</code> instance |
| converter | [FirestoreDataConverter](./firestore_.firestoredataconverter.md#firestoredataconverter_interface)<!-- -->&lt;NewAppModelType, NewDbModelType&gt; | |

<b>Returns:</b>

[DocumentReference](./firestore_.documentreference.md#documentreference_class)

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

## DocumentReference.fromJSON()

Builds a `DocumentReference` instance from a JSON object created by [DocumentReference.toJSON()](./firestore_.documentreference.md#documentreferencetojson)<!-- -->.

<b>Signature:</b>

```typescript
static fromJSON<NewAppModelType = DocumentData, NewDbModelType extends DocumentData = DocumentData>(firestore: Firestore, json: object, converter: FirestoreDataConverter<NewAppModelType, NewDbModelType>): DocumentReference<NewAppModelType, NewDbModelType>;
```

#### Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| firestore | [Firestore](./firestore_.firestore.md#firestore_class) | The [Firestore](./firestore_.firestore.md#firestore_class) instance the snapshot should be loaded for. |
| json | object | a JSON object represention of a <code>DocumentReference</code> instance |
| converter | [FirestoreDataConverter](./firestore_.firestoredataconverter.md#firestoredataconverter_interface)<!-- -->&lt;NewAppModelType, NewDbModelType&gt; | Converts objects to and from Firestore. |

<b>Returns:</b>

Expand Down
6 changes: 5 additions & 1 deletion docs-devsite/firestore_.documentsnapshot.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ 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. |
| [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 @@ -147,6 +147,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 +158,5 @@ toJSON(): object;

object

a JSON representation of this object.

Loading
Loading