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