forked from AbdullahZN/dynamo-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
38 lines (31 loc) · 1.09 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
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) => {
AWS.config.update({ region });
if (typeof config === 'string') {
AWS.config.loadFromPath(config);
} else if (typeof config === 'object') {
AWS.config.update(config);
}
const dynamoDB = new AWS.DynamoDB();
if (!dynamoDB.config.credentials) {
throw new Error('Can not load AWS credentials');
}
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),
};
};