UNPKG

1.61 kBMarkdownView Raw
1# Dynatable
2A DynamoDB table wrapper with a promise wrapped simplified API for the AWS SDK's DynamoDB.DocumentClient.
3
4## Why?
5DynamoDB.DocumentClient is the official way to talk to your DynamoDB databases, but the API is a little clunky and not very JavaScripty.
6
7Dynatable gives you an easier to use API which borrows a little inspiration from MongoDB.
8
9## Usage
10```
11const AWS = require('aws-sdk');
12const dynatable = require('dynatable');
13
14AWS.config.update({
15 region: "eu-west-1",
16 accessKeyId: "YOUR_KEY_HERE",
17 secretAccessKey: "YOUR_SECRET_KEY_HERE"
18});
19const docClient = new AWS.DynamoDB.DocumentClient();
20
21// Imagine you have a table called `users`, which is set up with an `id` key in DynamoDB
22const users = dynatable(docClient, 'users', { id: 'N' });
23
24// You now have
25users.get({ id: 1 })
26 .then(users => console.log(users));
27// [{ id: 1, name: 'Dynatable', interests: 'API wrapping, getting, putting, updating and deleting'}]
28```
29
30Or the way I use it, define (and export) all tables of your project in one file, and then import them where needed:
31
32**tables.js**
33```
34// all the setup from the previous example here
35
36export const userTable = dynatable(docClient, 'users', { id: 'N' });
37export const postTable = dynatable(docClient, 'users', { id: 'N' });
38```
39
40**posts.js**
41```
42import { userTable, postTable } from './tables';
43
44// Get user details
45const userDetails = userTable.findOne({ id: userId });
46
47// Get user's post
48const userPosts = postTable.find({ userId });
49
50Promise.all([ userDetails, userPosts ])
51 .then(([details, posts]) => {
52 // Do something with details and posts here
53 });
54```