prorm API Reference
    Preparing search index...

    Class EtcdStore

    EtcdStore wraps an etcd3 client and exposes etcd's real primitives: key/value get/put/delete (single-key and prefix-range), watch, leases, distributed locking, and compare-and-swap transactions.

    Implements

    Index
    name: "etcd" = 'etcd'

    The name of the store (e.g. 'mongodb', 'redis', 'dynamodb')

    library: "etcd3" = 'etcd3'

    The client library being used

    • Type Parameters

      • T

      Parameters

      • key: string

      Returns Promise<T | null>

    • Parameters

      • key: string

      Returns Promise<string | null>

    • Parameters

      • key: string

      Returns Promise<Buffer<ArrayBufferLike> | null>

    • Parameters

      • key: string
      • value: string | number | Buffer<ArrayBufferLike>
      • options: EtcdPutOptions = {}

      Returns Promise<void>

    • Deletes a single key. Returns the number of keys actually deleted (0 or 1).

      Parameters

      • key: string

      Returns Promise<number>

    • Parameters

      • key: string

      Returns Promise<boolean>

    • All key/value pairs whose key starts with prefix.

      Parameters

      • prefix: string

      Returns Promise<Record<string, string>>

    • All keys (without values) starting with prefix.

      Parameters

      • prefix: string

      Returns Promise<string[]>

    • Number of keys starting with prefix, without transferring their values.

      Parameters

      • prefix: string

      Returns Promise<number>

    • Deletes every key starting with prefix. Returns the number of keys deleted.

      Parameters

      • prefix: string

      Returns Promise<number>

    • Watch every key starting with prefix for changes, invoking handler for every put/delete on any matching key. Returns a handle whose cancel() stops the underlying watch stream.

      Parameters

      Returns Promise<EtcdWatchHandle>

    • Grants a new lease with the given TTL (in seconds). By default the lease is kept alive automatically in the background (autoKeepAlive); pass { autoKeepAlive: false } to manage keep-alives manually via keepAliveOnce(). Returns the lease ID, which can be passed to put(key, value, { lease }) to associate keys with the lease so they expire automatically when it does.

      Parameters

      Returns Promise<string>

    • Puts key/value under a previously granted lease, so it expires when the lease does.

      Parameters

      • key: string
      • value: string | number | Buffer<ArrayBufferLike>
      • leaseId: string

      Returns Promise<void>

    • Fires a single, immediate keep-alive for a lease, resetting its TTL countdown.

      Parameters

      • leaseId: string

      Returns Promise<void>

    • Revokes a lease immediately, evicting every key still attached to it.

      Parameters

      • leaseId: string

      Returns Promise<void>

    • Stops sending keep-alives for a lease, letting it expire naturally when its TTL elapses, instead of revoking it immediately. Use revokeLease() to evict its keys right away.

      Parameters

      • leaseId: string

      Returns void

    • Registers a handler that fires when etcd indicates a lease has been lost (TTL expired without a successful keep-alive, or it was revoked server-side). Not fired when revokeLease()/releaseLease() is called locally and succeeds normally.

      Parameters

      • leaseId: string
      • handler: (err: Error) => void

      Returns void

    • Acquires a distributed lock on key, blocking (queueing behind the current holder, in acquisition order) until it's free rather than rejecting immediately. Under the hood this is a lease on the key that's revoked on release(), or timed out by etcd if the holder dies - ttlSeconds controls that lease's TTL (etcd3 defaults to 30s).

      Note: etcd3's own Lock#acquire() is a single compare-and-swap attempt - it rejects immediately with EtcdLockFailedError if the key already exists, rather than waiting, despite this store's docs (and the in-repo test mock) describing blocking/queueing semantics. To actually deliver that documented contract against a real etcd server, acquireLockOrThrow() below retries: on EtcdLockFailedError it watches the key until it's deleted (i.e. the current holder released or its lease expired), then retries the CAS. Multiple waiters can wake and race the retry simultaneously; that's fine because the retry is itself a CAS - at most one of them wins each round, and the rest go back to waiting.

      Parameters

      • key: string
      • OptionalttlSeconds: number

      Returns Promise<EtcdLockHandle>

    • Acquires a distributed lock on key, runs fn, and releases the lock once fn's result settles (whether it resolves or rejects). This is the recommended way to use locks - it can't leak a held lock the way manual acquireLock()/release() pairing can if fn throws.

      Type Parameters

      • T

      Parameters

      • key: string
      • fn: () => T | Promise<T>
      • OptionalttlSeconds: number

      Returns Promise<T>

    • Starts a raw etcd transaction: if (key.<column> <cmp> value) { ... }. Returns etcd3's ComparatorBuilder directly so callers get its full .and()/.then()/.else()/.commit() chain; combine with putOp()/ deleteOp()/getOp() below to build the then/else clauses.

      const result = await store
      .ifCompare('config-version', 'Value', '==', '1')
      .then(store.putOp('config-version', '2'))
      .else(store.getOp('config-version'))
      .commit();

      Parameters

      • key: string
      • column: "Version" | "Value" | "Create" | "Mod" | "Lease"
      • cmp: "==" | "!=" | ">" | "<" | "===" | "!=="
      • value: string | number | Buffer<ArrayBufferLike>

      Returns ComparatorBuilder

    • A put operation builder, for use as a .then()/.else() clause in ifCompare().

      Parameters

      • key: string
      • value: string | number | Buffer<ArrayBufferLike>

      Returns PutBuilder

    • A delete operation builder, for use as a .then()/.else() clause in ifCompare().

      Parameters

      • key: string

      Returns DeleteBuilder

    • A get operation builder, for use as a .then()/.else() clause in ifCompare().

      Parameters

      • key: string

      Returns SingleRangeBuilder

    • Convenience compare-and-swap: atomically sets key to newValue only if its current value equals expectedValue (or, if expectedValue is null, only if the key does not currently exist). Returns whether the swap happened.

      Parameters

      • key: string
      • expectedValue: string | null
      • newValue: string | number | Buffer<ArrayBufferLike>

      Returns Promise<boolean>

    • Atomically deletes key only if its current value equals expectedValue. Returns whether the delete happened.

      Parameters

      • key: string
      • expectedValue: string

      Returns Promise<boolean>