@probitas/client-deno-kv

Deno KV client for Probitas scenario testing framework.

This package provides a Deno KV client designed for integration testing of applications using Deno KV storage.

Features

  • Key-Value Operations: get, set, delete with structured keys
  • Listing: Iterate over keys by prefix, start, end
  • Atomic Transactions: Atomic operations with version checking
  • Type Safety: Generic type parameters for stored values
  • Resource Management: Implements AsyncDisposable for proper cleanup

Installation

deno add jsr:@probitas/client-deno-kv

Quick Start

import { createDenoKvClient } from "@probitas/client-deno-kv";

const kv = await createDenoKvClient();

// Set a value
const setResult = await kv.set(["users", "1"], { name: "Alice", age: 30 });
console.log("Versionstamp:", setResult.versionstamp);

// Get a value with type
const getResult = await kv.get<{ name: string; age: number }>(["users", "1"]);
console.log("User:", getResult.value);

// List entries by prefix
const listResult = await kv.list<{ name: string }>({ prefix: ["users"] });
console.log("Entries:", listResult.entries);

await kv.close();

Atomic Operations

// Atomic transaction with version check
const atomic = kv.atomic();
atomic.check({ key: ["counter"], versionstamp: null }); // Only if key doesn't exist
atomic.set(["counter"], 1n);
await atomic.commit();

// Atomic increment
const current = await kv.get<bigint>(["counter"]);
const atomic2 = kv.atomic();
atomic2.check({ key: ["counter"], versionstamp: current.versionstamp });
atomic2.set(["counter"], (current.value ?? 0n) + 1n);
await atomic2.commit();

Using with using Statement

await using kv = await createDenoKvClient();

await kv.set(["test"], "value");
const result = await kv.get(["test"]);
console.log(result.value);
// Client automatically closed when block exits
Package Description
@probitas/client Core utilities and types
@probitas/client-redis Redis client

Installation

deno add jsr:@probitas/client-deno-kv

Classes

class

#DenoKvAtomicBuilderImpl

class DenoKvAtomicBuilderImpl implements DenoKvAtomicBuilder

Implementation of DenoKvAtomicBuilder.

NameDescription
check()
set()
delete()
sum()
min()
max()
commit()
Constructor
new DenoKvAtomicBuilderImpl(kv: Deno.Kv)
Methods
check(): unknown
set(): unknown
delete(): unknown
sum(): unknown
min(): unknown
max(): unknown
commit(): unknown
class

#DenoKvAtomicCheckError

class DenoKvAtomicCheckError extends DenoKvError

Error thrown when an atomic operation fails due to check failures.

NameDescription
name
kind
failedChecksThe keys whose checks failed.
Constructor
new DenoKvAtomicCheckError(message: string, failedChecks: readonly Deno.KvKey[], options?: ErrorOptions)
Properties
  • readonlynamestring
  • readonlykind"atomic_check"
  • readonlyfailedChecksreadonly Deno.KvKey[]

    The keys whose checks failed.

class

#DenoKvError

class DenoKvError extends ClientError

Base error class for Deno KV operations.

NameDescription
name
Constructor
new DenoKvError(message: string, _: unknown, options?: ErrorOptions)
Properties
  • readonlynamestring
class

#DenoKvQuotaError

class DenoKvQuotaError extends DenoKvError

Error thrown when a quota limit is exceeded.

NameDescription
name
kind
Constructor
new DenoKvQuotaError(message: string, options?: ErrorOptions)
Properties
  • readonlynamestring
  • readonlykind"quota"

Interfaces

interface

#DenoKvAtomicBuilder

interface DenoKvAtomicBuilder

Builder for atomic KV operations.

NameDescription
check()Add version checks to the atomic operation.
set()Set a value in the KV store.
delete()Delete a key from the KV store.
sum()Atomically add to a bigint value (Deno.KvU64).
min()Atomically set to minimum of current and provided value.
max()Atomically set to maximum of current and provided value.
commit()Commit the atomic operation.
Methods
check(_: Deno.AtomicCheck[]): this

