Skip to content

Commit 6e5e864

Browse files
committed
docs: note about database store adapter
1 parent c493332 commit 6e5e864

1 file changed

Lines changed: 76 additions & 4 deletions

File tree

docs/content/docs/extend/custom_lock_store.md

Lines changed: 76 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,81 @@ const verrou = new Verrou({
4141
})
4242
```
4343

44+
## Create an adapter for the DatabaseStore
45+
46+
If your want to use a database to store your locks, you are not forced to create a full driver. You can leverage the adapter system available in the database store.
47+
48+
We only ship adapter for Kysely and Knex to interact with the database for now. If ever you want to use another library, you can create your own adapter by implementing the `DatabaseAdapter` interface accessible from `@verrou/core/types`. The interface is defined as follows:
49+
50+
```ts
51+
export interface DatabaseAdapter {
52+
/**
53+
* Set the table name to store the locks
54+
*/
55+
setTableName(tableName: string): void
56+
57+
/**
58+
* Create the table to store the locks if it doesn't exist
59+
*/
60+
createTableIfNotExists(): Promise<void>
61+
62+
/**
63+
* Insert the given lock in the store
64+
*/
65+
insertLock(lock: { key: string; owner: string; expiration: number | null }): Promise<void>
66+
67+
/**
68+
* Acquire the lock by updating the owner and expiration date.
69+
*
70+
* The adapter should check if expiration date is in the past
71+
* and return the number of updated rows.
72+
*/
73+
acquireLock(lock: { key: string; owner: string; expiration: number | null }): Promise<number>
74+
75+
/**
76+
* Delete a lock from the store.
77+
*
78+
* If owner is provided, the lock should only be deleted if the owner matches.
79+
*/
80+
deleteLock(key: string, owner?: string): Promise<void>
81+
82+
/**
83+
* Extend the expiration date of the lock by the given
84+
* duration ( Date.now() + duration ).
85+
*
86+
* The owner must match.
87+
*/
88+
extendLock(key: string, owner: string, duration: number): Promise<number>
89+
90+
/**
91+
* Returns the current owner and expiration date of the lock
92+
*/
93+
getLock(key: string): Promise<{ owner: string; expiration: number | null } | undefined>
94+
}
95+
```
96+
97+
You can take a look at the code of the [Kysely adapter](https://github.com/Julien-R44/verrou/blob/main/packages/verrou/src/drivers/kysely.ts#L22) or the [Knex adapter](https://github.com/Julien-R44/verrou/blob/main/packages/verrou/src/drivers/kysely.ts#L22) for inspiration.
98+
99+
Once you defined your adapter, you can create your own store that use the DatabaseStore and your adapter:
100+
101+
```ts
102+
export class PrismaAdapter implements DatabaseAdapter {
103+
// ...
104+
}
105+
106+
import { DatabaseStore } from '@verrou/core/drivers/database'
107+
108+
export function prismaStore(config: PrismaOptions) {
109+
return {
110+
config,
111+
factory: () => {
112+
const adapter = new PrismaAdapter(config.connection)
113+
return new DatabaseStore(adapter, config)
114+
},
115+
}
116+
}
117+
```
118+
44119
## Tests
45120

46121
If you want to test your driver and its compliance, Verrou is shipped with a test suite for [Japa](https://japa.dev/docs) that you can use. Note that you will also need to have `@japa/assert` installed. Then, you can use it like this:
@@ -54,10 +129,7 @@ import { registerStoreTestSuite } from '@verrou/core/test_suite'
54129
test.group('My Store', (group) => {
55130
registerStoreTestSuite({
56131
test,
57-
store: MyStore,
58-
config: {
59-
// Your driver options
60-
}
132+
createStore: () => new MyStore()
61133
})
62134
})
63135
```

0 commit comments

Comments
 (0)