DynamoDB storage adapter for Keyv
npm install @keyv/dynamo> DynamoDB storage adapter for Keyv




DynamoDB storage adapter for Keyv.
Uses TTL indexes to automatically remove expired documents. However DynamoDB doesn't guarantee data will be deleted immediately upon expiration.
``shell`
npm install --save keyv @keyv/dynamo
`js
import Keyv from 'keyv';
import KeyvDynamo from '@keyv/dynamo';
const keyv = new Keyv(new KeyvDynamo());
keyv.on('error', handleConnectionError);
`
You can specify the table name, by default 'keyv' is used.
e.g:
`js`
const keyv = new KeyvDynamo({ tableName: 'cacheTable' });
The createKeyv function is a convenience method that creates a new Keyv instance with the DynamoDB adapter. It automatically sets useKeyPrefix to false to allow the adapter to handle key prefixing:
`js
import { createKeyv } from '@keyv/dynamo';
const keyv = createKeyv({
endpoint: 'http://localhost:8000',
tableName: 'cacheTable',
namespace: 'my-app'
});
`
The DynamoDB client is exposed as a property for advanced use cases:
`js
const store = new KeyvDynamo();
const dynamoClient = store.client; // DynamoDBDocument instance
// You can use the client directly for custom operations
await dynamoClient.get({ TableName: 'keyv', Key: { id: 'myKey' } });
`
Since DynamoDB has a 400KB limit per item, compressing data can help in some cases.
`js
import { Keyv } from 'keyv'
import { KeyvDynamo } from '@keyv/dynamo'
import { DynamoModule } from '@lyfe/dynamo-module'
import { CacheModule } from '@nestjs/cache-manager'
import { Module } from '@nestjs/common'
@Module({
imports: [
CacheModule.registerAsync({
isGlobal: true,
useFactory: async () => {
return {
stores: [
new Keyv({
store: new KeyvDynamo({
tableName: 'TableName',
}),
}),
],
}
},
}),
],
exports: [DynamoModule],
})
export class InfrastructureModule {}
`
`js
import { Keyv } from 'keyv'
import KeyvBrotli from '@keyv/compress-brotli'
import { KeyvDynamo } from '@keyv/dynamo'
import { DynamoModule } from '@lyfe/dynamo-module'
import { CacheModule } from '@nestjs/cache-manager'
import { Module } from '@nestjs/common'
@Module({
imports: [
CacheModule.registerAsync({
isGlobal: true,
useFactory: async () => {
return {
stores: [
new Keyv({
store: new KeyvDynamo({
tableName: 'TableName',
}),
compression: new KeyvBrotli(),
}),
],
}
},
}),
],
exports: [DynamoModule],
})
export class InfrastructureModule {}
``