-
Notifications
You must be signed in to change notification settings - Fork 21
/
03-explicit-data-type.js
94 lines (76 loc) · 2.62 KB
/
03-explicit-data-type.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
/* This example demonstrates use of explicit data type.
* Run this example on the command line as follows:
* $ node 03-explicit-data-type.js
* Observe the output dump shows the data structures as required by DynamoDb.
* When running this example, you may choose to use a live DynamoDb instance
* (optinal) by setting `USE_LIVE_DB'. If you choose to use a live DynamoDb
* instance, you will to setup a DynamoDb table with name and key mentioned
* below. */
const ddt = require('dynamodb-data-types');
const attr = ddt.AttributeValue;
const attrUpdate = ddt.AttributeValueUpdate;
const USE_LIVE_DB = false;
const REGION = 'us-east-1';
const TABLE_NAME = "TestTableForDynamoDbDataTypes"; // Hash key: `id' (Number).
const dynamo = dynamoDb(USE_LIVE_DB);
function putData(data, id, data_opts, callback) {
data.id = id;
const opts = {
TableName: TABLE_NAME,
Item: attr.wrap(data, data_opts)
};
dynamo.putItem(opts, function(err){
if (err)
console.error("put error:", err);
callback(err);
});
}
function getData(id, callback) {
const opts = {
TableName: TABLE_NAME,
Key: attr.wrap({id: id})
};
dynamo.getItem(opts, function(err, data){
if (err)
console.error("put error:", err);
else
callback(null, data.Item);
});
}
function dynamoDb(useLiveDb) {
// If not using a live db, just return dummy functions.
if (!useLiveDb) {
return {
putItem: function dummyPut(){},
getItem: function dummyGet(){
console.log('To read data from DynamoDB, set USE_LIVE_DB=true');
}
};
}
/* If using a live db, you need to create the necessary table and key.
* Note: As per Amazon's recommended configuration, `accessKeyId' and
* `secretAccessKey' are read from environment variables
* `AWS_SECRET_ACCESS_KEY' and `AWS_ACCESS_KEY_ID'.
*/
const AWS = require('aws-sdk');
AWS.config.update({region: REGION});
const dynamo = new AWS.DynamoDB({apiVersion: '2013-10-16'});
return dynamo;
}
const data = {
alphabets: ['c', 'a', 'b']
};
const opts = { types: { alphabets: 'L' } };
console.log("Put data without explicit type:\n", JSON.stringify(attr.wrap(data)));
// Put data without explicit type:
// {"alphabets":{"SS":["c","a","b"]},"id":{"N":"3"}}
console.log("Put data with explicity type 'L':\n", JSON.stringify(attr.wrap(data, opts)));
// Put data with explicity type 'L':
// {"alphabets":{"L":[{"S":"c"},{"S":"a"},{"S":"b"}]},"id":{"N":"3"}}
const dbId = 3;
putData(data, dbId, opts, function(err) {
console.log('put alphabets:', data.alphabets);
getData(dbId, function(err, data) {
console.log('get alphabets:', attr.unwrap(data).alphabets);
});
});