Last active
November 17, 2020 03:04
-
-
Save polidog/cf10ccc1b22bfb1fa0850a542491b7be to your computer and use it in GitHub Desktop.
firestoreをfunctionsで使う時のあれ
This file contains hidden or 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
import admin from 'firebase-admin' | |
const db = admin.firestore() | |
export type Document<T> = { | |
readonly id: string | |
readonly ref: admin.firestore.DocumentReference<admin.firestore.DocumentData> | |
readonly exists: boolean | |
data: () => T | |
} | |
export const findDoc = async <T>(path: string): Promise<Document<T>> => { | |
const doc = await db.doc(path).get() | |
return { | |
id: doc.id, | |
ref: doc.ref, | |
exists: doc.exists, | |
data: (): T => { | |
if (!doc.exists) { | |
throw Error('data not found.') | |
} | |
return ({ ...doc.data(), id: doc.id } as unknown) as T | |
}, | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment