vue-firestore
Vue.js binding for firebase cloud firestore.
Prerequisites
Firebase ^7.6.1
Demo
Try it out:Installation
Globally (Browser)
vue-firestore will be installed automatically.
<!-- Vue --> <!-- Firebase --> <!-- Firestore --> <!-- vue-firestore -->
npm
Installation via npm : npm install vue-firestore --save
Usage
vue-firestore supports binding for the both (collections/docs) in realtime, you can bind your properties in two ways using firestore
option or bind them manually with $binding
.
- using
firestore
option.
Vue var firebaseApp = Firebase const firestore = firebaseApp; var vm = el: '#app' { return // Collection persons: firestore // Doc ford: firestore }
You can pass an object to the firestore()
function.
As you may know, firestore source returns a promise, so you can handle it if it's resolved by resolve
function
or rejected by reject
function, this case is really useful when we want to wait for data to be rendered and do some specific actions.
{ return persons: // collection reference. ref: firestore // Bind the collection as an object if you would like to. objects: true { // collection is resolved } { // collection is rejected } }
- Manually binding
You can bind your docs/collection manually using this.$binding
, and wait for data to be resolved, this case is really useful when we want to wait for data to be rendered and do some specific actions.
... { // Binding Collections this // Binding Docs this }...
Vue firestore latest release supports binding collections as objects, you can bind the collection manually by this.$bindCollectionAsObject(key, source)
or you can explicitly do that by adding {objects: true}
to firestore()
function, see the previous example above.
The normalized resutls of $bindCollectionAsObject
:
tjlAXoQ3VAoNiJcka9: firstname: "Jhon" lastname: "Doe" fb7AcoG3QAoCiJcKa9: firstname: "Houssain" lastname: "Amrani"
You can get access to firestore properties with this.$firestore
.
Adding Data to collections
var vm = el: '#app' { return persons: firestore } methods: { this$firestorepersons }
Each record of the array will contain a .key
property which specifies the key where the record is stored.
The Result of persons
collection will be normalized as :
You could delete or update a json document of a collection using the property .key
of a given object.
// Vue methods { this$firestorepersons} { this$firestorepersons}
You can customize the name of the .key
property by passing an option when initializing vue-firestore:
Vue
This would allow you to do person.id
instead of person['.key']
.