Skip to content

Commit

Permalink
chore: cleanup tests and overwrite existing session on put
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Sep 26, 2024
1 parent 74e7f6d commit ed838d5
Show file tree
Hide file tree
Showing 8 changed files with 215 additions and 119 deletions.
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ services:
dynamodb-local:
image: amazon/dynamodb-local
ports:
- 8000:8000
- 8002:8000
command: '-jar DynamoDBLocal.jar -inMemory -sharedDb'
working_dir: /home/dynamodblocal
healthcheck:
Expand All @@ -36,7 +36,7 @@ services:
condition: service_healthy
image: amazon/aws-cli
volumes:
- './schemas:/tmp/dynamo'
- './tests_helpers/dynamodb_schemas:/tmp/dynamo'
environment:
AWS_ACCESS_KEY_ID: 'accessKeyId'
AWS_SECRET_ACCESS_KEY: 'secretAccessKey'
Expand Down
41 changes: 20 additions & 21 deletions src/stores/dynamodb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,30 @@ import type { SessionStoreContract, SessionData } from '../types.js'
*/
export class DynamoDBStore implements SessionStoreContract {
#client: DynamoDBClient
#tableName: string = 'Session'
#keyAttribute: string = 'key'
#tableName: string
#keyAttribute: string
#ttlSeconds: number
#valueAttribute: string = 'value'
#expiresAtAttribute: string = 'expires_at'
#ttlSeconds: number

constructor(
client: DynamoDBClient,
tableName: string = this.#tableName,
age: string | number,
keyAttribute: string | undefined = this.#keyAttribute
options?: {
/**
* Defaults to "Session"
*/
tableName?: string

/**
* Defaults to "key"
*/
keyAttribute?: string
}
) {
this.#client = client
this.#tableName = tableName
this.#keyAttribute = keyAttribute
this.#tableName = options?.tableName ?? 'Session'
this.#keyAttribute = options?.keyAttribute ?? 'key'
this.#ttlSeconds = string.seconds.parse(age)
debug('initiating dynamodb store')
}
Expand Down Expand Up @@ -95,22 +104,12 @@ export class DynamoDBStore implements SessionStoreContract {
debug('dynamodb store: writing session data %s, %O', sessionId, values)

const message = new MessageBuilder().build(values, undefined, sessionId)
const item = marshall({
[this.#keyAttribute]: sessionId,
[this.#valueAttribute]: message,
[this.#expiresAtAttribute]: Date.now() + this.#ttlSeconds * 1000,
})

const command = new PutItemCommand({
TableName: this.#tableName,
Item: item,
ConditionExpression: 'attribute_not_exists(#key) OR #expires_at > :now',
ExpressionAttributeNames: {
'#key': this.#keyAttribute,
'#expires_at': this.#expiresAtAttribute,
},
ExpressionAttributeValues: marshall({
':now': Date.now(),
Item: marshall({
[this.#keyAttribute]: sessionId,
[this.#valueAttribute]: message,
[this.#expiresAtAttribute]: Date.now() + this.#ttlSeconds * 1000,
}),
})

Expand Down
2 changes: 1 addition & 1 deletion tests/plugins/api_client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ test.group('Api client', (group) => {
await client.get(url).withSession({ username: 'virk' })
assert.lengthOf(MemoryStore.sessions, 0)
})
})
}).timeout(4000)

test('set flash messages from the client', async ({ assert }) => {
const server = httpServer.create(async (req, res) => {
Expand Down
Loading

0 comments on commit ed838d5

Please sign in to comment.