Fastify Plugin to share a common Google Cloud Firestore configuration across your project
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.gitignore
.travis.yml test node 8, 9 and lts Jun 21, 2018
LICENSE
README.md
index.js
now-rads-core-dev-c1237372ba4e.json.enc
package-lock.json
package.json
test.js
yarn.lock

README.md

fastify-firestore

js-standard-style Build Status

This plugin shares Google Cloud Firestore object, so you can easily use Firestore with Fastify.

Install

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

Usage

Add it to you project with register and you are done!

const fastify = require('fastify')

fastify.register(require('@now-ims/fastify-firestore'), {
    projectId: 'your-project-id',
    keyFilename: '/path/to/keyfile.json'
  })

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

You can access the Google Cloud Firestore via fastify.fs. In your route file you can simply do all gets, queries, transactions e.g.:

async function singleRoute(fastify, options) {
  fastify.get(
    '/users/:id',
    async (request, reply) => {
      const { id } = request.params;
      const docRef = fastify.fs.doc(`users\${id}`)
      try {
        const doc = await docRef.get()
      } catch (e) {
         reply.send(e)
      }
      
      // return { ...doc.data() } <- would return empty if document does not exists
      // check if doc.exists and if not return empty array
      return {data: (doc.exists) ? doc.data() : [] }
    },
  )
}

Acknowledgement

This project is kindly sponsored by Now IMS

License

Licensed under MIT.