GitXplorerGitXplorer
n

storage-kv

public
17 stars
1 forks
22 issues

Commits

List of commits on branch master.
Verified
96bdfa4a88b4f5fdb53f50b1b7ce313d689a909d

Merge pull request #13 from nickbalestra/updade-deps

nnickbalestra committed 5 years ago
Unverified
3f787c6869e034d21c957355cf184cacede778dd

Bumped version

committed 5 years ago
Unverified
335c6e27cf6df8ffeb1a7db72756465fde3068f5

Updated deps

committed 5 years ago
Verified
7254c741aea70485a099cbd4ec5efa81e7f047cf

Merge pull request #12 from robertcepa/master

nnickbalestra committed 5 years ago
Unverified
36b13973e81a549c9e804a558bbe184cd6990b52

Remove engines requirement, bump the version

rrobertcepa committed 5 years ago
Verified
78f9e4d2114bdba34001cd0e019ed2c2c51bbac7

Merge pull request #4 from nickbalestra/updated-axios

nnickbalestra committed 6 years ago

README

The README file for this repository.

storage-kv

Node.js client for Cloudflare's KV Storage: a global, highly distributed, low-latency, key-value data store.

Workers KV is a highly distributed, eventually consistent, key-value store that spans Cloudflare's global edge. It allows you to store billions of key-value pairs and read them with ultra-low latency anywhere in the world. Now you can build entire applications with the performance of a CDN static cache.

This project follows the std:kv-storage specs.

⚠️ Early preview release (0.0.7) - Don't use in production.

Usage

Install

yarn add storage-kv
const { StorageArea } = require("storage-kv");

Configure

Creates a new StorageArea that provides an async key/value store view onto a Cloudflare KV namespace.

const storage = new StorageArea("cats");

This does not actually fetch or create the namespace yet; that is done lazily when other methods are called. This means that all other methods can reject with namespace-related exceptions in failure cases.

Options

You can specify in the options the path to Cloudflare credentials file:

const storage = new StorageArea("cats", {
  keyFilename: "/path/to/keyFilename.json"
});

or passing the credentials directly

const storage = new StorageArea("cats", { credentials: { id, email, key } });

If none is given it will look for credential CF_KEY, CF_ID, CF_EMAIL or CF_KEYFILENAME in global env, falling back to "./cf-credentials.json".

Storing values

Asynchronously stores the given value so that it can later be retrieved by the given key. Values types are automatically inferred and can be of type [String, ReadableStream, ArrayBuffer, FormData]. The returned promise will fulfill with undefined on success.

await storage.set("one-cat", "birman");

Options

Keys can be set to expire:

  • exp: seconds since epoch
  • ttl: seconds from now
await storage.set("one-cat", "birman", { exp: 1558853089 });
await storage.set("another-cat", "merican curl", { ttl: 60 });

Retrieving values

Asynchronously retrieves the value stored at the given key, or undefined if there is no value stored at key.

const cat = await storage.get("one-cat");
console.assert(cat === "birman");

Deleting single values

Asynchronously deletes the entry at the given key. This is equivalent to storage.set(key, undefined). The returned promise will fulfill with undefined on success.

await storage.delete("one-cat");

Deleting all values

Asynchronously deletes all entries in this storage area. This is done by actually deleting the underlying namespace. The returned promise will fulfill with undefined on success.

await storage.clear();

Retrieving all keys

Retrieves an async iterator containing the keys of all entries in this storage area. Keys will be yielded in ascending order;

for await (const key of storage.keys()) {
  console.log(key);
}

// "one-cat"
// "another-cat"

Retrieving all values

Retrieves an async iterator containing the values of all entries in this storage area. Values will be ordered as corresponding to their keys; see keys().

for await (const value of storage.values()) {
  console.log(value);
}

// "birman"
// "american curl"

Retrieving all entries

Retrieves an async iterator containing the [key, value] of all entries in this storage area. Entries will be ordered as corresponding to their keys; see keys().

for await (const [key, value] of storage.entries()) {
  console.log(key, value);
}

// "one-cat", "birman"
// "another-cat", "american curl"