Skip to content

Commit

Permalink
docs: note about database store adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien-R44 committed Mar 12, 2024
1 parent c493332 commit 6e5e864
Showing 1 changed file with 76 additions and 4 deletions.
80 changes: 76 additions & 4 deletions docs/content/docs/extend/custom_lock_store.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,81 @@ const verrou = new Verrou({
})
```

## Create an adapter for the DatabaseStore

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.

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:

```ts
export interface DatabaseAdapter {
/**
* Set the table name to store the locks
*/
setTableName(tableName: string): void

/**
* Create the table to store the locks if it doesn't exist
*/
createTableIfNotExists(): Promise<void>

/**
* Insert the given lock in the store
*/
insertLock(lock: { key: string; owner: string; expiration: number | null }): Promise<void>

/**
* Acquire the lock by updating the owner and expiration date.
*
* The adapter should check if expiration date is in the past
* and return the number of updated rows.
*/
acquireLock(lock: { key: string; owner: string; expiration: number | null }): Promise<number>

/**
* Delete a lock from the store.
*
* If owner is provided, the lock should only be deleted if the owner matches.
*/
deleteLock(key: string, owner?: string): Promise<void>

/**
* Extend the expiration date of the lock by the given
* duration ( Date.now() + duration ).
*
* The owner must match.
*/
extendLock(key: string, owner: string, duration: number): Promise<number>

/**
* Returns the current owner and expiration date of the lock
*/
getLock(key: string): Promise<{ owner: string; expiration: number | null } | undefined>
}
```

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.

Once you defined your adapter, you can create your own store that use the DatabaseStore and your adapter:

```ts
export class PrismaAdapter implements DatabaseAdapter {
// ...
}

import { DatabaseStore } from '@verrou/core/drivers/database'

export function prismaStore(config: PrismaOptions) {
return {
config,
factory: () => {
const adapter = new PrismaAdapter(config.connection)
return new DatabaseStore(adapter, config)
},
}
}
```

## Tests

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:
Expand All @@ -54,10 +129,7 @@ import { registerStoreTestSuite } from '@verrou/core/test_suite'
test.group('My Store', (group) => {
registerStoreTestSuite({
test,
store: MyStore,
config: {
// Your driver options
}
createStore: () => new MyStore()
})
})
```
Expand Down

0 comments on commit 6e5e864

Please sign in to comment.