-
Notifications
You must be signed in to change notification settings - Fork 938
Add getDoc() to firestore-exp #3274
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
fe88bbc
Add getDoc() to firestore-exp
schmidt-sebastian 64162a2
Simplify
schmidt-sebastian 9cf0d84
Review
schmidt-sebastian b09863f
Better error
schmidt-sebastian fa37773
Create popular-beds-yell.md
schmidt-sebastian 4188756
Create popular-beds-yell2.md
schmidt-sebastian 2cf802a
Update popular-beds-yell.md
schmidt-sebastian 11bc080
Update popular-beds-yell2.md
schmidt-sebastian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/** | ||
* @license | ||
* Copyright 2020 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import * as firestore from '../../index'; | ||
|
||
import { _getProvider } from '@firebase/app-exp'; | ||
import { FirebaseApp, _FirebaseService } from '@firebase/app-types-exp'; | ||
import { Provider } from '@firebase/component'; | ||
|
||
import { FirebaseAuthInternalName } from '@firebase/auth-interop-types'; | ||
import { FirestoreClient } from '../../../src/core/firestore_client'; | ||
import { AsyncQueue } from '../../../src/util/async_queue'; | ||
import { | ||
ComponentProvider, | ||
MemoryComponentProvider | ||
} from '../../../src/core/component_provider'; | ||
|
||
import { Firestore as LiteFirestore } from '../../../lite/src/api/database'; | ||
|
||
/** | ||
* The root reference to the Firestore database and the entry point for the | ||
* tree-shakeable SDK. | ||
*/ | ||
export class Firestore extends LiteFirestore | ||
implements firestore.FirebaseFirestore, _FirebaseService { | ||
private readonly _queue = new AsyncQueue(); | ||
private readonly _persistenceKey: string; | ||
private _componentProvider: ComponentProvider = new MemoryComponentProvider(); | ||
|
||
// Assigned via _getFirestoreClient() | ||
private _firestoreClientPromise?: Promise<FirestoreClient>; | ||
|
||
// We override the Settings property of the Lite SDK since the full Firestore | ||
// SDK supports more settings. | ||
protected _settings?: firestore.Settings; | ||
|
||
constructor( | ||
app: FirebaseApp, | ||
authProvider: Provider<FirebaseAuthInternalName> | ||
) { | ||
super(app, authProvider); | ||
this._persistenceKey = app.name; | ||
} | ||
|
||
_getSettings(): firestore.Settings { | ||
if (!this._settings) { | ||
this._settings = {}; | ||
} | ||
return this._settings; | ||
} | ||
|
||
_getFirestoreClient(): Promise<FirestoreClient> { | ||
if (!this._firestoreClientPromise) { | ||
const settings = this._getSettings(); | ||
const databaseInfo = this._makeDatabaseInfo( | ||
settings.host, | ||
settings.ssl, | ||
settings.experimentalForceLongPolling | ||
); | ||
|
||
const firestoreClient = new FirestoreClient( | ||
databaseInfo, | ||
this._credentials, | ||
this._queue | ||
); | ||
|
||
this._firestoreClientPromise = firestoreClient | ||
.start(this._componentProvider, { durable: false }) | ||
.then(() => firestoreClient); | ||
} | ||
|
||
return this._firestoreClientPromise; | ||
} | ||
|
||
async delete(): Promise<void> { | ||
// TODO(firestoreexp): Call terminate() once implemented | ||
} | ||
} | ||
|
||
export function initializeFirestore( | ||
app: FirebaseApp, | ||
settings: firestore.Settings | ||
): Firestore { | ||
const firestore = _getProvider( | ||
app, | ||
'firestore-exp' | ||
).getImmediate() as Firestore; | ||
firestore._configureClient(settings); | ||
return firestore; | ||
} | ||
|
||
export function getFirestore(app: FirebaseApp): Firestore { | ||
return _getProvider(app, 'firestore-exp').getImmediate() as Firestore; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/** | ||
* @license | ||
* Copyright 2020 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
// See https://github.com/typescript-eslint/typescript-eslint/issues/363 | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
import * as firestore from '../../index'; | ||
|
||
import { Firestore } from './database'; | ||
import { DocumentKeyReference } from '../../../src/api/user_data_reader'; | ||
import { debugAssert } from '../../../src/util/assert'; | ||
import { cast } from '../../../lite/src/api/util'; | ||
import { DocumentSnapshot } from './snapshot'; | ||
import { | ||
getDocViaSnapshotListener, | ||
SnapshotMetadata | ||
} from '../../../src/api/database'; | ||
import { ViewSnapshot } from '../../../src/core/view_snapshot'; | ||
import { DocumentReference } from '../../../lite/src/api/reference'; | ||
|
||
export function getDoc<T>( | ||
reference: firestore.DocumentReference<T> | ||
): Promise<firestore.DocumentSnapshot<T>> { | ||
const ref = cast<DocumentReference<T>>(reference, DocumentReference); | ||
const firestore = cast<Firestore>(ref.firestore, Firestore); | ||
return firestore._getFirestoreClient().then(async firestoreClient => { | ||
const viewSnapshot = await getDocViaSnapshotListener(firestoreClient, ref); | ||
return convertToDocSnapshot(firestore, ref, viewSnapshot); | ||
}); | ||
} | ||
|
||
/** | ||
* Converts a ViewSnapshot that contains the single document specified by `ref` | ||
* to a DocumentSnapshot. | ||
*/ | ||
function convertToDocSnapshot<T>( | ||
firestore: Firestore, | ||
ref: DocumentKeyReference<T>, | ||
snapshot: ViewSnapshot | ||
): DocumentSnapshot<T> { | ||
debugAssert( | ||
snapshot.docs.size <= 1, | ||
'Too many documents returned on a document query' | ||
); | ||
const doc = snapshot.docs.get(ref._key); | ||
|
||
return new DocumentSnapshot( | ||
firestore, | ||
ref._key, | ||
doc, | ||
ref._converter, | ||
new SnapshotMetadata(snapshot.hasPendingWrites, snapshot.fromCache) | ||
); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/** | ||
* @license | ||
* Copyright 2020 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import * as firestore from '../../index'; | ||
|
||
import { DocumentKey } from '../../../src/model/document_key'; | ||
import { Document } from '../../../src/model/document'; | ||
import { | ||
ServerTimestampBehavior, | ||
UserDataWriter | ||
} from '../../../src/api/user_data_writer'; | ||
import { | ||
fieldPathFromArgument, | ||
DocumentSnapshot as LiteDocumentSnapshot | ||
} from '../../../lite/src/api/snapshot'; | ||
import { Firestore } from './database'; | ||
import { cast } from '../../../lite/src/api/util'; | ||
import { DocumentReference } from '../../../lite/src/api/reference'; | ||
import { SnapshotMetadata } from '../../../src/api/database'; | ||
|
||
const DEFAULT_SERVER_TIMESTAMP_BEHAVIOR: ServerTimestampBehavior = 'none'; | ||
|
||
export class DocumentSnapshot<T = firestore.DocumentData> | ||
extends LiteDocumentSnapshot<T> | ||
implements firestore.DocumentSnapshot<T> { | ||
private readonly _firestoreImpl: Firestore; | ||
|
||
constructor( | ||
readonly _firestore: Firestore, | ||
key: DocumentKey, | ||
document: Document | null, | ||
converter: firestore.FirestoreDataConverter<T> | null, | ||
readonly metadata: firestore.SnapshotMetadata | ||
) { | ||
super(_firestore, key, document, converter); | ||
this._firestoreImpl = cast(_firestore, Firestore); | ||
} | ||
|
||
exists(): this is firestore.QueryDocumentSnapshot<T> { | ||
return super.exists(); | ||
} | ||
|
||
data(options: firestore.SnapshotOptions = {}): T | undefined { | ||
if (!this._document) { | ||
return undefined; | ||
} else if (this._converter) { | ||
// We only want to use the converter and create a new DocumentSnapshot | ||
// if a converter has been provided. | ||
const snapshot = new QueryDocumentSnapshot( | ||
this._firestore, | ||
this._key, | ||
this._document, | ||
/* converter= */ null, | ||
this.metadata | ||
); | ||
return this._converter.fromFirestore(snapshot); | ||
} else { | ||
const userDataWriter = new UserDataWriter( | ||
this._firestoreImpl._databaseId, | ||
/* timestampsInSnapshots= */ true, | ||
options.serverTimestamps || DEFAULT_SERVER_TIMESTAMP_BEHAVIOR, | ||
key => | ||
new DocumentReference(this._firestore, key, /* converter= */ null) | ||
); | ||
return userDataWriter.convertValue(this._document.toProto()) as T; | ||
} | ||
} | ||
|
||
get( | ||
fieldPath: string | firestore.FieldPath, | ||
options: firestore.SnapshotOptions = {} | ||
): unknown { | ||
if (this._document) { | ||
const value = this._document | ||
.data() | ||
.field(fieldPathFromArgument('DocumentSnapshot.get', fieldPath)); | ||
if (value !== null) { | ||
const userDataWriter = new UserDataWriter( | ||
this._firestoreImpl._databaseId, | ||
/* timestampsInSnapshots= */ true, | ||
options.serverTimestamps || DEFAULT_SERVER_TIMESTAMP_BEHAVIOR, | ||
key => new DocumentReference(this._firestore, key, this._converter) | ||
); | ||
return userDataWriter.convertValue(value); | ||
} | ||
} | ||
return undefined; | ||
} | ||
} | ||
|
||
export class QueryDocumentSnapshot<T = firestore.DocumentData> | ||
extends DocumentSnapshot<T> | ||
implements firestore.QueryDocumentSnapshot<T> { | ||
data(options: firestore.SnapshotOptions = {}): T { | ||
return super.data(options) as T; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All these exports can be re-used without modification since they don't rely on latency-compensation.