@sagi.io/workers-kv is a Cloudflare Workers KV API for Node.js.
$ npm i @sagi.io/workers-kv
First, instantiate a WorkersKVREST instance:
const WorkersKVREST = require('@sagi.io/workers-kv')
const cfAccountId = process.env.CLOUDFLARE_ACCOUNT_ID;
const cfAuthKey = process.env.CLOUDFLARE_AUTH_KEY;
const cfEmail = process.env.CLOUDFLARE_EMAIL;
const WorkersKV = new WorkersKVREST({ cfAccountId, cfAuthKey, cfEmail })Then, access it's instance methods. For instance:
const namespaceId = '...'
const allKeys = await KV.listAllKeys({ namespaceId })We adhere to Cloudflare's Workers KV REST API.
Instantiates a WorkersKV object with the defined below methods.
Function definition:
const WorkersKVREST = function({
cfAccountId,
cfEmail,
cfAuthKey,
namespaceId = '',
}){ ... }Where:
cfAccountIdrequired Your Cloudflare account id.cfEmailrequired The email you registered with Cloudflare. Or, use the string "token" ifcfAuthKeyis actually an Auth Token.cfAuthKeyrequired Your Cloudflare Auth Key, or Auth Token (see above).namespaceIdoptional TheWorkers KVnamespace id. This argument is optional - either provide it here, or via the methods below.
Function definition:
const listKeys = async ({
namespaceId = '',
limit = MAX_KEYS_LIMIT,
cursor = undefined,
prefix = undefined,
} = {}) => { ... }Where:
namespaceIdoptional The namespace id (can also be provided while instantiatingWorkersKV).limitoptional 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.cursoroptional 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.prefixoptional 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.
Cursors through listKeys requests for you.
Function definition:
const listAllKeys = async ({
namespaceId = '',
prefix = undefined,
limit = MAX_KEYS_LIMIT,
} = {}) => { ... }Where:
namespaceIdoptional The namespace id (can also be provided while instantiatingWorkersKV).cursoroptional 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.prefixoptional 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.
Function definition:
const listNamespaces = async ({
page = 1,
per_page = 50,
} = {}) => { ... }Where:
pageoptional Page number of paginated results.per_pageoptional Maximum number of results per page.
Function definition:
const readKey = async ({
key,
namespaceId = '',
}) => { ... }Where:
keyrequired the key name.namespaceIdoptional The namespace id (can also be provided while instantiatingWorkersKV).
Function definition:
const deleteKey= async ({
key,
namespaceId = '',
}) => { ... }Where:
keyrequired the key name.namespaceIdoptional The namespace id (can also be provided while instantiatingWorkersKV).
Function definition:
const writeKey=> async ({
key,
value,
namespaceId = '',
expiration = undefined,
expiration_ttl = undefined,
}) => { ... }Where:
keyrequired A key's name. The name may be at most 512 bytes. All printable, non-whitespace characters are valid.valuerequired A UTF-8 encoded string to be stored, up to 2 MB in length.namespaceIdoptional Is the namespace id (can also be provided while instantiatingWorkersKV).expirationoptional The time, measured in number of seconds since the UNIX epoch, at which the key should expire.expiration_ttloptional The number of seconds for which the key should be visible before it expires. At least 60.
Function definition:
const writeMultipleKeys => async ({
keyValueMap,
namespaceId = '',
expiration = undefined,
expiration_ttl = undefined,
}) => { ... }Where:
keyValueMaprequired Is an object with string keys and values. e.g{ keyName1: 'keyValue1', keyName2: 'keyValue2' }namespaceIdoptional Is the namespace id (can also be provided while instantiatingWorkersKV).expirationoptional The time, measured in number of seconds since the UNIX epoch, at which the key should expire.expiration_ttloptional The number of seconds for which the key should be visible before it expires. At least 60.
Function definition:
const deleteMultipleKeys = async ({
keys,
namespaceId = '',
}) => { ... }Where:
keysrequired An array of keys to be deleted.namespaceIdoptional The namespace id (can also be provided while instantiatingWorkersKV).