Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ typed abstractions that work with Firebase's web SDK (`firebase/firestore`).
- `FsDocument<T>` - Immutable document with `id` and `data`
- `FsMutableDocument<T>` - Adds `ref`, `update`, `updateWithPartial`, and
`delete` methods
- `FsMutableDocumentInTransaction<T>` - Transaction variant
- `FsMutableDocumentTx<T>` - Transaction variant

**Main Exports**:

Expand Down
21 changes: 11 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
16 changes: 11 additions & 5 deletions src/get-document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T extends DocumentData>(
collectionRef: CollectionReference<T>,
Expand Down Expand Up @@ -52,7 +52,7 @@ export async function getDocumentDataMaybe<T extends DocumentData>(
return snapshot.data();
}

export async function getDocumentInTransaction<T extends DocumentData>(
export async function getDocumentTx<T extends DocumentData>(
transaction: Transaction,
collectionRef: CollectionReference<T>,
documentId: string,
Expand All @@ -61,10 +61,10 @@ export async function getDocumentInTransaction<T extends DocumentData>(

invariant(snapshot.exists(), `No document available at ${collectionRef.path}/${documentId}`);

return makeMutableDocument(snapshot);
return makeMutableDocumentTx(snapshot, transaction);
}

export async function getDocumentInTransactionMaybe<T extends DocumentData>(
export async function getDocumentMaybeTx<T extends DocumentData>(
transaction: Transaction,
collectionRef: CollectionReference<T>,
documentId: string,
Expand All @@ -75,5 +75,11 @@ export async function getDocumentInTransactionMaybe<T extends DocumentData>(
return;
}

return makeMutableDocument(snapshot);
return makeMutableDocumentTx(snapshot, transaction);
}

/** @deprecated Use `getDocumentTx` instead */
export const getDocumentInTransaction = getDocumentTx;

/** @deprecated Use `getDocumentMaybeTx` instead */
export const getDocumentInTransactionMaybe = getDocumentMaybeTx;
10 changes: 6 additions & 4 deletions src/get-specific-document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T extends DocumentData>(
documentRef: DocumentReference<T>,
Expand All @@ -28,13 +27,16 @@ export async function getSpecificDocumentData<T extends DocumentData>(
return docSnap.data();
}

export async function getSpecificDocumentFromTransaction<T extends DocumentData>(
export async function getSpecificDocumentTx<T extends DocumentData>(
transaction: Transaction,
documentRef: DocumentReference<T>,
) {
const snapshot = await transaction.get(documentRef);

invariant(snapshot.exists(), `No document available at ${documentRef.path}`);

return makeDocument(snapshot);
return makeMutableDocumentTx(snapshot, transaction);
}

/** @deprecated Use `getSpecificDocumentTx` instead */
export const getSpecificDocumentFromTransaction = getSpecificDocumentTx;
9 changes: 6 additions & 3 deletions src/make-mutable-document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T extends DocumentData>(
doc: DocumentSnapshot<T>,
Expand All @@ -25,10 +25,10 @@ export function makeMutableDocument<T extends DocumentData>(
};
}

export function makeMutableDocumentInTransaction<T extends DocumentData>(
export function makeMutableDocumentTx<T extends DocumentData>(
doc: DocumentSnapshot<T>,
tx: Transaction,
): FsMutableDocumentInTransaction<T> {
): FsMutableDocumentTx<T> {
const data = doc.data();
if (!data) {
throw new Error(`Document ${doc.ref.path} exists but has no data`);
Expand All @@ -42,3 +42,6 @@ export function makeMutableDocumentInTransaction<T extends DocumentData>(
delete: () => tx.delete(doc.ref),
};
}

/** @deprecated Use `makeMutableDocumentTx` instead */
export const makeMutableDocumentInTransaction = makeMutableDocumentTx;
5 changes: 4 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export type FsMutableDocument<T> = Readonly<{
}> &
FsDocument<T>;

export type FsMutableDocumentInTransaction<T> = Readonly<{
export type FsMutableDocumentTx<T> = Readonly<{
ref: DocumentReference;
Comment thread
0x80 marked this conversation as resolved.
update: (data: UpdateData<T>) => Transaction;
/**
Expand All @@ -34,3 +34,6 @@ export type FsMutableDocumentInTransaction<T> = Readonly<{
delete: () => Transaction;
}> &
FsDocument<T>;

/** @deprecated Use `FsMutableDocumentTx` instead */
export type FsMutableDocumentInTransaction<T> = FsMutableDocumentTx<T>;