@socketsupply/dynavolt
v3.0.0
Published
A highly opinionated DynamoDB client for aws-sdk v3 using esm.
Downloads
8
Readme
SYNOPSIS
A highly opinionated DynamoDB client for aws-sdk v3 using esm.
USAGE
import Dynavolt from 'dynavolt'
const db = new Dynavolt({ region: 'us-west-2' })
TABLES
CREATE
const { err, data: table } = await db.create('artists')
You can also specify hash
, range
, and options
.
const opts = { TimeToLiveSpecification: {
AttributeName: 'ttl',
Enabled: true
}
const { err } = await db.create('artists', 'genres', 'artists', opts)
OPEN
Open a database and optionally create it if it doesnt exist.
const { err, data: table } = await db.open('artists', { create: true })
METHODS
PUT
Dynavolt will automatically (and recursively) deduce the types of your data and annotate them correctly, so there is no need to write "dynamodb json".
const { err } = await table.put('glen', 'danzig', { height: 'quite-short' })
PUT IF NOT EXISTS
Dynavolt will automatically (and recursively) deduce the types of your data and annotate them correctly, so there is no need to write "dynamodb json".
const { err } = await table.put('glen', 'danzig', { height: 'quite-short' })
UPDATE
const expr = `SET count = count + N(${value})`
const { err, data } = await table.update('iggy', 'pop', expr)
GET
const { err, data } = await table.get('iggy', 'pop')
DELETE
const { err } = await table.delete('henry', 'rollins')
BATCH WRITE
const { err } = await table.batchWrite([
['foo', 'bar', { beep: 'boop' }],
['foo', 'bar']
])
BATCH READ
const { err } = await table.batchRead([
['foo', 'bazz'],
['beep', 'boop']
])
QUERY
Query takes a Key Condition Expression. For syntax refernece see the Comparison Operator and Function Reference.
const iterator = table.query(`hash = N(greetings) AND begins_with(range, S(hell))`)
for await (const { err, data: { key, value } } of iterator) {
console.log(key, value)
}
You can also chain a Filter Expression and Projection Expression clauses onto querties. More info about Projection Expression syntax here.
const iterator = table
.query(`hash = N(songs) AND begins_with(range, S(moth))`)
.filter(`contains(artists.name, S(danzig)`)
.properties('artists.weight', 'artists.height')
for await (const { err, data: { key, value } } of iterator) {
console.log(key, value)
}
SCAN
Scan takes a Filter Expression.
const iterator = table.scan(`contains(artists.name, S(danzig)`)
for await (const { err, data: { key, value } } of iterator) {
console.log(key, value)
}
TTL
Records in your database can be set to expire by specifying a TTL
attribute
on your table.
const { err } = await table.setTTL('stillCool')
Now one minute after adding the following record, it will be removed.
const opts = {
stillCool: 6e4
}
const { err } = await table.put('brian', 'setzer', { cool: true }, opts)