Skip to content

Commit d4b65e3

Browse files
committed
docs: update drivers examples with both apis
1 parent a4c7f38 commit d4b65e3

2 files changed

Lines changed: 145 additions & 15 deletions

File tree

docs/content/docs/drivers.md

Lines changed: 144 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ const verrou = new Verrou({
4242
import { Verrou, LockFactory } from '@verrou/core'
4343
import { redisStore } from '@verrou/core/drivers/redis'
4444

45-
const lockFactory = new LockFactory(
46-
redisStore({
47-
connection: { host: 'localhost', port: 6379 }
48-
})
49-
)
45+
const store = redisStore({
46+
connection: { host: 'localhost', port: 6379 }
47+
})
48+
49+
const lockFactory = new LockFactory(store)
5050
```
5151

5252
:::
@@ -84,8 +84,10 @@ The memory store is a simple in-memory store, so don't use it in a multi-server
8484

8585
Use [async-mutex](https://www.npmjs.com/package/async-mutex) under the hood.
8686

87+
:::codegroup
8788

8889
```ts
90+
// title: Verrou API
8991
import { Verrou } from '@verrou/core'
9092
import { memoryStore } from '@verrou/core/drivers/memory'
9193

@@ -97,19 +99,33 @@ const verrou = new Verrou({
9799
})
98100
```
99101

102+
```ts
103+
// title: LockFactory API
104+
import { Verrou, LockFactory } from '@verrou/core'
105+
import { memoryStore } from '@verrou/core/drivers/memory'
106+
107+
const store = memoryStore()
108+
const lockFactory = new LockFactory(store)
109+
```
110+
111+
:::
112+
100113
## DynamoDB
101114

102115
DynamoDB is also supported by Verrou. You will need to install `@aws-sdk/client-dynamodb` to use this driver.
103116

117+
:::codegroup
118+
104119
```ts
120+
// title: Verrou API
105121
import { Verrou } from '@verrou/core'
106122
import { dynamodbStore } from '@verrou/core/drivers/dynamodb'
107123

108124
const verrou = new Verrou({
109125
default: 'dynamo',
110126
stores: {
111127
dynamo: {
112-
driver: dynamoDbDriver({
128+
driver: dynamodbStore({
113129
endpoint: '...',
114130
region: 'eu-west-3',
115131
table: {
@@ -128,6 +144,31 @@ const verrou = new Verrou({
128144
})
129145
```
130146

147+
```ts
148+
// title: LockFactory API
149+
import { Verrou, LockFactory } from '@verrou/core'
150+
import { dynamodbStore } from '@verrou/core/drivers/dynamodb'
151+
152+
const store = dynamodbStore({
153+
endpoint: '...',
154+
region: 'eu-west-3',
155+
table: {
156+
// Name of the table where the locks will be stored
157+
name: 'locks'
158+
},
159+
160+
// Credentials to use to connect to DynamoDB
161+
credentials: {
162+
accessKeyId: '...',
163+
secretAccessKey: '...'
164+
}
165+
})
166+
167+
const lockFactory = new LockFactory(store)
168+
169+
```
170+
:::
171+
131172
The DynamoDB table will be automatically created if it does not exists. Otherwise, you can create it manually and specify the name of the table in the options.
132173

133174
### Options
@@ -155,88 +196,177 @@ All SQL drivers accept the following options:
155196

156197
You will need to install `pg` to use this driver.
157198

199+
:::codegroup
200+
158201
```ts
202+
// title: Verrou API
159203
import { Verrou } from '@verrou/core'
160204
import { databaseStore } from '@verrou/core/drivers/database'
161205

162206
const verrou = new Verrou({
163207
default: 'pg',
164208
stores: {
165209
pg: {
166-
driver: {
210+
driver: databaseStore({
211+
dialect: 'pg',
167212
connection: {
168213
user: 'root',
169214
password: 'root',
170215
database: 'postgres',
171216
port: 5432
172217
}
173-
}
218+
})
174219
}
175220
}
176221
})
177222
```
178223

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+
179243
### MySQL
180244

181245
You will need to install `mysql2` to use this driver.
182246

247+
:::codegroup
248+
183249
```ts
250+
// title: Verrou API
184251
import { Verrou } from '@verrou/core'
185252
import { databaseStore } from '@verrou/core/drivers/database'
186253

187254
const verrou = new Verrou({
188255
default: 'mysql',
189256
stores: {
190257
mysql: {
191-
driver: {
258+
driver: databaseStore({
259+
dialect: 'mysql',
192260
connection: {
193261
user: 'root',
194262
password: 'root',
195263
database: 'mysql',
196264
port: 3306
197265
}
198-
}
266+
})
199267
}
200268
}
201269
})
202270
```
203271

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+
204292
### SQLite ( better-sqlite3 )
205293

206294
You will need to install `better-sqlite3` to use this driver.
207295

296+
:::codegroup
297+
208298
```ts
299+
// title: Verrou API
209300
import { Verrou } from '@verrou/core'
210301
import { databaseStore } from '@verrou/core/drivers/database'
211302

212303
const verrou = new Verrou({
213-
default: 'sqlite3',
304+
default: 'sqlite',
214305
stores: {
215-
sqlite3: {
216-
driver: { connection: { filename: 'cache.sqlite3' } }
306+
sqlite: {
307+
driver: databaseStore({
308+
dialect: 'better-sqlite3',
309+
connection: { filename: 'cache.sqlite3' }
310+
})
217311
}
218312
}
219313
})
220314
```
221315

316+
```ts
317+
// title: LockFactory API
318+
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+
})
325+
326+
const lockFactory = new LockFactory(store)
327+
```
328+
329+
:::
330+
331+
222332
### SQLite ( sqlite3 )
223333

224334
You will need to install `sqlite3` to use this driver.
225335

336+
:::codegroup
337+
226338
```ts
339+
// title: Verrou API
227340
import { Verrou } from '@verrou/core'
228341
import { databaseStore } from '@verrou/core/drivers/database'
229342

230343
const verrou = new Verrou({
231344
default: 'sqlite',
232345
stores: {
233346
sqlite: {
234-
driver: { connection: { filename: 'cache.sqlite3' } }
347+
driver: databaseStore({
348+
connection: { filename: 'cache.sqlite3' }
349+
})
235350
}
236351
}
237352
})
238353
```
239354

355+
```ts
356+
// title: LockFactory API
357+
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+
})
364+
365+
const lockFactory = new LockFactory(store)
366+
```
367+
368+
:::
369+
240370
### Implementation details
241371

242372
The database drivers use the same strategy as Laravel's Locks. The strategy is fairly simple : Just insert a row and rely on the database primary key constraint to prevent duplicates. If the insert fails, it means the lock already exists. This is briefly explained by Aaron Francis in [this article](https://aaronfrancis.com/2021/the-exceeding-cleverness-of-laravels-database-locks).

docs/content/docs/usage.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { verrou } from './verrou.js'
1616
const lock = verrou.createLock('my-resource', '1s')
1717
```
1818

19-
The first argument is the resource/key to lock. This is an arbitrary string that will be used to identify the lock. The second argument is the duration of the lock. It can be a number of milliseconds, a string like `1s` or `1m` ( see [lukee/ms documentation](https://github.com/lukeed/ms), or even `null` if you want to create a lock that never expires.
19+
The first argument is the resource/key to lock. This is an arbitrary string that will be used to identify the lock. The second argument is the duration of the lock. It can be a number of milliseconds, a string like `1s` or `1m` ( see [lukeed/ms documentation](https://github.com/lukeed/ms), or even `null` if you want to create a lock that never expires.
2020

2121
Note that the duration you are passing is the duration of the lease. This means that the lock will be automatically released after this duration. This is safe to always pass a duration, even if you are releasing the lock manually afterwards ( see below ). Having a duration will prevent the lock from being stuck forever if the process crashes before releasing it ( We call this a **Deadlock** ).
2222

0 commit comments

Comments
 (0)