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-crossfire

v1.5.1

Published

Dead simple two-way Firestore syncing in Vue

Downloads

61

Readme

vue-crossfire

npm npm bundle size

Dead simple two-way Firestore syncing in Vue

Works with projects built with Firebase V9 and Vue 2

Install

$ npm install vue-crossfire

Usage

import Crossfire from 'vue-crossfire'

const firestoreDoc = new Crossfire(firestoreReference[, options ])

Constructors

Crossfire( firestoreReference[, options ] )

This returns a live snapshot of the provided Firestore reference, structured identially to a snapshot you would receive from a normal Firestore onSnapshot() method. Single document data is still accessed with .data(), and query/collection doc data is still accessed at .docs[x].data().

Every instance of document data is binded two way. Mutating the document data will instantly update it in Firestore as well. (Before the initial snapshot of the reference is received, .data() and .docs will be undefined. You can access .loading to see if the initial state has been received yet). Any remote changes to the data in Firestore will instantly update the local object as well.

Once you're done with the documents, call .destroy() on the instance to stop listening to the document/collection

import crossfire from 'vue-crossfire'

const documentSync = crossfire(doc(db, 'docCollection', 'docID'))
const collectionSync = crossfire(collection(db, 'colletionName'), { readOnly: true })
const querySync = crossfire(query(collection(db, 'colletionName'), where('fieldID', '==', true)))

firestoreReference is a reference to a Firestore V9 document, collection, or query The method will return the data contained at that reference: If reference is a document, it will be the document data Otherwise, it will be an array of document data of the results Before the first state has been downloaded, it will return null

The optional options object can be configured with the following fields:


onError: function (error) - default undefined

Callback for any errors that occur during document writes

readOnly: Boolean - default: false

If true, don't update firestore if the local data is changed

ignoreUnchangedFields: Boolean - default: false

When updating firestore docs, only update the specific fields that were changed in the data. This is slightly more computationally expensive, but is very useful when many users are editing a document at the same time and may overwrite data with their local state before they've received new changes

transformUpdate: function (changedData) - default undefined

Whenever a document is to be updated in firestore, the data to be saved will instead use the result of this function (if it's provided). Useful if you want to attach update metadata to the document (such as "dateModified" or "lastModifiedBy")

Example

<template>
  <div>
    <h1>Single Document Syncing</h1>
    <div v-if="docSync.data()">
      <h2>{{ docSync.data().title }}</h2>
      <input v-model="docSync.data().text" label="Text">
      <input type="checkbox" v-model="docSync.data().active" label="Active">
    </div>
    
    <h1>Collection/Query Syncing</h1>
    <div v-if="querySync.docs">
      <div v-for="doc in querySync.docs" :key="doc.id">
        <h2>{{ doc.data().title }}</h2>
        <input v-model="doc.data().text" label="Text">
        <input type="checkbox" v-model="doc.data().active" label="Active">
      </div>
    </div>
  </div>
</template>

<script>
  import { doc } from 'firebase/firestore'

  // Pass a firestore reference into crossfire(),
  // then simply read/write to the resulting snapshot as any regular object variable
  export default {
    name: 'VueComponent',
    data: function () {
      return {
        docSync: null,
        querySync: null,
      }
    }
    created: function () {
      this.docSync = crossfire(doc(db, 'someCollection', 'docID'))
      this.querySync = crossfire(query(collection(db, 'someCollection'), where('author', '==', 'uuid')))
    },
    destroyed: function () {
      this.docSync.destroy()
      this.querySync.destroy()
    },
  }
</script>

Demo