𝘉𝘭𝘶𝘦𝘚𝘱𝘢𝘳𝘬
Firestore library for TypeScript using io-ts
Installation
yarn add firebase bluespark
Usage
import firebase from 'firebase'
import { Blue, Spark } from 'bluespark'
const app = firebase.initializeApp({
apiKey: '### FIREBASE API KEY ###',
authDomain: '### FIREBASE AUTH DOMAIN ###',
projectId: '### CLOUD FIRESTORE PROJECT ID ###',
})
const db = app.firestore()
type IPost = {
id: number
date: Dayjs | Blue.FieldValue
text: string
tags: string[]
user: Blue.DocReference
}
const Post = Spark<IPost>('posts', {
encoder: deepConvert<dayjs.Dayjs, Date>(dayjs.isDayjs, a => a.toDate()),
decoder: deepConvert<Blue.Timestamp, dayjs.Dayjs>(isTimestamp, a =>
dayjs(a.toDate()),
),
})
const postCl = Post.collectionWithin(db)
get
const postRef = postCl.doc('doc-path')
const post = await Post.ref(postRef).get()
const post = await postRef.get().then(Post.snap)
set, setMerge, update
const postRef = postCl.doc('doc-path')
await Post.ref(postRef).set({
id: 17,
date: dayjs(),
text: 'text',
tags: ['a', 'b'],
})