fastify-firestore
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() }
},
)
}
License
Licensed under MIT.