Hey guys, I’m using Table for DynamoDB and want to...
# sst
a
Hey guys, I’m using Table for DynamoDB and want to setup TTL of 30 days. How to do this with SST/CDK?
Copy code
/**
     * The name of TTL attribute.
     *
     * @default - TTL is disabled
     * @stability stable
     */
    readonly timeToLiveAttribute?: string;
What should be the value?
a
It's whatever property in your data structure contains a Unix timestamp.
a
Yeah just found that out.
It’s intersting.
So I’m in charge of generating the timestamp.
a
I set mine to "_ttl", and then when I write an item to the DB set it to
new Date().getTime() / MillisecondsInSecond
a
Nice!
a
Oh, plus something. That would expire the moment it is added. 😄
a
Hahaha yeah.
a
Oh, and Math.floor() it to avoid decimal places - don't know what would happen. Here's a more complete clip.
Copy code
const now = new Date();
    const historySeconds = (await this.historyDays.get()) * SecondsInDay;
    const ttl = Math.floor(now.getTime() / MillisecondsInSecond) + historySeconds;
d
+1 to everything @Adam Fanello said, but I use
expiresAt
. I also recommend the lib
ms
to easily add time.
a
Awesome guys! thank you very much.
m
There is a caveat that is worth mentioning (and if you all already know please disregard): An item doesn't get purged immediately after the TTL has expired. It could take up to 48 hours (though I've typically seen within one hour). This means expired entries will still be returned in results. So if you want to exclude them, be sure to filter them out