From 9808fc68b4cd02d9c679fa79a16f45d5931edda8 Mon Sep 17 00:00:00 2001 From: Julien Ripouteau Date: Tue, 12 Mar 2024 20:16:36 +0100 Subject: [PATCH] refactor: drivers only accept instance instead of creating it --- packages/verrou/src/drivers/dynamodb.ts | 25 ++++++++----------- packages/verrou/src/drivers/redis.ts | 8 ++---- packages/verrou/src/types/drivers.ts | 21 +++------------- .../verrou/tests/drivers/dynamodb.spec.ts | 13 ++++++---- 4 files changed, 24 insertions(+), 43 deletions(-) diff --git a/packages/verrou/src/drivers/dynamodb.ts b/packages/verrou/src/drivers/dynamodb.ts index d4080a4..0168825 100644 --- a/packages/verrou/src/drivers/dynamodb.ts +++ b/packages/verrou/src/drivers/dynamodb.ts @@ -1,8 +1,8 @@ +import type { DynamoDBClient } from '@aws-sdk/client-dynamodb' import { ConditionalCheckFailedException, CreateTableCommand, DeleteItemCommand, - DynamoDBClient, GetItemCommand, PutItemCommand, ResourceInUseException, @@ -22,9 +22,9 @@ export class DynamoDBStore implements LockStore { #initialized: Promise /** - * DynamoDB client + * DynamoDB connection */ - #client: DynamoDBClient + #connection: DynamoDBClient /** * DynamoDB table name @@ -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() } @@ -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 @@ -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 @@ -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() } @@ -124,7 +119,7 @@ export class DynamoDBStore implements LockStore { Key: { key: { S: key } }, }) - await this.#client.send(command) + await this.#connection.send(command) } /** @@ -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 @@ -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() } diff --git a/packages/verrou/src/drivers/redis.ts b/packages/verrou/src/drivers/redis.ts index 1f8e1fa..e25027f 100644 --- a/packages/verrou/src/drivers/redis.ts +++ b/packages/verrou/src/drivers/redis.ts @@ -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' @@ -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 } /** diff --git a/packages/verrou/src/types/drivers.ts b/packages/verrou/src/types/drivers.ts index 2c4e938..233d882 100644 --- a/packages/verrou/src/types/drivers.ts +++ b/packages/verrou/src/types/drivers.ts @@ -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 @@ -49,7 +49,7 @@ export type RedisStoreOptions = { /** * The Redis connection */ - connection: IoRedis | IoRedisOptions + connection: IoRedis } /** @@ -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 } /** diff --git a/packages/verrou/tests/drivers/dynamodb.spec.ts b/packages/verrou/tests/drivers/dynamodb.spec.ts index 840e036..839e0f0 100644 --- a/packages/verrou/tests/drivers/dynamodb.spec.ts +++ b/packages/verrou/tests/drivers/dynamodb.spec.ts @@ -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 () => { @@ -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)