|
| 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 | +} |
0 commit comments