diff --git a/packages/verrou/src/drivers/database.ts b/packages/verrou/src/drivers/database.ts index e0ca977..ea69fa2 100644 --- a/packages/verrou/src/drivers/database.ts +++ b/packages/verrou/src/drivers/database.ts @@ -29,7 +29,11 @@ export class DatabaseStore implements LockStore { constructor(config: DatabaseStoreOptions) { this.#connection = this.#createConnection(config) this.#tableName = config.tableName || this.#tableName - this.#initialized = this.#createTableIfNotExists() + if (config.autoCreateTable !== false) { + this.#initialized = this.#createTableIfNotExists() + } else { + this.#initialized = Promise.resolve() + } } /** diff --git a/packages/verrou/src/types/drivers.ts b/packages/verrou/src/types/drivers.ts index 06a9976..fadf40c 100644 --- a/packages/verrou/src/types/drivers.ts +++ b/packages/verrou/src/types/drivers.ts @@ -17,9 +17,15 @@ export type DatabaseStoreOptions = { /** * The table name to use ( to store the locks ) - * Table will be automatically created if it doesn't exist */ tableName?: string + + /** + * Set to true to automatically create the table if it doesn't exist + * + * @default true + */ + autoCreateTable?: boolean } export type RedisStoreOptions = { diff --git a/packages/verrou/tests/drivers/database.spec.ts b/packages/verrou/tests/drivers/database.spec.ts index af1ea55..6fed0be 100644 --- a/packages/verrou/tests/drivers/database.spec.ts +++ b/packages/verrou/tests/drivers/database.spec.ts @@ -25,6 +25,13 @@ test.group('Database Driver', (group) => { assert.lengthOf(locks, 1) }) + test('doesnt create table if autoCreateTable is false', async ({ assert }) => { + new DatabaseStore({ connection: db, dialect: 'pg', autoCreateTable: false }) + + const hasTable = await db.schema.hasTable('verrou') + assert.isFalse(hasTable) + }) + test('null ttl', async ({ assert }) => { const store = new DatabaseStore({ connection: db, dialect: 'pg' })