Add version checks to the atomic operation. If any check fails, the entire operation will fail.

Parameters
  • _Deno.AtomicCheck[]
set<T>(key: Deno.KvKey, value: T, options?: { expireIn?: number }): this

Set a value in the KV store.

Parameters
  • keyDeno.KvKey
  • valueT
  • options?{ expireIn?: number }
delete(key: Deno.KvKey): this

Delete a key from the KV store.

Parameters
  • keyDeno.KvKey
sum(key: Deno.KvKey, n: bigint): this

Atomically add to a bigint value (Deno.KvU64).

Parameters
  • keyDeno.KvKey
  • nbigint
min(key: Deno.KvKey, n: bigint): this

Atomically set to minimum of current and provided value.

Parameters
  • keyDeno.KvKey
  • nbigint
max(key: Deno.KvKey, n: bigint): this

Atomically set to maximum of current and provided value.

Parameters
  • keyDeno.KvKey
  • nbigint
commit(): Promise<DenoKvAtomicResult>

Commit the atomic operation.

interface

#DenoKvAtomicResult

interface DenoKvAtomicResult

Result of an atomic operation.

NameDescription
type
ok
versionstamp
duration
Properties
  • readonlytype"deno-kv:atomic"
  • readonlyokboolean
  • readonlyversionstamp?string
  • readonlydurationnumber
interface

#DenoKvClient

interface DenoKvClient extends AsyncDisposable

Deno KV client for Probitas scenario testing.

NameDescription
configClient configuration.
get()Get a single value by key.
getMany()Get multiple values by keys.
set()Set a value.
delete()Delete a key.
list()List entries by selector.
atomic()Create an atomic operation builder.
close()Close the KV connection.
Properties
Methods
get<T>(key: Deno.KvKey, options?: CommonOptions): Promise<DenoKvGetResult<T>>

Get a single value by key.

Parameters
getMany<T extends readonly unknown[]>(keys: readonly [unknown], options?: CommonOptions): Promise<unknown>

Get multiple values by keys.

Parameters
set<T>(key: Deno.KvKey, value: T, options?: DenoKvSetOptions): Promise<DenoKvSetResult>

Set a value.

Parameters
delete(key: Deno.KvKey, options?: CommonOptions): Promise<DenoKvDeleteResult>

Delete a key.

Parameters
list<T>(selector: Deno.KvListSelector, options?: DenoKvListOptions): Promise<DenoKvListResult<T>>

List entries by selector.

Parameters
atomic(): DenoKvAtomicBuilder

Create an atomic operation builder.

close(): Promise<void>

Close the KV connection.

interface

#DenoKvClientConfig

interface DenoKvClientConfig extends CommonOptions

Configuration for DenoKvClient.

NameDescription
pathPath to the KV database file.
Properties
  • readonlypath?string

    Path to the KV database file. If not specified, uses in-memory storage or Deno Deploy's KV.

interface

#DenoKvDeleteResult

interface DenoKvDeleteResult

Result of a delete operation.

NameDescription
type
ok
duration
Properties
  • readonlytype"deno-kv:delete"
  • readonlyokboolean
  • readonlydurationnumber
interface

#DenoKvEntries

interface DenoKvEntries<T> extends ReadonlyArray<DenoKvEntry<T>>

Collection of KV entries with helper methods.

NameDescription
first()Returns the first entry, or undefined if empty.
firstOrThrow()Returns the first entry, or throws if empty.
last()Returns the last entry, or undefined if empty.
lastOrThrow()Returns the last entry, or throws if empty.
Methods
first(): DenoKvEntry<T> | undefined

Returns the first entry, or undefined if empty.

firstOrThrow(): DenoKvEntry<T>

Returns the first entry, or throws if empty.

last(): DenoKvEntry<T> | undefined

