choutianxius / lucia-adapter-dynamodb

A DynamoDB adapter for lucia-auth
MIT License
3 stars 2 forks source link

Add a DynamoDB TTL Column #2

Open erikrj opened 1 month ago

erikrj commented 1 month ago

DynamoDB has TTL feature which can automatically delete expired session records from the table. The following has an explanation and an example:

https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/time-to-live-ttl-before-you-start.html

If the adapter were modified to include a column called "expiresAt" or "ttl" which contains the number of seconds since epoch, I can simply enable the TTL feature of DynamoDB to expire the sessions rather than calling deleteExpiredSessions.

choutianxius commented 1 month ago

Hi @erikrj , currently there is an internal ExpiresAt key which stores ISO time strings for handling expired sessions, which I think is totally fine to be converted to store Unix timestamps.

I also noticed the TTL feature of DynamoDB when writing the adapter. Still, my concern is that DynamoDB doesn't immediately delete expired records, so I think it's better to handle these stuff manually to meet the requirements.

If you need to incoporate TTL with other data in your system, you can always add your own TTL attribute, just avoid using the same name as ExpiresAt (or the name specified in the expiresAt option passed to the constructor).

erikrj commented 1 month ago

Thanks @choutianxius.

DynamoDB may not expire items immediately, but that won't matter because if an expired session is returned Lucia handles it properly. It's a lot more friendly to have DynamoDB handle expirations especially when session volumes are really high than execute a batch job which also costs you additional compute albeit likely small.

I went ahead with modifications in my fork in the develop branch located at https://github.com/erikrj/lucia-adapter-dynamodb. The changes include

I also modified the code to support just one schema pattern which I required. The schema pattern optimizes some of the methods you had to reduce the number of calls.

In any case, I did not issue you a pull request because the changes are likely too large and include stuff you may not want like default value changes which simplify my world. However, you're welcome to take what you like or none of it at all and close this request.

Also, all tests pass when I run.

docker compose run --rm app npm run test && docker compose down

I appreciate you getting me the command which I have added to the package.json as a script called docker-test.

Please note I need this modified pattern for three projects I'm working on so I'll be releasing my changes under @nr1e/lucida-adapter-dynamodb on npm this week, but I will be referencing back to your repo in case people come across it.

choutianxius commented 1 month ago

Hi @erikrj , I really appreciate your efforts and I feel glad that this work has helped your projects. This library (a tiny one, I would say) started from a Next.js app I was working on where my DB infrastructure was limited to DynamoDB, and I really messed up with the default Auth.js solutions so I switched to Lucia and created this repository along the way. I worked on it mostly as a hobby project and published it mainly to "try new things out", and thus I didn't quite expect someone else to notice it and even make contributions. Again I'm super excited to see these issues and PRs coming out which makes me feel more like a "pro" now😎

In all, this project is like a tiny baby to me and thus I think I will examine your code and update this library at my own discretion, as you have suggested🥳 Please feel free to work on your projects and publish new libraries, since this project is on MIT license after all