firex-store
-
If you use this npm, you can reference firestore data, easily
-
It is inspired by vuexfire
-
node v8.9.4 ~
Installation
npm install --save firex-store
Example
others comming soon
Important!
-
Return values or state values bounded to Firestore has docId
(documentId in Firestore) property.
-
A store module cannot subscribe to more than one 'collection' and 'document'
-
If you want to subscribe again after unsubscribing 'collection', set the property of the store you want to subscribe to []
and then subscribe.
Usage
Before Start...
- You have to initailize firebase
.....
firebase.initializeApp({
apiKey: [your firebase api key],
projectId: [your project id],
.....
})
export const firestore = firebase.firestore()
1. Subscribe Firestore, using firex-store actions
part1. Add below mutations to namespaced Store
- method:
firestoreMutations
- argments:
- statePropName: you want to bind in the state
- type: 'collection' or 'document'
Ex. Subscribe collection
export default {
namespaced: true,
state: {
comments: [],
comment: null
},
mutations: {
...firestoreMutations({ statePropName: 'comments', type: 'collection' })
},
.....
}
part2. Add below actions to namespaced Store
Ex. Subscribe collection
export default {
namespaced: true,
state: {
comments: [],
comment: null
},
mutations: {
...firestoreMutations({ statePropName: 'comments', type: 'collection' })
},
actions: {
...firestoreSubscribeActions({ ref: firestore.collection('/comments') })
}
.....
}
part3. Call firex-store actionType to subscribe data
Ex. Subscribe collection
<script>
import { actionTypes } from 'firex-store'
export default {
name: 'Comments',
created() {
this.$store.dispatch(`comment/${actionTypes.collection.SUBSCRIBE}`)
}
}
</script>
2. Subscribe Firestore, using custom actions
part1. Add below mutations to namespaced Store
- method:
firestoreMutations
- argments:
- statePropName: you want to bind in the state
- type: 'collection' or 'document'
Ex. Subscribe collection
export default {
namespaced: true,
state: {
comments: [],
comment: null
},
mutations: {
...firestoreMutations({ statePropName: 'comments', type: 'collection' })
},
.....
}
part2. Add subscribeFirestore
in custom-actions
- method:
subscribeFirestore
- argments:
- state: State
- commit: Commit
- ref: firebase.firestore.DocumentReference | firebase.firestore.CollectionReference | firebase.firestore.Query
- options?: see Options
export default {
namespaced: true,
state: {
comments: [],
comment: null
},
mutations: {
...firestoreMutations({ statePropName: 'comments', type: 'collection' })
},
actions: {
subscribe: ({ state, commit }) => {
subscribeFirestore({
state,
commit,
ref: firestore.collection('/comments'),
options
})
}
}
.....
}
3. Unsubscribe Firestore, using firex-store actions
Ex. Unsubscribe collection
part1. Add firestoreUnsubscribeActions
in actions
export default {
namespaced: true,
state: {
comments: [],
comment: null
},
mutations: {
...firestoreMutations({ statePropName: 'comments', type: 'collection' })
},
actions: {
...firestoreSubscribeActions({ ref: firestore.collection('/comments') }),
...firestoreUnsubscribeActions({ type: 'collection' })
}
.....
}
part2. Call firex-store actionType to unsubscribe data
<script>
import { actionTypes } from 'firex-store'
export default {
name: 'Comments',
created() {
this.$store.dispatch(`comment/${actionTypes.collection.UNSUBSCRIBE}`)
}
}
</script>
4. Unsubscribe Firestore, using custom actions
export default {
namespaced: true,
state: {
comments: [],
comment: null
},
mutations: {
...firestoreMutations({ statePropName: 'comments', type: 'collection' })
},
actions: {
subscribe: ({ state, commit }) => {
subscribeFirestore({
state,
commit,
ref: firestore.collection('/comments'),
options
})
},
unsubscribe: ({ state }) => {
unsubscribeFirestore({
state,
type: 'collection'
})
}
}
.....
}
5. Fetch at Once
EX. Call in Store Action, to fetch collection
export default {
namespaced: true,
state: {},
getters: {},
mutations: {},
actions: {
fetchComments: async ({ commit }) => {
const ref = firestore.collection('/comments')
const result = await findFirestore({ ref })
commit(***, result)
}
}
}
Options
Ex.
const mapUser = (data) => ({
id: data.id
name: data.name
.....
})
const errorHandler = (error) => {
console.error(`[App Name]: ${error}`)
}
const completionHandler = () => {
console.log('completed!')
}
const afterMutationCalled = (payload) => {
if (payload.isLast === false) {
commit('SET_LOADING', true)
} else if (payload.isLast === true) {
commit('SET_LOADING', false)
}
}
const notFoundHandler = (type, isAll) => {
console.log('not found')
}
const notFoundHandler = (type, isAll) => {
console.log('not found')
}
export default {
namespaced: true,
state: {
comments: [],
comment: null,
isLoading: false
},
mutations: {
...firestoreMutations({ statePropName: 'comments', type: 'collection' }),
SET_LOADING(state, isLoading) {
state.isLoading = isLoading
}
},
actions: {
subscribe: ({ state, commit }) => {
subscribeFirestore({
state,
commit,
ref: firestore.collection('/comments'),
options: {
mapper: mapUser,
errorHandler,
completionHandler,
afterMutationCalled,
notFoundHandler
}
})
}
}
.....
}