forked from AbdullahZN/dynamo-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
65 lines (54 loc) · 2.24 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
const AWS = require('aws-sdk');
const ConditionalQueryBuilder = require('./lib/ConditionalQueryBuilder');
const getPromise = func => (method, params) => new Promise((resolve, reject) => {
func[method](params, (err, data) => (err ? reject(err) : resolve(data)));
});
// Exports DynamoDB function that returns an object of methods
module.exports = (region = 'eu-central-1', config) => {
var dynamoDB, credentialsObj, configObj;
/*
This is for overwriting Global AWS.config and pass parameters directly trough the constructor
// http://docs.amazonaws.cn/en_us/AWSJavaScriptSDK/guide/node-configuring.html
// https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB.html#constructor-property
if (typeof config === 'string') {
configObj = Object.assign(JSON.parse(require('fs').readFileSync(config, 'utf8')),
{ region: region, sessionToken: null });
} else if (typeof config === 'object') {
configObj = Object.assign(config, { region: region })
} else {
let chain = new AWS.CredentialProviderChain()
chain.defaultProviders = [
function () { return new AWS.EnvironmentCredentials('AWS') },
function () { return new AWS.SharedIniFileCredentials() },
// function () { return new AWS.SharedIniFileCredentials({profile: 'my_profile_name'}); },
function () { return new AWS.EC2MetadataCredentials() }
]
chain.resolve((err, cred) => {
// console.log('cred', cred, err)
if (!err) {
credentialsObj = cred
configObj = { accessKeyId: credentialsObj.accessKeyId, sessionToken: credentialsObj.sessionToken }
}
})
}
dynamoDB = new AWS.DynamoDB(configObj);
if (!dynamoDB.config.credentials) {
throw new Error('Can not load AWS credentials');
}
*/
AWS.config.update({ region: region })
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),
};
};