Skip to content

Commit

Permalink
chore: add dynamodb in playground
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien-R44 committed Oct 1, 2024
1 parent f077d30 commit c5e646c
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
6 changes: 4 additions & 2 deletions playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
"type": "module",
"private": true,
"scripts": {
"start": "NODE_NO_WARNINGS=1 node --loader ts-node/esm src/index.ts"
"start": "cross-env NODE_NO_WARNINGS=1 node --loader ts-node/esm src/index.ts"
},
"dependencies": {
"@aws-sdk/client-dynamodb": "^3.658.1",
"@bentocache/plugin-prometheus": "workspace:*",
"bentocache": "workspace:*"
},
"devDependencies": {
"prom-client": "^15.1.2"
"cross-env": "7.0.3",
"prom-client": "^15.1.3"
}
}
36 changes: 36 additions & 0 deletions playground/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,38 @@
import { BentoCache, bentostore } from 'bentocache'
import { redisDriver } from 'bentocache/drivers/redis'
import { memoryDriver } from 'bentocache/drivers/memory'
import { dynamoDbDriver } from 'bentocache/drivers/dynamodb'
import { CreateTableCommand, DynamoDBClient, ListTablesCommand } from '@aws-sdk/client-dynamodb'

const dynamoConfig = {
region: 'eu-west-3',
endpoint: 'http://localhost:8000',
credentials: { accessKeyId: 'foo', secretAccessKey: 'foo' },
}

const dynamoClient = new DynamoDBClient(dynamoConfig)

/**
* Create the table for storing the cache if it does not exist
*/
async function initDynamoDb() {
const tables = await dynamoClient.send(new ListTablesCommand())
if (tables.TableNames?.includes('cache')) return

await dynamoClient.send(
new CreateTableCommand({
TableName: 'cache',
KeySchema: [{ AttributeName: 'key', KeyType: 'HASH' }],
AttributeDefinitions: [{ AttributeName: 'key', AttributeType: 'S' }],
ProvisionedThroughput: {
ReadCapacityUnits: 4,
WriteCapacityUnits: 4,
},
}),
)
}

await initDynamoDb()

const bento = new BentoCache({
default: 'multitier',
Expand All @@ -10,6 +42,10 @@ const bento = new BentoCache({
multitier: bentostore()
.useL1Layer(memoryDriver({ maxSize: 10_000 }))
.useL2Layer(redisDriver({ connection: { host: '127.0.0.1', port: 6379 } })),

dynamodb: bentostore().useL2Layer(
dynamoDbDriver({ table: { name: 'cache' }, ...dynamoConfig }),
),
},
})

Expand Down
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c5e646c

Please sign in to comment.