Coder Social home page Coder Social logo

dynamodb's Introduction

@saibotsivad/dynamodb

Generate signed HTTP requests to AWS DynamoDB.

Install

The normal way:

npm install @saibotsivad/dynamodb

Overview

Instantiate with the normal AWS credentials and a fetch-like interface (see below for compatability notes), then call it to make requests:

import { dynamodb } from '@saibotsivad/dynamodb'

// Normal AWS IAM credentials:
const credentials = {
	region: 'us-east-1',
	secretAccessKey: 'hKVU_EXAMPLE_SECRET_5rjo',
	accessKeyId: 'AKIA_EXAMPLE_KEY'
}

// In a Worker environment (aka Cloudflare Workers) you can use the
// global fetch directly:
const db = dynamodb({ credentials, fetch: globalThis.fetch })

// But in the NodeJS environment, you'll need to use something like "httpie":
import { post } from 'httpie' // only POST is used
const fetch = async (url, options) => post(url, options)
const db = dynamodb({ credentials, fetch })

const response = await db('PutItem', {
	ReturnConsumedCapacity: 'TOTAL',
	TableName: 'Music',
	Item: {
		AlbumTitle: {
			S: 'Somewhat Famous'
		},
		Artist: {
			S: 'No One You Know'
		},
		SongTitle: {
			S: 'Call Me Today'
		}
	},
})
// response => { ConsumedCapacity: { CapacityUnits: 1, TableName: 'Music' } }

Error Handling

If the response is an error, for example if you try to PutItem on a table that doesn't exist, calling await db will throw an error named AwsException that contains the following properties:

  • name: String - Will always be AwsException.
  • method: String - This is simply the method name that you provided, e.g. PutItem.
  • params: Object - These are the parameters that were used, e.g. { TableName, Item }.
  • type: String - The long-form error code from AWS, e.g. com.amazonaws.dynamodb.v20120810#ResourceNotFoundException.
  • code: String - The short-form error code, e.g. ResourceNotFoundException.

API

Instantiate a database using the AWS credentials, and a fetch-like object:

import { dynamodb } from '@saibotsivad/dynamodb'
const db = dynamodb({
	credentials: {
		region: 'us-east-1',
		secretAccessKey: 'hKVU_EXAMPLE_SECRET_5rjo',
		accessKeyId: 'AKIA_EXAMPLE_KEY'
	},
	fetch: globalThis.fetch
})

The fetch function must have a signature like async (method: string, params: object) and return one of the following:

  • { json: async function } - The normal fetch does this.
  • { data: string | object } - If the data property is an object it'll get returned, or if it's a string it'll get JSON-parsed.
  • { body: string | object } - Same thing with the body property.

Methods

The DynamoDB API supports all valid DynamoDB service methods.

How to Read the Docs

To figure out what your request parameters and response object should look like, have a look at the AWS docs.

  • v2: For each link, there is a description of the service and an example of its use. For each service, after the example is a long, sometimes difficult to read, list of parameters and their meaning.
  • v3: For each link you'll find the same description and examples, but the request parameters are called "input" and the response is called "output". For example, on the BatchGetItem docs page, you'll find a link to BatchGetItemCommandInput and BatchGetItemCommandOutput which you'll need to click into to figure out the parameters of each.

License

Published and released under the VOL.

dynamodb's People

Contributors

saibotsivad avatar

Stargazers

ig avatar  avatar Anton Rusinov avatar Josh Duff avatar

Watchers

 avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.