generated from jphacks/JP_sample
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
1 changed file
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,54 @@ | ||
// src/firebase/firestore.ts | ||
import { db } from "./client-app"; | ||
import { | ||
collection, | ||
addDoc, | ||
getDocs, | ||
updateDoc, | ||
deleteDoc, | ||
doc, | ||
//追加 | ||
WithFieldValue, | ||
DocumentData, | ||
getDoc, | ||
} from "firebase/firestore"; | ||
|
||
/** dataが WithFieldValue<DocumentData> を拡張するように制約を追加 */ | ||
export async function createData<T extends WithFieldValue<DocumentData>>( | ||
collectionName: string, | ||
data: T | ||
): Promise<string> { | ||
const docRef = await addDoc(collection(db, collectionName), data); | ||
return docRef.id; | ||
} | ||
|
||
export async function readData<T>(collectionName: string): Promise<T[]> { | ||
const snapshot = await getDocs(collection(db, collectionName)); | ||
return snapshot.docs.map((doc) => ({ id: doc.id, ...doc.data() } as T)); | ||
} | ||
|
||
export async function readSingleData<T>( | ||
collectionName: string, | ||
id: string | ||
): Promise<T | null> { | ||
const docRef = doc(db, collectionName, id); | ||
const snapshot = await getDoc(docRef); | ||
return snapshot.exists() ? (snapshot.data() as T) : null; | ||
} | ||
|
||
export async function updateData<T>( | ||
collectionName: string, | ||
id: string, | ||
data: Partial<T> | ||
): Promise<void> { | ||
const docRef = doc(db, collectionName, id); | ||
await updateDoc(docRef, data); | ||
} | ||
|
||
export async function deleteData( | ||
collectionName: string, | ||
id: string | ||
): Promise<void> { | ||
const docRef = doc(db, collectionName, id); | ||
await deleteDoc(docRef); | ||
} |