Perform Kv database operations

Hierarchy

  • WorkersKv

Constructors

  • See

    Example

    const kvMonitor = new WorkersKvMonitor();
    const workersKv = new WorkersKv(process.env["CF_EMAIL"], process.env["CF_ACCOUNT_ID"], process.env["CF_GLOBAL_API_KEY"], true, kvMonitor.dbListener.bind(kvMonitor));

    Parameters

    • accountEmail: string

      An email of the Cloudflare account

    • accountId: string

      An Id of the Cloudflare account

    • globalApiKey: string
    • Optional isValidateCfResponse: boolean = true
    • Optional Rest ...extensionArg: Function[]

      Extensional function that is wanted to be executed after the database operation is performed

    Returns WorkersKv

Properties

cfAuth: { accountEmail: string; accountId: string; globalApiKey: string }

Type declaration

  • accountEmail: string
  • accountId: string
  • globalApiKey: string
delete: ((relativePathParam: { keyName: string; namespaceId: string }) => Promise<boolean>) = ...

Type declaration

    • (relativePathParam: { keyName: string; namespaceId: string }): Promise<boolean>
    • Remove a KV pair from the Namespace. Use URL-encoding to use special characters (e.g. :, !, %) in the key name.

      Async

      Function

      deleteKeyValuePair

      See

      https://api.cloudflare.com/#workers-kv-namespace-delete-key-value-pair

      Returns

      True if the key is successfully removed, false otherwise.

      Throws

      The Kv operation request is failed.

      Parameters

      • relativePathParam: { keyName: string; namespaceId: string }

        The parameters in the relative path

        • keyName: string

          The name of the key

        • namespaceId: string

          The namespace identifier

      Returns Promise<boolean>

extensionArg: Function[]
isValidateCfResponse: boolean
read: ((relativePathParam: { keyName: string; namespaceId: string }) => Promise<string>) = ...

Type declaration

    • (relativePathParam: { keyName: string; namespaceId: string }): Promise<string>
    • Returns the value associated with the given key in the given namespace. Use URL-encoding to use special characters (e.g. :, !, %) in the key name. If the KV-pair is set to expire at some point, the expiration time as measured in seconds since the UNIX epoch will be returned in the "Expiration" response header.

      Async

      Function

      readKeyValuePair

      See

      https://api.cloudflare.com/#workers-kv-namespace-read-key-value-pair

      Returns

      The key value.

      Throws

      The Kv operation request is failed.

      Parameters

      • relativePathParam: { keyName: string; namespaceId: string }

        The parameters in the relative path

        • keyName: string

          The name of the key

        • namespaceId: string

          The namespace identifier

      Returns Promise<string>

write: ((relativePathParam: { keyName: string; namespaceId: string }, value: string, urlParam?: { expiration?: number; expiration_ttl?: number }) => Promise<boolean>) = ...

Type declaration

    • (relativePathParam: { keyName: string; namespaceId: string }, value: string, urlParam?: { expiration?: number; expiration_ttl?: number }): Promise<boolean>
    • Write a value identified by a key. Use URL-encoding to use special characters (e.g. :, !, %) in the key name. Body should be the value to be stored. Existing values and expirations will be overwritten. If neither expiration nor expiration_ttl is specified, the key-value pair will never expire. If both are set, expiration_ttl is used and expiration is ignored.

      Async

      Function

      writeKeyValuePair

      See

      https://api.cloudflare.com/#workers-kv-namespace-write-key-value-pair

      Returns

      True if key is successfully modified or added, false otherwise.

      Throws

      The Kv operation request is failed.

      Parameters

      • relativePathParam: { keyName: string; namespaceId: string }

        Parameters in the relative path

        • keyName: string

          The name of the key

        • namespaceId: string

          The namespace identifier

      • value: string

        A UTF-8 encoded string to be stored, up to 10 MB in length.

      • Optional urlParam: { expiration?: number; expiration_ttl?: number }

        The parameters at the end of URL

        • Optional expiration?: number

          The time, measured in number of seconds since the UNIX epoch, at which the key should expire.

        • Optional expiration_ttl?: number

          The number of seconds for which the key should be visible before it expires. At least 60.

      Returns Promise<boolean>

