-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdynamo.test.html
More file actions
157 lines (139 loc) · 5.66 KB
/
dynamo.test.html
File metadata and controls
157 lines (139 loc) · 5.66 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<html>
<head>
<script src="https://sdk.amazonaws.com/js/aws-sdk-2.7.16.js"></script>
<script>
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();
var docClient = new AWS.DynamoDB.DocumentClient()
function getData(key) {
const params = {
TableName: 'Users',
Key: {
Email: `${key}`
}
}
docClient.get(params, function(err, data) {
if (err) console.log(err, err.stack) // an error occurred
else console.log(data) // successful response
if (err) {
document.getElementById('textarea').innerHTML = 'Unable to get data by key: ' + '\n' + JSON.stringify(err, undefined, 2)
} else {
document.getElementById('textarea').innerHTML = JSON.stringify(data, undefined, 2)
}
})
}
function 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'
}
docClient.update(params, function(err, data) {
if (err) console.log(err, err.stack) // an error occurred
else console.log(data) // successful response
if (err) {
document.getElementById('textarea').innerHTML = 'Unable to udate: ' + '\n' + JSON.stringify(err, undefined, 2)
} else {
document.getElementById('textarea').innerHTML = JSON.stringify(data, undefined, 2)
}
})
}
function createData(key, src = 'ru', tgt = 'en', name) {
const params = {
TableName: 'Users',
Item: {
Email: `${key}`,
Languages: {
src,
tgt
},
Name: name
}
}
docClient.put(params, function(err, data) {
if (err) console.log(err, err.stack) // an error occurred
else console.log(data) // successful response
if (err) {
document.getElementById('textarea').innerHTML = 'Unable to crate: ' + '\n' + JSON.stringify(err, undefined, 2)
} else {
document.getElementById('textarea').innerHTML = JSON.stringify(data, undefined, 2)
}
})
}
// function createMovies() {
// var params = {
// TableName : "Movies",
// KeySchema: [
// { AttributeName: "year", KeyType: "HASH"},
// { AttributeName: "title", KeyType: "RANGE" }
// ],
// AttributeDefinitions: [
// { AttributeName: "year", AttributeType: "N" },
// { AttributeName: "title", AttributeType: "S" }
// ],
// ProvisionedThroughput: {
// ReadCapacityUnits: 5,
// WriteCapacityUnits: 5
// }
// };
//
// dynamodb.createTable(params, function(err, data) {
// if (err) {
// document.getElementById('textarea').innerHTML = "Unable to create table: " + "\n" + JSON.stringify(err, undefined, 2);
// } else {
// document.getElementById('textarea').innerHTML = "Created table: " + "\n" + JSON.stringify(data, undefined, 2);
// }
// });
// }
</script>
</head>
<body>
<script>
function update() {
const key = document.getElementById('key').value
const src = document.getElementById('src').value
const tgt = document.getElementById('tgt').value
updateData(key, src, tgt)
}
function create() {
const key = document.getElementById('key').value
const src = document.getElementById('src').value
const tgt = document.getElementById('tgt').value
const name = document.getElementById('name').value
createData(key, src, tgt, name)
}
</script>
<label for="key">Key</label><input id="key" type="text" value="sergeydaub@gmail.com" style="width: 300px"/>
<input id="getDataButton" type="button" value="get data" onclick="getData(document.getElementById('key').value)"/>
<br>
<label for="src">Source lang</label><input id="src" type="text" value="de" style="width: 30px"/>
<label for="tgt">Target lang</label><input id="tgt" type="text" value="de" style="width: 30px"/>
<input id="updateDataButton" type="button" value="update data" onclick="update()"/>
<hr>
<br><br>
<label for="name">Name:</label><input id="name" type="text" value="de" style="width: 300px"/>
<input id="createDataButton" type="button" value="create item" onclick="create()"/>
<textarea readonly id="textarea" style="width:400px; height:800px"></textarea>
<p>https://aws.amazon.com/ru/sdk-for-browser/?nc1=h_ls</p>
<p>https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/dynamodb-dg.pdf</p>
<p>https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB.html#getItem-property</p>
<p>https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/WorkingWithItems.html#WorkingWithItems.ReadingData</p>
</body>
</html>