Skip to content
UniKVS
Esc
↑↓navigate↵open⌘Jpreview
On this page

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 with set() and get().
  • 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/memory
pnpm add unikvs @unikvs/memory
yarn add unikvs @unikvs/memory
bun add unikvs @unikvs/memory
nub add unikvs @unikvs/memory
aube add unikvs @unikvs/memory

Assemble 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.

Next Steps

Was this page helpful?