1.1.0 • Published 6 years ago

@now-ims/fastify-datastore v1.1.0

Weekly downloads
1
License
MIT
Repository
github
Last release
6 years ago

fastify-datastore

js-standard-style Build Status

This plugin shares Google Cloud Datastore object, so you can easy use Datastore with Fastify.

Install

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

Usage

Add it to you project with register and you are done!
You can access the Google Cloud Datastore via fastify.ds.

const fastify = require('fastify')

fastify.register(require('@now-ims/fastify-datastore'), {
    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}`)
})

In your route file you can simply do all gets, queries, scans e.g.:

async function singleRoute(fastify, options) {
  fastify.get(
    '/users/:id',
    async (request, reply) => {
      let data
      const { id } = request.params;
      const key = fastify.ds.key({
          namespace: 'multi-tenant-customer',
          path: ['User', id]
      })
      try {
        data = await fastify.ds.get(key)
      } catch (e) {
         reply.send(e)
      }
      return { data[0] }
    },
  )
}

License

Licensed under MIT.