diff --git a/CLAUDE.md b/CLAUDE.md index 2c08bc9..8b9bba6 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -27,7 +27,7 @@ typed abstractions that work with Firebase's web SDK (`firebase/firestore`). - `FsDocument` - Immutable document with `id` and `data` - `FsMutableDocument` - Adds `ref`, `update`, `updateWithPartial`, and `delete` methods -- `FsMutableDocumentInTransaction` - Transaction variant +- `FsMutableDocumentTx` - Transaction variant **Main Exports**: diff --git a/README.md b/README.md index 5141ca1..d1bcf03 100644 --- a/README.md +++ b/README.md @@ -113,16 +113,17 @@ const { data, isError } = useQuery({ }); ``` -| Function | Description | -| ---------------------------------- | -------------------------------------------------------------- | -| `getDocument` | Fetch a document | -| `getDocumentData` | Fetch only the data part of a document | -| `getDocumentMaybe` | Fetch a document that might not exist | -| `getDocumentInTransaction` | Fetch a document as part of a transaction | -| `getDocumentInTransactionMaybe` | Fetch a document that might not exist as part of a transaction | -| `getSpecificDocument` | Fetch a specific document | -| `getSpecificDocumentData` | Fetch only the data part of a specific document | -| `getSpecificDocumentInTransaction` | Fetch a specific document as part of a transaction | +| Function | Description | +| ------------------------- | -------------------------------------------------------------- | +| `getDocument` | Fetch a document | +| `getDocumentData` | Fetch only the data part of a document | +| `getDocumentMaybe` | Fetch a document that might not exist | +| `getDocumentDataMaybe` | Fetch only the data part of a document that might not exist | +| `getDocumentTx` | Fetch a document as part of a transaction | +| `getDocumentMaybeTx` | Fetch a document that might not exist as part of a transaction | +| `getSpecificDocument` | Fetch a specific document | +| `getSpecificDocumentData` | Fetch only the data part of a specific document | +| `getSpecificDocumentTx` | Fetch a specific document as part of a transaction | ### Write Functions diff --git a/src/get-document.ts b/src/get-document.ts index 9ca95de..0a902a1 100644 --- a/src/get-document.ts +++ b/src/get-document.ts @@ -6,7 +6,7 @@ import { type Transaction, } from "firebase/firestore"; import { invariant } from "~/utils"; -import { makeMutableDocument } from "./make-mutable-document"; +import { makeMutableDocument, makeMutableDocumentTx } from "./make-mutable-document"; export async function getDocument( collectionRef: CollectionReference, @@ -52,7 +52,7 @@ export async function getDocumentDataMaybe( return snapshot.data(); } -export async function getDocumentInTransaction( +export async function getDocumentTx( transaction: Transaction, collectionRef: CollectionReference, documentId: string, @@ -61,10 +61,10 @@ export async function getDocumentInTransaction( invariant(snapshot.exists(), `No document available at ${collectionRef.path}/${documentId}`); - return makeMutableDocument(snapshot); + return makeMutableDocumentTx(snapshot, transaction); } -export async function getDocumentInTransactionMaybe( +export async function getDocumentMaybeTx( transaction: Transaction, collectionRef: CollectionReference, documentId: string, @@ -75,5 +75,11 @@ export async function getDocumentInTransactionMaybe( return; } - return makeMutableDocument(snapshot); + return makeMutableDocumentTx(snapshot, transaction); } + +/** @deprecated Use `getDocumentTx` instead */ +export const getDocumentInTransaction = getDocumentTx; + +/** @deprecated Use `getDocumentMaybeTx` instead */ +export const getDocumentInTransactionMaybe = getDocumentMaybeTx; diff --git a/src/get-specific-document.ts b/src/get-specific-document.ts index 9b7e304..76efee9 100644 --- a/src/get-specific-document.ts +++ b/src/get-specific-document.ts @@ -5,8 +5,7 @@ import { type Transaction, } from "firebase/firestore"; import { invariant } from "~/utils"; -import { makeDocument } from "./make-document"; -import { makeMutableDocument } from "./make-mutable-document"; +import { makeMutableDocument, makeMutableDocumentTx } from "./make-mutable-document"; export async function getSpecificDocument( documentRef: DocumentReference, @@ -28,7 +27,7 @@ export async function getSpecificDocumentData( return docSnap.data(); } -export async function getSpecificDocumentFromTransaction( +export async function getSpecificDocumentTx( transaction: Transaction, documentRef: DocumentReference, ) { @@ -36,5 +35,8 @@ export async function getSpecificDocumentFromTransaction invariant(snapshot.exists(), `No document available at ${documentRef.path}`); - return makeDocument(snapshot); + return makeMutableDocumentTx(snapshot, transaction); } + +/** @deprecated Use `getSpecificDocumentTx` instead */ +export const getSpecificDocumentFromTransaction = getSpecificDocumentTx; diff --git a/src/make-mutable-document.ts b/src/make-mutable-document.ts index 93171e3..583a091 100644 --- a/src/make-mutable-document.ts +++ b/src/make-mutable-document.ts @@ -6,7 +6,7 @@ import { type Transaction, type UpdateData, } from "firebase/firestore"; -import type { FsMutableDocument, FsMutableDocumentInTransaction } from "~/types"; +import type { FsMutableDocument, FsMutableDocumentTx } from "~/types"; export function makeMutableDocument( doc: DocumentSnapshot, @@ -25,10 +25,10 @@ export function makeMutableDocument( }; } -export function makeMutableDocumentInTransaction( +export function makeMutableDocumentTx( doc: DocumentSnapshot, tx: Transaction, -): FsMutableDocumentInTransaction { +): FsMutableDocumentTx { const data = doc.data(); if (!data) { throw new Error(`Document ${doc.ref.path} exists but has no data`); @@ -42,3 +42,6 @@ export function makeMutableDocumentInTransaction( delete: () => tx.delete(doc.ref), }; } + +/** @deprecated Use `makeMutableDocumentTx` instead */ +export const makeMutableDocumentInTransaction = makeMutableDocumentTx; diff --git a/src/types.ts b/src/types.ts index 9173556..0af6344 100644 --- a/src/types.ts +++ b/src/types.ts @@ -22,7 +22,7 @@ export type FsMutableDocument = Readonly<{ }> & FsDocument; -export type FsMutableDocumentInTransaction = Readonly<{ +export type FsMutableDocumentTx = Readonly<{ ref: DocumentReference; update: (data: UpdateData) => Transaction; /** @@ -34,3 +34,6 @@ export type FsMutableDocumentInTransaction = Readonly<{ delete: () => Transaction; }> & FsDocument; + +/** @deprecated Use `FsMutableDocumentTx` instead */ +export type FsMutableDocumentInTransaction = FsMutableDocumentTx;