|
| 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 { Document } from '../../../src/model/document'; |
| 22 | +import { |
| 23 | + ServerTimestampBehavior, |
| 24 | + UserDataWriter |
| 25 | +} from '../../../src/api/user_data_writer'; |
| 26 | +import { |
| 27 | + fieldPathFromArgument, |
| 28 | + DocumentSnapshot as LiteDocumentSnapshot |
| 29 | +} from '../../../lite/src/api/snapshot'; |
| 30 | +import { Firestore } from './database'; |
| 31 | +import { cast } from '../../../lite/src/api/util'; |
| 32 | +import { DocumentReference } from '../../../lite/src/api/reference'; |
| 33 | +import { SnapshotMetadata } from '../../../src/api/database'; |
| 34 | + |
| 35 | +const DEFAULT_SERVER_TIMESTAMP_BEHAVIOR: ServerTimestampBehavior = 'none'; |
| 36 | + |
| 37 | +export class DocumentSnapshot<T = firestore.DocumentData> |
| 38 | + extends LiteDocumentSnapshot<T> |
| 39 | + implements firestore.DocumentSnapshot<T> { |
| 40 | + private readonly _firestoreImpl: Firestore; |
| 41 | + |
| 42 | + constructor( |
| 43 | + readonly _firestore: Firestore, |
| 44 | + key: DocumentKey, |
| 45 | + document: Document | null, |
| 46 | + converter: firestore.FirestoreDataConverter<T> | null, |
| 47 | + readonly metadata: firestore.SnapshotMetadata |
| 48 | + ) { |
| 49 | + super(_firestore, key, document, converter); |
| 50 | + this._firestoreImpl = cast(_firestore, Firestore); |
| 51 | + } |
| 52 | + |
| 53 | + exists(): this is firestore.QueryDocumentSnapshot<T> { |
| 54 | + return super.exists(); |
| 55 | + } |
| 56 | + |
| 57 | + data(options: firestore.SnapshotOptions = {}): T | undefined { |
| 58 | + if (!this._document) { |
| 59 | + return undefined; |
| 60 | + } else if (this._converter) { |
| 61 | + // We only want to use the converter and create a new DocumentSnapshot |
| 62 | + // if a converter has been provided. |
| 63 | + const snapshot = new QueryDocumentSnapshot( |
| 64 | + this._firestore, |
| 65 | + this._key, |
| 66 | + this._document, |
| 67 | + /* converter= */ null, |
| 68 | + this.metadata |
| 69 | + ); |
| 70 | + return this._converter.fromFirestore(snapshot); |
| 71 | + } else { |
| 72 | + const userDataWriter = new UserDataWriter( |
| 73 | + this._firestoreImpl._databaseId, |
| 74 | + /* timestampsInSnapshots= */ true, |
| 75 | + options.serverTimestamps || DEFAULT_SERVER_TIMESTAMP_BEHAVIOR, |
| 76 | + key => |
| 77 | + new DocumentReference(this._firestore, key, /* converter= */ null) |
| 78 | + ); |
| 79 | + return userDataWriter.convertValue(this._document.toProto()) as T; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + get( |
| 84 | + fieldPath: string | firestore.FieldPath, |
| 85 | + options: firestore.SnapshotOptions = {} |
| 86 | + ): unknown { |
| 87 | + if (this._document) { |
| 88 | + const value = this._document |
| 89 | + .data() |
| 90 | + .field(fieldPathFromArgument('DocumentSnapshot.get', fieldPath)); |
| 91 | + if (value !== null) { |
| 92 | + const userDataWriter = new UserDataWriter( |
| 93 | + this._firestoreImpl._databaseId, |
| 94 | + /* timestampsInSnapshots= */ true, |
| 95 | + options.serverTimestamps || DEFAULT_SERVER_TIMESTAMP_BEHAVIOR, |
| 96 | + key => new DocumentReference(this._firestore, key, this._converter) |
| 97 | + ); |
| 98 | + return userDataWriter.convertValue(value); |
| 99 | + } |
| 100 | + } |
| 101 | + return undefined; |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +export class QueryDocumentSnapshot<T = firestore.DocumentData> |
| 106 | + extends DocumentSnapshot<T> |
| 107 | + implements firestore.QueryDocumentSnapshot<T> { |
| 108 | + data(options: firestore.SnapshotOptions = {}): T { |
| 109 | + return super.data(options) as T; |
| 110 | + } |
| 111 | +} |
0 commit comments