-
Notifications
You must be signed in to change notification settings - Fork 46
/
db.js
46 lines (41 loc) · 1.79 KB
/
db.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
var AWS = require('aws-sdk')
// Because the AWS SDK works in the browser as well, we can share this file and all its
// functions and reuse them on both the server and the browser
var db = module.exports = new AWS.DynamoDB({
// This endpoint will try to connect to a DynamoDB running locally
// Comment this out if you want to connect to a live/production AWS DynamoDB instance
endpoint: 'https://localhost:4567',
region: 'us-east-1',
// These credentials are only necessary if connecting to AWS,
// but including credentials in your client-side code is obviously
// problematic if your project is public facing.
// See https://docs.aws.amazon.com/AWSJavaScriptSDK/guide/browser-configuring.html#Loading_Credentials_in_the_Client_s_Browser
// for other (safer) methods to authenticate users for the AWS SDK
credentials: {accessKeyId: 'akid', secretAccessKey: 'secret'},
})
// This function will fetch the id, date and title of all posts in our
// "grumblr" table
db.getAllPosts = function(cb) {
db.scan({TableName: 'grumblr', AttributesToGet: ['id', 'date', 'title']}, function(err, res) {
if (err) return cb(err)
cb(null, res.Items.map(fromDynamo).sort(function(post1, post2) {
return post1.date.localeCompare(post2.date)
}))
})
}
// This function will fetch the detail of a particular posts from our "grumblr"
// table
db.getPost = function(id, cb) {
db.getItem({TableName: 'grumblr', Key: {id: {S: id}}}, function(err, res) {
if (err) return cb(err)
if (!res.Item) return cb(new Error('NotFound'))
cb(null, fromDynamo(res.Item))
})
}
// A simple utility function to flatten a DynamoDB object
function fromDynamo(dynamoObj) {
return Object.keys(dynamoObj).reduce(function(obj, key) {
obj[key] = dynamoObj[key][Object.keys(dynamoObj[key])[0]]
return obj
}, {})
}