|
1 | 1 | import { DynamoDBClient, QueryCommand } from "@aws-sdk/client-dynamodb"; |
2 | 2 | import { unmarshall } from "@aws-sdk/util-dynamodb"; |
3 | 3 | import { withRetry } from "../util/retry"; |
4 | | -import { isRecordExpired, RecordWithExpiry } from "./util/is-record-expired"; |
5 | | -import { RecordExpiredError, RecordNotFoundError, TooManyRecordsError } from "./exceptions/errors"; |
6 | | -import { Logger } from "@aws-lambda-powertools/logger"; |
| 4 | +import { RecordNotFoundError, TooManyRecordsError } from "./exceptions/errors"; |
| 5 | +import { UnixSecondsTimestamp } from "../types/brands"; |
| 6 | +import { logger } from "../util/logger"; |
7 | 7 |
|
8 | | -export type SessionIdRecord = { sessionId: string } & RecordWithExpiry; |
| 8 | +export type SessionIdRecord = { sessionId: string; expiryDate?: UnixSecondsTimestamp }; |
9 | 9 |
|
10 | 10 | /** |
11 | 11 | * Retrieves a record from DynamoDB, given a table name and session ID. |
12 | | - * Handles retries and expiry validation, and returns an array of valid entries. |
13 | | - * The array will usually have length 1, but it's possible that multiple rows will be returned. |
| 12 | + * Handles retries and expiry validation, and returns a single valid entry. |
14 | 13 | * |
15 | 14 | * Use a type parameter to set the type of the entity that will be returned. |
16 | 15 | */ |
17 | 16 | export async function getRecordBySessionId< |
18 | 17 | /** The type that will be returned by the function. Must include sessionId key. */ |
19 | 18 | ReturnType extends SessionIdRecord, |
20 | | ->( |
21 | | - /** The name of the table in DynamoDB. Probably looks like "some-table-some-stack". */ |
22 | | - tableName: string, |
23 | | - /** The session ID to search for. */ |
24 | | - sessionId: string, |
25 | | - logger: Logger, |
26 | | - opts?: { allowNoEntries?: boolean; allowMultipleEntries?: boolean }, |
27 | | - dynamoClient = new DynamoDBClient() |
28 | | -) { |
29 | | - const allowNoEntries = opts?.allowNoEntries ?? false; |
30 | | - const allowMultipleEntries = opts?.allowMultipleEntries ?? false; |
31 | | - |
| 19 | +>(dynamoClient: DynamoDBClient, tableName: string, sessionId: string, expiryColumn: keyof ReturnType) { |
32 | 20 | async function queryRecord() { |
| 21 | + // Use ExpressionAttributeNames as it's possible the expiry column name is a reserved word. |
| 22 | + // Notably, 'ttl' is a reserved word. |
| 23 | + // https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/ReservedWords.html |
| 24 | + // https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.ExpressionAttributeNames.html |
| 25 | + |
33 | 26 | const command = new QueryCommand({ |
34 | 27 | TableName: tableName, |
35 | | - KeyConditionExpression: "sessionId = :value", |
| 28 | + KeyConditionExpression: `sessionId = :value`, |
| 29 | + FilterExpression: `#expiry > :expiry`, |
| 30 | + ExpressionAttributeNames: { |
| 31 | + "#expiry": String(expiryColumn), |
| 32 | + }, |
36 | 33 | ExpressionAttributeValues: { |
37 | 34 | ":value": { |
38 | 35 | S: sessionId, |
39 | 36 | }, |
| 37 | + ":expiry": { |
| 38 | + N: Math.floor(Date.now() / 1000).toString(), |
| 39 | + }, |
40 | 40 | }, |
41 | 41 | }); |
42 | 42 |
|
43 | 43 | const result = await dynamoClient.send(command); |
44 | 44 |
|
45 | | - if (!allowNoEntries && (result.Count === 0 || !result.Items)) { |
| 45 | + if (result.Count === 0 || !result.Items) { |
46 | 46 | throw new RecordNotFoundError(tableName, sessionId); |
47 | 47 | } |
48 | 48 |
|
49 | | - return result.Items ?? []; |
| 49 | + return result.Items; |
50 | 50 | } |
51 | 51 |
|
52 | 52 | const queryResult = await withRetry(queryRecord, logger, { |
53 | 53 | maxRetries: 3, |
54 | 54 | baseDelay: 300, |
55 | 55 | }); |
56 | 56 |
|
57 | | - const retrievedRecords = queryResult.map((v) => unmarshall(v)) as ReturnType[]; |
58 | | - |
59 | | - const validRecords = retrievedRecords.filter((v) => !isRecordExpired(v)); |
60 | | - |
61 | | - if (retrievedRecords.length > 0 && validRecords.length === 0) { |
62 | | - throw new RecordExpiredError( |
63 | | - tableName, |
64 | | - sessionId, |
65 | | - retrievedRecords.map((v) => v.expiryDate ?? v.ttl ?? -1) |
66 | | - ); |
67 | | - } |
68 | | - |
69 | | - if (!allowMultipleEntries && validRecords.length > 1) { |
70 | | - throw new TooManyRecordsError(tableName, sessionId, validRecords.length); |
| 57 | + if (queryResult.length > 1) { |
| 58 | + throw new TooManyRecordsError(tableName, sessionId, queryResult.length); |
71 | 59 | } |
72 | 60 |
|
73 | | - return validRecords; |
| 61 | + return unmarshall(queryResult[0]) as ReturnType; |
74 | 62 | } |
0 commit comments