Skip to content

Commit 949b839

Browse files
committed
fix(firestore): adjust modular types for withConverter
1 parent 30fe1f5 commit 949b839

File tree

7 files changed

+1044
-66
lines changed

7 files changed

+1044
-66
lines changed

packages/firestore/lib/modular/index.d.ts

Lines changed: 251 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import FieldValue = FirebaseFirestoreTypes.FieldValue;
88
import FieldPath = FirebaseFirestoreTypes.FieldPath;
99
import PersistentCacheIndexManager = FirebaseFirestoreTypes.PersistentCacheIndexManager;
1010
import AggregateQuerySnapshot = FirebaseFirestoreTypes.AggregateQuerySnapshot;
11+
import SetOptions = FirebaseFirestoreTypes.SetOptions;
1112

1213
/** Primitive types. */
1314
export type Primitive = string | number | boolean | undefined | null;
@@ -535,6 +536,240 @@ export interface DocumentReference<
535536
): DocumentReference<NewAppModelType, NewDbModelType>;
536537
}
537538

539+
/**
540+
* A write batch, used to perform multiple writes as a single atomic unit.
541+
*
542+
* A `WriteBatch` object can be acquired by calling {@link writeBatch}. It
543+
* provides methods for adding writes to the write batch. None of the writes
544+
* will be committed (or visible locally) until {@link WriteBatch.commit} is
545+
* called.
546+
*/
547+
export class WriteBatch {
548+
/**
549+
* Writes to the document referred to by the provided {@link
550+
* DocumentReference}. If the document does not exist yet, it will be created.
551+
*
552+
* @param documentRef - A reference to the document to be set.
553+
* @param data - An object of the fields and values for the document.
554+
* @returns This `WriteBatch` instance. Used for chaining method calls.
555+
*/
556+
set<AppModelType, DbModelType extends DocumentData>(
557+
documentRef: DocumentReference<AppModelType, DbModelType>,
558+
data: WithFieldValue<AppModelType>,
559+
): WriteBatch;
560+
/**
561+
* Writes to the document referred to by the provided {@link
562+
* DocumentReference}. If the document does not exist yet, it will be created.
563+
* If you provide `merge` or `mergeFields`, the provided data can be merged
564+
* into an existing document.
565+
*
566+
* @param documentRef - A reference to the document to be set.
567+
* @param data - An object of the fields and values for the document.
568+
* @param options - An object to configure the set behavior.
569+
* @throws Error - If the provided input is not a valid Firestore document.
570+
* @returns This `WriteBatch` instance. Used for chaining method calls.
571+
*/
572+
set<AppModelType, DbModelType extends DocumentData>(
573+
documentRef: DocumentReference<AppModelType, DbModelType>,
574+
data: PartialWithFieldValue<AppModelType>,
575+
options: SetOptions,
576+
): WriteBatch;
577+
/**
578+
* Updates fields in the document referred to by the provided {@link
579+
* DocumentReference}. The update will fail if applied to a document that does
580+
* not exist.
581+
*
582+
* @param documentRef - A reference to the document to be updated.
583+
* @param data - An object containing the fields and values with which to
584+
* update the document. Fields can contain dots to reference nested fields
585+
* within the document.
586+
* @throws Error - If the provided input is not valid Firestore data.
587+
* @returns This `WriteBatch` instance. Used for chaining method calls.
588+
*/
589+
update<AppModelType, DbModelType extends DocumentData>(
590+
documentRef: DocumentReference<AppModelType, DbModelType>,
591+
data: UpdateData<DbModelType>,
592+
): WriteBatch;
593+
/**
594+
* Updates fields in the document referred to by this {@link
595+
* DocumentReference}. The update will fail if applied to a document that does
596+
* not exist.
597+
*
598+
* Nested fields can be update by providing dot-separated field path strings
599+
* or by providing `FieldPath` objects.
600+
*
601+
* @param documentRef - A reference to the document to be updated.
602+
* @param field - The first field to update.
603+
* @param value - The first value.
604+
* @param moreFieldsAndValues - Additional key value pairs.
605+
* @throws Error - If the provided input is not valid Firestore data.
606+
* @returns This `WriteBatch` instance. Used for chaining method calls.
607+
*/
608+
update<AppModelType, DbModelType extends DocumentData>(
609+
documentRef: DocumentReference<AppModelType, DbModelType>,
610+
field: string | FieldPath,
611+
value: unknown,
612+
...moreFieldsAndValues: unknown[]
613+
): WriteBatch;
614+
update<AppModelType, DbModelType extends DocumentData>(
615+
documentRef: DocumentReference<AppModelType, DbModelType>,
616+
fieldOrUpdateData: string | FieldPath | UpdateData<DbModelType>,
617+
value?: unknown,
618+
...moreFieldsAndValues: unknown[]
619+
): WriteBatch;
620+
621+
/**
622+
* Deletes the document referred to by the provided {@link DocumentReference}.
623+
*
624+
* @param documentRef - A reference to the document to be deleted.
625+
* @returns This `WriteBatch` instance. Used for chaining method calls.
626+
*/
627+
delete<AppModelType, DbModelType extends DocumentData>(
628+
documentRef: DocumentReference<AppModelType, DbModelType>,
629+
): WriteBatch;
630+
/**
631+
* Commits all of the writes in this write batch as a single atomic unit.
632+
*
633+
* The result of these writes will only be reflected in document reads that
634+
* occur after the returned promise resolves. If the client is offline, the
635+
* write fails. If you would like to see local modifications or buffer writes
636+
* until the client is online, use the full Firestore SDK.
637+
*
638+
* @returns A `Promise` resolved once all of the writes in the batch have been
639+
* successfully written to the backend as an atomic unit (note that it won't
640+
* resolve while you're offline).
641+
*/
642+
commit(): Promise<void>;
643+
}
644+
645+
/**
646+
* A reference to a transaction.
647+
*
648+
* The `Transaction` object passed to a transaction's `updateFunction` provides
649+
* the methods to read and write data within the transaction context. See
650+
* {@link runTransaction}.
651+
*/
652+
export class Transaction extends LiteTransaction {
653+
/**
654+
* Reads the document referenced by the provided {@link DocumentReference}.
655+
*
656+
* @param documentRef - A reference to the document to be read.
657+
* @returns A `DocumentSnapshot` with the read data.
658+
*/
659+
get<AppModelType, DbModelType extends DocumentData>(
660+
documentRef: DocumentReference<AppModelType, DbModelType>,
661+
): Promise<DocumentSnapshot<AppModelType, DbModelType>>;
662+
663+
/**
664+
* Writes to the document referred to by the provided {@link
665+
* DocumentReference}. If the document does not exist yet, it will be created.
666+
*
667+
* @param documentRef - A reference to the document to be set.
668+
* @param data - An object of the fields and values for the document.
669+
* @throws Error - If the provided input is not a valid Firestore document.
670+
* @returns This `Transaction` instance. Used for chaining method calls.
671+
*/
672+
set<AppModelType, DbModelType extends DocumentData>(
673+
documentRef: DocumentReference<AppModelType, DbModelType>,
674+
data: WithFieldValue<AppModelType>,
675+
): this;
676+
/**
677+
* Writes to the document referred to by the provided {@link
678+
* DocumentReference}. If the document does not exist yet, it will be created.
679+
* If you provide `merge` or `mergeFields`, the provided data can be merged
680+
* into an existing document.
681+
*
682+
* @param documentRef - A reference to the document to be set.
683+
* @param data - An object of the fields and values for the document.
684+
* @param options - An object to configure the set behavior.
685+
* @throws Error - If the provided input is not a valid Firestore document.
686+
* @returns This `Transaction` instance. Used for chaining method calls.
687+
*/
688+
set<AppModelType, DbModelType extends DocumentData>(
689+
documentRef: DocumentReference<AppModelType, DbModelType>,
690+
data: PartialWithFieldValue<AppModelType>,
691+
options: SetOptions,
692+
): this;
693+
694+
/**
695+
* Updates fields in the document referred to by the provided {@link
696+
* DocumentReference}. The update will fail if applied to a document that does
697+
* not exist.
698+
*
699+
* @param documentRef - A reference to the document to be updated.
700+
* @param data - An object containing the fields and values with which to
701+
* update the document. Fields can contain dots to reference nested fields
702+
* within the document.
703+
* @throws Error - If the provided input is not valid Firestore data.
704+
* @returns This `Transaction` instance. Used for chaining method calls.
705+
*/
706+
update<AppModelType, DbModelType extends DocumentData>(
707+
documentRef: DocumentReference<AppModelType, DbModelType>,
708+
data: UpdateData<DbModelType>,
709+
): this;
710+
/**
711+
* Updates fields in the document referred to by the provided {@link
712+
* DocumentReference}. The update will fail if applied to a document that does
713+
* not exist.
714+
*
715+
* Nested fields can be updated by providing dot-separated field path
716+
* strings or by providing `FieldPath` objects.
717+
*
718+
* @param documentRef - A reference to the document to be updated.
719+
* @param field - The first field to update.
720+
* @param value - The first value.
721+
* @param moreFieldsAndValues - Additional key/value pairs.
722+
* @throws Error - If the provided input is not valid Firestore data.
723+
* @returns This `Transaction` instance. Used for chaining method calls.
724+
*/
725+
update<AppModelType, DbModelType extends DocumentData>(
726+
documentRef: DocumentReference<AppModelType, DbModelType>,
727+
field: string | FieldPath,
728+
value: unknown,
729+
...moreFieldsAndValues: unknown[]
730+
): this;
731+
update<AppModelType, DbModelType extends DocumentData>(
732+
documentRef: DocumentReference<AppModelType, DbModelType>,
733+
fieldOrUpdateData: string | FieldPath | UpdateData<DbModelType>,
734+
value?: unknown,
735+
...moreFieldsAndValues: unknown[]
736+
): this;
737+
/**
738+
* Deletes the document referred to by the provided {@link DocumentReference}.
739+
*
740+
* @param documentRef - A reference to the document to be deleted.
741+
* @returns This `Transaction` instance. Used for chaining method calls.
742+
*/
743+
delete<AppModelType, DbModelType extends DocumentData>(
744+
documentRef: DocumentReference<AppModelType, DbModelType>,
745+
): this;
746+
}
747+
748+
/**
749+
* Executes the given `updateFunction` and then attempts to commit the changes
750+
* applied within the transaction. If any document read within the transaction
751+
* has changed, Cloud Firestore retries the `updateFunction`. If it fails to
752+
* commit after 5 attempts, the transaction fails.
753+
*
754+
* The maximum number of writes allowed in a single transaction is 500.
755+
*
756+
* @param firestore - A reference to the Firestore database to run this
757+
* transaction against.
758+
* @param updateFunction - The function to execute within the transaction
759+
* context.
760+
* @param options - An options object to configure maximum number of attempts to
761+
* commit.
762+
* @returns If the transaction completed successfully or was explicitly aborted
763+
* (the `updateFunction` returned a failed promise), the promise returned by the
764+
* `updateFunction `is returned here. Otherwise, if the transaction failed, a
765+
* rejected promise with the corresponding failure error is returned.
766+
*/
767+
export function runTransaction<T>(
768+
firestore: Firestore,
769+
updateFunction: (transaction: Transaction) => Promise<T>,
770+
options?: TransactionOptions,
771+
): Promise<T>;
772+
538773
/**
539774
* Gets a `DocumentReference` instance that refers to the document at the
540775
* specified absolute path.
@@ -717,7 +952,10 @@ export function collectionGroup(
717952
* @returns A `Promise` resolved once the data has been successfully written
718953
* to the backend (note that it won't resolve while you're offline).
719954
*/
720-
export function setDoc<T>(reference: DocumentReference<T>, data: WithFieldValue<T>): Promise<void>;
955+
export function setDoc<AppModelType, DbModelType extends DocumentData>(
956+
reference: DocumentReference<AppModelType, DbModelType>,
957+
data: WithFieldValue<AppModelType>,
958+
): Promise<void>;
721959

