Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/matrus2/fastify-dynamodb
Plugin to share a common DynamoDB configuration across Fastify
https://github.com/matrus2/fastify-dynamodb
Last synced: 3 months ago
JSON representation
Plugin to share a common DynamoDB configuration across Fastify
- Host: GitHub
- URL: https://github.com/matrus2/fastify-dynamodb
- Owner: matrus2
- License: mit
- Created: 2018-01-16T09:56:34.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2023-03-27T10:29:30.000Z (almost 2 years ago)
- Last Synced: 2024-09-14T12:40:45.026Z (4 months ago)
- Language: JavaScript
- Size: 97.7 KB
- Stars: 7
- Watchers: 4
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-fastify - `fastify-dynamodb`
README
# fastify-dynamoDB
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](http://standardjs.com/)
[![Build Status](https://travis-ci.org/matrus2/fastify-dynamodb.svg?branch=master)](https://travis-ci.org/matrus2/fastify-dynamodb)This plugin shares [AWS.DynamoDB.DocumentClient()](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html) 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*](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/modules/_aws_sdk_lib_dynamodb.html) via `fastify.dynamo`. The [low-level client](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-dynamodb/index.html) is also available via `fastify.dynamoClient`.
```js
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](https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/dynamodb-example-dynamodb-utilities.html).```js
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](./LICENSE).