KVReadModelStore
API reference for KVReadModelStore
Class: KVReadModelStore
Section titled “Class: KVReadModelStore”Cloudflare KV implementation of the IReadModelStore interface. Uses Cloudflare’s KV namespace to store, retrieve, delete, and list key-value pairs. Updated to use native batch operations for better performance.
Implements
Section titled “Implements”Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new KVReadModelStore(
namespace
,options
):KVReadModelStore
Creates a new KV read model store.
Parameters
Section titled “Parameters”namespace
Section titled “namespace”any
The Cloudflare KV namespace to use for storage
options
Section titled “options”Optional configuration for the store
defaultPrefix?
Section titled “defaultPrefix?”string
Optional prefix to apply to all keys This can be used to create virtual “tables” within a single KV namespace
Returns
Section titled “Returns”KVReadModelStore
Methods
Section titled “Methods”batchDelete()
Section titled “batchDelete()”batchDelete(
keys
,options?
):Promise
<boolean
[]>
Batch deletion of multiple items
Parameters
Section titled “Parameters”string
[]
options?
Section titled “options?”Returns
Section titled “Returns”Promise
<boolean
[]>
Implementation of
Section titled “Implementation of”batchGet()
Section titled “batchGet()”batchGet<
T
>(keys
,options?
):Promise
<object
[]>
Native batch retrieval of multiple items by their keys using KV’s new batch API This is much more efficient than individual get operations
Type Parameters
Section titled “Type Parameters”T
= unknown
Parameters
Section titled “Parameters”string
[]
Array of keys to retrieve (max 100 keys)
options?
Section titled “options?”Optional settings for the batch get operation
Returns
Section titled “Returns”Promise
<object
[]>
A promise that resolves with an array of key-value pairs
Implementation of
Section titled “Implementation of”batchGetWithMetadata()
Section titled “batchGetWithMetadata()”batchGetWithMetadata<
T
>(keys
,options?
):Promise
<object
[]>
Enhanced batch get with metadata support
Type Parameters
Section titled “Type Parameters”T
= unknown
Parameters
Section titled “Parameters”string
[]
options?
Section titled “options?”Returns
Section titled “Returns”Promise
<object
[]>
batchPut()
Section titled “batchPut()”batchPut<
T
>(items
,options?
):Promise
<void
>
Batch storage of multiple items
Type Parameters
Section titled “Type Parameters”T
= unknown
Parameters
Section titled “Parameters”object
[]
options?
Section titled “options?”WriteOptions
& ExpirationOptions
Returns
Section titled “Returns”Promise
<void
>
Implementation of
Section titled “Implementation of”delete()
Section titled “delete()”delete(
key
,options?
):Promise
<boolean
>
Deletes a value by its key.
Parameters
Section titled “Parameters”string
The key of the value to delete
options?
Section titled “options?”Optional settings for the delete operation
Returns
Section titled “Returns”Promise
<boolean
>
A promise that resolves to true if the key existed and was deleted, false otherwise
Implementation of
Section titled “Implementation of”get<
T
>(key
,options?
):Promise
<null
|T
>
Retrieves a value by its key.
Type Parameters
Section titled “Type Parameters”T
= unknown
Parameters
Section titled “Parameters”string
The key of the value to retrieve
options?
Section titled “options?”Optional settings for the retrieval operation
Returns
Section titled “Returns”Promise
<null
| T
>
A promise that resolves with the value, or null if not found
Implementation of
Section titled “Implementation of”getAll()
Section titled “getAll()”getAll<
T
>(options?
):Promise
<object
[]>
Retrieves all values, optionally filtered by a key prefix.
Type Parameters
Section titled “Type Parameters”T
= unknown
Parameters
Section titled “Parameters”options?
Section titled “options?”Optional settings for retrieving all values
Returns
Section titled “Returns”Promise
<object
[]>
A promise that resolves with an array of key-value pairs
Implementation of
Section titled “Implementation of”getCapabilities()
Section titled “getCapabilities()”getCapabilities():
StoreCapabilities
Returns enhanced capabilities for KV store
Returns
Section titled “Returns”Implementation of
Section titled “Implementation of”IReadModelStore
.getCapabilities
getNativeClient()
Section titled “getNativeClient()”getNativeClient<
C
>():C
Gets access to the native KV namespace
Type Parameters
Section titled “Type Parameters”C
= any
Returns
Section titled “Returns”C
Implementation of
Section titled “Implementation of”IReadModelStore
.getNativeClient
getWithMetadata()
Section titled “getWithMetadata()”getWithMetadata<
T
>(key
,options?
):Promise
<{metadata
:null
|Record
<string
,unknown
>;value
:null
|T
; }>
Helper method to add metadata support
Type Parameters
Section titled “Type Parameters”T
= unknown
Parameters
Section titled “Parameters”string
options?
Section titled “options?”Returns
Section titled “Returns”Promise
<{ metadata
: null
| Record
<string
, unknown
>; value
: null
| T
; }>
listKeys()
Section titled “listKeys()”listKeys(
options?
):Promise
<{cursor?
:string
;keys
:object
[];list_complete
:boolean
; }>
Lists keys in the store, optionally filtered by prefix.
Parameters
Section titled “Parameters”options?
Section titled “options?”Optional settings for the list operation
cursor?
Section titled “cursor?”string
limit?
Section titled “limit?”number
prefix?
Section titled “prefix?”string
tableName?
Section titled “tableName?”string
Returns
Section titled “Returns”Promise
<{ cursor?
: string
; keys
: object
[]; list_complete
: boolean
; }>
A promise that resolves with the list result containing keys and metadata
Implementation of
Section titled “Implementation of”put<
T
>(key
,value
,options?
):Promise
<void
>
Stores a value with the given key.
Type Parameters
Section titled “Type Parameters”T
= unknown
Parameters
Section titled “Parameters”string
The key under which to store the value
T
The value to store (will be serialized to JSON if it’s not a string or ArrayBuffer)
options?
Section titled “options?”WriteOptions
& ExpirationOptions
& MetadataOptions
Optional settings for the storage operation
Returns
Section titled “Returns”Promise
<void
>
A promise that resolves when the value has been stored
Implementation of
Section titled “Implementation of”query()
Section titled “query()”query<
T
>(options
):Promise
<object
[]>
Enhanced query with better prefix support
Type Parameters
Section titled “Type Parameters”T
= unknown
Parameters
Section titled “Parameters”options
Section titled “options”QueryOptions
& object
Returns
Section titled “Returns”Promise
<object
[]>