Skip to content

Commit 7d39e76

Browse files
authored
feat: add option to set custom dynamo ttl attribute name. (#164)
* feat: add option to set custom dynamo `ttl` attribute name. * Add way to modify the dynamo ttl attribute name
1 parent f2a3026 commit 7d39e76

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

.changeset/six-eyes-design.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@openauthjs/openauth": patch
3+
---
4+
5+
Add way to modify the dynamo ttl attribute name

packages/openauth/src/storage/dynamo.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,24 @@ export interface DynamoStorageOptions {
4141
table: string
4242
/**
4343
* The primary key column name.
44+
* @default "pk"
4445
*/
4546
pk?: string
4647
/**
4748
* The sort key column name.
49+
* @default "sk"
4850
*/
4951
sk?: string
5052
/**
5153
* Endpoint URL for the DynamoDB service. Useful for local testing.
5254
* @default "https://dynamodb.{region}.amazonaws.com"
5355
*/
5456
endpoint?: string
57+
/**
58+
* The name of the time to live attribute.
59+
* @default "expiry"
60+
*/
61+
ttl?: string
5562
}
5663

5764
/**
@@ -62,6 +69,7 @@ export function DynamoStorage(options: DynamoStorageOptions): StorageAdapter {
6269
const c = client()
6370
const pk = options.pk || "pk"
6471
const sk = options.sk || "sk"
72+
const ttl = options.ttl || "expiry"
6573
const tableName = options.table
6674

6775
function parseKey(key: string[]) {
@@ -109,7 +117,7 @@ export function DynamoStorage(options: DynamoStorageOptions): StorageAdapter {
109117
}
110118
const result = await dynamo("GetItem", params)
111119
if (!result.Item) return
112-
if (result.Item.expiry && result.Item.expiry.N < Date.now() / 1000) {
120+
if (result.Item[ttl] && result.Item[ttl].N < Date.now() / 1000) {
113121
return
114122
}
115123
return JSON.parse(result.Item.value.S)
@@ -124,7 +132,7 @@ export function DynamoStorage(options: DynamoStorageOptions): StorageAdapter {
124132
[sk]: { S: parsed.sk },
125133
...(expiry
126134
? {
127-
expiry: { N: Math.floor(expiry.getTime() / 1000).toString() },
135+
[ttl]: { N: Math.floor(expiry.getTime() / 1000).toString() },
128136
}
129137
: {}),
130138
value: { S: JSON.stringify(value) },
@@ -172,7 +180,7 @@ export function DynamoStorage(options: DynamoStorageOptions): StorageAdapter {
172180
const result = await dynamo("Query", params)
173181

174182
for (const item of result.Items || []) {
175-
if (item.expiry && item.expiry.N < now) {
183+
if (item[ttl] && item[ttl].N < now) {
176184
continue
177185
}
178186
yield [[item[pk].S, item[sk].S], JSON.parse(item.value.S)]

0 commit comments

Comments
 (0)