-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynamo.js
65 lines (56 loc) · 1.5 KB
/
dynamo.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
const AWS = require('aws-sdk')
require('dotenv').config()
AWS.config.update({
region: process.env.REGION,
accessKeyId: process.env.ACCESS,
secretAccessKey: process.env.SECRET,
})
const dynamoClient = new AWS.DynamoDB.DocumentClient()
const TABLE_NAME = 'MeditationDatabase'
const getSessions = async () => {
const params = {
TableName: TABLE_NAME,
}
const sessions = await dynamoClient.scan(params).promise()
// sorts the array of items by date before displaying
sessions.Items.sort((a, b) => b.dateMilliseconds - a.dateMilliseconds)
return sessions
}
const addOrUpdateSession = async (session) => {
const params = {
TableName: TABLE_NAME,
Item: session,
}
return await dynamoClient.put(params).promise()
}
const getSessionByID = async (SessionID) => {
const params = {
TableName: TABLE_NAME,
Key: {
SessionID
},
}
response = await dynamoClient.get(params).promise()
// if the response is an empty object, throw error and app.js sends 404
if (!Object.keys(response).length){
throw `No Resource with id of ${SessionID}`
}else{
return response
}
}
const deleteSession = async (SessionID) => {
const params = {
TableName: TABLE_NAME,
Key: {
SessionID
},
}
return await dynamoClient.delete(params).promise()
}
module.exports = {
dynamoClient,
getSessionByID,
getSessions,
deleteSession,
addOrUpdateSession,
}