-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathdynamodb_connection.ts
59 lines (51 loc) · 1.42 KB
/
dynamodb_connection.ts
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
import { Connection } from "./connection";
import * as AWS from "aws-sdk";
import { DynamoDB } from "aws-sdk";
import * as HTTP from "http";
import * as HTTPS from "https";
const AWSXRay = require("aws-xray-sdk-core");
export class DynamoDBConnection implements Connection {
constructor(options: {
endpoint: string | undefined,
enableAWSXray: boolean,
}) {
const dynamoDBOptions = {
endpoint: options.endpoint,
httpOptions: {
agent: this.httpAgent(options.endpoint),
}
};
if (options.enableAWSXray) {
const aws = AWSXRay.captureAWS(AWS);
this.__client = new aws.DynamoDB(dynamoDBOptions);
this.__documentClient = new aws.DynamoDB.DocumentClient({
service: this.__client,
});
} else {
this.__client = new DynamoDB(dynamoDBOptions);
this.__documentClient = new DynamoDB.DocumentClient({
service: this.__client,
});
}
}
private httpAgent(endpoint: string | undefined) {
if (endpoint && endpoint.startsWith("http://")) {
return new HTTP.Agent({
keepAlive: true
});
} else {
return new HTTPS.Agent({
rejectUnauthorized: true,
keepAlive: true
});
}
}
private __documentClient: AWS.DynamoDB.DocumentClient;
public get documentClient() {
return this.__documentClient;
}
private __client: AWS.DynamoDB;
public get client() {
return this.__client;
}
}