A simple library to use DynamoDB with Soto to back Vapor's sessions.
Add the library in your dependencies array in Package.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:
targets: [
.target(name: "App", dependencies: [
.product(name: "Vapor", package: "vapor"),
// ...,
.product(name: "VaporDynamoDBSessions", package: "VaporDynamoDBSessions")
]),
// ...
]To start, you must configure the DynamoDBSessionsProvider with the AWSClient and a table name. In configure.swift set the provider on the application:
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.
Next, tell Vapor to use DynamoDB for sessions:
app.sessions.use(.dynamodb)
app.middleware.use(app.sessions.middleware)Note: You must set DynamoDB as the SessionDriver before adding the SessionsMiddleware.
VaporDynamoDBSessions will work with its own table or as part of an application using a single-table design. 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.
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:
// 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 feature to auto-delete expired session data and clean up the database.