dinamo
v2.0.1
Published
Amazon DynamoDB utilities for Node.js
Downloads
173
Readme
Dinamo
Amazon Dynamo (Amazon DynamoDB) opinionated utilities for Node.js.
Getting started
First install the library:
npm i dinamo @aws-sdk/client-dynamodb @aws-sdk/lib-dynamodb
Note that
@aws-sdk/client-dynamodb
and@aws-sdk/lib-dynamodb
are peer dependencies for this library, so instead of bundling it with the package you MUST install it separately.
Then create an instance in your code:
import Dinamo from 'dinamo'
const dinamo = new Dinamo({ tableName: 'my-table' })
If you are using DynamoDB in a Docker container you can pass the endpoint as a parameter:
const dinamo = new Dinamo({
endpoint: 'http://localhost:8000',
tableName: 'my-table',
})
You can also configure the AWS SDK client logger:
const dinamo = new Dinamo({
logger: console,
tableName: 'my-table',
})
Usage
batchGet
Gets items in batch.
await dinamo.batchGet({ keys: [{ id: 'a' }, { id: 'b' }, { id: 'c' }] })
decrement
Decrements an item. Step is optional.
await dinamo.decrement({ key: { id: 'a' }, field: 'count', step: 1 })
delete
Soft deletes an item, i.e., adds a flag deletedAt
with the timestamp of deletion. This is true by default and query
and scan
will filter out the deleted items by default too.
await dinamo.delete({ key: { id: 'a' }, soft: true })
Deletes an item from the database.
await dinamo.delete({ key: { id: 'a' }, soft: false })
get
Gets a single item.
await dinamo.get({ key: { id: 'a' } })
increment
Increments an item. Step is optional.
await dinamo.increment({ key: { id: 'a' }, field: 'count', step: 1 })
put
Puts an item.
await dinamo.put({ item: { id: 'a', foo: 'bar' } })
query
Queries items from the database.
await dinamo.query({ key: { id: 'a' } })
With indexName
.
await dinamo.query({ key: { id: 'a' }, indexName: 'dateIdIndex' })
Filtering items.
await dinamo.query({ key: { id: 'a' }, query: { foo: 'bar' } })
Disable filtering soft deletes.
await dinamo.query({ key: { id: 'a' }, filterDeleted: false })
Limiting items.
await dinamo.query({ key: { id: 'a' }, limit: 10 })
Reverse ordering items based on range key.
await dinamo.query({ key: { id: 'a' }, scanIndexForward: true })
scan
Scans items from the database.
await dinamo.scan({ query: { id: 'a' } })
Recursively scan items.
await dinamo.scan({ query: { id: 'a' }, recursive: true })
Disable filtering soft deletes
await dinamo.scan({ query: { id: 'a' }, filterDeleted: false })
update
Updates an item.
await dinamo.update({ key: { id: 'a' }, item: { foo: 'baz' } })
Contributing
Issues and pull requests are welcome.