Skip to content

Examples

Here are a few examples using the Table API. For most data accesses, the Model API is used.

Setup

1
2
3
4
5
6
7
import {Table} from 'dynamodb-onetable'

const table = new Table({
    client: DocumentClientInstance,
    name: 'MyTable',
    schema: Schema,
})

Fetch an Item Collection

1
2
3
let items = await table.fetch(['User', 'Product'], {pk: 'account:AcmeCorp'})
let users = items.User
let products = items.Product

Query Items and then Group by Type

1
2
3
4
let items = await table.queryItems({pk: 'account:AcmeCorp'}, {parse: true, hidden: true})
items = table.groupByType(items)
let users = items.User
let products = items.Product

Get Item

//  Fetch an account by the ID which is used to create the primary key value
let account = await table.get('Account', {id})

Transactional Update

1
2
3
4
let transaction = {}
await table.update('Account', {id: account.id, status: 'active'}, {transaction})
await table.update('User', {id: user.id, role: 'user'}, {transaction})
await table.transact('write', transaction)

Scan

1
2
3
//  Get the number of accounts without reading the items
let accounts = await table.scan('Account')
let count = accounts.count