Skip to content

Commit

Permalink
refactor: drivers only accept instance instead of creating it
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien-R44 committed Mar 12, 2024
1 parent c96d91e commit 9808fc6
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 43 deletions.
25 changes: 10 additions & 15 deletions packages/verrou/src/drivers/dynamodb.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { DynamoDBClient } from '@aws-sdk/client-dynamodb'
import {
ConditionalCheckFailedException,
CreateTableCommand,
DeleteItemCommand,
DynamoDBClient,
GetItemCommand,
PutItemCommand,
ResourceInUseException,
Expand All @@ -22,9 +22,9 @@ export class DynamoDBStore implements LockStore {
#initialized: Promise<any>

/**
* DynamoDB client
* DynamoDB connection
*/
#client: DynamoDBClient
#connection: DynamoDBClient

/**
* DynamoDB table name
Expand All @@ -33,12 +33,7 @@ export class DynamoDBStore implements LockStore {

constructor(config: DynamoDbOptions) {
this.#tableName = config.table.name

this.#client = new DynamoDBClient({
region: config.region,
credentials: config.credentials,
endpoint: config.endpoint,
})
this.#connection = config.connection

this.#initialized = this.#createTableIfNotExists()
}
Expand All @@ -58,7 +53,7 @@ export class DynamoDBStore implements LockStore {
})

try {
await this.#client.send(command)
await this.#connection.send(command)
} catch (error) {
if (error instanceof ResourceInUseException) return
throw error
Expand Down Expand Up @@ -87,7 +82,7 @@ export class DynamoDBStore implements LockStore {
ExpressionAttributeValues: { ':now': { N: Date.now().toString() } },
})

const result = await this.#client.send(command)
const result = await this.#connection.send(command)
return result.$metadata.httpStatusCode === 200
} catch (err) {
if (err instanceof ConditionalCheckFailedException) return false
Expand All @@ -109,7 +104,7 @@ export class DynamoDBStore implements LockStore {
})

try {
await this.#client.send(command)
await this.#connection.send(command)
} catch (err) {
throw new E_LOCK_NOT_OWNED()
}
Expand All @@ -124,7 +119,7 @@ export class DynamoDBStore implements LockStore {
Key: { key: { S: key } },
})

await this.#client.send(command)
await this.#connection.send(command)
}

/**
Expand All @@ -137,7 +132,7 @@ export class DynamoDBStore implements LockStore {
Key: { key: { S: key } },
})

const result = await this.#client.send(command)
const result = await this.#connection.send(command)
const isExpired = result.Item?.expires_at?.N && result.Item.expires_at.N < Date.now().toString()

return result.Item !== undefined && !isExpired
Expand All @@ -161,7 +156,7 @@ export class DynamoDBStore implements LockStore {
})

try {
await this.#client.send(command)
await this.#connection.send(command)
} catch (err) {
throw new E_LOCK_NOT_OWNED()
}
Expand Down
8 changes: 2 additions & 6 deletions packages/verrou/src/drivers/redis.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Redis as IoRedis } from 'ioredis'
import type { Redis as IoRedis } from 'ioredis'

import { E_LOCK_NOT_OWNED } from '../errors.js'
import type { LockStore, RedisStoreOptions } from '../types/main.js'
Expand All @@ -17,11 +17,7 @@ export class RedisStore implements LockStore {
#connection: IoRedis

constructor(options: RedisStoreOptions) {
if (options.connection instanceof IoRedis) {
this.#connection = options.connection
} else {
this.#connection = new IoRedis(options.connection)
}
this.#connection = options.connection
}

/**
Expand Down
21 changes: 4 additions & 17 deletions packages/verrou/src/types/drivers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Knex } from 'knex'
import type { Kysely } from 'kysely'
import type { DynamoDBClientConfig } from '@aws-sdk/client-dynamodb'
import type { RedisOptions as IoRedisOptions, Redis as IoRedis } from 'ioredis'
import type { Redis as IoRedis } from 'ioredis'
import type { DynamoDBClient } from '@aws-sdk/client-dynamodb'

/**
* Common options for database stores
Expand Down Expand Up @@ -49,7 +49,7 @@ export type RedisStoreOptions = {
/**
* The Redis connection
*/
connection: IoRedis | IoRedisOptions
connection: IoRedis
}

/**
Expand All @@ -63,20 +63,7 @@ export type DynamoDbOptions = {
name: string
}

/**
* AWS credentials
*/
credentials?: DynamoDBClientConfig['credentials']

/**
* Region of your DynamoDB instance
*/
region: DynamoDBClientConfig['region']

/**
* Endpoint to your DynamoDB instance
*/
endpoint: DynamoDBClientConfig['endpoint']
connection: DynamoDBClient
}

/**
Expand Down
13 changes: 8 additions & 5 deletions packages/verrou/tests/drivers/dynamodb.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import { DeleteTableCommand, DynamoDBClient, GetItemCommand } from '@aws-sdk/cli
import { DynamoDBStore } from '../../src/drivers/dynamodb.js'
import { registerStoreTestSuite } from '../../src/test_suite.js'

const credentials = {
const dynamoClient = new DynamoDBClient({
region: 'eu-west-3',
endpoint: process.env.DYNAMODB_ENDPOINT,
credentials: { accessKeyId: 'foo', secretAccessKey: 'foo' },
}
const config = { ...credentials, table: { name: 'verrou' } }
const dynamoClient = new DynamoDBClient(credentials)
})

const config = { connection: dynamoClient, table: { name: 'verrou' } }

function deleteTableTeardown(tableName: string) {
return async () => {
Expand All @@ -22,7 +22,10 @@ function deleteTableTeardown(tableName: string) {
test.group('DynamoDB Store', (group) => {
group.each.teardown(deleteTableTeardown('verrou'))

registerStoreTestSuite({ test, createStore: () => new DynamoDBStore(config) })
registerStoreTestSuite({
test,
createStore: () => new DynamoDBStore(config),
})

test('should automatically create table', async ({ assert }) => {
const store = new DynamoDBStore(config)
Expand Down

0 comments on commit 9808fc6

Please sign in to comment.