https://github.com/brokenhandsio/vapor-dynamodb-sessions
A DynamoDB integration for Vapor's Sessions
https://github.com/brokenhandsio/vapor-dynamodb-sessions
Last synced: about 1 year ago
JSON representation
A DynamoDB integration for Vapor's Sessions
- Host: GitHub
- URL: https://github.com/brokenhandsio/vapor-dynamodb-sessions
- Owner: brokenhandsio
- License: mit
- Created: 2021-08-25T18:10:33.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2021-08-26T10:03:01.000Z (almost 5 years ago)
- Last Synced: 2025-04-02T22:18:49.754Z (about 1 year ago)
- Language: Swift
- Size: 41 KB
- Stars: 5
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Vapor DynamoDB Sessions
A simple library to use DynamoDB with [Soto](https://github.com/soto-project/soto) to back Vapor's sessions.
## Installation
Add the library in your dependencies array in **Package.swift**:
```swift
dependencies: [
// ...,
package(name: "VaporDynamoDBSessions", url: "https://github.com/brokenhandsio/vapor-dynamodb-sessions.git", from: "1.0.0"),
],
```
Also ensure you add it as a dependency to your target:
```swift
targets: [
.target(name: "App", dependencies: [
.product(name: "Vapor", package: "vapor"),
// ...,
.product(name: "VaporDynamoDBSessions", package: "VaporDynamoDBSessions")
]),
// ...
]
```
## Usage
To start, you must configure the `DynamoDBSessionsProvider` with the `AWSClient` and a table name. In **configure.swift** set the provider on the application:
```swift
app.dynamoDBSessions.provider = DynamoDBSessionsProvider(client: app.aws.client, tableName: tableName)
```
The `DynamoDBSessionsProvider` also takes an optional AWS `region` and endpoint if you need to configure these. To learn how to configure the `AWSClient` see the [Soto Documentation](https://soto.codes/user-guides/using-soto-with-vapor.html).
Next, tell Vapor to use DynamoDB for sessions:
```swift
app.sessions.use(.dynamodb)
app.middleware.use(app.sessions.middleware)
```
**Note**: You must set DynamoDB as the `SessionDriver` before adding the `SessionsMiddleware`.
### Database Requirements
`VaporDynamoDBSessions` will work with its own table or as part of an application using a [single-table design](https://www.alexdebrie.com/posts/dynamodb-single-table/). The only requirements for the library to work is that the table must have a partition key named `pk` and a sort key named `sk`.
## Session Expiry
`VaporDynamoDBSessions` supports adding an expiry date to sessions. Any session that has expired will be discarded by the driver. To configure this, pass a session duration when configuring the provider:
```swift
// 30 days
let sessionLength: TimeInterval = 60 * 60 * 24 * 30
app.dynamoDBSessions.provider = DynamoDBSessionsProvider(client: app.aws.client, tableName: tableName, region: .useast1, endpoint: dynamoDBEndpoint, sessionLength: sessionLength)
```
This will add a field to the session record with the key `expiryDate` as a `number` in epoch time. This allows you to use DynamoDB's [TTL](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/TTL.html) feature to auto-delete expired session data and clean up the database.