722960
/**
723961
* Writes to the document referred to by the specified `DocumentReference`. If
@@ -730,18 +968,11 @@ export function setDoc<T>(reference: DocumentReference<T>, data: WithFieldValue<
730968
* @returns A Promise resolved once the data has been successfully written
731969
* to the backend (note that it won't resolve while you're offline).
732970
*/
733-
export function setDoc<T>(
734-
reference: DocumentReference<T>,
735-
data: PartialWithFieldValue<T>,
736-
options: FirebaseFirestoreTypes.SetOptions,
737-
): Promise<void>;
738-
739-
export function setDoc<T>(
740-
reference: DocumentReference<T>,
741-
data: PartialWithFieldValue<T>,
742-
options?: FirebaseFirestoreTypes.SetOptions,
971+
export function setDoc<AppModelType, DbModelType extends DocumentData>(
972+
reference: DocumentReference<AppModelType, DbModelType>,
973+
data: PartialWithFieldValue<AppModelType>,
974+
options: SetOptions,
743975
): Promise<void>;
744-
745976
/**
746977
* Updates fields in the document referred to by the specified
747978
* `DocumentReference`. The update will fail if applied to a document that does
@@ -754,7 +985,10 @@ export function setDoc<T>(
754985
* @returns A `Promise` resolved once the data has been successfully written
755986
* to the backend (note that it won't resolve while you're offline).
756987
*/
757-
export function updateDoc<T>(reference: DocumentReference<T>, data: UpdateData<T>): Promise<void>;
988+
export function updateDoc<AppModelType, DbModelType extends DocumentData>(
989+
reference: DocumentReference<AppModelType, DbModelType>,
990+
data: UpdateData<DbModelType>,
991+
): Promise<void>;
758992
/**
759993
* Updates fields in the document referred to by the specified
760994
* `DocumentReference` The update will fail if applied to a document that does
@@ -770,13 +1004,12 @@ export function updateDoc<T>(reference: DocumentReference<T>, data: UpdateData<T
7701004
* @returns A `Promise` resolved once the data has been successfully written
7711005
* to the backend (note that it won't resolve while you're offline).
7721006
*/
773-
export function updateDoc(
774-
reference: DocumentReference<unknown>,
1007+
export function updateDoc<AppModelType, DbModelType extends DocumentData>(
1008+
reference: DocumentReference<AppModelType, DbModelType>,
7751009
field: string | FieldPath,
7761010
value: unknown,
7771011
...moreFieldsAndValues: unknown[]
7781012
): Promise<void>;
779-
7801013
/**
7811014
* Add a new document to specified `CollectionReference` with the given data,
7821015
* assigning it a document ID automatically.
@@ -912,7 +1145,7 @@ export function setLogLevel(logLevel: LogLevel): void;
9121145
*/
9131146
export function runTransaction<T>(
9141147
firestore: Firestore,
915-
updateFunction: (transaction: FirebaseFirestoreTypes.Transaction) => Promise<T>,
1148+
updateFunction: (transaction: Transaction) => Promise<T>,
9161149
): Promise<T>;
9171150

9181151
/**
@@ -1120,7 +1353,7 @@ export function namedQuery(firestore: Firestore, name: string): Promise<Query |
11201353
* @returns A `WriteBatch` that can be used to atomically execute multiple
11211354
* writes.
11221355
*/
1123-
export function writeBatch(firestore: Firestore): FirebaseFirestoreTypes.WriteBatch;
1356+
export function writeBatch(firestore: Firestore): WriteBatch;
11241357

11251358
/**
11261359
* Gets the `PersistentCacheIndexManager` instance used by this Cloud Firestore instance.

packages/firestore/lib/modular/index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
/**
22
* @typedef {import('..').FirebaseFirestoreTypes} FirebaseFirestoreTypes
3-
* @typedef {import('..').FirebaseFirestoreTypes.CollectionReference} CollectionReference
43
* @typedef {import('..').FirebaseFirestoreTypes.DocumentData} DocumentData
5-
* @typedef {import('..').FirebaseFirestoreTypes.DocumentReference} DocumentReference
64
* @typedef {import('..').FirebaseFirestoreTypes.FieldPath} FieldPath
75
* @typedef {import('..').FirebaseFirestoreTypes.Module} Firestore
8-
* @typedef {import('..').FirebaseFirestoreTypes.Query} Query
96
* @typedef {import('..').FirebaseFirestoreTypes.SetOptions} SetOptions
107
* @typedef {import('..').FirebaseFirestoreTypes.Settings} FirestoreSettings
118
* @typedef {import('..').FirebaseFirestoreTypes.PersistentCacheIndexManager} PersistentCacheIndexManager
9+
* @typedef {import('.').CollectionReference} CollectionReference
10+
* @typedef {import('.').DocumentReference} DocumentReference
11+
* @typedef {import('./query').Query} Query
12+
1213
* @typedef {import('@firebase/app').FirebaseApp} FirebaseApp
1314
*/
1415

0 commit comments

Comments
 (0)