Skip to content

Commit 257971b

Browse files
committed
doc: update documentation about drivers usage
1 parent d34b727 commit 257971b

1 file changed

Lines changed: 47 additions & 178 deletions

File tree

docs/content/docs/drivers.md

Lines changed: 47 additions & 178 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@ The driver uses the [ioredis](https://github.com/redis/ioredis) library under th
2222

2323
```ts
2424
// title: Verrou API
25+
import { Redis } from 'ioredis'
2526
import { Verrou } from '@verrou/core'
2627
import { redisStore } from '@verrou/core/drivers/redis'
2728

29+
const redis = new Redis({ host: 'localhost', port: 6379 })
2830
const verrou = new Verrou({
2931
default: 'redis',
3032
drivers: {
31-
redis: {
32-
driver: redisStore({
33-
connection: { host: 'localhost', port: 6379 }
34-
})
33+
redis: {
34+
driver: redisStore({ connection: redis })
3535
},
3636
}
3737
})
@@ -42,37 +42,19 @@ const verrou = new Verrou({
4242
import { Verrou, LockFactory } from '@verrou/core'
4343
import { redisStore } from '@verrou/core/drivers/redis'
4444

45-
const store = redisStore({
46-
connection: { host: 'localhost', port: 6379 }
47-
})
45+
const redis = new Redis({ host: 'localhost', port: 6379 })
46+
const store = redisStore({ connection: redis })
4847

4948
const lockFactory = new LockFactory(store)
5049
```
5150

5251
:::
5352

54-
It is also possible to directly pass an Ioredis instance to reuse a connection.
55-
56-
```ts
57-
import { Redis } from 'ioredis'
58-
import { Verrou } from '@verrou/core'
59-
import { redisStore } from '@verrou/core/drivers/redis'
60-
61-
const ioredis = new Redis()
62-
63-
const verrou = new Verrou({
64-
default: 'redis',
65-
drivers: {
66-
redis: { driver: redisStore({ connection: ioredis }) },
67-
}
68-
})
69-
```
70-
7153
### Options
7254

7355
| Option | Description | Default |
7456
| --- | --- | --- |
75-
| `connection` | The connection options to use to connect to Redis or an instance of `ioredis` | N/A |
57+
| `connection` | An instance of `ioredis` | N/A |
7658

7759
### Implementation details
7860

@@ -119,25 +101,18 @@ DynamoDB is also supported by Verrou. You will need to install `@aws-sdk/client-
119101
```ts
120102
// title: Verrou API
121103
import { Verrou } from '@verrou/core'
104+
import { DynamoDBClient } from '@aws-sdk/client-dynamodb'
122105
import { dynamodbStore } from '@verrou/core/drivers/dynamodb'
123106

107+
const dynamoClient = new DynamoDBClient(/* ... */)
124108
const verrou = new Verrou({
125109
default: 'dynamo',
126110
stores: {
127111
dynamo: {
128112
driver: dynamodbStore({
129-
endpoint: '...',
130-
region: 'eu-west-3',
131-
table: {
132-
// Name of the table where the locks will be stored
133-
name: 'locks'
134-
},
135-
136-
// Credentials to use to connect to DynamoDB
137-
credentials: {
138-
accessKeyId: '...',
139-
secretAccessKey: '...'
140-
}
113+
connection: dynamoClient,
114+
// Name of the table where the locks will be stored
115+
table: { name: 'locks' },
141116
})
142117
}
143118
}
@@ -147,21 +122,16 @@ const verrou = new Verrou({
147122
```ts
148123
// title: LockFactory API
149124
import { Verrou, LockFactory } from '@verrou/core'
125+
import { DynamoDBClient } from '@aws-sdk/client-dynamodb'
150126
import { dynamodbStore } from '@verrou/core/drivers/dynamodb'
151127

128+
const dynamoClient = new DynamoDBClient(/* ... */)
152129
const store = dynamodbStore({
153-
endpoint: '...',
154-
region: 'eu-west-3',
130+
connection: dynamoClient,
131+
// Name of the table where the locks will be stored
155132
table: {
156-
// Name of the table where the locks will be stored
157133
name: 'locks'
158134
},
159-
160-
// Credentials to use to connect to DynamoDB
161-
credentials: {
162-
accessKeyId: '...',
163-
secretAccessKey: '...'
164-
}
165135
})
166136

167137
const lockFactory = new LockFactory(store)
@@ -176,192 +146,91 @@ The DynamoDB table will be automatically created if it does not exists. Otherwis
176146
| Option | Description | Default |
177147
| --- | --- | --- |
178148
| `table.name` | The name of the table that will be used to store the cache. | `cache` |
179-
| `credentials` | The credentials to use to connect to DynamoDB. | N/A |
180-
| `endpoint` | The endpoint to use to connect to DynamoDB. | N/A |
181-
| `region` | The region to use to connect to DynamoDB. | N/A |
149+
| `connection` | An instance of `DynamoDBClient` | N/A |
182150

183151
## Databases
184152

185-
We offer several drivers to use a database as the locks store. Under the hood, we use [Knex](https://knexjs.org/). So all Knex options are available, feel free to check out the documentation.
153+
We offer several drivers to use a database as the locks store. The database store should use an adapter for your database. Out of the box, we support [Knex](https://knexjs.org/) and [Kysely](https://kysely.dev/) to interact with the database. Knex and Kysely support many databases : SQLite, MySQL, PostgreSQL, MSSQL, Oracle, and more.
154+
155+
:::note
186156

187-
All SQL drivers accept the following options:
157+
Note that you can easily create your own adapter by implementing the `DatabaseAdapter` interface if you are using another library not supported by Verrou. See the [documentation](/docs/advanced/custom-adapters) for more details.
158+
159+
:::
160+
161+
All Database drivers accept the following common options:
188162

189163
| Option | Description | Default |
190164
| --- | --- | --- |
191165
| `tableName` | The name of the table that will be used to store the locks. | `verrou` |
192166
| `autoCreateTable` | If the table should be automatically created if it does not exist. | `true` |
193-
| `connection` | The connection options to use to connect to the database or an instance of `knex`. | N/A |
194167

195-
### PostgreSQL
168+
### Knex
196169

197-
You will need to install `pg` to use this driver.
170+
You must provide a Knex instance to use the Knex driver. Feel free to check the [Knex documentation](https://knexjs.org/) for more details about the configuration. Knex support many databases : SQLite, MySQL, PostgreSQL, MSSQL, Oracle, and more.
198171

199172
:::codegroup
200173

201174
```ts
202175
// title: Verrou API
176+
import knex from 'knex'
203177
import { Verrou } from '@verrou/core'
204-
import { databaseStore } from '@verrou/core/drivers/database'
178+
import { knexStore } from '@verrou/core/drivers/knex'
205179

206-
const verrou = new Verrou({
207-
default: 'pg',
208-
stores: {
209-
pg: {
210-
driver: databaseStore({
211-
dialect: 'pg',
212-
connection: {
213-
user: 'root',
214-
password: 'root',
215-
database: 'postgres',
216-
port: 5432
217-
}
218-
})
219-
}
220-
}
221-
})
222-
```
223-
224-
```ts
225-
// title: LockFactory API
226-
import { Verrou, LockFactory } from '@verrou/core'
227-
import { databaseStore } from '@verrou/core/drivers/database'
228-
229-
const store = databaseStore({
230-
dialect: 'pg',
231-
connection: {
232-
user: 'root',
233-
password: 'root',
234-
database: 'postgres',
235-
port: 5432
236-
}
237-
})
238-
const lockFactory = new LockFactory(store)
239-
```
240-
241-
:::
242-
243-
### MySQL
244-
245-
You will need to install `mysql2` to use this driver.
246-
247-
:::codegroup
248-
249-
```ts
250-
// title: Verrou API
251-
import { Verrou } from '@verrou/core'
252-
import { databaseStore } from '@verrou/core/drivers/database'
253-
254-
const verrou = new Verrou({
255-
default: 'mysql',
256-
stores: {
257-
mysql: {
258-
driver: databaseStore({
259-
dialect: 'mysql',
260-
connection: {
261-
user: 'root',
262-
password: 'root',
263-
database: 'mysql',
264-
port: 3306
265-
}
266-
})
267-
}
268-
}
269-
})
270-
```
271-
272-
```ts
273-
// title: LockFactory API
274-
import { Verrou, LockFactory } from '@verrou/core'
275-
import { databaseStore } from '@verrou/core/drivers/database'
276-
277-
const store = databaseStore({
278-
dialect: 'mysql',
279-
connection: {
280-
user: 'root',
281-
password: 'root',
282-
database: 'mysql',
283-
port: 3306
284-
}
285-
})
286-
287-
const lockFactory = new LockFactory(store)
288-
```
289-
290-
:::
291-
292-
### SQLite ( better-sqlite3 )
293-
294-
You will need to install `better-sqlite3` to use this driver.
295-
296-
:::codegroup
297-
298-
```ts
299-
// title: Verrou API
300-
import { Verrou } from '@verrou/core'
301-
import { databaseStore } from '@verrou/core/drivers/database'
180+
const db = knex({ client: 'mysql2', connection: MYSQL_CREDENTIALS })
302181

303182
const verrou = new Verrou({
304183
default: 'sqlite',
305184
stores: {
306-
sqlite: {
307-
driver: databaseStore({
308-
dialect: 'better-sqlite3',
309-
connection: { filename: 'cache.sqlite3' }
310-
})
311-
}
185+
sqlite: { driver: knexStore({ connection: db }) }
312186
}
313187
})
314188
```
315189

316190
```ts
317191
// title: LockFactory API
192+
import knex from 'knex'
318193
import { Verrou, LockFactory } from '@verrou/core'
319-
import { databaseStore } from '@verrou/core/drivers/database'
320-
321-
const store = databaseStore({
322-
dialect: 'better-sqlite3',
323-
connection: { filename: 'cache.sqlite3' }
324-
})
194+
import { knexStore } from '@verrou/core/drivers/knex'
325195

196+
const db = knex({ client: 'mysql2', connection: MYSQL_CREDENTIALS })
197+
const store = knexStore({ dialect: 'sqlite', connection: db })
326198
const lockFactory = new LockFactory(store)
327199
```
328200

329201
:::
330202

203+
### Kysely
331204

332-
### SQLite ( sqlite3 )
205+
You must provide a Kysely instance to use the Kysely driver. Feel free to check the [Kysely documentation](https://kysely.dev/) for more details about the configuration. Kysely support the following databases : SQLite, MySQL, PostgreSQL and MSSQL.
333206

334-
You will need to install `sqlite3` to use this driver.
335207

336208
:::codegroup
337209

338210
```ts
339211
// title: Verrou API
212+
import { Kysely } from 'kysely'
340213
import { Verrou } from '@verrou/core'
341-
import { databaseStore } from '@verrou/core/drivers/database'
214+
import { kyselyStore } from '@verrou/core/drivers/kysely'
215+
216+
const db = new Kysely<Database>({ dialect })
342217

343218
const verrou = new Verrou({
344-
default: 'sqlite',
219+
default: 'kysely',
345220
stores: {
346-
sqlite: {
347-
driver: databaseStore({
348-
connection: { filename: 'cache.sqlite3' }
349-
})
350-
}
221+
kysely: { driver: kyselyStore({ connection: db }) }
351222
}
352223
})
353224
```
354225

355226
```ts
356227
// title: LockFactory API
228+
import { Kysely } from 'kysely'
357229
import { Verrou, LockFactory } from '@verrou/core'
358-
import { databaseStore } from '@verrou/core/drivers/database'
359-
360-
const store = databaseStore({
361-
dialect: 'sqlite',
362-
connection: { filename: 'cache.sqlite3' }
363-
})
230+
import { kyselyStore } from '@verrou/core/drivers/kysely'
364231

232+
const db = new Kysely<Database>({ dialect })
233+
const store = kyselyStore({ connection: db })
365234
const lockFactory = new LockFactory(store)
366235
```
367236

0 commit comments

Comments
 (0)