Skip to content

Commit 2d980cf

Browse files
committed
feat: add autoCreateTable options for database driver
1 parent b7cf5ce commit 2d980cf

3 files changed

Lines changed: 19 additions & 2 deletions

File tree

packages/verrou/src/drivers/database.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ export class DatabaseStore implements LockStore {
2929
constructor(config: DatabaseStoreOptions) {
3030
this.#connection = this.#createConnection(config)
3131
this.#tableName = config.tableName || this.#tableName
32-
this.#initialized = this.#createTableIfNotExists()
32+
if (config.autoCreateTable !== false) {
33+
this.#initialized = this.#createTableIfNotExists()
34+
} else {
35+
this.#initialized = Promise.resolve()
36+
}
3337
}
3438

3539
/**

packages/verrou/src/types/drivers.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,15 @@ export type DatabaseStoreOptions = {
1717

1818
/**
1919
* The table name to use ( to store the locks )
20-
* Table will be automatically created if it doesn't exist
2120
*/
2221
tableName?: string
22+
23+
/**
24+
* Set to true to automatically create the table if it doesn't exist
25+
*
26+
* @default true
27+
*/
28+
autoCreateTable?: boolean
2329
}
2430

2531
export type RedisStoreOptions = {

packages/verrou/tests/drivers/database.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ test.group('Database Driver', (group) => {
2525
assert.lengthOf(locks, 1)
2626
})
2727

28+
test('doesnt create table if autoCreateTable is false', async ({ assert }) => {
29+
new DatabaseStore({ connection: db, dialect: 'pg', autoCreateTable: false })
30+
31+
const hasTable = await db.schema.hasTable('verrou')
32+
assert.isFalse(hasTable)
33+
})
34+
2835
test('null ttl', async ({ assert }) => {
2936
const store = new DatabaseStore({ connection: db, dialect: 'pg' })
3037

0 commit comments

Comments
 (0)