Returns the last entry, or undefined if empty.

lastOrThrow(): DenoKvEntry<T>

Returns the last entry, or throws if empty.

interface

#DenoKvEntry

interface DenoKvEntry<T>

A single entry in the KV store.

NameDescription
key
value
versionstamp
Properties
  • readonlykeyDeno.KvKey
  • readonlyvalueT
  • readonlyversionstampstring
interface

#DenoKvGetResult

interface DenoKvGetResult<T>

Result of a get operation.

NameDescription
type
ok
key
value
versionstamp
duration
Properties
  • readonlytype"deno-kv:get"
  • readonlyokboolean
  • readonlykeyDeno.KvKey
  • readonlyvalueT | null
  • readonlyversionstampstring | null
  • readonlydurationnumber
interface

#DenoKvListOptions

interface DenoKvListOptions extends CommonOptions

Options for list operations.

NameDescription
limitMaximum number of entries to return.
cursorCursor for pagination.
reverseWhether to iterate in reverse order.
Properties
  • readonlylimit?number

    Maximum number of entries to return.

  • readonlycursor?string

    Cursor for pagination.

  • readonlyreverse?boolean

    Whether to iterate in reverse order.

interface

#DenoKvListResult

interface DenoKvListResult<T>

Result of a list operation.

NameDescription
type
ok
entries
duration
Properties
  • readonlytype"deno-kv:list"
  • readonlyokboolean
  • readonlyentriesDenoKvEntries<T>
  • readonlydurationnumber
interface

#DenoKvSetOptions

interface DenoKvSetOptions extends CommonOptions

Options for set operations.

NameDescription
expireInTime-to-live in milliseconds.
Properties
  • readonlyexpireIn?number

    Time-to-live in milliseconds. The entry will automatically expire after this duration.

interface

#DenoKvSetResult

interface DenoKvSetResult

Result of a set operation.

NameDescription
type
ok
versionstamp
duration
Properties
  • readonlytype"deno-kv:set"
  • readonlyokboolean
  • readonlyversionstampstring
  • readonlydurationnumber

Functions

function

#createDenoKvClient

async function createDenoKvClient(config?: DenoKvClientConfig): Promise<DenoKvClient>

Create a new Deno KV client instance.

The client provides key-value operations with support for atomic transactions, time-to-live (TTL), and prefix-based listing.

Parameters
Returns

Promise<DenoKvClient> — A promise resolving to a new Deno KV client instance

Examples

Basic usage with in-memory database

const kv = await createDenoKvClient();

await kv.set(["users", "123"], { name: "Alice", email: "alice@example.com" });

const result = await kv.get<User>(["users", "123"]);
console.log(result.value);  // { name: "Alice", email: "alice@example.com" }

await kv.close();

Using persistent storage

const kv = await createDenoKvClient({
  path: "./data.kv",
});

Set with expiration (TTL)

await kv.set(["sessions", sessionId], sessionData, {
  expireIn: 3600_000,  // Expire in 1 hour
});

List entries by prefix

const result = await kv.list<User>({ prefix: ["users"] });
for (const entry of result.entries) {
  console.log(entry.key, entry.value);
}

Atomic transactions

const atomicResult = await kv.atomic()
  .check({ key: ["counter"], versionstamp: null })
  .set(["counter"], 1)
  .commit();

Using await using for automatic cleanup

await using kv = await createDenoKvClient();

await kv.set(["test"], "value");
// Client automatically closed when scope exits
function

#createDenoKvEntries

function createDenoKvEntries<T>(entries: DenoKvEntry<T>[]): DenoKvEntries<T>

Creates a DenoKvEntries instance from an array of entries.

Parameters

Type Aliases

type

#DenoKvResult

type DenoKvResult<T = unknown> = DenoKvGetResult<T> | DenoKvSetResult | DenoKvDeleteResult | DenoKvListResult<T> | DenoKvAtomicResult

Union of all Deno KV result types.

Search Documentation