|
1 | 1 | //helper for dealing with DynamoDB's proprietary attribute requirements
|
2 | 2 |
|
3 | 3 | var Dal = function () {
|
4 |
| - this.AWS = require('aws-sdk'); |
5 |
| - //load your aws credentials from a file |
6 |
| - this.AWS.config.loadFromPath(__dirname + '/aws.json'); |
7 |
| - //change the endpoint to match your dynamodb endpoint |
8 |
| - this.db = new this.AWS.DynamoDB({ |
9 |
| - endpoint: "https://dynamodb.us-east-1.amazonaws.com/" |
10 |
| - }); |
11 |
| -} |
| 4 | + this.AWS = require('aws-sdk'); |
| 5 | + //load your aws credentials from a file |
| 6 | + this.AWS.config.loadFromPath(__dirname + '/aws.json'); |
| 7 | + //change the endpoint to match your dynamodb endpoint |
| 8 | + this.db = new this.AWS.DynamoDB({ |
| 9 | + endpoint: "https://dynamodb.us-east-1.amazonaws.com/" |
| 10 | + }); |
| 11 | +}; |
12 | 12 |
|
13 | 13 | Dal.prototype = {
|
14 |
| - decodeValue: function (map) { |
15 |
| - if (typeof map.N !== 'undefined') |
16 |
| - return parseFloat(map.N); |
17 |
| - if (typeof map.S !== 'undefined') |
18 |
| - return map.S; |
19 |
| - return map.B |
20 |
| - }, |
21 |
| - decodeValues: function (obj, vals) { |
22 |
| - var self = this; |
23 |
| - for (var key in vals) { |
24 |
| - if (vals.hasOwnProperty(key)) { |
25 |
| - obj[key] = self.decodeValue(vals[key]); |
26 |
| - } |
27 |
| - } |
28 |
| - }, |
29 |
| - formatAttributes: function (obj) { |
30 |
| - var item = {}; |
31 |
| - for (var p in obj) { |
32 |
| - if (obj.hasOwnProperty(p)) { |
33 |
| - if (obj[p] === 0 || typeof obj[p] == "number") { |
34 |
| - item[p] = {"N": obj[p].toString()}; |
35 |
| - } |
36 |
| - else { |
37 |
| - item[p] = {"S": obj[p]}; |
38 |
| - } |
39 |
| - } |
40 |
| - } |
41 |
| - return item; |
42 |
| - }, |
43 |
| - deleteEmptyProperties: function (obj) { |
44 |
| - //DynamoDB does not allow you to store empty values |
45 |
| - for (var p in obj) { |
46 |
| - if (!obj.hasOwnProperty(p)) |
47 |
| - continue; |
48 |
| - if (obj[p] !== 0 && (obj[p] == null || obj[p] == "")) { |
49 |
| - delete(obj[p]); |
50 |
| - } |
51 |
| - } |
52 |
| - }, |
53 |
| - doGet: function (tableName, keyHash, consistent, callback) { |
54 |
| - consistent = typeof consistent !== 'undefined' ? consistent : false; |
55 |
| - var self = this; |
56 |
| - var result = this.db.getItem({ |
57 |
| - TableName: tableName, |
58 |
| - Key: keyHash, |
59 |
| - ConsistentRead: consistent |
60 |
| - }, function (err, data) { |
61 |
| - var obj = {}; |
62 |
| - if (err != null) { |
63 |
| - if (callback) callback(err, null); |
64 |
| - return; |
65 |
| - } |
66 |
| - if (typeof data.Item != "undefined") { |
67 |
| - self.decodeValues(obj, data.Item); |
68 |
| - } |
69 |
| - if (callback) callback(err, obj); |
70 |
| - }); |
71 |
| - }, |
72 |
| - doSet: function (obj, tableName, keyHash, callback) { |
73 |
| - this.deleteEmptyProperties(obj); |
74 |
| - this.db.putItem({ |
75 |
| - TableName: tableName, |
76 |
| - Item: this.formatAttributes(obj) |
77 |
| - }, function (err, data) { |
| 14 | + decodeValue: function (map) { |
| 15 | + if (typeof map.N !== 'undefined') |
| 16 | + return parseFloat(map.N); |
| 17 | + if (typeof map.S !== 'undefined') |
| 18 | + return map.S; |
| 19 | + return map.B; |
| 20 | + }, |
| 21 | + decodeValues: function (obj, vals) { |
| 22 | + var self = this; |
| 23 | + for (var key in vals) { |
| 24 | + if (vals.hasOwnProperty(key)) { |
| 25 | + obj[key] = self.decodeValue(vals[key]); |
| 26 | + } |
| 27 | + } |
| 28 | + }, |
| 29 | + formatAttributes: function (obj) { |
| 30 | + var item = {}; |
| 31 | + for (var p in obj) { |
| 32 | + if (obj.hasOwnProperty(p)) { |
| 33 | + if (obj[p] === 0 || typeof obj[p] === "number") { |
| 34 | + item[p] = {"N": obj[p].toString()}; |
| 35 | + } |
| 36 | + else { |
| 37 | + item[p] = {"S": obj[p]}; |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + return item; |
| 42 | + }, |
| 43 | + deleteEmptyProperties: function (obj) { |
| 44 | + //DynamoDB does not allow you to store empty values |
| 45 | + for (var p in obj) { |
| 46 | + if (!obj.hasOwnProperty(p)) |
| 47 | + continue; |
| 48 | + if (obj[p] !== 0 && (obj[p] === null || obj[p] === "")) { |
| 49 | + delete(obj[p]); |
| 50 | + } |
| 51 | + } |
| 52 | + }, |
| 53 | + doGet: function (tableName, keyHash, consistent, callback) { |
| 54 | + consistent = typeof consistent !== 'undefined' ? consistent : false; |
| 55 | + var self = this; |
| 56 | + var result = this.db.getItem({ |
| 57 | + TableName: tableName, |
| 58 | + Key: keyHash, |
| 59 | + ConsistentRead: consistent |
| 60 | + }, function (err, data) { |
| 61 | + var obj = {}; |
| 62 | + if (err !== null) { |
| 63 | + if (callback) callback(err, null); |
| 64 | + return; |
| 65 | + } |
| 66 | + if (typeof data.Item !== "undefined") { |
| 67 | + self.decodeValues(obj, data.Item); |
| 68 | + } |
| 69 | + if (callback) callback(err, obj); |
| 70 | + }); |
| 71 | + }, |
| 72 | + doSet: function (obj, tableName, keyHash, callback) { |
| 73 | + this.deleteEmptyProperties(obj); |
| 74 | + this.db.putItem({ |
| 75 | + TableName: tableName, |
| 76 | + Item: this.formatAttributes(obj) |
| 77 | + }, function (err, data) { |
78 | 78 |
|
79 |
| - if (err != null) { |
80 |
| - callback(err, null); |
81 |
| - return; |
82 |
| - } |
83 |
| - callback(err, obj); |
84 |
| - }); |
85 |
| - } |
| 79 | + if (err !== null) { |
| 80 | + callback(err, null); |
| 81 | + return; |
| 82 | + } |
| 83 | + callback(err, obj); |
| 84 | + }); |
| 85 | + } |
86 | 86 | };
|
87 | 87 |
|
88 | 88 | module.exports = new Dal();
|
89 |
| - |
90 |
| - |
0 commit comments