Skip to content

Commit f305182

Browse files
committed
DocumentReference impl
1 parent 05a49aa commit f305182

File tree

5 files changed

+133
-0
lines changed

5 files changed

+133
-0
lines changed

common/api-review/firestore-lite.api.md

+2
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,11 @@ export function documentId(): FieldPath;
137137
export class DocumentReference<AppModelType = DocumentData, DbModelType extends DocumentData = DocumentData> {
138138
readonly converter: FirestoreDataConverter<AppModelType, DbModelType> | null;
139139
readonly firestore: Firestore;
140+
static fromJSON<NewAppModelType = DocumentData, NewDbModelType extends DocumentData = DocumentData>(firestore: Firestore, json: object, converter?: FirestoreDataConverter<NewAppModelType, NewDbModelType>): DocumentReference<NewAppModelType, NewDbModelType>;
140141
get id(): string;
141142
get parent(): CollectionReference<AppModelType, DbModelType>;
142143
get path(): string;
144+
toJSON(): object;
143145
readonly type = "document";
144146
withConverter<NewAppModelType, NewDbModelType extends DocumentData = DocumentData>(converter: FirestoreDataConverter<NewAppModelType, NewDbModelType>): DocumentReference<NewAppModelType, NewDbModelType>;
145147
withConverter(converter: null): DocumentReference<DocumentData, DocumentData>;

common/api-review/firestore.api.md

+2
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,11 @@ export function documentId(): FieldPath;
163163
export class DocumentReference<AppModelType = DocumentData, DbModelType extends DocumentData = DocumentData> {
164164
readonly converter: FirestoreDataConverter<AppModelType, DbModelType> | null;
165165
readonly firestore: Firestore;
166+
static fromJSON<NewAppModelType = DocumentData, NewDbModelType extends DocumentData = DocumentData>(firestore: Firestore, json: object, converter?: FirestoreDataConverter<NewAppModelType, NewDbModelType>): DocumentReference<NewAppModelType, NewDbModelType>;
166167
get id(): string;
167168
get parent(): CollectionReference<AppModelType, DbModelType>;
168169
get path(): string;
170+
toJSON(): object;
169171
readonly type = "document";
170172
withConverter<NewAppModelType, NewDbModelType extends DocumentData = DocumentData>(converter: FirestoreDataConverter<NewAppModelType, NewDbModelType>): DocumentReference<NewAppModelType, NewDbModelType>;
171173
withConverter(converter: null): DocumentReference<DocumentData, DocumentData>;

docs-devsite/firestore_.documentreference.md

+37
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ export declare class DocumentReference<AppModelType = DocumentData, DbModelType
3333

3434
| Method | Modifiers | Description |
3535
| --- | --- | --- |
36+
| [fromJSON(firestore, json, converter)](./firestore_.documentreference.md#documentreferencefromjson) | <code>static</code> | Builds a <code>DocumentReference</code> instance from a JSON serialized version of <code>DocumentReference</code>. |
37+
| [toJSON()](./firestore_.documentreference.md#documentreferencetojson) | | Returns a JSON-serializable representation of this DocumentReference. |
3638
| [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>. |
3739
| [withConverter(converter)](./firestore_.documentreference.md#documentreferencewithconverter) | | Removes the current converter. |
3840

@@ -96,6 +98,41 @@ The type of this Firestore reference.
9698
readonly type = "document";
9799
```
98100

101+
## DocumentReference.fromJSON()
102+
103+
Builds a `DocumentReference` instance from a JSON serialized version of `DocumentReference`<!-- -->.
104+
105+
<b>Signature:</b>
106+
107+
```typescript
108+
static fromJSON<NewAppModelType = DocumentData, NewDbModelType extends DocumentData = DocumentData>(firestore: Firestore, json: object, converter?: FirestoreDataConverter<NewAppModelType, NewDbModelType>): DocumentReference<NewAppModelType, NewDbModelType>;
109+
```
110+
111+
#### Parameters
112+
113+
| Parameter | Type | Description |
114+
| --- | --- | --- |
115+
| firestore | [Firestore](./firestore_.firestore.md#firestore_class) | |
116+
| json | object | |
117+
| converter | [FirestoreDataConverter](./firestore_.firestoredataconverter.md#firestoredataconverter_interface)<!-- -->&lt;NewAppModelType, NewDbModelType&gt; | |
118+
119+
<b>Returns:</b>
120+
121+
[DocumentReference](./firestore_.documentreference.md#documentreference_class)<!-- -->&lt;NewAppModelType, NewDbModelType&gt;
122+
123+
## DocumentReference.toJSON()
124+
125+
Returns a JSON-serializable representation of this DocumentReference.
126+
127+
<b>Signature:</b>
128+
129+
```typescript
130+
toJSON(): object;
131+
```
132+
<b>Returns:</b>
133+
134+
object
135+
99136
## DocumentReference.withConverter()
100137

101138
Applies a custom data converter to this `DocumentReference`<!-- -->, 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 `DocumentReference` instance, the provided converter will convert between Firestore data of type `NewDbModelType` and your custom type `NewAppModelType`<!-- -->.

docs-devsite/firestore_lite.documentreference.md

+37
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ export declare class DocumentReference<AppModelType = DocumentData, DbModelType
3333

3434
| Method | Modifiers | Description |
3535
| --- | --- | --- |
36+
| [fromJSON(firestore, json, converter)](./firestore_lite.documentreference.md#documentreferencefromjson) | <code>static</code> | Builds a <code>DocumentReference</code> instance from a JSON serialized version of <code>DocumentReference</code>. |
37+
| [toJSON()](./firestore_lite.documentreference.md#documentreferencetojson) | | Returns a JSON-serializable representation of this DocumentReference. |
3638
| [withConverter(converter)](./firestore_lite.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>. |
3739
| [withConverter(converter)](./firestore_lite.documentreference.md#documentreferencewithconverter) | | Removes the current converter. |
3840

@@ -96,6 +98,41 @@ The type of this Firestore reference.
9698
readonly type = "document";
9799
```
98100

101+
## DocumentReference.fromJSON()
102+
103+
Builds a `DocumentReference` instance from a JSON serialized version of `DocumentReference`<!-- -->.
104+
105+
<b>Signature:</b>
106+
107+
```typescript
108+
static fromJSON<NewAppModelType = DocumentData, NewDbModelType extends DocumentData = DocumentData>(firestore: Firestore, json: object, converter?: FirestoreDataConverter<NewAppModelType, NewDbModelType>): DocumentReference<NewAppModelType, NewDbModelType>;
109+
```
110+
111+
#### Parameters
112+
113+
| Parameter | Type | Description |
114+
| --- | --- | --- |
115+
| firestore | [Firestore](./firestore_lite.firestore.md#firestore_class) | |
116+
| json | object | |
117+
| converter | [FirestoreDataConverter](./firestore_lite.firestoredataconverter.md#firestoredataconverter_interface)<!-- -->&lt;NewAppModelType, NewDbModelType&gt; | |
118+
119+
<b>Returns:</b>
120+
121+
[DocumentReference](./firestore_lite.documentreference.md#documentreference_class)<!-- -->&lt;NewAppModelType, NewDbModelType&gt;
122+
123+
## DocumentReference.toJSON()
124+
125+
Returns a JSON-serializable representation of this DocumentReference.
126+
127+
<b>Signature:</b>
128+
129+
```typescript
130+
toJSON(): object;
131+
```
132+
<b>Returns:</b>
133+
134+
object
135+
99136
## DocumentReference.withConverter()
100137

101138
Applies a custom data converter to this `DocumentReference`<!-- -->, 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 `DocumentReference` instance, the provided converter will convert between Firestore data of type `NewDbModelType` and your custom type `NewAppModelType`<!-- -->.

packages/firestore/src/lite-api/reference.ts

+55
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,61 @@ export class DocumentReference<
278278
this._key
279279
);
280280
}
281+
282+
/** Returns a JSON-serializable representation of this DocumentReference. */
283+
toJSON(): object {
284+
return {
285+
type: 'firestore/documentReference/1.0',
286+
referencePath: this._key.toString()
287+
};
288+
}
289+
290+
/** Builds a `DocumentReference` instance from a JSON serialized version of `DocumentReference`. */
291+
static fromJSON<
292+
NewAppModelType = DocumentData,
293+
NewDbModelType extends DocumentData = DocumentData
294+
>(
295+
firestore: Firestore,
296+
json: object,
297+
converter?: FirestoreDataConverter<NewAppModelType, NewDbModelType>
298+
): DocumentReference<NewAppModelType, NewDbModelType> {
299+
const requiredFields = ['type', 'referencePath'];
300+
let error: string | undefined = undefined;
301+
let path: string = '';
302+
for (const key of requiredFields) {
303+
if (!(key in json)) {
304+
error = `json missing required field: ${key}`;
305+
}
306+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
307+
const value = (json as any)[key];
308+
if (key === 'type') {
309+
if (typeof value !== 'string') {
310+
error = `json field 'type' must be a string.`;
311+
break;
312+
} else if (value !== 'firestore/documentReference/1.0') {
313+
error =
314+
"Expected 'type' field to equal 'firestore/documentReference/1.0'";
315+
break;
316+
}
317+
} else if (key === 'referencePath') {
318+
if (typeof value !== 'string') {
319+
error = `json field 'referencePath' must be a string.`;
320+
break;
321+
}
322+
path = value;
323+
}
324+
}
325+
if (error) {
326+
throw new FirestoreError(Code.INVALID_ARGUMENT, error);
327+
}
328+
const resourcePath = ResourcePath.fromString(path);
329+
const documentKey = new DocumentKey(resourcePath);
330+
return new DocumentReference<NewAppModelType, NewDbModelType>(
331+
firestore,
332+
converter ? converter : null,
333+
documentKey
334+
);
335+
}
281336
}
282337

283338
/**

0 commit comments

Comments
 (0)