fastify-dynamodb

1.0.7 • Public • Published

fastify-dynamoDB

js-standard-style Build Status

This plugin shares AWS.DynamoDB.DocumentClient() object, so you can easy use DynamoBD with fastify.

Install

npm i fastify-dynamodb -S

Usage

Add it to you project with register and you are done!
You can access the DynamoDB DocumentClient via fastify.dynamo.

const fastify = require('fastify')
 
fastify.register(require('fastify-dynamodb'), {
    endpoint: 'http://localhost:8000',
    region: AWS_REGION
  })
 
fastify.listen(3000, 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 params = {
        TableName: TABLE_NAME,
        Key: {
          user_id: id
        },
      };
      try {
        data = await fastify.dynamo.get(params).promise();
      } catch (e) {
         reply.send(e)
      }
      return { data }
    },
  )
}

License

Licensed under MIT.

Package Sidebar

Install

npm i fastify-dynamodb

Weekly Downloads

422

Version

1.0.7

License

MIT

Unpacked Size

4.42 kB

Total Files

6

Last publish

Collaborators

  • matrus2