Skip to content

Commit b34e340

Browse files
committed
cleanup
1 parent e14de0b commit b34e340

File tree

5 files changed

+126
-40
lines changed

5 files changed

+126
-40
lines changed

packages/firestore/src/api/aggregate.ts

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18-
import {AggregateField, AggregateSpec, DocumentData, Query} from '../api';
18+
import { AggregateField, AggregateSpec, DocumentData, Query } from '../api';
1919
import { AggregateImpl } from '../core/aggregate';
2020
import { firestoreClientRunAggregateQuery } from '../core/firestore_client';
2121
import { count } from '../lite-api/aggregate';
@@ -57,7 +57,16 @@ export {
5757
* retrieved from `snapshot.data().count`, where `snapshot` is the
5858
* `AggregateQuerySnapshot` to which the returned Promise resolves.
5959
*/
60-
export function getCountFromServer<AppType = DocumentData, DbType extends DocumentData = AppType extends DocumentData ? AppType : DocumentData>(query: Query<AppType, DbType>): Promise<AggregateQuerySnapshot<{ count: AggregateField<number> }, AppType, DbType>> {
60+
export function getCountFromServer<
61+
AppType = DocumentData,
62+
DbType extends DocumentData = AppType extends DocumentData
63+
? AppType
64+
: DocumentData
65+
>(
66+
query: Query<AppType, DbType>
67+
): Promise<
68+
AggregateQuerySnapshot<{ count: AggregateField<number> }, AppType, DbType>
69+
> {
6170
const countQuerySpec: { count: AggregateField<number> } = {
6271
count: count()
6372
};
@@ -99,7 +108,16 @@ export function getCountFromServer<AppType = DocumentData, DbType extends Docume
99108
* ```
100109
* @internal TODO (sum/avg) remove when public
101110
*/
102-
export function getAggregateFromServer<AggregateSpecType extends AggregateSpec, AppType = DocumentData, DbType extends DocumentData = AppType extends DocumentData ? AppType : DocumentData>(query: Query<AppType, DbType>, aggregateSpec: AggregateSpecType): Promise<AggregateQuerySnapshot<AggregateSpecType, AppType, DbType>> {
111+
export function getAggregateFromServer<
112+
AggregateSpecType extends AggregateSpec,
113+
AppType = DocumentData,
114+
DbType extends DocumentData = AppType extends DocumentData
115+
? AppType
116+
: DocumentData
117+
>(
118+
query: Query<AppType, DbType>,
119+
aggregateSpec: AggregateSpecType
120+
): Promise<AggregateQuerySnapshot<AggregateSpecType, AppType, DbType>> {
103121
const firestore = cast(query.firestore, Firestore);
104122
const client = ensureFirestoreConfigured(firestore);
105123

@@ -128,7 +146,15 @@ export function getAggregateFromServer<AggregateSpecType extends AggregateSpec,
128146
* @param aggregateResult Core aggregation result
129147
* @internal
130148
*/
131-
function convertToAggregateQuerySnapshot<AggregateSpecType extends AggregateSpec, AppType, DbType extends DocumentData>(firestore: Firestore, query: Query<AppType, DbType>, aggregateResult: ObjectValue): AggregateQuerySnapshot<AggregateSpecType, AppType, DbType> {
149+
function convertToAggregateQuerySnapshot<
150+
AggregateSpecType extends AggregateSpec,
151+
AppType,
152+
DbType extends DocumentData
153+
>(
154+
firestore: Firestore,
155+
query: Query<AppType, DbType>,
156+
aggregateResult: ObjectValue
157+
): AggregateQuerySnapshot<AggregateSpecType, AppType, DbType> {
132158
const userDataWriter = new ExpUserDataWriter(firestore);
133159
return new AggregateQuerySnapshot<AggregateSpecType, AppType, DbType>(
134160
query,

packages/firestore/src/api/reference_impl.ts

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ import { FieldPath } from '../lite-api/field_path';
4040
import { validateHasExplicitOrderByForLimitToLast } from '../lite-api/query';
4141
import {
4242
CollectionReference,
43-
doc, DocumentData,
43+
doc,
44+
DocumentData,
4445
DocumentReference,
4546
PartialWithFieldValue,
4647
Query,
@@ -91,8 +92,18 @@ export interface SnapshotListenOptions {
9192
* @returns A Promise resolved with a `DocumentSnapshot` containing the
9293
* current document contents.
9394
*/
94-
export function getDoc<AppType = DocumentData, DbType extends DocumentData = AppType extends DocumentData ? AppType : DocumentData>(reference: DocumentReference<AppType, DbType>): Promise<DocumentSnapshot<AppType, DbType>> {
95-
reference = cast<DocumentReference<AppType, DbType>>(reference, DocumentReference);
95+
export function getDoc<
96+
AppType = DocumentData,
97+
DbType extends DocumentData = AppType extends DocumentData
98+
? AppType
99+
: DocumentData
100+
>(
101+
reference: DocumentReference<AppType, DbType>
102+
): Promise<DocumentSnapshot<AppType, DbType>> {
103+
reference = cast<DocumentReference<AppType, DbType>>(
104+
reference,
105+
DocumentReference
106+
);
96107
const firestore = cast(reference.firestore, Firestore);
97108
const client = ensureFirestoreConfigured(firestore);
98109

@@ -132,7 +143,10 @@ export function getDocFromCache<
132143
>(
133144
reference: DocumentReference<AppType, DbType>
134145
): Promise<DocumentSnapshot<AppType, DbType>> {
135-
reference = cast<DocumentReference<AppType, DbType>>(reference, DocumentReference);
146+
reference = cast<DocumentReference<AppType, DbType>>(
147+
reference,
148+
DocumentReference
149+
);
136150
const firestore = cast(reference.firestore, Firestore);
137151
const client = ensureFirestoreConfigured(firestore);
138152
const userDataWriter = new ExpUserDataWriter(firestore);
@@ -168,7 +182,10 @@ export function getDocFromServer<
168182
>(
169183
reference: DocumentReference<AppType, DbType>
170184
): Promise<DocumentSnapshot<AppType, DbType>> {
171-
reference = cast<DocumentReference<AppType, DbType>>(reference, DocumentReference);
185+
reference = cast<DocumentReference<AppType, DbType>>(
186+
reference,
187+
DocumentReference
188+
);
172189
const firestore = cast(reference.firestore, Firestore);
173190
const client = ensureFirestoreConfigured(firestore);
174191

@@ -219,9 +236,7 @@ export function getDocsFromCache<
219236
DbType extends DocumentData = AppType extends DocumentData
220237
? AppType
221238
: DocumentData
222-
>(
223-
query: Query<AppType, DbType>
224-
): Promise<QuerySnapshot<AppType, DbType>> {
239+
>(query: Query<AppType, DbType>): Promise<QuerySnapshot<AppType, DbType>> {
225240
query = cast<Query<AppType, DbType>>(query, Query);
226241
const firestore = cast(query.firestore, Firestore);
227242
const client = ensureFirestoreConfigured(firestore);
@@ -243,9 +258,7 @@ export function getDocsFromServer<
243258
DbType extends DocumentData = AppType extends DocumentData
244259
? AppType
245260
: DocumentData
246-
>(
247-
query: Query<AppType, DbType>
248-
): Promise<QuerySnapshot<AppType, DbType>> {
261+
>(query: Query<AppType, DbType>): Promise<QuerySnapshot<AppType, DbType>> {
249262
query = cast<Query<AppType, DbType>>(query, Query);
250263
const firestore = cast(query.firestore, Firestore);
251264
const client = ensureFirestoreConfigured(firestore);
@@ -307,7 +320,10 @@ export function setDoc<
307320
data: PartialWithFieldValue<AppType>,
308321
options?: SetOptions
309322
): Promise<void> {
310-
reference = cast<DocumentReference<AppType, DbType>>(reference, DocumentReference);
323+
reference = cast<DocumentReference<AppType, DbType>>(
324+
reference,
325+
DocumentReference
326+
);
311327
const firestore = cast(reference.firestore, Firestore);
312328

313329
const convertedValue = applyFirestoreDataConverter(
@@ -387,7 +403,10 @@ export function updateDoc<
387403
value?: unknown,
388404
...moreFieldsAndValues: unknown[]
389405
): Promise<void> {
390-
reference = cast<DocumentReference<AppType, DbType>>(reference, DocumentReference);
406+
reference = cast<DocumentReference<AppType, DbType>>(
407+
reference,
408+
DocumentReference
409+
);
391410
const firestore = cast(reference.firestore, Firestore);
392411

393412
const dataReader = newUserDataReader(firestore);
@@ -434,9 +453,7 @@ export function deleteDoc<
434453
DbType extends DocumentData = AppType extends DocumentData
435454
? AppType
436455
: DocumentData
437-
>(
438-
reference: DocumentReference<AppType, DbType>
439-
): Promise<void> {
456+
>(reference: DocumentReference<AppType, DbType>): Promise<void> {
440457
const firestore = cast(reference.firestore, Firestore);
441458
const mutations = [new DeleteMutation(reference._key, Precondition.none())];
442459
return executeWrite(firestore, mutations);

packages/firestore/src/api/snapshot.ts

Lines changed: 54 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -83,23 +83,30 @@ import { SnapshotListenOptions } from './reference_impl';
8383
* }
8484
* ```
8585
*/
86-
export interface FirestoreDataConverter<AppType = DocumentData, DbType extends DocumentData = AppType extends DocumentData ? AppType : DocumentData> extends LiteFirestoreDataConverter<AppType, DbType> {
86+
export interface FirestoreDataConverter<
87+
AppType = DocumentData,
88+
DbType extends DocumentData = AppType extends DocumentData
89+
? AppType
90+
: DocumentData
91+
> extends LiteFirestoreDataConverter<AppType, DbType> {
8792
/**
88-
* Called by the Firestore SDK to convert a custom model object of type `T`
89-
* into a plain JavaScript object (suitable for writing directly to the
90-
* Firestore database). To use `set()` with `merge` and `mergeFields`,
91-
* `toFirestore()` must be defined with `PartialWithFieldValue<T>`.
93+
* Called by the Firestore SDK to convert a custom model object of type
94+
* `AppType` into a plain JavaScript object, `DbType` (suitable for writing
95+
* directly to the Firestore database). To use `set()` with `merge` and
96+
* mergeFields`, `toFirestore()` must be defined with
97+
* `PartialWithFieldValue<T>`.
9298
*
9399
* The `WithFieldValue<T>` type extends `T` to also allow FieldValues such as
94100
* {@link (deleteField:1)} to be used as property values.
95101
*/
96102
toFirestore(modelObject: WithFieldValue<AppType>): WithFieldValue<DbType>;
97103

98104
/**
99-
* Called by the Firestore SDK to convert a custom model object of type `T`
100-
* into a plain JavaScript object (suitable for writing directly to the
101-
* Firestore database). Used with {@link (setDoc:1)}, {@link (WriteBatch.set:1)}
102-
* and {@link (Transaction.set:1)} with `merge:true` or `mergeFields`.
105+
* Called by the Firestore SDK to convert a custom model object of type
106+
* `AppType` into a plain JavaScript object, `DbType` (suitable for writing
107+
* directly to the Firestore database). Used with {@link (setDoc:1)},
108+
* {@link (WriteBatch.set:1)} and {@link (Transaction.set:1)} with
109+
* `merge:true` or `mergeFields`.
103110
*
104111
* The `PartialWithFieldValue<T>` type extends `Partial<T>` to allow
105112
* FieldValues such as {@link (arrayUnion:1)} to be used as property values.
@@ -113,7 +120,8 @@ export interface FirestoreDataConverter<AppType = DocumentData, DbType extends D
113120

114121
/**
115122
* Called by the Firestore SDK to convert Firestore data into an object of
116-
* type T. You can access your data by calling: `snapshot.data(options)`.
123+
* type `AppType`. You can access your data by calling:
124+
* `snapshot.data(options)`.
117125
*
118126
* @param snapshot - A `QueryDocumentSnapshot` containing your data and metadata.
119127
* @param options - The `SnapshotOptions` from the initial call to `data()`.
@@ -199,7 +207,12 @@ export type DocumentChangeType = 'added' | 'removed' | 'modified';
199207
* A `DocumentChange` represents a change to the documents matching a query.
200208
* It contains the document affected and the type of change that occurred.
201209
*/
202-
export interface DocumentChange<AppType = DocumentData, DbType extends DocumentData = AppType extends DocumentData ? AppType : DocumentData> {
210+
export interface DocumentChange<
211+
AppType = DocumentData,
212+
DbType extends DocumentData = AppType extends DocumentData
213+
? AppType
214+
: DocumentData
215+
> {
203216
/** The type of change ('added', 'modified', or 'removed'). */
204217
readonly type: DocumentChangeType;
205218

@@ -231,7 +244,12 @@ export interface DocumentChange<AppType = DocumentData, DbType extends DocumentD
231244
* access will return 'undefined'. You can use the `exists()` method to
232245
* explicitly verify a document's existence.
233246
*/
234-
export class DocumentSnapshot<AppType = DocumentData, DbType extends DocumentData = AppType extends DocumentData ? AppType : DocumentData> extends LiteDocumentSnapshot<AppType, DbType> {
247+
export class DocumentSnapshot<
248+
AppType = DocumentData,
249+
DbType extends DocumentData = AppType extends DocumentData
250+
? AppType
251+
: DocumentData
252+
> extends LiteDocumentSnapshot<AppType, DbType> {
235253
private readonly _firestoreImpl: Firestore;
236254

237255
/**
@@ -343,7 +361,12 @@ export class DocumentSnapshot<AppType = DocumentData, DbType extends DocumentDat
343361
* `exists` property will always be true and `data()` will never return
344362
* 'undefined'.
345363
*/
346-
export class QueryDocumentSnapshot<AppType = DocumentData, DbType extends DocumentData = AppType extends DocumentData ? AppType : DocumentData> extends DocumentSnapshot<AppType, DbType> {
364+
export class QueryDocumentSnapshot<
365+
AppType = DocumentData,
366+
DbType extends DocumentData = AppType extends DocumentData
367+
? AppType
368+
: DocumentData
369+
> extends DocumentSnapshot<AppType, DbType> {
347370
/**
348371
* Retrieves all fields in the document as an `Object`.
349372
*
@@ -369,7 +392,12 @@ export class QueryDocumentSnapshot<AppType = DocumentData, DbType extends Docume
369392
* number of documents can be determined via the `empty` and `size`
370393
* properties.
371394
*/
372-
export class QuerySnapshot<AppType = DocumentData, DbType extends DocumentData = AppType extends DocumentData ? AppType : DocumentData> {
395+
export class QuerySnapshot<
396+
AppType = DocumentData,
397+
DbType extends DocumentData = AppType extends DocumentData
398+
? AppType
399+
: DocumentData
400+
> {
373401
/**
374402
* Metadata about this snapshot, concerning its source and if it has local
375403
* modifications.
@@ -454,7 +482,9 @@ export class QuerySnapshot<AppType = DocumentData, DbType extends DocumentData =
454482
* changes (i.e. only `DocumentSnapshot.metadata` changed) should trigger
455483
* snapshot events.
456484
*/
457-
docChanges(options: SnapshotListenOptions = {}): Array<DocumentChange<AppType, DbType>> {
485+
docChanges(
486+
options: SnapshotListenOptions = {}
487+
): Array<DocumentChange<AppType, DbType>> {
458488
const includeMetadataChanges = !!options.includeMetadataChanges;
459489

460490
if (includeMetadataChanges && this._snapshot.excludesMetadataChanges) {
@@ -583,7 +613,15 @@ export function resultChangeType(type: ChangeType): DocumentChangeType {
583613
* @param right - A snapshot to compare.
584614
* @returns true if the snapshots are equal.
585615
*/
586-
export function snapshotEqual<AppType = DocumentData, DbType extends DocumentData = AppType extends DocumentData ? AppType : DocumentData>(left: DocumentSnapshot<AppType, DbType> | QuerySnapshot<AppType, DbType>, right: DocumentSnapshot<AppType, DbType> | QuerySnapshot<AppType, DbType>): boolean {
616+
export function snapshotEqual<
617+
AppType = DocumentData,
618+
DbType extends DocumentData = AppType extends DocumentData
619+
? AppType
620+
: DocumentData
621+
>(
622+
left: DocumentSnapshot<AppType, DbType> | QuerySnapshot<AppType, DbType>,
623+
right: DocumentSnapshot<AppType, DbType> | QuerySnapshot<AppType, DbType>
624+
): boolean {
587625
if (left instanceof DocumentSnapshot && right instanceof DocumentSnapshot) {
588626
return (
589627
left._firestore === right._firestore &&

packages/firestore/src/api/transaction.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
DEFAULT_TRANSACTION_OPTIONS,
2323
validateTransactionOptions
2424
} from '../core/transaction_options';
25-
import {DocumentData, DocumentReference} from '../lite-api/reference';
25+
import { DocumentData, DocumentReference } from '../lite-api/reference';
2626
import { Transaction as LiteTransaction } from '../lite-api/transaction';
2727
import { validateReference } from '../lite-api/write_batch';
2828
import { cast } from '../util/input_validation';
@@ -57,8 +57,13 @@ export class Transaction extends LiteTransaction {
5757
* @param documentRef - A reference to the document to be read.
5858
* @returns A `DocumentSnapshot` with the read data.
5959
*/
60-
get<AppType, DbType extends DocumentData>(documentRef: DocumentReference<AppType, DbType>): Promise<DocumentSnapshot<AppType, DbType>> {
61-
const ref = validateReference<AppType, DbType>(documentRef, this._firestore);
60+
get<AppType, DbType extends DocumentData>(
61+
documentRef: DocumentReference<AppType, DbType>
62+
): Promise<DocumentSnapshot<AppType, DbType>> {
63+
const ref = validateReference<AppType, DbType>(
64+
documentRef,
65+
this._firestore
66+
);
6267
const userDataWriter = new ExpUserDataWriter(this._firestore);
6368
return super
6469
.get(documentRef)

packages/firestore/src/lite-api/snapshot.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ import { AbstractUserDataWriter } from './user_data_writer';
5656
* }
5757
* }
5858
*
59-
* const postConverter = {
59+
* const postConverter: FirestoreDataConverter<Post, DocumentData> = {
6060
* toFirestore(post: WithFieldValue<Post>): DocumentData {
6161
* return {title: post.title, author: post.author};
6262
* },

0 commit comments

Comments
 (0)