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

@now-ims/fastify-firebase

v2.0.5

Published

Fastify Firebase Admin plugin

Downloads

602

Readme

fastify-firebase

Known Vulnerabilities Coverage Status js-standard-style npm (scoped)

This plugin adds the Firebase Admin SDK to Fastify so you can easy use Firebase Auth, Firestore, Cloud Storage, Cloud Messaging, and more.

Install

yarn add @now-ims/fastify-firebase
npm i @now-ims/fastify-firebase -S

Usage

Add it to you project with register and you're done!
This plugin will add a firebase namespace in your Fastify instance - you can access the Firebase SDK objects via fastify.firebase:

auth - e.g., fastify.firebase.auth().createUser()
firestore - e.g., fastify.firebase.firestore().collection('users').get()
storage - e.g., fastify.firebase.store().bucket('thumbnails')
machineLearning - e.g., fastify.firebase.machineLearning().createModel()

API

  • options - if deploying from Google Cloud options are optional
    • name (Default: 'default', Type: string) is required if you want to load configurations for multiple projects
    • cert (Optional, Type: string) is required if you you are not on Google Cloud or want to load multiple configurations
    • databaseURL (Optional, Type: string) is required for Realtime Database
    • storageBucket (Optional, Type: string) e.g., <BUCKET_NAME>.appspot.com - do not include any gs://
    • projectId (Default: 'cert.projectId', Type: string) e.g., my-google-cloud-project

Example

Google Cloud - Application Default Credentials

const fastify = require('fastify')({ logger: true });

fastify.register(require('fastify-sensible'));
fastify.register(require('@now-ims/fastify-firebase'));

fastify.get('/user/:id', async (req, reply) => {
  const user = await fastify.firebase
    .firestore()
    .collection('users')
    .doc(req.params.id)
    .get();

  if (!user.exists) {
    return reply.notFound();
  }

  return user.data();
});

fastify.listen(4331, (err) => {
  if (err) throw err;
  console.log(`server listening on ${fastify.server.address().port}`);
});

Register multiple Firebase projects w/ options and Separate Route File

const fastify = require('fastify')({ logger: true });

fastify.register(require('fastify-sensible'));

// Here we will load multiple configurations for multiple projects!
// You most likely will not need this, but it is available should you need it.
fastify.register(require('@now-ims/fastify-firebase'), {
  name: 'client1',
  cert: '/path/to/my/cert/',
});
// this can be called with either fastify.firebase or fastify.firebase['client1']

fastify.register(require('@now-ims/fastify-firebase'), {
  name: 'client2',
  cert: process.env.FIREBASE_CONFIG,
  storageBucket: 'thumbnails.appspot.com',
});
// this can only be called with fastify.firebase['client2']

fastify.listen(4331, (err) => {
  if (err) throw err;
  console.log(`server listening on ${fastify.server.address().port}`);
});

In your route file you can simply do the following e.g.:

async function userRoute(fastify, options) {
  fastify.get('/users/:id', async (req, reply) => {
    const { id } = req.params;
    const user = await fastify.firebase['client2']
      .firestore()
      .collection('users')
      .get(id)
      .get();

    if (!user.exists) {
      return reply.notFound();
    }

    return user.data();
  });
}

Acknowledgements

This project is sponsored and maintained by:

License

Licensed under MIT.