-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdynamodb-helpers.ts
More file actions
56 lines (49 loc) · 1.48 KB
/
dynamodb-helpers.ts
File metadata and controls
56 lines (49 loc) · 1.48 KB
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
import {
DeleteItemCommand,
DeleteItemCommandOutput,
DynamoDBClient,
PutItemCommand,
PutItemCommandOutput,
} from '@aws-sdk/client-dynamodb';
import { QueryCommand, QueryCommandOutput } from '@aws-sdk/lib-dynamodb';
import { marshall } from '@aws-sdk/util-dynamodb';
import { REGION, TTL_TABLE_NAME } from 'constants/backend-constants';
import { TtlDynamodbRecord } from 'utils';
const dynamoDbClient = new DynamoDBClient({ region: REGION });
export async function getTtl(messageUri: string) {
const params = {
TableName: TTL_TABLE_NAME,
KeyConditionExpression: `PK = :messageUri`,
ExpressionAttributeValues: {
':messageUri': messageUri,
},
};
const request = new QueryCommand(params);
const { Items }: QueryCommandOutput = await dynamoDbClient.send(request);
return Items ?? [];
}
export async function putTtl(ttlItem: TtlDynamodbRecord) {
const params = {
TableName: TTL_TABLE_NAME,
Item: marshall(ttlItem),
};
const request = new PutItemCommand(params);
const output: PutItemCommandOutput = await dynamoDbClient.send(request);
return output.$metadata.httpStatusCode;
}
export async function deleteTtl(messageUri: string) {
const params = {
TableName: TTL_TABLE_NAME,
Key: {
PK: {
S: messageUri,
},
SK: {
S: 'TTL',
},
},
};
const request = new DeleteItemCommand(params);
const output: DeleteItemCommandOutput = await dynamoDbClient.send(request);
return output.$metadata.httpStatusCode;
}