forked from AbdullahZN/dynamo-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
67 lines (58 loc) · 1.92 KB
/
index.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
const AWS = require('aws-sdk');
const ConditionalQueryBuilder = require('./lib/ConditionalQueryBuilder');
const getPromise = func => (method, params) => new Promise((resolve, reject) => {
if (method === 'scan') {
let allData = []
const onScan = (err, data) => {
if (err) {
console.error("Unable to scan the table. Error JSON:", JSON.stringify(err, null, 2));
reject(err)
} else {
allData = allData.concat(data.Items)
if (typeof data.LastEvaluatedKey != "undefined") {
params.ExclusiveStartKey = data.LastEvaluatedKey;
func[method](params, onScan)
}
else {
resolve(allData)
}
}
}
func[method](params, onScan)
}
else {
func[method](params, (err, data) => (err ? reject(err) : resolve(data)))
}
})
// Exports DynamoDB function that returns an object of methods
module.exports = (conf) => {
AWS.config.update({ region: 'ap-northeast-1' });
AWS.config.update(conf);
AWS.CredentialProviderChain.defaultProviders = [
function () { return new AWS.EnvironmentCredentials('AWS'); },
function () { return new AWS.EnvironmentCredentials('AMAZON'); },
function () { return new AWS.SharedIniFileCredentials(); },
function () {
// if AWS_CONTAINER_CREDENTIALS_RELATIVE_URI is set
if (process.env.AWS_CONTAINER_CREDENTIALS_RELATIVE_URI) {
return new AWS.ECSCredentials();
} else {
return new AWS.EC2MetadataCredentials();
}
}
]
const dynamoDB = new AWS.DynamoDB();
const docClient = new AWS.DynamoDB.DocumentClient();
const db = getPromise(dynamoDB);
const doc = getPromise(docClient);
return {
config: dynamoDB.config,
// Select Table and return method object for further queries
select: TableName => new ConditionalQueryBuilder(TableName, {
docClient,
doc,
db,
}),
createSet: params => docClient.createSet(params),
};
};