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

@unikvs/redis.bun

How to use @unikvs/redis.bun, which stores byte sequences in Redis from Bun.

Overview

@unikvs/redis.bun is a storage plugin that stores byte sequences in Redis. Bun only Use it for caches or temporary data shared across nodes.

  • name is "Redis", and isOpen reports whether it is open.
  • open() connects, and close() disconnects when done.
  • This storage prefixes keys with "unikvs:" by default, limiting the scope of clear() deletions.
  • When the connection URL is omitted, the client resolves it from environment variables (REDIS_URL, VALKEY_URL).

Install it with the following command:

npm install @unikvs/redis.bun
pnpm add @unikvs/redis.bun
yarn add @unikvs/redis.bun
bun add @unikvs/redis.bun
nub add @unikvs/redis.bun
aube add @unikvs/redis.bun

A Redis server is required separately.

Usage

Redis implements IStorage. Pass it to UniKvs via persist().

import { Redis } from "@unikvs/redis.bun";

Redis

Storage that saves to Redis. Callers open it with open() and close it with close() when done.

The constructor arguments are as follows:

PropType
url?string | undefined

The connection URL. Resolved by the client from environment variables when omitted.

Typestring | undefined
options?RedisStorageOptions

Options configuring the client and this package's behavior.

TypeRedisStorageOptions

RedisStorageOptions

Bun RedisOptions plus this package’s own fields. Bun fields are passed through as-is.

PropType
keyPrefix?string

The prefix prepended to every key. Defaults to `"unikvs:"`. Specify an empty string to use keys as-is.

Typestring

The following example saves a value and reads it back:

import { Redis } from "@unikvs/redis.bun";
import * as unikvs from "unikvs";

const kvs = new unikvs.Builder<{ file: unikvs.Value<Uint8Array> }>()
  .persist(new Redis("redis://127.0.0.1:6379"))
  .build();

await kvs.open();

await kvs.set("file", new Uint8Array([0, 1, 2]));

const data = await kvs.get("file");
console.log(data); // Uint8Array(3) [0, 1, 2]

await kvs.close();

Data types

Operation Data type
write / read Uint8Array<ArrayBuffer>
getWritable / getReadable chunks Uint8Array<ArrayBuffer>
  • write / read use byte sequences (Uint8Array<ArrayBuffer>).
  • Supports streamed reading and writing with Uint8Array<ArrayBuffer> chunks.
  • Write streams append to a temporary key and atomically replace it with a rename on completion.
  • Read streams emit the fetched bytes as a single chunk.

Errors

This package has no package-specific errors. Reading a missing key throws the shared @unikvs/core error KeyNotFoundError, and opening it on a runtime other than Bun throws the shared error UnsupportedRuntimeError.

Was this page helpful?