This file contains 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
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 | |
} | |
} |
This file contains 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
store.sync = (key, ref) => { | |
// Document | |
if (ref instanceof DocumentReference) { | |
return onSnapshot(ref, (ds) => { | |
if (ds.exists()) { | |
store.$patch({ [key]: ds.data() }) | |
} | |
}) | |
} |
This file contains 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 { 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() }) |
This file contains 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 { PiniaPluginContext } from "pinia"; | |
// Plugin | |
const magicNumPlugin = ({ store }: PiniaPluginContext) => { | |
store.magicNumber = 5 | |
} | |
// Typing | |
declare module 'pinia' { | |
export interface PiniaCustomProperties { |
This file contains 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 { collection, getFirestore, query, where } from "firebase/firestore" | |
import { defineStore } from "pinia" | |
type ExampleDoc = { | |
name: string, | |
age: number | |
} | |
export type State = { | |
queryData: ExampleDoc[] | null, | |
} |
This file contains 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
this.sync( | |
'queryData' // Property name of State | |
query // Query | |
) |
This file contains 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 { collection, getFirestore } from "firebase/firestore" | |
import { defineStore } from "pinia" | |
type ExampleDoc = { | |
name: string, | |
age: number | |
} | |
export type State = { | |
collectionData: ExampleDoc[] | null, |
This file contains 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
this.sync( | |
'collectionData' // State property name of State | |
collectionRef // Collection Reference | |
) |
This file contains 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 { doc, getFirestore } from "firebase/firestore" | |
import { defineStore } from "pinia" | |
type ExampleDoc = { | |
name: string, | |
age: number | |
} | |
export type State = { | |
docData: ExampleDoc | null, |
This file contains 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 { doc, getFirestore } from "firebase/firestore" | |
import { defineStore } from "pinia" | |
type ExampleDoc = { | |
name: string, | |
age: number | |
} | |
export type State = { | |
docData: ExampleDoc | null, |
NewerOlder