Skip to content

Commit 9808fc6

Browse files
committed
refactor: drivers only accept instance instead of creating it
1 parent c96d91e commit 9808fc6

4 files changed

Lines changed: 24 additions & 43 deletions

File tree

packages/verrou/src/drivers/dynamodb.ts

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
import type { DynamoDBClient } from '@aws-sdk/client-dynamodb'
12
import {
23
ConditionalCheckFailedException,
34
CreateTableCommand,
45
DeleteItemCommand,
5-
DynamoDBClient,
66
GetItemCommand,
77
PutItemCommand,
88
ResourceInUseException,
@@ -22,9 +22,9 @@ export class DynamoDBStore implements LockStore {
2222
#initialized: Promise<any>
2323

2424
/**
25-
* DynamoDB client
25+
* DynamoDB connection
2626
*/
27-
#client: DynamoDBClient
27+
#connection: DynamoDBClient
2828

2929
/**
3030
* DynamoDB table name
@@ -33,12 +33,7 @@ export class DynamoDBStore implements LockStore {
3333

3434
constructor(config: DynamoDbOptions) {
3535
this.#tableName = config.table.name
36-
37-
this.#client = new DynamoDBClient({
38-
region: config.region,
39-
credentials: config.credentials,
40-
endpoint: config.endpoint,
41-
})
36+
this.#connection = config.connection
4237

4338
this.#initialized = this.#createTableIfNotExists()
4439
}
@@ -58,7 +53,7 @@ export class DynamoDBStore implements LockStore {
5853
})
5954

6055
try {
61-
await this.#client.send(command)
56+
await this.#connection.send(command)
6257
} catch (error) {
6358
if (error instanceof ResourceInUseException) return
6459
throw error
@@ -87,7 +82,7 @@ export class DynamoDBStore implements LockStore {
8782
ExpressionAttributeValues: { ':now': { N: Date.now().toString() } },
8883
})
8984

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

111106
try {
112-
await this.#client.send(command)
107+
await this.#connection.send(command)
113108
} catch (err) {
114109
throw new E_LOCK_NOT_OWNED()
115110
}
@@ -124,7 +119,7 @@ export class DynamoDBStore implements LockStore {
124119
Key: { key: { S: key } },
125120
})
126121

127-
await this.#client.send(command)
122+
await this.#connection.send(command)
128123
}
129124

130125
/**
@@ -137,7 +132,7 @@ export class DynamoDBStore implements LockStore {
137132
Key: { key: { S: key } },
138133
})
139134

140-
const result = await this.#client.send(command)
135+
const result = await this.#connection.send(command)
141136
const isExpired = result.Item?.expires_at?.N && result.Item.expires_at.N < Date.now().toString()
142137

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

163158
try {
164-
await this.#client.send(command)
159+
await this.#connection.send(command)
165160
} catch (err) {
166161
throw new E_LOCK_NOT_OWNED()
167162
}

packages/verrou/src/drivers/redis.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Redis as IoRedis } from 'ioredis'
1+
import type { Redis as IoRedis } from 'ioredis'
22

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

1919
constructor(options: RedisStoreOptions) {
20-
if (options.connection instanceof IoRedis) {
21-
this.#connection = options.connection
22-
} else {
23-
this.#connection = new IoRedis(options.connection)
24-
}
20+
this.#connection = options.connection
2521
}
2622

2723
/**

packages/verrou/src/types/drivers.ts

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { Knex } from 'knex'
22
import type { Kysely } from 'kysely'
3-
import type { DynamoDBClientConfig } from '@aws-sdk/client-dynamodb'
4-
import type { RedisOptions as IoRedisOptions, Redis as IoRedis } from 'ioredis'
3+
import type { Redis as IoRedis } from 'ioredis'
4+
import type { DynamoDBClient } from '@aws-sdk/client-dynamodb'
55

66
/**
77
* Common options for database stores
@@ -49,7 +49,7 @@ export type RedisStoreOptions = {
4949
/**
5050
* The Redis connection
5151
*/
52-
connection: IoRedis | IoRedisOptions
52+
connection: IoRedis
5353
}
5454

5555
/**
@@ -63,20 +63,7 @@ export type DynamoDbOptions = {
6363
name: string
6464
}
6565

66-
/**
67-
* AWS credentials
68-
*/
69-
credentials?: DynamoDBClientConfig['credentials']
70-
71-
/**
72-
* Region of your DynamoDB instance
73-
*/
74-
region: DynamoDBClientConfig['region']
75-
76-
/**
77-
* Endpoint to your DynamoDB instance
78-
*/
79-
endpoint: DynamoDBClientConfig['endpoint']
66+
connection: DynamoDBClient
8067
}
8168

8269
/**

packages/verrou/tests/drivers/dynamodb.spec.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import { DeleteTableCommand, DynamoDBClient, GetItemCommand } from '@aws-sdk/cli
44
import { DynamoDBStore } from '../../src/drivers/dynamodb.js'
55
import { registerStoreTestSuite } from '../../src/test_suite.js'
66

7-
const credentials = {
7+
const dynamoClient = new DynamoDBClient({
88
region: 'eu-west-3',
99
endpoint: process.env.DYNAMODB_ENDPOINT,
1010
credentials: { accessKeyId: 'foo', secretAccessKey: 'foo' },
11-
}
12-
const config = { ...credentials, table: { name: 'verrou' } }
13-
const dynamoClient = new DynamoDBClient(credentials)
11+
})
12+
13+
const config = { connection: dynamoClient, table: { name: 'verrou' } }
1414

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

25-
registerStoreTestSuite({ test, createStore: () => new DynamoDBStore(config) })
25+
registerStoreTestSuite({
26+
test,
27+
createStore: () => new DynamoDBStore(config),
28+
})
2629

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

0 commit comments

Comments
 (0)