Jump into code

Start defining your first model by adding at least a @Model() decorater and define one property as @PartitionKey()

person.model.ts
import { Model, PartitionKey } from '@shiftcoders/dynamo-easy'

@Model()
export class Person {
  @PartitionKey()
  id: string
  name: string
  age: number
}

This Person model will be used with the table name persons. The table name is built with ${kebabCase(modelName)}s - unless you provide one with @Model({tablename: 'yourCustomTableName').

then create a DynamoStore instance to execute actions against the DynamoDB.

app.ts
import { DynamoStore } from '@shiftcoders/dynamo-easy'
import * as AWS from 'aws-sdk/global'
import { Person } from './models'

// update the aws config with your credentials to enable successful connection
AWS.config.update({ region: 'yourAwsRegion' })

const personStore = new DynamoStore(Person)

// add a new item
personStore.put({ id: 'wernerv', name: 'Werner Hans Peter Vogels', yearOfBirth: 1958 })
  .exec()
  .then(() => {
    console.log('person stored')
  })

// search for a single person by known id
personStore.query()
  .wherePartitionKey('wernerv')
  .execSingle()
  .then(person => {
    console.log('got person', person)
  })

// returns all persons where the name starts with w
personStore.scan()
  .whereAttribute('name').beginsWith('w')
  .exec()
  .then((persons: Person[]) => {
    console.log('all persons', persons)
  })

We created a demo project to receive a good impression how dynamo-easy can be used. See Link below.

dynamo-easy demo running in stackblitz

Last updated