Skip to content

Commit f243a6c

Browse files
Add DocumentReference
1 parent e40faf4 commit f243a6c

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

packages/firestore/lite/index.node.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ export {
2626
getFirestore
2727
} from './src/api/database';
2828

29+
export { DocumentReference } from './src/api/reference';
30+
2931
export function registerFirestore(): void {
3032
_registerComponent(
3133
new Component(
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* @license
3+
* Copyright 2020 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
import * as firestore from '../../index';
19+
20+
import { DocumentKey } from '../../../src/model/document_key';
21+
import { Firestore } from './database';
22+
import { DocumentKeyReference } from '../../../src/api/user_data_reader';
23+
24+
/**
25+
* A reference to a particular document in a collection in the database.
26+
*/
27+
export class DocumentReference<T = firestore.DocumentData>
28+
extends DocumentKeyReference<T>
29+
implements firestore.DocumentReference<T> {
30+
constructor(
31+
key: DocumentKey,
32+
readonly firestore: Firestore,
33+
readonly _converter?: firestore.FirestoreDataConverter<T>
34+
) {
35+
super(firestore._databaseId, key, _converter);
36+
}
37+
38+
get id(): string {
39+
return this._key.path.lastSegment();
40+
}
41+
42+
get path(): string {
43+
return this._key.path.canonicalString();
44+
}
45+
46+
withConverter<U>(
47+
converter: firestore.FirestoreDataConverter<U>
48+
): DocumentReference<U> {
49+
return new DocumentReference<U>(this._key, this.firestore, converter);
50+
}
51+
}

packages/firestore/src/api/user_data_reader.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,29 @@ import { DocumentReference } from './database';
5050

5151
const RESERVED_FIELD_REGEX = /^__.*__$/;
5252

53+
/**
54+
* An untyped Firestore Data Converter interface that is shared between the
55+
* lite, full and legacy SDK.
56+
*/
57+
export interface UntypedFirestoreDataConverter<T> {
58+
toFirestore(modelObject: T): firestore.DocumentData;
59+
fromFirestore(snapshot: unknown, options?: unknown): T;
60+
}
61+
62+
/**
63+
* A reference to a document in a Firebase project.
64+
*
65+
* This class serves as a common base class for the public DocumentReferences
66+
* exposed in the lite, full and legacy SDK.
67+
*/
68+
export class DocumentKeyReference<T> {
69+
constructor(
70+
public readonly _databaseId: DatabaseId,
71+
public readonly _key: DocumentKey,
72+
public readonly _converter?: UntypedFirestoreDataConverter<T>
73+
) {}
74+
}
75+
5376
/** The result of parsing document data (e.g. for a setData call). */
5477
export class ParsedSetData {
5578
constructor(

0 commit comments

Comments
 (0)