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

nuxt-firebase-composition

v0.0.2

Published

> Firebase Composition API for Nuxt.js

Downloads

9

Readme

nuxt-firebase-composition

Firebase Composition API for Nuxt.js

Use all the power of Composition API with Firebase in your Nuxt.js projects.

Usage

Example of ToDos-App in pages/index.vue:

<template>
  <div v-if="!uid">
    <button @click="signInWithGoogle">Sign in</button>
  </div>
  <div v-else>
    <div>Signed in UID: {{ uid }}</div>
    <ul>
      <li v-for="todo in docs" :key="todo.id">{{ todo.title }}</li>
    </ul>
    <input v-model="newTodo" />
    <button @click="addTodo">Add</button>
  </div>
</template>

<script>
import { useAuth, useCollection, useDoc } from 'nuxt-firebase-composition'
import { ref } from '@nuxtjs/composition-api'

export default {
  setup() {
    const { uid, signInWithGoogle } = useAuth()
    const { docs } = useCollection('todos')
    const { create } = useDoc('todos')

    const newTodo = ref('')

    const addTodo = () => {
      create({
        title: newTodo.value,
      })
      newTodo.value = ''
    }

    return {
      newTodo,
      addTodo,
      signInWithGoogle,
      uid,
      docs,
      create,
    }
  },
}
</script>

Why?

How is this better than nuxt/firebase and vuexfire and vuex-easy-firestore?

You can create re-usable modules. For example let's take a code above and put it under ~/use/todos.js:

import { useCollection, useDoc } from 'nuxt-firebase-composition'
import { ref } from '@nuxtjs/composition-api'

export default () => {
  const { docs: todos } = useCollection('todos')
  const { create } = useDoc('todos')

  const newTodo = ref('')

  const addTodo = () => {
    create({
      title: newTodo.value,
    })
    newTodo.value = ''
  }

  return {
    newTodo,
    addTodo,
    todos,
  }
}

Now you can replace the <script> section of initial code to:

import { useAuth } from 'nuxt-firebase-composition'
import useTodos from '~/use/todos.js'

export default {
  setup() {
    const { uid, signInWithGoogle } = useAuth()
    const { newTodo, addTodo, todos } = useTodos()

    return {
      signInWithGoogle,
      uid,
      newTodo,
      addTodo,
      todos,
    }
  },
}

I also believe that if all your modules will use useCollection and useDoc and useAuth as interface it will be easier later to switch from Firebase to other provider. And as you see in code Firebase and Firestore are not used directly.

There is a big room for improvement, but I believe foundation is there.

Setup

  1. Create Nuxt.js project
yarn create nuxt-app my-project
  1. Install module and dependencies
yarn add nuxt-firebase-composition firebase @nuxtjs/composition-api
  1. Create ~/plugins/firebase.client.js:
import { initFirebase } from 'nuxt-firebase-composition'
import { onGlobalSetup } from '@nuxtjs/composition-api'

export default ({ env: { firebase } }) => {
  onGlobalSetup(() => {
    initFirebase(firebase)
  })
}
  1. Enable plugin in the plugins section of nuxt.config.js
{
  plugins: [
    '~/plugins/firebase.client'
  ],
}
  1. Add Firebase configuration to the env section of nuxt.config.js
{
  env: {
    firebase: {
      config: {
          // REQUIRED: Official config for firebase.initializeApp(config):
          apiKey: '<apiKey>',
          authDomain: '<authDomain>',
          databaseURL: '<databaseURL>',
          projectId: '<projectId>',
          storageBucket: '<storageBucket>',
          messagingSenderId: '<messagingSenderId>',
          appId: '<appId>',
          measurementId: '<measurementId>'
      },
      services: {
        auth: true,
        firestore: true,
        analytics: false
      }
    }
  }
}

Your firebase config snippet and other Firebase specific parameters. You can retrieve this information from your Firebase project's overview page:

https://console.firebase.google.com/project/<your-project-id>/overview

ToDos

nuxt@2 with @nuxtjs/composition-api is still limited and doesn't offer the whole spectrum of features that we can have with vue3, I hope it's going to be solved with nuxt@3.

Initially I tried to create a compatible vue package, not only for nuxt, but I got a problem with provide from @vue/composition-api in Nuxtjs.

I tried to create a module for Nuxtjs, but got some weird errors, that I wasn't able to solve yet. It would be great to have it as a module though.

I would love to get some feedback and ideas.

Please let me know if you tried to install it and if it worked smoothly or you got a problem.

Support me

If you like the idea please give a star to nuxt-firebase-composition on Github.

If something doesn't work create an issue.