-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbHelper.js
132 lines (119 loc) · 2.8 KB
/
dbHelper.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/**
* Helper functions to communicate with Dynamodb
*/
const AWS = require('aws-sdk')
const config = require('config')
let documentClient = null
/**
* Create tasks table and initialize DocumentClient
*/
async function initialize () {
AWS.config.update({
region: process.env.REGION,
accessKeyId: process.env.ACCESS_KEY_ID,
secretAccessKey: process.env.SECRET_ACCESS_KEY,
endpoint: process.env.DYNAMODB_ENDPOINT
})
documentClient = new AWS.DynamoDB.DocumentClient()
}
/**
* Returns an instance of DocumentClient
*/
async function getDocumentClient () {
if (!documentClient) {
return initialize()
}
}
/**
* Create an item in Dynamodb
* @param {Object} params
*/
async function put (params) {
await getDocumentClient()
return documentClient.put(params).promise()
}
/**
* Query for item from Dynamodb table
* @param {Object} params
*/
async function query (params) {
await getDocumentClient()
return documentClient.query(params).promise()
}
/**
* Update an item in Dynamodb table
* @param {Object} params
*/
async function update (params) {
await getDocumentClient()
return documentClient.update(params).promise()
}
/**
* Get a project by id
* @param {String} projectId
*/
async function getProject (projectId) {
return ((await query({
TableName: config.get('DYNAMODB.PROJECT_TABLE_NAME'),
KeyConditionExpression: 'id = :idVal',
ExpressionAttributeValues: {
':idVal': projectId
}
})).Items[0])
}
/**
* Updates project status
* @param {String} projectId
* @param {String} newStatus
*/
async function updateProjectStatus (projectId, newStatus) {
return update({
TableName: config.get('DYNAMODB.PROJECT_TABLE_NAME'),
Key: {
id: projectId
},
UpdateExpression: 'set #st = :s',
ExpressionAttributeValues: {
':s': newStatus
},
ExpressionAttributeNames: {
'#st': 'status'
}
})
}
/**
* Get project by the thread that it was launched in by the client
* @param {String} clientSlackThread
*/
async function getProjectByClientSlackThread (clientSlackThread) {
return ((await query({
TableName: config.get('DYNAMODB.PROJECT_TABLE_NAME'),
IndexName: config.get('DYNAMODB.CLIENT_SLACK_THREAD_INDEX'),
KeyConditionExpression: 'clientSlackThread = :c',
ExpressionAttributeValues: {
':c': clientSlackThread
}
})).Items[0])
}
/**
* Returns the client by team id
* @param {String} teamId
*/
async function getClientByTeamId (teamId) {
return ((await query({
TableName: config.get('DYNAMODB.SLACK_CLIENTS_TABLE_NAME'),
KeyConditionExpression: 'teamId = :teamIdVal',
ExpressionAttributeValues: {
':teamIdVal': teamId
}
})).Items[0])
}
module.exports = {
put,
query,
update,
getProject,
updateProjectStatus,
getProjectByClientSlackThread,
getClientByTeamId
}