Methods

  • Private

    Handling a database operation that wants to be performed. It conveys a database operation request to the fetch function, receives a response from the fetch function, and send the whole database operation information to the funcArgHandlers for extensional purpose.

    Function

    bridge

    Async

    Returns

    A full information about the HTTP request, database operation perform status, and other Cloudflare responses

    Parameters

    • command: BridgeCommand

      Information about the requested database operation

    • http: httpFetchOptions

      Data related to the db operation for the HTTP request

    • validateCfResponseMethod: "string" | "full" | "withoutResult" = "full"

    Returns Promise<OwnFetchResponse>

  • Creates a namespace under the given title. A 400 is returned if the account already owns a namespace with this title. A namespace must be explicitly deleted to be replaced.

    Function

    createNamespace

    See

    https://api.cloudflare.com/#workers-kv-namespace-create-a-namespace

    Returns

    Information about the new namespace.

    Throws

    The Kv operation request is failed.

    Parameters

    • data: { title: string }

      Data for the HTTP body that will send to Cloudflare

      • title: string

        A human-readable string name for a Namespace.

    Returns Promise<NamespaceRes>

  • Remove a KV pair from the Namespace. Use URL-encoding to use special characters (e.g. :, !, %) in the key name.

    Async

    Function

    deleteKeyValuePair

    See

    https://api.cloudflare.com/#workers-kv-namespace-delete-key-value-pair

    Returns

    True if the key is successfully removed, false otherwise.

    Throws

    The Kv operation request is failed.

    Parameters

    • relativePathParam: { keyName: string; namespaceId: string }

      The parameters in the relative path

      • keyName: string

        The name of the key

      • namespaceId: string

        The namespace identifier

    Returns Promise<boolean>

  • Remove multiple KV pairs from the Namespace. Body should be an array of up to 10,000 keys to be removed.

    Async

    Function

    deleteMultipleKeyValuePairs

    See

    https://api.cloudflare.com/#workers-kv-namespace-delete-multiple-key-value-pairs

    Returns

    True if keys are successfully removed, false otherwise.

    Throws

    The Kv operation request is failed.

    Parameters

    • relativePathParam: { namespaceId: string }

      The parameters in the relative path

      • namespaceId: string

        The namespace identifier

    • data: { keyName: string[] }

      The data that will send to Cloudflare

      • keyName: string[]

        The name of the key

    Returns Promise<boolean>

  • Sending a database operation information to the function placed in the extensionArg in the class constructor

    Function

    funcArgHandlers

    Parameters

    • processSuccess: null | boolean

      True if the database operation is completed successfully, false otherwise. Null if it is uncertain that whether the operation is completed successfully or not.

    • command: BridgeCommand

      Information about the requested database operation

    • cfFetchRes: null | OwnFetchResponse = null

      A full information about the HTTP request, database operation perform status, and other Cloudflare responses

    • errDetail: null | { [key: string]: any } = null

      The error detail of the database operation

    Returns void

  • Parsing and returning the Cloudflare response.

    Function

    genReturnFromCfRes

    Returns

    A Cloudflare result that is shorten and pretty formatted.

    Throws

    The Kv operation request is failed.

    Parameters

    • method: "string" | "boolean" | "fullResult"

      A desired Cloudflare response format

    • req: OwnFetchResponse

      A full information about the HTTP request, database operation perform status, and other Cloudflare responses

    • command: string

      A short description of the performed operation.

    Returns any

  • Lists a namespace's keys.

    Async

    Function

    listNamespaceKeys

    See

    https://api.cloudflare.com/#workers-kv-namespace-list-a-namespace-s-keys

    Returns

    A list of namespace's key as well as the page and cursor information.

    Throws

    The Kv operation request is failed.

    Parameters

    • relativePathParam: { namespaceId: string }

      Parameters in the relative path

      • namespaceId: string

        The namespace identifier

    • Optional urlParam: { cursor?: string; limit?: number; prefix?: string }

      The parameters at the end of URL

      • Optional cursor?: string

        Opaque token indicating the position from which to continue when requesting the next set of records if the amount of list results was limited by the limit parameter. A valid value for the cursor can be obtained from the cursors object in the result_info structure.

      • Optional limit?: number

        The number of keys to return. The cursor attribute may be used to iterate over the next batch of keys if there are more than the limit.

      • Optional prefix?: string

        A string prefix used to filter down which keys will be returned. Exact matches and any key names that begin with the prefix will be returned.

    Returns Promise<NamespaceKeysRes>

  • Returns the namespaces owned by an account

    Function

    listNamespaces

    See

    https://api.cloudflare.com/#workers-kv-namespace-list-namespaces

    Returns

    Information about the new namespace.

    Throws

    The Kv operation request is failed.

    Parameters

    • Optional urlParam: { direction?: "asc" | "desc"; order?: "id" | "title"; page?: number; per_page?: number }

      The parameters that are in the URL

      • Optional direction?: "asc" | "desc"

        Direction to order namespaces

      • Optional order?: "id" | "title"

        Field to order results by

      • Optional page?: number

        Page number of paginated results

      • Optional per_page?: number

    Returns Promise<NamespaceRes[]>

  • Returns the metadata associated with the given key in the given namespace. Use URL-encoding to use special characters (e.g. :, !, %) in the key name.

    Async

    Function

    readKeyMeta

    See

    https://api.cloudflare.com/#workers-kv-namespace-read-the-metadata-for-a-key

    Returns

    An object containing the key and value of the metadata.

    Throws

    The Kv operation request is failed.

    Parameters

    • relativePathParam: { keyName: string; namespaceId: string }

      Parameters in the relative path

      • keyName: string

        The name of the key

      • namespaceId: string

        The namespace identifier

    Returns Promise<any>

  • Returns the value associated with the given key in the given namespace. Use URL-encoding to use special characters (e.g. :, !, %) in the key name. If the KV-pair is set to expire at some point, the expiration time as measured in seconds since the UNIX epoch will be returned in the "Expiration" response header.

    Async

    Function

    readKeyValuePair

    See

    https://api.cloudflare.com/#workers-kv-namespace-read-key-value-pair

    Returns

    The key value.

    Throws

    The Kv operation request is failed.

    Parameters

    • relativePathParam: { keyName: string; namespaceId: string }

      The parameters in the relative path

      • keyName: string

        The name of the key

      • namespaceId: string

        The namespace identifier

    Returns Promise<string>

  • Deletes the namespace corresponding to the given ID.

    Async

    Function

    removeNamespace

    See

    https://api.cloudflare.com/#workers-kv-namespace-remove-a-namespace

    Returns

    True if the namespace is successfully removed, false otherwise.

    Throws

    The Kv operation request is failed.

    Parameters

    • relativePathParam: { namespaceId: string }

      Parameters in the relative path

      • namespaceId: string

        The namespace identifier

    Returns Promise<boolean>

  • Modifies a namespace's title.

    Async

    Function

    renameNamespace

    See

    https://api.cloudflare.com/#workers-kv-namespace-rename-a-namespace

    Returns

    True if the namespace is successfully renamed, false otherwise.

    Throws

    The Kv operation request is failed.

    Parameters

    • relativePathParam: { namespaceId: string }

      Parameters in the relative path

      • namespaceId: string

        The namespace identifier

    • data: { title: string }

      Data for the HTTP body that will send to Cloudflare

      • title: string

        A human-readable string name for a Namespace.

    Returns Promise<boolean>

  • Write a value identified by a key. Use URL-encoding to use special characters (e.g. :, !, %) in the key name. Body should be the value to be stored. Existing values and expirations will be overwritten. If neither expiration nor expiration_ttl is specified, the key-value pair will never expire. If both are set, expiration_ttl is used and expiration is ignored.

    Async

    Function

    writeKeyValuePair

    See

    https://api.cloudflare.com/#workers-kv-namespace-write-key-value-pair

    Returns

    True if key is successfully modified or added, false otherwise.

    Throws

    The Kv operation request is failed.

    Parameters

    • relativePathParam: { keyName: string; namespaceId: string }

      Parameters in the relative path

      • keyName: string

        The name of the key

      • namespaceId: string

        The namespace identifier

    • value: string

      A UTF-8 encoded string to be stored, up to 10 MB in length.

    • Optional urlParam: { expiration?: number; expiration_ttl?: number }

      The parameters at the end of URL

      • Optional expiration?: number

        The time, measured in number of seconds since the UNIX epoch, at which the key should expire.

      • Optional expiration_ttl?: number

        The number of seconds for which the key should be visible before it expires. At least 60.

    Returns Promise<boolean>

  • Write a value identified by a key. Use URL-encoding to use special characters (e.g. :, !, %) in the key name. Body should be the value to be stored along with json metadata to be associated with the key/value pair. Existing values, expirations and metadata will be overwritten. If neither expiration nor expiration_ttl is specified, the key-value pair will never expire. If both are set, expiration_ttl is used and expiration is ignored.

    Async

    Function

    writeKeyValuePairMeta

    See

    https://api.cloudflare.com/#workers-kv-namespace-write-key-value-pair-with-metadata

    Returns

    True if key is successfully modified or added, false otherwise.

    Throws

    The Kv operation request is failed.

    Parameters

    • relativePathParam: { keyName: string; namespaceId: string }

      The parameters in the relative path

      • keyName: string

        The name of the key

      • namespaceId: string

        The namespace identifier

    • data: { metadata: { [key: string]: any }; value: string }

      The data that will send to Cloudflare

      • metadata: { [key: string]: any }

        Arbitrary JSON to be associated with a key/value pair

        • [key: string]: any
      • value: string

        A byte sequence to be stored, up to 10 MB in length.

    • Optional urlParam: { expiration?: number; expiration_ttl?: number }

      The parameters at the end of URL

      • Optional expiration?: number

        The time, measured in number of seconds since the UNIX epoch, at which the key should expire.

      • Optional expiration_ttl?: number

        The number of seconds for which the key should be visible before it expires. At least 60.

    Returns Promise<boolean>

  • Write multiple keys and values at once. Body should be an array of up to 10,000 key-value pairs to be stored, along with optional expiration information. Existing values and expirations will be overwritten. If neither expiration nor expiration_ttl is specified, the key-value pair will never expire. If both are set, expiration_ttl is used and expiration is ignored. The entire request size must be 100 megabytes or less.

    Async

    Function

    writeMultipleKeyValuePairs

    See

    https://api.cloudflare.com/#workers-kv-namespace-write-multiple-key-value-pairs

    Returns

    True if keys are successfully modified or added, false otherwise.

    Throws

    The Kv operation request is failed.

    Parameters

    • relativePathParam: { namespaceId: string }

      The parameters in the relative path

      • namespaceId: string

        The namespace identifier

    • data: { base64?: boolean; expiration?: number; expiration_ttl?: number; key: string; metadata?: { [key: string]: any }; value: string }[]

      The data that will send to Cloudflare

    Returns Promise<boolean>