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, |
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( | |
'docData', // Document Dataと同期させたいpiniaのプロパティ名 | |
docRef // Document 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 { PiniaFirestoreSync } from 'pinia-plugin-firestore-sync' | |
// Add plugin | |
const pinia = createPinia().use(firestoreSyncPlugin) | |
app.use(pinia).mount('#app' |
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
npm install pinia-plugin-firestore-sync |
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
async setup() { | |
// Yay🙌 | |
this.sync('docDataA', docRefA) | |
this.sync('docDataB', docRefB) | |
this.sync('docDataC', docRefC) | |
} |
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
async setup() { | |
// It's very vervose... | |
onSnapshot(docRefA, (ds) => { | |
if (ds.exists()) { | |
this.$patch({ docDataA: ds.data() }) | |
} | |
}) | |
onSnapshot(docRefB, (ds) => { | |
if (ds.exists()) { | |
this.$patch({ docDataB: 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
export const useExampleStore = defineStore('expamle', { | |
state: {/*...*/} | |
actions: { | |
async setup() { | |
// ... | |
//🌟 Sync Pinia's state and Firestore document | |
onSnapshot(docRef, (ds) => { | |
if (ds.exists()) { | |
this.$patch({ docData: 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
export const useExampleStore = defineStore('expamle', { | |
state: {/*...*/} | |
actions: { | |
async setup() { | |
// ... | |
//🌟 docDataとdocRefを同期 | |
onSnapshot(docRef, (ds) => { | |
if (ds.exists()) { | |
this.$patch({ docData: 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
{"lastUpload":"2021-08-19T09:12:09.549Z","extensionVersion":"v3.4.3"} |