Class for accessing a Fastly KV-store.

A kv store is a persistent, globally consistent key-value store.

Note: Can only be used when processing requests, not during build-time initialization.

Example

In this example we connect to an KV Store named 'files' and save an entry to the store under the key 'hello' and then read back the value and return it to the client.

/// <reference types="@fastly/js-compute" />

import { KVStore } from "fastly:kv-store";

async function app(event) {
const files = new KVStore('files')

await files.put('hello', 'world')

const entry = await files.get('hello')

return new Response(await entry.text())
}

addEventListener("fetch", (event) => event.respondWith(app(event)))

Hierarchy

  • KVStore

Constructors

Methods

Constructors

  • Creates a new JavaScript KVStore object which interacts with the Fastly KV Store named name.

    Parameters

    • name: string

      Name of the Fastly KV Store to interact with. A name cannot be empty, contain Control characters, or be longer than 255 characters.

    Returns KVStore

Methods

  • Delete the value associated with the key key in the KV Store.

    Parameters

    • key: string

      The key to retrieve from within the KV Store. A key cannot:

      • Be any of the strings "", ".", or ".."
      • Start with the string ".well-known/acme-challenge/""
      • Contain any of the characters "#;?^|\n\r"
      • Be longer than 1024 characters

    Returns Promise<undefined>

  • Gets the value associated with the key key in the KV Store. When the key is present, a resolved Promise containing an KVStoreEntry will be returned which contains the associated value. When the key is absent, a resolved Promise containing null is returned.

    Parameters

    • key: string

      The key to retrieve from within the KV Store. A key cannot:

      • Be any of the strings "", ".", or ".."
      • Start with the string ".well-known/acme-challenge/""
      • Contain any of the characters "#;?^|\n\r"
      • Be longer than 1024 characters

    Returns Promise<null | KVStoreEntry>

  • Returns an async iterator for the values of the KV Store optionally taking a prefix and limit

    Parameters

    • Optional options: {
          cursor?: string;
          limit?: number;
          noSync?: boolean;
          prefix?: string;
      }
      Optional
      • Optional cursor?: string

        Cursor

        The base64 cursor string representing the last listing operation

      • Optional limit?: number

        Limit the number of keys provided per listing.

      • Optional noSync?: boolean

        Do not wait to sync the key list, and instead immediately return the current cached key list.

      • Optional prefix?: string

        String prefix for keys to list.

    Returns {
        cursor: undefined | string;
        list: string[];
    }

    • cursor: undefined | string

      Pass this base64 cursor into a subsequent list call to obtain the next listing.

      The cursor is undefined when the end of the list is reached.

    • list: string[]
  • Write the value of value into the KV Store under the key key.

    Note: KV Store is eventually consistent, this means that the updated contents associated with the key key may not be available to read from all edge locations immediately and some edge locations may continue returning the previous contents associated with the key.

    Parameters

    • key: string

      The key to associate with the value. A key cannot:

      • Be any of the strings "", ".", or ".."
      • Start with the string ".well-known/acme-challenge/""
      • Contain any of the characters "#;?^|\n\r"
      • Be longer than 1024 characters
    • value: BodyInit

      The value to store within the KV Store.

    • Optional options: {
          gen?: number;
          metadata?: string | ArrayBufferView | ArrayBuffer;
          mode?: "overwrite" | "add" | "append" | "prepend";
          ttl?: number;
      }
      Optional
      • Optional gen?: number

        If generation match integer.

      • Optional metadata?: string | ArrayBufferView | ArrayBuffer

        Optional metadata to be associated with the entry.

        If passing a string, UTF-8 encoding is used

      • Optional mode?: "overwrite" | "add" | "append" | "prepend"

        Insert mode, defaults to 'overwrite'.

      • Optional ttl?: number

        TTL for the entry.

    Returns Promise<undefined>

Generated using TypeDoc