0.0.49 • Published 11 months ago

@awsless/dynamodb v0.0.49

Weekly downloads
-
License
MIT
Repository
github
Last release
11 months ago

@awsless/dynamodb

The @awsless/dynamodb package provides a small and tree-shakable layer around aws-sdk v3, to make working with the DynamoDB API super simple.

The Problem

  • Floating Point Precision - Almost no DynamoDB clients are suitable when floating point precision is important. We use our @awsless/big-float package to solve this problem.
  • Tree-shakable - The API is designed to balance tree-shakability vs providing a fully typed API.
  • Query Builder - Type safe & easy to use query expression builder.
  • Testing - We provide a local DynamoDB server and mock that will route all DynamoDB requests to the local DynamoDB server.

Setup

Install with (NPM):

npm i @awsless/dynamodb

Basic Usage

import { putItem, getItem, define, ... } from '@awsless/dynamodb';

const posts = define('posts', {
	hash: 'userId',
	sort: 'id',
	schema: object({
		id: number(),
		userId: number(),
		title: string(),
	})
})

// Insert a post into the posts table
await putItem(posts, {
	id: 1,
	userId: 1,
	title: 'Hello World'
})

// Get a post from the posts table
const user = await getItem(posts, { userId: 1, id: 1 })

// Update a post in the posts table
await updateItem(posts, { userId: 1, id: 1 }, {
	update(exp) {
		return exp.update('title').set('Hi...')
	}
})

// Delete a post in the posts table
await deleteItem(posts, { userId: 1, id: 1 })

// List posts from user 1
const { items, cursor } = await query(posts, {
	keyCondition(exp) {
		return exp.where('userId').eq(1)
	}
})

// List posts from user 1 but with stringified cursors
const { items, cursor } = await paginateQuery(posts, {
	keyCondition(exp) {
		return exp.where('userId').eq(1)
	}
})

// List posts
const { items, cursor } = await scan(posts)

// List all posts
for await(const items of scanAll(posts, { batch: 100 })) {
	// Processing batches of 100 items at a time...
	...
}

// Write a transaction
await transactWrite({
	items: [
		transactConditionCheck(posts, { userId: 1, id: 0 }, {
			condition(exp) {
        		return exp.where('id').not.exists
        	}
		}),

		transactPut(posts, { userId: 1, id: 1, title: 'Post 1' }),
		transactPut(posts, { userId: 1, id: 2, title: 'Post 2' }),
		transactPut(posts, { userId: 1, id: 3, title: 'Post 3' }),
	]
})

Mock for testing

import { mockDynamoDB, seedTable, ... } from "@awsless/dynamodb";

const posts = define('posts', {
	hash: 'id',
	schema: object({
		id: number(),
		title: string(),
	})
})

mockDynamoDB({
  tables: [ posts ],
  seeds: [
	seedTable(posts, {
		id: 1,
		title: 'Hello World',
	})
  ]
})

it('your test', async () => {
	const result = await getItem(posts, { id: 1 })
})

Schema Types

We provides the following schema types:

  • array
  • bigfloat
  • bigint
  • binary
  • boolean
  • date
  • enums
  • number
  • object
  • optional
  • record
  • string
  • ttl
  • unknown
  • uuid
  • bigintSet
  • binarySet
  • numberSet
  • stringSet

The binary & binarySet schema type supports the following input value types:

ArrayBuffer, Blob, Buffer, DataView, File, Int8Array, Uint8Array, Uint8ClampedArray, Int16Array, Uint16Array, Int32Array, Uint32Array, Float32Array, Float64Array, BigInt64Array, BigUint64Array

And the output value type is always Uint8Array.

Operation Functions

TypeDescription
getItemGet an item
putItemStore an item
updateItemUpdate an item
deleteItemDelete an item
getIndexedItemGet an item from a specific index
queryQuery a list of items
queryAllQuery a list of items
paginateQueryPagination helper to query a list of items
scanScan the table for items
scanAllScan the table for items
paginateScanPagination helper to scan a list of items
batchGetItemBatch get items
batchPutItemBatch store items
batchDeleteItemBatch delete items
transactWriteExecute a transactional write
transactPutStore an item transactionally
transactUpdateUpdate an item transactionally
transactDeleteDelete an item transactionally
transactConditionCheckConditional check transactionally

Typescript Infer Types

InferInput<T>

Infer the item type of a table definition when inserting an item.

InferOutput<T>

Infer the item type of a table definition when querying an item.

import { define, InferInput, InferOutput, object, uuid, number, string, binary } from "@awsless/dynamodb";

const posts = define('posts', {
	hash: 'id',
	schema: object({
		id: uuid(),
		title: string(),
		likes: number(),
		data: binary(),
	})
})

/* {
  id: UUID,
  title: string,
  likes: number,
  data: BinaryValue
} */
type Post1 = InferInput<typeof posts>

/* {
  id: UUID,
  title: string,
  likes: number,
  data: Uint8Array
} */
type Post2 = InferOutput<typeof posts>

License

MIT

0.0.45

12 months ago

0.0.46

12 months ago

0.0.47

12 months ago

0.0.48

11 months ago

0.0.49

11 months ago

0.0.43

1 year ago

0.0.44

1 year ago

0.0.42

2 years ago

0.0.41

2 years ago

0.0.40

2 years ago

0.0.39

2 years ago

0.0.37

2 years ago

0.0.38

2 years ago

0.0.36

2 years ago

0.0.30

2 years ago

0.0.31

2 years ago

0.0.32

2 years ago

0.0.33

2 years ago

0.0.34

2 years ago

0.0.35

2 years ago

0.0.26

2 years ago

0.0.27

2 years ago

0.0.28

2 years ago

0.0.29

2 years ago

0.0.20

2 years ago

0.0.21

2 years ago

0.0.22

2 years ago

0.0.23

2 years ago

0.0.24

2 years ago

0.0.25

2 years ago

0.0.15

2 years ago

0.0.16

2 years ago

0.0.17

2 years ago

0.0.18

2 years ago

0.0.14

2 years ago

0.0.13

2 years ago

0.0.12

2 years ago

0.0.11

2 years ago

0.0.10

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago