31 lines
748 B
TypeScript
31 lines
748 B
TypeScript
import {
|
|
collection,
|
|
doc,
|
|
type DocumentData,
|
|
type FirestoreDataConverter,
|
|
type QueryDocumentSnapshot,
|
|
type SnapshotOptions,
|
|
} from 'firebase/firestore';
|
|
|
|
import { db } from '@/lib/firebase';
|
|
import type { UserDocument } from '@/types/firestore';
|
|
|
|
const converter = <T extends DocumentData>(): FirestoreDataConverter<T> => ({
|
|
toFirestore: (value: T) => value,
|
|
fromFirestore: (
|
|
snapshot: QueryDocumentSnapshot,
|
|
options: SnapshotOptions
|
|
) => snapshot.data(options) as T,
|
|
});
|
|
|
|
export const collectionNames = {
|
|
users: 'users',
|
|
} as const;
|
|
|
|
export const usersCollection = collection(
|
|
db,
|
|
collectionNames.users
|
|
).withConverter(converter<UserDocument>());
|
|
|
|
export const userDoc = (uid: string) => doc(usersCollection, uid);
|