Cloudflare - v1.3.0
    Preparing search index...

    Class KVBinding<E>

    Accessor for the Workers KV namespace binding (kv_namespaces in wrangler.json).

    const cache = new KVBinding() // binding name and default TTL from the configuration layer
    await cache.putJson(ctx.env, 'session:42', { userId: 42 })
    const session = await cache.getJson<Session>(ctx.env, 'session:42')

    Bayu Dwiyan Satria

    1.0.0

    1.0.0

    Type Parameters

    Hierarchy (View Summary)

    Index
    expirationTtl?: number

    Default expiration applied to writes, in seconds.

    settings: () => KVSettings

    This accessor’s settings, resolved on first read.

    • get name(): string

      The binding name this instance resolves.

      Returns string

      The binding name as declared in wrangler.json.

    • Deletes a key.

      Deleting a key that does not exist is not an error.

      Parameters

      • env: E

        The Worker environment.

      • key: string

        The key to delete.

      Returns Promise<void>

    • Reads a value as text.

      Parameters

      • env: E

        The Worker environment.

      • key: string

        The key to read.

      • OptionalcacheTtl: number

        How long, in seconds, the edge may serve this read from cache.

      Returns Promise<string>

      The stored value, or null when the key is absent or expired.

      MissingBindingError When the KV binding is absent.

    • Reads a value and parses it as JSON.

      Type Parameters

      • T = unknown

      Parameters

      • env: E

        The Worker environment.

      • key: string

        The key to read.

      • OptionalcacheTtl: number

        How long, in seconds, the edge may serve this read from cache.

      Returns Promise<T>

      The parsed value, or null when the key is absent or expired.

    • Reads a value together with its metadata.

      Type Parameters

      • Metadata = unknown

      Parameters

      • env: E

        The Worker environment.

      • key: string

        The key to read.

      Returns Promise<KVNamespaceGetWithMetadataResult<string, Metadata>>

      The value and metadata pair — both null when the key is absent.

    • Checks whether the binding is available on the given environment.

      Use this to degrade gracefully when a binding is optional.

      Parameters

      • env: E

        The Worker environment.

      Returns boolean

      true when the binding is present.

    • Lists keys in the namespace.

      Type Parameters

      • Metadata = unknown

      Parameters

      • env: E

        The Worker environment.

      • options: KVNamespaceListOptions = {}

        List options — prefix, limit, and cursor.

      Returns Promise<KVNamespaceListResult<Metadata, string>>

      The matched keys and pagination state.

      Results are paginated — check list_complete and pass the returned cursor back in to read the next page.

    • Writes a value, applying the configured default TTL when none is supplied.

      Parameters

      • env: E

        The Worker environment.

      • key: string

        The key to write.

      • value: string | ReadableStream<any> | ArrayBuffer | ArrayBufferView

        The value to store.

      • options: KVNamespacePutOptions = {}

        Put options — expiration, TTL, and metadata.

      Returns Promise<void>

    • Serialises a value to JSON and writes it.

      Parameters

      • env: E

        The Worker environment.

      • key: string

        The key to write.

      • value: unknown

        The value to serialise and store.

      • options: KVNamespacePutOptions = {}

        Put options — expiration, TTL, and metadata.

      Returns Promise<void>

    • Resolves the binding without throwing.

      Parameters

      • env: E

        The Worker environment.

      Returns KVNamespace<string>

      The resolved binding, or null when it is not configured.

    • Applies the configured default TTL to put options that do not set one.

      Parameters

      • options: KVNamespacePutOptions

        The caller-supplied put options.

      Returns KVNamespacePutOptions

      Put options with an expiration applied where appropriate.