Skip to content

Instantly share code, notes, and snippets.

@polidog
Last active November 17, 2020 03:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save polidog/cf10ccc1b22bfb1fa0850a542491b7be to your computer and use it in GitHub Desktop.
Save polidog/cf10ccc1b22bfb1fa0850a542491b7be to your computer and use it in GitHub Desktop.
firestoreをfunctionsで使う時のあれ
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