Storage
gokv provides a low-latency coordination and consistent storage.
Initialize Storage
You can specify the namespace
for current application, default is default
.
import gokv from "https://deno.land/x/gokv@0.0.23/mod.ts";
const kv = gokv.Storage({ namespace: "xxx" });
Try online: https://dash.deno.com/playground/gokv-storage-example
Reading key-value pairs
await kv.get("foo"); // "bar"
// get multiple records
await kv.get(["foo", "baz"]); // { foo: "bar", baz: "qux" }
/*
By default, the system will pause delivery of I/O events
to the object while a storage operation is in progress,
in order to avoid unexpected race conditions.
Pass `allowConcurrency: true` to opt out of this behavior
and allow concurrent events to be delivered.
*/
await kv.get("foo", { allowConcurrency: true });
await kv.get(["foo", "baz"], { allowConcurrency: true });
Writing key-value pairs
await kv.put("foo", "bar");
// put multiple records
await kv.put({ foo: "bar", baz: "qux" });
/*
By default, the system will pause outgoing network messages
from the Durable Object until all previous writes have been
confirmed flushed to disk. Set `allowUnconfirmed:true` to
opt out of the default behavior.
*/
await kv.put("foo", "bar", { allowUnconfirmed: true });
Deleting key-value pairs
await kv.delete("foo");
// delete multiple records
await kv.delete(["foo", "baz"]);
// delete by condition
await kv.delete({ prefix: "user:1:", limit: 10 });
await kv.list({ start: "foo", end: "baz" });
/*
By default, the system will pause outgoing network messages
from the Durable Object until all previous writes have been
confirmed flushed to disk. Set `allowUnconfirmed:true` to
opt out of the default behavior.
*/
await kv.delete("foo", { allowUnconfirmed: true });
Listing records
// listing all records
await kv.list(); // { foo: "bar", baz: "qux" }
// listing by prefix
await kv.list({ prefix: "user:1:", limit: 10 });
// listing by key range
await kv.list({ start: "foo", end: "baz" });
// listing by reverse
await kv.list({ limit: 10, reverse: true });
// same as the option to `get()`, above.
await kv.list({ allowConcurrency: true });