0.8.6 • Published 2 years ago

dynamodb-std v0.8.6

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

dynamodb-std

Simple DynamoDB single-table design implementation (aws-sdk-v3 compatible)

This library is in the early stage of development, not feature full and not stable. DON'T USE IT IN PRODUCTION CODE.

Usage

In accordance with single-table design all the data you need in your application or microservice you may store in one DynamoDB table. With dynamodb-std, you may defined this table using Storage:

import { Storage } from 'dynamodb-std'

export const storage = new Storage({
  tableName: process.env.TABLE_NAME,
  indices: ['s1k', 's2k'],
})

Then, for each data model you have in you application, you need to define Schema:

export interface IUser {
  id: string
  role: string
  status: string
  modified: number
}

export const userSchema = new Schema<IUser>({
  storage,
  schema: {
    pk: {
      hash: 'id',
      sort: 'id',
    },
    s1k: {
      hash: () => 'BYROLE',
      sort: 'role',
    },
    s2k: {
      hash: () => 'BYSTATUS',
      sort: ['status', 'modified'],
    },
  },
})

And use it in you repository (check @aws-sdk/lib-dynamodb to setup ddbDocClient):

import { ddbDocClient as client } from '../utils/db'
import { decodeToken, encodeToken } from 'dynamodb-std'

export async function put(user: IUser) {
  const commandInput: PutCommandInput = userSchema.putParams(user)
  await client.send(new PutCommand(commandInput))

  return user
}

export async function getById(params: Pick<IUser, 'id'>): Promise<IUser | undefined> {
  const commandInput = userSchema.getParams(params)
  const { Item } = await client.send(new GetCommand(commandInput))

  return Item as IUser
}

export async function delete(params: Pick<IUser, 'id'>) {
  const commandInput = userSchema.deleteParams(params)
  return await client.send(new DeleteCommand(commandInput))
}

export interface IListParams {
  limit?: number
  nextToken?: string | null
}

export async function list({ limit = 20, nextToken }: IListParams = {}) {
  const startKey = nextToken ? decodeToken(nextToken) : undefined
  const indexName = 's1k'
  const commandInput = userSchema.queryParams({
    limit,
    indexName,
    hash: 'BYROLE',
    startKey,
  })

  const { Items, LastEvaluatedKey } = await client.send(
    new QueryCommand(commandInput)
  )

  return {
    items: (Items || []) as IUser[],
    nextToken:
      LastEvaluatedKey &&
      encodeToken({
        index: indexName,
        ...userSchema.storage.getPageToken(indexName, LastEvaluatedKey),
      }),
  }
}
0.8.5

2 years ago

0.8.4

2 years ago

0.8.6

2 years ago

0.8.0

2 years ago

0.8.3

2 years ago

0.8.2

2 years ago

0.7.0

2 years ago

0.6.0

2 years ago

0.5.0

2 years ago

0.4.4

3 years ago

0.4.3

3 years ago

0.4.2

3 years ago

0.4.1

3 years ago

0.4.0

3 years ago

0.3.0

3 years ago

0.1.0

3 years ago