npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vue-firestore

v0.3.30

Published

Vue plugin for firestore

Downloads

600

Readme

vue-firestore

Vue.js binding for firebase cloud firestore.

Prerequisites

Firebase ^7.6.1

Try it out: Demo

Installation

Globally (Browser)

vue-firestore will be installed automatically.

<!-- Vue -->   
<script src="https://unpkg.com/vue"></script>
<!-- Firebase -->   
<script src="https://www.gstatic.com/firebasejs/5.3.1/firebase.js"></script>
<!-- Firestore -->   
<script src="https://www.gstatic.com/firebasejs/5.3.1/firebase-firestore.js"></script>
<!-- vue-firestore -->   
<script src="https://unpkg.com/vue-firestore"></script>

<script>        
  // Firebase config.
  var config = {
        apiKey: "your-apik-ey",
        authDomain: "your-auth-domain",
        databaseURL: "your-database-url",
        projectId: "your-project-id",
        storageBucket: "your-storage-bucket",
        messagingSenderId: "your-messaing-sender-id"
      }

  // Initialize Firebase.
  firebase.initializeApp(config);
</script>

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.

  1. using firestore option.

import Vue from 'vue'
import VueFirestore from 'vue-firestore'
import Firebase from 'firebase'

require('firebase/firestore')

Vue.use(VueFirestore)

var firebaseApp = Firebase.initializeApp({ ... })

const firestore = firebaseApp.firestore();

var vm = new Vue({
  el: '#app',
  firestore () {
    return {
        // Collection
        persons: firestore.collection('persons'),
        // Doc
        ford: firestore.collection('cars').doc('ford')
    }
  }
})

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.

firestore () {
  return {
    persons: {
      // collection reference.
      ref: firestore.collection('persons'),
      // Bind the collection as an object if you would like to.
      objects: true,
      resolve: (data) => {
          // collection is resolved
      },
      reject: (err) => {
          // collection is rejected
      }
    }
  }
}
  1. 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.

...
mounted () {
  // Binding Collections
  this.$binding("users", firestore.collection("users"))
  .then((users) => {
    console.log(users) // => __ob__: Observer
  })

  // Binding Docs
  this.$binding("Ford", firestore.collection("cars").doc("ford"))
  .then((ford) => {
    console.log(ford) // => __ob__: Observer
  }).catch(err => {
    console.error(err)
  })
}
...

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 = new Vue({
  el: '#app',
  firestore: function () {
    return {
        persons: firestore.collection('persons')
    }
  },
  methods:{
    addData: function () {
        this.$firestore.persons.add({
            firstname: "Amrani",
            lastname: "Houssain"
        })
    }
  }
})

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 :

[
    {
        ".key": "-Jtjl482BaXBCI7brMT8",
        "firstname": "Amrani",
        "lastname": "Houssain"
    },
    {
        ".key": "-JtjlAXoQ3VAoNiJcka9",
        "firstname": "John",
        "lastname": "Doe"
    }
]

You could delete or update a json document of a collection using the property .key of a given object.

// Vue methods
deletePerson: function (person) {
    this.$firestore.persons.doc(person['.key']).delete()
},
updatePerson: function (person) {
    this.$firestore.persons.doc(person['.key']).update({
        name: "Amrani Houssain"
        github: "@amranidev"
    })
}

You can customize the name of the .key property by passing an option when initializing vue-firestore:

require('firebase/firestore')

Vue.use(VueFirestore, {
    key: 'id',         // the name of the property. Default is '.key'.
    enumerable: true  //  whether it is enumerable or not. Default is true.
})

This would allow you to do person.id instead of person['.key'].

More Resources

LICENSE

MIT