Skip to content

Commit

Permalink
feat: add autoCreateTable options for database driver
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien-R44 committed Mar 10, 2024
1 parent b7cf5ce commit 2d980cf
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
6 changes: 5 additions & 1 deletion packages/verrou/src/drivers/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

/**
Expand Down
8 changes: 7 additions & 1 deletion packages/verrou/src/types/drivers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*

Check warning on line 25 in packages/verrou/src/types/drivers.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `·`
* @default true
*/
autoCreateTable?: boolean
}

export type RedisStoreOptions = {
Expand Down
7 changes: 7 additions & 0 deletions packages/verrou/tests/drivers/database.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' })

Expand Down

0 comments on commit 2d980cf

Please sign in to comment.