Skip to content

Plugin to share a common DynamoDB configuration across Fastify

License

Notifications You must be signed in to change notification settings

matrus2/fastify-dynamodb

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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. The low-level client is also available via fastify.dynamoClient.

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 use the dynamodb client to perform queries: For further documentation on querying, see the DynamoDBDocumentClient docs.

const { GetCommand } = require("@aws-sdk/lib-dynamodb")

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.send(new GetCommand(params));
      } catch (e) {
         reply.send(e)
      }
      return { data }
    },
  )
}

License

Licensed under MIT.

About

Plugin to share a common DynamoDB configuration across Fastify

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •