Skip to content

Commit fbb5e60

Browse files
committed
docs: fix LockFactory instructions
Fix #7
1 parent 4c3ca28 commit fbb5e60

4 files changed

Lines changed: 67 additions & 62 deletions

File tree

docs/content/docs/api.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ const [executed, result] = await lock.run(() => {
6969
})
7070
```
7171

72-
7372
### `runImmediately`
7473

7574
Same as `run`, but try to acquire the lock immediately ( without retrying ).
@@ -157,7 +156,9 @@ await lock.forceRelease()
157156
Create a lock.
158157

159158
```ts
160-
const lockFactory = new LockFactory(redisStore())
159+
import { RedisStore } from '@verrou/core/drivers/redis'
160+
161+
const lockFactory = new LockFactory(new RedisStore())
161162
const lock1 = lockFactory.createLock('key')
162163
const lock2 = lockFactory.createLock('key', '5m')
163164
const lock3 = lockFactory.createLock('key', 30_000)
@@ -170,14 +171,16 @@ First argument is the lock key. Second argument is optional and is the lock expi
170171
Restore a lock. Useful when sharing a lock between multiple processes. See [Sharing a lock between multiple processes](./usage.md#sharing-a-lock-between-multiple-processes) for more details.
171172

172173
```ts
173-
const lockFactory = new LockFactory(redisStore())
174+
import { RedisStore } from '@verrou/core/drivers/redis'
175+
176+
const lockFactory = new LockFactory(new RedisStore())
174177
const lock1 = lockFactory.createLock('key', 'owner')
175178
const lock2 = lockFactory.restoreLock(lock1.serialize())
176179
```
177180

178181
## Verrou API
179182

180-
Verrou API is a wrapper around the LockFactory API.
183+
Verrou API is a wrapper around the LockFactory API.
181184

182185
```ts
183186
const verrou = new Verrou({
@@ -191,7 +194,7 @@ const verrou = new Verrou({
191194
})
192195
```
193196

194-
Every method available on the `LockFactory` class is available on the `Verrou` class, and will be called on the default store.
197+
Every method available on the `LockFactory` class is available on the `Verrou` class, and will be called on the default store.
195198

196199
If you need to use a different store, you can use the `use` method.
197200

@@ -210,4 +213,3 @@ As explained above, the `use` method allows you to use a different store than th
210213
```ts
211214
verrou.use('myMemoryStore').createLock('key')
212215
```
213-

docs/content/docs/digging_deeper/logging.md

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,25 @@ Your logger must comply with the following interface:
1010

1111
```ts
1212
export interface Logger {
13-
trace(msg: string | LogObject): void;
14-
trace(obj: LogObject, msg: string): void;
13+
trace(msg: string | LogObject): void
14+
trace(obj: LogObject, msg: string): void
1515

16-
debug(msg: string | LogObject): void;
17-
debug(obj: LogObject, msg: string): void;
16+
debug(msg: string | LogObject): void
17+
debug(obj: LogObject, msg: string): void
1818

19-
info(msg: string | LogObject): void;
20-
info(obj: LogObject, msg: string): void;
19+
info(msg: string | LogObject): void
20+
info(obj: LogObject, msg: string): void
2121

22-
warn(msg: string): void;
23-
warn(obj: LogObject, msg: string): void;
22+
warn(msg: string): void
23+
warn(obj: LogObject, msg: string): void
2424

25-
error(msg: string): void;
26-
error(obj: ErrorObject, msg: string): void;
25+
error(msg: string): void
26+
error(obj: ErrorObject, msg: string): void
2727

28-
fatal(msg: string): void;
29-
fatal(obj: ErrorObject, msg: string): void;
28+
fatal(msg: string): void
29+
fatal(obj: ErrorObject, msg: string): void
3030

31-
child(childObj: LogObject): Logger;
31+
child(childObj: LogObject): Logger
3232
}
3333
```
3434

@@ -41,7 +41,7 @@ import { pino } from 'pino'
4141

4242
const logger = pino({
4343
level: 'trace',
44-
transport: { target: 'pino-pretty' }
44+
transport: { target: 'pino-pretty' },
4545
})
4646

4747
const verrou = new Verrou({
@@ -57,11 +57,12 @@ If using the `LockFactory` API, you can also do the same :
5757
```ts
5858
import { pino } from 'pino'
5959
import { LockFactory } from '@verrou/core'
60+
import { MemoryStore } from '@verrou/core/drivers/memory'
6061

6162
const logger = pino({
6263
level: 'trace',
63-
transport: { target: 'pino-pretty' }
64+
transport: { target: 'pino-pretty' },
6465
})
6566

66-
const lockFactory = new LockFactory(memoryStore(), { logger })
67+
const lockFactory = new LockFactory(new MemoryStore(), { logger })
6768
```

docs/content/docs/drivers.md

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Verrou supports multiple drivers to store the locks. No matter which driver you
1111
You will need to install `ioredis` to use this driver.
1212

1313
The Redis driver can be used with many different providers:
14+
1415
- Upstash
1516
- Vercel KV
1617
- DragonFly
@@ -31,19 +32,19 @@ const verrou = new Verrou({
3132
default: 'redis',
3233
drivers: {
3334
redis: {
34-
driver: redisStore({ connection: redis })
35+
driver: redisStore({ connection: redis }),
3536
},
36-
}
37+
},
3738
})
3839
```
3940

4041
```ts
4142
// title: LockFactory API
4243
import { Verrou, LockFactory } from '@verrou/core'
43-
import { redisStore } from '@verrou/core/drivers/redis'
44+
import { RedisStore } from '@verrou/core/drivers/redis'
4445

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

4849
const lockFactory = new LockFactory(store)
4950
```
@@ -52,17 +53,17 @@ const lockFactory = new LockFactory(store)
5253

5354
### Options
5455

55-
| Option | Description | Default |
56-
| --- | --- | --- |
57-
| `connection` | An instance of `ioredis` | N/A |
56+
| Option | Description | Default |
57+
| ------------ | ------------------------ | ------- |
58+
| `connection` | An instance of `ioredis` | N/A |
5859

5960
### Implementation details
6061

6162
Note that the Redis store does **not** use the redlock algorithm. It uses a simple `setnx` as described in the [Redis documentation](https://redis.io/commands/set/). We may introduce a redlock strategy in the future.
6263

6364
## Memory
6465

65-
The memory store is a simple in-memory store, so don't use it in a multi-server environment.
66+
The memory store is a simple in-memory store, so don't use it in a multi-server environment.
6667

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

@@ -77,16 +78,16 @@ const verrou = new Verrou({
7778
default: 'memory',
7879
drivers: {
7980
memory: { driver: memoryStore() },
80-
}
81+
},
8182
})
8283
```
8384

8485
```ts
8586
// title: LockFactory API
8687
import { Verrou, LockFactory } from '@verrou/core'
87-
import { memoryStore } from '@verrou/core/drivers/memory'
88+
import { MemoryStore } from '@verrou/core/drivers/memory'
8889

89-
const store = memoryStore()
90+
const store = new MemoryStore()
9091
const lockFactory = new LockFactory(store)
9192
```
9293

@@ -113,40 +114,40 @@ const verrou = new Verrou({
113114
connection: dynamoClient,
114115
// Name of the table where the locks will be stored
115116
table: { name: 'locks' },
116-
})
117-
}
118-
}
117+
}),
118+
},
119+
},
119120
})
120121
```
121122

122123
```ts
123124
// title: LockFactory API
124125
import { Verrou, LockFactory } from '@verrou/core'
125126
import { DynamoDBClient } from '@aws-sdk/client-dynamodb'
126-
import { dynamodbStore } from '@verrou/core/drivers/dynamodb'
127+
import { DynamoDBStore } from '@verrou/core/drivers/dynamodb'
127128

