Skip to content

Instantly share code, notes, and snippets.

View kazuooooo's full-sized avatar
:octocat:
Taking a closer look, wombat!

kazuwombat kazuooooo

:octocat:
Taking a closer look, wombat!
View GitHub Profile
declare module 'pinia' {
export interface PiniaCustomProperties<Id, S, G, A> {
sync(key: string, ref: DocumentReference): Unsubscribe
sync(key: string, ref: CollectionReference): Unsubscribe
sync(key: string, ref: Query): Unsubscribe
}
}
store.sync = (key, ref) => {
// Document
if (ref instanceof DocumentReference) {
return onSnapshot(ref, (ds) => {
if (ds.exists()) {
store.$patch({ [key]: ds.data() })
}
})
}
import { CollectionReference, DocumentReference, onSnapshot, Query, Unsubscribe } from "firebase/firestore";
import { PiniaPluginContext } from "pinia";
export const PiniaFirestoreSync = ({ store }: PiniaPluginContext) => {
store.sync = (key, ref) => {
// Document
if (ref instanceof DocumentReference) {
return onSnapshot(ref, (ds) => {
if (ds.exists()) {
store.$patch({ [key]: ds.data() })
import { PiniaPluginContext } from "pinia";
// Plugin
const magicNumPlugin = ({ store }: PiniaPluginContext) => {
store.magicNumber = 5
}
// Typing
declare module 'pinia' {
export interface PiniaCustomProperties {
import { collection, getFirestore, query, where } from "firebase/firestore"
import { defineStore } from "pinia"
type ExampleDoc = {
name: string,
age: number
}
export type State = {
queryData: ExampleDoc[] | null,
}
this.sync(
'queryData' // Property name of State
query // Query
)
import { collection, getFirestore } from "firebase/firestore"
import { defineStore } from "pinia"
type ExampleDoc = {
name: string,
age: number
}
export type State = {
collectionData: ExampleDoc[] | null,
this.sync(
'collectionData' // State property name of State
collectionRef // Collection Reference
)
import { doc, getFirestore } from "firebase/firestore"
import { defineStore } from "pinia"
type ExampleDoc = {
name: string,
age: number
}
export type State = {
docData: ExampleDoc | null,
import { doc, getFirestore } from "firebase/firestore"
import { defineStore } from "pinia"
type ExampleDoc = {
name: string,
age: number
}
export type State = {
docData: ExampleDoc | null,