import { Knex } from 'knex'; import { Kysely } from 'kysely'; import { Redis } from 'ioredis'; import { DynamoDBClient } from '@aws-sdk/client-dynamodb'; /** * Common options for database stores */ interface DatabaseOptions { /** * The table name to use ( to store the locks ) * * @default 'verrou' */ tableName?: string; /** * Set to true to automatically create the table if it doesn't exist * * @default true */ autoCreateTable?: boolean; } /** * Options for the Knex store */ interface KnexStoreOptions extends DatabaseOptions { /** * The Knex instance */ connection: Knex; } /** * Options for the Kysely store */ interface KyselyOptions extends DatabaseOptions { /** * The Kysely instance */ connection: Kysely; } /** * Options for the Redis store */ type RedisStoreOptions = { /** * The Redis connection */ connection: Redis; }; /** * Options for the DynamoDB store */ type DynamoDbOptions = { /** * DynamoDB table name to use. */ table: { name: string; }; connection: DynamoDBClient; }; /** * An adapter for the DatabaseStore */ 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; /** * Insert the given lock in the store */ insertLock(lock: { key: string; owner: string; expiration: number | null; }): Promise; /** * 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; /** * 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; /** * 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; /** * Returns the current owner and expiration date of the lock */ getLock(key: string): Promise<{ owner: string; expiration: number | null; } | undefined>; } export type { DynamoDbOptions as D, KnexStoreOptions as K, RedisStoreOptions as R, DatabaseAdapter as a, KyselyOptions as b, DatabaseOptions as c };