Upstash Redis LevelDB Implementation

1.1.1 · active · verified Wed Apr 22

upstash-redis-level is an `abstract-level` database implementation that utilizes @upstash/redis as its underlying data store. The `abstract-level` interface provides a common API for lexicographically sorted key-value databases, featuring support for encodings, sublevels, events, and hooks, enabling developers to swap out storage backends easily. This package, currently at version 1.1.1, integrates with Upstash's serverless, low-latency, HTTP/REST-based Redis offering, making it particularly well-suited for edge and serverless environments like Vercel, Cloudflare Workers, and Next.js where traditional TCP-based Redis connections are often suboptimal or unavailable. It differentiates itself by leveraging the connectionless nature of @upstash/redis to avoid common issues like connection limits in ephemeral environments.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize `upstash-redis-level` with an `@upstash/redis` client and perform basic `put`, `get`, and iteration operations. It highlights using environment variables for Redis credentials and the `namespace` option.

import { RedisLevel } from 'upstash-redis-level';
import { Redis } from '@upstash/redis';

const UPSTASH_REDIS_REST_URL = process.env.KV_REST_API_URL ?? 'http://localhost:8079';
const UPSTASH_REDIS_REST_TOKEN = process.env.KV_REST_API_TOKEN ?? 'example_token';

async function runRedisLevelExample() {
  if (!UPSTASH_REDIS_REST_URL || !UPSTASH_REDIS_REST_TOKEN) {
    console.error('Missing UPSTASH_REDIS_REST_URL or UPSTASH_REDIS_REST_TOKEN environment variables.');
    return;
  }

  // 1. Initialize the Upstash Redis client
  const redisClient = new Redis({
    url: UPSTASH_REDIS_REST_URL,
    token: UPSTASH_REDIS_REST_TOKEN,
  });

  // 2. Initialize the RedisLevel database
  const db = new RedisLevel({
    redis: redisClient,
    namespace: 'my-app-data',
    debug: true,
  });

  try {
    // 3. Perform basic operations
    await db.put('user:1', JSON.stringify({ name: 'Alice', age: 30 }));
    console.log('Put user:1');

    const user1 = await db.get('user:1');
    console.log('Got user:1:', user1); // Should output '{"name":"Alice","age":30}'

    await db.put('user:2', JSON.stringify({ name: 'Bob', age: 25 }));

    // 4. Iterate over entries
    console.log('\nIterating through users:');
    for await (const [key, value] of db.iterator({ gte: 'user:' })) {
      console.log(`${key}: ${value}`);
    }

    // 5. Delete an entry
    await db.del('user:1');
    console.log('\nDeleted user:1');

    const user1AfterDelete = await db.get('user:1');
    console.log('Got user:1 after delete:', user1AfterDelete); // Should be undefined

  } catch (error) {
    console.error('Error during RedisLevel operations:', error);
  } finally {
    // In real serverless functions, connections are often managed by the runtime.
    // Explicitly quitting might not be necessary or even desirable in some environments.
    // However, for local scripts, it's good practice to close connections.
    // (Note: @upstash/redis often manages its HTTP connections implicitly)
    // await redisClient.quit(); // @upstash/redis doesn't have a 'quit' method in this context.
  }
}

runRedisLevelExample();

view raw JSON →