-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.js
64 lines (55 loc) · 2.31 KB
/
handler.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
const AWS = require('aws-sdk');
const { assert, isPlainObject, packageName } = require('./utils');
const { RedynTransactionBlock } = require('./transactions');
/* eslint-disable no-invalid-this */
function createStandaloneHandler(name, command) {
return async function method(...args) {
try {
const { client } = this;
assert(client instanceof AWS.DynamoDB, 'Expected client to be an instance of AWS.DynamoDB');
const handler = async body => {
assert(isPlainObject(body), new TypeError('Expected handler body to be an object'));
assert(Object.keys(body).length === 1, new TypeError('Expected handler body to have one key'));
const { Get, Put, Update, Delete } = body;
let result = null;
if (Get) {
// console.log(JSON.stringify({ getItem: Get }, null, 2));
result = await client.getItem(Get).promise();
// console.log(JSON.stringify({ getItem: result }, null, 2));
} else if (Put) {
// console.log(JSON.stringify({ putItem: Put }, null, 2));
result = await client.putItem(Put).promise();
// console.log(JSON.stringify({ putItem: result }, null, 2));
} else if (Update) {
// console.log(JSON.stringify({ updateItem: Update }, null, 2));
result = await client.updateItem(Update).promise();
// console.log(JSON.stringify({ updateItem: result }, null, 2));
} else if (Delete) {
// console.log(JSON.stringify({ deleteItem: Delete }, null, 2));
result = await client.deleteItem(Delete).promise();
// console.log(JSON.stringify({ deleteItem: result }, null, 2));
} else {
const [ key ] = Object.keys(body);
assert(false, new TypeError(`Unknown key ${key} for non-transact handler`));
}
return result;
};
handler.command = name;
handler.isTransaction = false;
const result = await command.call(this, handler, ...args);
return result;
} catch (err) {
err.message = `[${packageName}][${name}]: ${err.message}`;
throw err;
}
};
}
function createTransactionHandler(name, command) {
return function transact(...args) {
return new RedynTransactionBlock(name, command, args);
};
}
module.exports = {
createStandaloneHandler,
createTransactionHandler,
};