sindre.bakken.naesset@gmail.com
Using Firestore with Typescript
const app = initializeApp(firebaseConfig); export const auth = getAuth(app); export const firestore = getFirestore(app); const converter = <T extends DocumentData>(): FirestoreDataConverter<T> => ({ toFirestore: (user: T) => user, fromFirestore: (snapshot: QueryDocumentSnapshot) => { return snapshot.data() as T; }, }); const dataPoint = <T extends DocumentData>(collectionPath: string, ...pathSegments: string[]) => collection(firestore, collectionPath, ...pathSegments).withConverter(converter<T>()); const db = { users: dataPoint<FirestoreUser>('users'), matches: dataPoint<FirestoreMatch>('matches'), messages: (matchId: string) => dataPoint<ChatMessage>('matches', matchId, 'messages'), user: (userId: string) => doc(dataPoint<FirestoreUser>('users'), userId), match: (matchId: string) => doc(dataPoint<FirestoreMatch>('matches'), matchId), message: (matchId: string, messageId: string) => doc(dataPoint<ChatMessage>('matches', matchId, 'messages'), messageId), }; export default db;