Skip to content

Commit 3ac0fe3

Browse files
schmidt-sebastianXuechunHou
authored andcommitted
Add DocumentReference (firebase#3123)
1 parent e10388c commit 3ac0fe3

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

packages/firestore/lite/index.node.ts

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ export {
2828
getFirestore
2929
} from './src/api/database';
3030

31+
export { DocumentReference } from './src/api/reference';
32+
3133
// TOOD(firestorelite): Add tests when setDoc() is available
3234
export {
3335
FieldValue,
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+
readonly firestore: Firestore,
32+
key: DocumentKey,
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+
): firestore.DocumentReference<U> {
49+
return new DocumentReference<U>(this.firestore, this._key, converter);
50+
}
51+
}

packages/firestore/src/api/user_data_reader.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@ import { PlatformSupport } from '../platform/platform';
4949

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

52+
/**
53+
* An untyped Firestore Data Converter interface that is shared between the
54+
* lite, full and legacy SDK.
55+
*/
56+
export interface UntypedFirestoreDataConverter<T> {
57+
toFirestore(modelObject: T): firestore.DocumentData;
58+
fromFirestore(snapshot: unknown, options?: unknown): T;
59+
}
60+
5261
/**
5362
* A reference to a document in a Firebase project.
5463
*
@@ -59,7 +68,7 @@ export class DocumentKeyReference<T> {
5968
constructor(
6069
public readonly _databaseId: DatabaseId,
6170
public readonly _key: DocumentKey,
62-
public readonly _converter?: firestore.FirestoreDataConverter<T>
71+
public readonly _converter?: UntypedFirestoreDataConverter<T>
6372
) {}
6473
}
6574

0 commit comments

Comments
 (0)