128129
const dynamoClient = new DynamoDBClient(/* ... */)
129-
const store = dynamodbStore({
130+
const store = new DynamoDBStore({
130131
connection: dynamoClient,
131132
// Name of the table where the locks will be stored
132133
table: {
133-
name: 'locks'
134+
name: 'locks',
134135
},
135136
})
136137

137138
const lockFactory = new LockFactory(store)
138-
139139
```
140+
140141
:::
141142

142143
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.
143144

144145
### Options
145146

146-
| Option | Description | Default |
147-
| --- | --- | --- |
147+
| Option | Description | Default |
148+
| ------------ | ----------------------------------------------------------- | ------- |
148149
| `table.name` | The name of the table that will be used to store the cache. | `cache` |
149-
| `connection` | An instance of `DynamoDBClient` | N/A |
150+
| `connection` | An instance of `DynamoDBClient` | N/A |
150151

151152
## Databases
152153

@@ -160,10 +161,10 @@ Note that you can easily create your own adapter by implementing the `DatabaseAd
160161

161162
All Database drivers accept the following common options:
162163

163-
| Option | Description | Default |
164-
| --- | --- | --- |
165-
| `tableName` | The name of the table that will be used to store the locks. | `verrou` |
166-
| `autoCreateTable` | If the table should be automatically created if it does not exist. | `true` |
164+
| Option | Description | Default |
165+
| ----------------- | ------------------------------------------------------------------ | -------- |
166+
| `tableName` | The name of the table that will be used to store the locks. | `verrou` |
167+
| `autoCreateTable` | If the table should be automatically created if it does not exist. | `true` |
167168

168169
### Knex
169170

@@ -182,19 +183,20 @@ const db = knex({ client: 'mysql2', connection: MYSQL_CREDENTIALS })
182183
const verrou = new Verrou({
183184
default: 'sqlite',
184185
stores: {
185-
sqlite: { driver: knexStore({ connection: db }) }
186-
}
186+
sqlite: { driver: knexStore({ connection: db }) },
187+
},
187188
})
188189
```
189190

190191
```ts
191192
// title: LockFactory API
192193
import knex from 'knex'
193194
import { Verrou, LockFactory } from '@verrou/core'
194-
import { knexStore } from '@verrou/core/drivers/knex'
195+
import { KnexAdapter } from '@verrou/core/drivers/knex'
196+
import { DatabaseStore } from '@verrou/core/drivers/database'
195197

196198
const db = knex({ client: 'mysql2', connection: MYSQL_CREDENTIALS })
197-
const store = knexStore({ dialect: 'sqlite', connection: db })
199+
const store = new DatabaseStore(new KnexAdapter(db))
198200
const lockFactory = new LockFactory(store)
199201
```
200202

@@ -204,7 +206,6 @@ const lockFactory = new LockFactory(store)
204206

205207
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.
206208

207-
208209
:::codegroup
209210

210211
```ts
@@ -218,19 +219,20 @@ const db = new Kysely<Database>({ dialect })
218219
const verrou = new Verrou({
219220
default: 'kysely',
220221
stores: {
221-
kysely: { driver: kyselyStore({ connection: db }) }
222-
}
222+
kysely: { driver: kyselyStore({ connection: db }) },
223+
},
223224
})
224225
```
225226

226227
```ts
227228
// title: LockFactory API
228229
import { Kysely } from 'kysely'
229230
import { Verrou, LockFactory } from '@verrou/core'
230-
import { kyselyStore } from '@verrou/core/drivers/kysely'
231+
import { KyselyAdapter } from '@verrou/core/drivers/kysely'
232+
import { DatabaseStore } from '@verrou/core/drivers/database'
231233

