@keyv/dynamo

DynamoDB storage adapter for Keyv

build codecov npm npm

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.

Install

npm install --save keyv @keyv/dynamo

Usage

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:

const keyv = new KeyvDynamo({ tableName: 'cacheTable' });

Usage with NestJS

Since DynamoDB has a 400KB limit per item, compressing data can help in some cases.

With a payload less than or equal to 400KB

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 {}

With a payload greater than 400KB

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 {}

License

MIT © Jared Wray