-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynamoDB.js
156 lines (132 loc) · 3.41 KB
/
dynamoDB.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
var helper = require('./helper'),
Q = require('q');
var DynamoDB = new helper.AWS.DynamoDB(),
TABLE_NAME = 'CS550';
function init(ip, callback) {
var deferred = Q.defer();
createTable().then(function (status) {
deferred.resolve(status);
}, function(status) {
deferred.reject(status);
});
return deferred.promise.nodeify(callback);
}
// NOTE: create table
function createTable(callback) {
var deferred = Q.defer();
var params = {
AttributeDefinitions: [ /* required */
{
AttributeName: 'key',
/* required */
AttributeType: 'S' /* required */
}
],
KeySchema: [ /* required */
{
AttributeName: 'key',
/* required */
KeyType: 'HASH' /* required */
}
],
ProvisionedThroughput: { /* required */
ReadCapacityUnits: 20,
/* required */
WriteCapacityUnits: 20 /* required */
},
TableName: TABLE_NAME,
/* required */
};
DynamoDB.createTable(params, function (err, data) {
if (err && err.code != 'ResourceInUseException') {
deferred.reject("createTable() : " + err + err.stack);
} else {
deferred.resolve(true);
}
});
return deferred.promise.nodeify(callback);
}
// NOTE: Add key-value to table (key : 'key')
function putValue(key, value, callback) {
var deferred = Q.defer();
var params = {
Item: { /* required */
'key': {
"S" : key.toString()
},
'value': {
"S" : value.toString()
}
},
TableName: TABLE_NAME,
/* required */
Expected: {
key: {
Exists: false
}
}
};
DynamoDB.putItem(params, function (err, data) {
if (err) {
console.log("Put:", err);
deferred.reject(false);
} else
deferred.resolve(true);
});
return deferred.promise.nodeify(callback);
}
// NOTE: Get value from table (key : 'key')
function getValue(key, callback) {
var deferred = Q.defer();
var params = {
Key: { /* required */
'key': {
"S" : key.toString()
}
},
TableName: TABLE_NAME,
AttributesToGet: [
'value'
],
};
DynamoDB.getItem(params, function (err, data) {
if (err) {
console.log("Get:", err);
deferred.reject(false);
} else
deferred.resolve(true);
});
return deferred.promise.nodeify(callback);
}
// NOTE: Get value from table (key : 'key')
function deleteKey(key, callback) {
var deferred = Q.defer();
var params = {
Key: { /* required */
'key': {
"S" : key.toString()
}
},
TableName: TABLE_NAME
};
DynamoDB.deleteItem(params, function (err, data) {
if (err) {
console.log("Delete:", err);
deferred.reject(false);
} else
deferred.resolve(true);
});
return deferred.promise.nodeify(callback);
}
function getSize(callback) {
var deferred = Q.defer();
deferred.resolve("SIZE of MongoDB");
return deferred.promise.nodeify(callback);
}
module.exports = {
init: init,
putValue: putValue,
getValue: getValue,
deleteKey: deleteKey,
getSize: getSize
};