232234
const db = new Kysely<Database>({ dialect })
233-
const store = kyselyStore({ connection: db })
235+
const store = new DatabaseStore(new KyselyAdapter(db))
234236
const lockFactory = new LockFactory(store)
235237
```
236238

docs/content/docs/quick_setup.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ You can install Verrou via your favorite package manager.
1010
Verrou is an ESM-only package. You will also need Node.js 18 or higher.
1111
:::
1212

13-
1413
:::codegroup
14+
1515
```sh
1616
// title: npm
1717
npm i @verrou/core
@@ -26,8 +26,8 @@ pnpm add @verrou/core
2626
// title: yarn
2727
yarn add @verrou/core
2828
```
29-
:::
3029

30+
:::
3131

3232
## Setup
3333

@@ -46,8 +46,8 @@ const verrou = new Verrou({
4646
default: 'redis',
4747
stores: {
4848
redis: { driver: redisStore() },
49-
memory: { driver: memoryStore() }
50-
}
49+
memory: { driver: memoryStore() },
50+
},
5151
})
5252
```
5353

@@ -61,9 +61,9 @@ Alternatively, if having to keep a global variable is a hassle for you, you can
6161

6262
```ts
6363
import { LockFactory } from '@verrou/core'
64-
import { redisStore } from '@verrou/core/drivers/redis'
64+
import { RedisStore } from '@verrou/core/drivers/redis'
6565

66-
const lockFactory = new LockFactory(redisStore())
66+
const lockFactory = new LockFactory(new RedisStore())
6767
await lockFactory.createLock('foo').run(async () => {
6868
// do something
6969
})

0 commit comments

Comments
 (0)