-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublish.js
37 lines (29 loc) · 891 Bytes
/
publish.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
const { DynamoDBClient } = require('@aws-sdk/client-dynamodb');
const { DynamoDBDocumentClient, PutCommand } = require('@aws-sdk/lib-dynamodb');
async function publishPayload(tableArn, payload, credentials = null) {
const region = tableArn.split(':')[3];
const table = tableArn.split('/')[1];
const client = ddbClient(region, credentials);
const docClient = DynamoDBDocumentClient.from(client);
return docClient.send(new PutCommand({
TableName: table,
Item: payload,
}));
}
function ddbClient(region, credentials = null) {
let options = {
region
};
if (credentials != null) {
options.credentials = credentials;
}
const endpoint = process.env.AWS_ENDPOINT_URL;
if (typeof endpoint == 'string' && endpoint !== '') {
options.endpoint = endpoint
}
return new DynamoDBClient(options);
}
module.exports = {
publishPayload,
ddbClient,
};