|
| 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 { Firestore } from './database'; |
| 21 | +import { DocumentReference } from './reference'; |
| 22 | +import { FieldPath } from './field_path'; |
| 23 | +import { cast } from './util'; |
| 24 | +import { DocumentKey } from '../../../src/model/document_key'; |
| 25 | +import { Document } from '../../../src/model/document'; |
| 26 | +import { UserDataWriter } from '../../../src/api/user_data_writer'; |
| 27 | +import { FieldPath as InternalFieldPath } from '../../../src/model/path'; |
| 28 | +import { fieldPathFromDotSeparatedString } from '../../../src/api/user_data_reader'; |
| 29 | + |
| 30 | +export class DocumentSnapshot<T = firestore.DocumentData> |
| 31 | + implements firestore.DocumentSnapshot<T> { |
| 32 | + // Note: This class is stripped down version of the DocumentSnapshot in |
| 33 | + // the legacy SDK. The changes are: |
| 34 | + // - No support for SnapshotMetadata. |
| 35 | + // - No support for SnapshotOptions. |
| 36 | + |
| 37 | + constructor( |
| 38 | + private _firestore: Firestore, |
| 39 | + private _key: DocumentKey, |
| 40 | + private _document: Document | null, |
| 41 | + private _converter?: firestore.FirestoreDataConverter<T> |
| 42 | + ) {} |
| 43 | + |
| 44 | + get id(): string { |
| 45 | + return this._key.path.lastSegment(); |
| 46 | + } |
| 47 | + |
| 48 | + get ref(): firestore.DocumentReference<T> { |
| 49 | + return new DocumentReference<T>( |
| 50 | + this._firestore, |
| 51 | + this._key, |
| 52 | + this._converter |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + exists(): this is firestore.QueryDocumentSnapshot<T> { |
| 57 | + return this._document !== null; |
| 58 | + } |
| 59 | + |
| 60 | + data(): T | undefined { |
| 61 | + if (!this._document) { |
| 62 | + return undefined; |
| 63 | + } else if (this._converter) { |
| 64 | + // We only want to use the converter and create a new DocumentSnapshot |
| 65 | + // if a converter has been provided. |
| 66 | + const snapshot = new QueryDocumentSnapshot( |
| 67 | + this._firestore, |
| 68 | + this._key, |
| 69 | + this._document |
| 70 | + ); |
| 71 | + return this._converter.fromFirestore(snapshot); |
| 72 | + } else { |
| 73 | + const userDataWriter = new UserDataWriter( |
| 74 | + this._firestore._databaseId, |
| 75 | + /* timestampsInSnapshots= */ false, |
| 76 | + /* serverTimestampBehavior=*/ 'none', |
| 77 | + key => new DocumentReference(this._firestore, key) |
| 78 | + ); |
| 79 | + return userDataWriter.convertValue(this._document.toProto()) as T; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + get(fieldPath: string | firestore.FieldPath): unknown { |
| 84 | + if (this._document) { |
| 85 | + const value = this._document |
| 86 | + .data() |
| 87 | + .field(fieldPathFromArgument('DocumentSnapshot.get', fieldPath)); |
| 88 | + if (value !== null) { |
| 89 | + const userDataWriter = new UserDataWriter( |
| 90 | + this._firestore._databaseId, |
| 91 | + /* timestampsInSnapshots= */ false, |
| 92 | + /* serverTimestampBehavior=*/ 'none', |
| 93 | + key => new DocumentReference(this._firestore, key, this._converter) |
| 94 | + ); |
| 95 | + return userDataWriter.convertValue(value); |
| 96 | + } |
| 97 | + } |
| 98 | + return undefined; |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +export class QueryDocumentSnapshot<T = firestore.DocumentData> |
| 103 | + extends DocumentSnapshot<T> |
| 104 | + implements firestore.QueryDocumentSnapshot<T> { |
| 105 | + data(): T { |
| 106 | + return super.data() as T; |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +/** |
| 111 | + * Helper that calls fromDotSeparatedString() but wraps any error thrown. |
| 112 | + */ |
| 113 | +export function fieldPathFromArgument( |
| 114 | + methodName: string, |
| 115 | + arg: string | firestore.FieldPath |
| 116 | +): InternalFieldPath { |
| 117 | + if (typeof arg === 'string') { |
| 118 | + return fieldPathFromDotSeparatedString(methodName, arg); |
| 119 | + } else { |
| 120 | + const path = cast(arg, FieldPath); |
| 121 | + return path._internalPath; |
| 122 | + } |
| 123 | +} |
0 commit comments