Type Alias IDatabaseLockAdapter

IDatabaseLockAdapter: {
    find(context: IReadableContext, key: string): Promise<null | ILockData>;
    remove(
        context: IReadableContext,
        key: string,
    ): Promise<null | ILockExpirationData>;
    removeIfOwner(
        context: IReadableContext,
        key: string,
        lockId: string,
    ): Promise<null | ILockData>;
    transaction<TReturn>(
        context: IReadableContext,
        fn: InvokableFn<[transaction: IDatabaseLockTransaction], Promise<TReturn>>,
    ): Promise<TReturn>;
    updateExpiration(
        context: IReadableContext,
        key: string,
        lockId: string,
        expiration: Date,
    ): Promise<number>;
}

Technology-agnostic storage adapter contract for lock persistence in databases. Implementations handle lock data storage using CRUD-capable backends (SQL databases, ORMs like TypeORM/MikroORM, etc.). Provides transactional support for atomic lock updates and consistency guarantees in distributed systems.

IMPORT_PATH: "@daiso-tech/core/lock/contracts"

Type declaration

  • find:function
  • remove:function
  • removeIfOwner:function
    • Removes a lock from the database only if owned by the specified lockId. Ownership verification prevents accidental deletion of locks held by others.

      Parameters

      • context: IReadableContext

        Readable execution context for the operation

      • key: string

        Unique identifier for the lock

      • lockId: string

        The unique identifier of the expected lock owner

      Returns Promise<null | ILockData>

      Promise resolving to the lock's owner and expiration data if successfully removed, null if the lock wasn't found or the owner didn't match

  • transaction:function
  • updateExpiration:function
    • Updates the expiration time of a lock if owned by the specified lockId. Ownership verification ensures only the lock owner can extend the lock.

      Parameters

      • context: IReadableContext

        Readable execution context for the operation

      • key: string

        Unique identifier for the lock

      • lockId: string

        The unique identifier of the expected lock owner

      • expiration: Date

        The new date and time when the lock should expire

      Returns Promise<number>

      Promise resolving to the number of locks updated (1 if successful, 0 if not found or ownership mismatch)