keyv-dynamodb
Dynamo DB storage for the Keyv project
Install
yarn add @keyv/dynamodb
- Create the DynamoDB table using the
aws
CLI tool. Alternatively you can use the web dashboard to create the table, just make sure to create the expected fields. It is important to keep the field names as provided in the example. You will need to provision the DynamoDB capacities based on your expected usage. Execute in a terminal:
# 1. Create the DynamoDb table.
# Add profile or key/secret information if necessary.
aws dynamodb create-table \
--table-name KeyvStore \
--attribute-definitions \
AttributeName=Cid,AttributeType=S \
--key-schema AttributeName=Cid,KeyType=HASH \
--provisioned-throughput ReadCapacityUnits=1,WriteCapacityUnits=1
# 2. Enable the TTL attribute. You may need to wait for the table to finish the
# creation process.
# Add profile or key/secret information if necessary.
aws dynamodb update-time-to-live \
--table-name KeyvStore \
--time-to-live-specification Enabled=true,AttributeName=Expiration
Why?
This project is interesting when used with the got HTTP client (or directly using cacheable-request) inside of a Serverless project. Traditional cache solutions like ElastiCache with Redis will force you to deploy inside of a VPC. This has negative implications with regards to performance (via Lambda cold-starts) and scalability (via limited subnet size). This will allow you to have an application cache backend that doesn't require a VPC, since DynamoDB connections from Lambda do not require to deploy into a VPC.
You can also use this project as a stand-alone arbitrary cache back-end, without got or cacheable-request).
Usage
const KeyvDynamoDb = require('@keyv/dynamodb');
const keyvDynamoDb = new KeyvDynamoDb({
tableName: 'KeyvStore',
clientOptions: {
// Any options here will be passed to the DynamoDB client.
region: 'eu-central-1',
},
});
keyvDynamoDb.on('error', handleConnectionError);
Or you can manually create a storage adapter instance and pass it to Keyv:
const Keyv = require('keyv');
const KeyvDynamoDb = require('@keyv/dynamodb');
const keyvDynamoDb = new KeyvDynamoDb({
tableName: 'KeyvStore',
clientOptions: {
// Any options here will be passed to the DynamoDB client.
region: 'eu-central-1',
},
});
const keyv = new Keyv({ store: keyvDynamoDb });
Contributors
License
keyv-dynamodb is GPL-2.0 licensed.