Your First UniKVS
Store and read back values with UniKVS using in-memory storage.
In this tutorial, you will store and read back values using in-memory storage. It takes about 10 minutes. Set the internals aside and build something working first.
Goal
When you finish, you will be able to:
- Assemble a client with
Builder. - Initialize with
open()and read/write withset()andget(). - Clean up with
close().
Prerequisites
- A working Node.js environment.
- A TypeScript project.
Steps
Install packages
Install unikvs and @unikvs/memory.
npm install unikvs @unikvs/memorypnpm add unikvs @unikvs/memoryyarn add unikvs @unikvs/memorybun add unikvs @unikvs/memorynub add unikvs @unikvs/memoryaube add unikvs @unikvs/memoryAssemble the client
Pass in-memory storage to Builder to create a client. Here the key greeting stores a string.
import { Memory } from "@unikvs/memory";
import * as unikvs from "unikvs";
const kvs = new unikvs.Builder<{
greeting: unikvs.PlainValue<string>;
}>()
.persist(new Memory())
.build();Store and read back
Initialize with open(), then run set() and get(). Close with close() when done.
await kvs.open();
await kvs.set("greeting", "hello");
const value = await kvs.get("greeting");
console.log(value); // "hello"
await kvs.close();Review
The resulting setup looks like this:
persist()registers a backend.build()creates the client.open()is required before reading and writing.