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)
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:
Note: You must set DynamoDB as the
SessionDriver before adding the
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
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.
|Last commit: 3 weeks ago|
This release adds support for expiring sessions and fixes the session ID generation