-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdynamoDB.js
More file actions
115 lines (105 loc) · 2.78 KB
/
dynamoDB.js
File metadata and controls
115 lines (105 loc) · 2.78 KB
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
/**
*
* @returns {Promise<DynamoDB>}
*/
export function getDB() {
return new Promise((resolve, reject) => {
const hdl = setInterval(() => {
if (window.AWS) {
clearInterval(hdl)
resolve(new DynamoDB(window.AWS))
}
}, 100)
})
}
class DynamoDB {
constructor(aws) {
this.aws = aws
aws.config.update({
region: 'us-east-1',
// endpoint: 'http://localhost:63342',
// endpoint: document.location.href,
// accessKeyId default can be used while using the downloadable version of DynamoDB.
// For security reasons, do not store AWS Credentials in your files. Use Amazon Cognito instead.
accessKeyId: 'AKIAWVVBQD4VXVL5UUHP',
// secretAccessKey default can be used while using the downloadable version of DynamoDB.
// For security reasons, do not store AWS Credentials in your files. Use Amazon Cognito instead.
secretAccessKey: 'q7ZHneCBptUJgdneDANGXznwR405mkFqrJyDHPys'
})
// var dynamodb = new AWS.DynamoDB();
this.docClient = new aws.DynamoDB.DocumentClient()
}
/**
* @param {string} key
* @returns {Promise<Object>}
*/
getData(key) {
const params = {
TableName: 'Users',
Key: {
Email: `${key}`
}
}
const { docClient } = this
return new Promise((resolve, reject) => {
docClient.get(params, function(err, data) {
if (err) reject(err) // an error occurred
else resolve(data) // successful response
})
})
}
/**
* @param {string} key
* @param {string} src
* @param {string} tgt
* @returns {Promise<Object>}
*/
updateData(key, src = 'ru', tgt = 'en') {
const params = {
TableName: 'Users',
Key: {
Email: `${key}`,
},
UpdateExpression: 'set Languages.tgt = :t, Languages.src=:s',
ExpressionAttributeValues: {
':t': `${tgt}`,
':s': `${src}`
},
ReturnValues: 'UPDATED_NEW'
}
const { docClient } = this
return new Promise((resolve, reject) => {
docClient.update(params, function(err, data) {
if (err) reject(err) // an error occurred
else resolve(data) // successful response
})
})
}
/**
* @param {string} key
* @param {string} src
* @param {string} tgt
* @param {string} name
* @returns {Promise<Object>}
*/
createData(key, src = 'ru', tgt = 'en', name) {
const params = {
TableName: 'Users',
Item: {
Email: `${key}`,
Languages: {
src,
tgt
},
Name: name
}
}
const { docClient } = this
return new Promise((resolve, reject) => {
docClient.put(params, function(err, data) {
if (err) reject(err) // an error occurred
else resolve(data) // successful response
})
})
}
}