Classic LevelDB Database

3.0.0 · active · verified Wed Apr 22

classic-level is an `abstract-level` compliant database implementation backed by LevelDB, serving as the successor to `leveldown`. It offers features such as built-in encodings, sublevels, events, hooks, and first-class support for `Uint8Array`. The current stable version is 3.0.0, with a release cadence that has seen several minor and major updates in the past year, indicating active maintenance. Key differentiators include its adherence to the `abstract-level` interface, providing a consistent API across various Level-family databases, and its direct use of the battle-tested LevelDB C++ library for high performance. It also ships with TypeScript type definitions, making it suitable for modern JavaScript and TypeScript projects. It supports Node.js versions >=18 and Electron >=30, providing prebuilt binaries for common platforms.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to initialize a `ClassicLevel` database, perform basic `put`, `batch`, `get`, and `iterator` operations, and handle database opening and closing. It uses ESM syntax and includes a cleanup step for repeated execution.

import { ClassicLevel } from 'classic-level';
import { rmSync } from 'node:fs';

const dbPath = './my-classic-level-db';

async function runExample() {
  // Clean up previous run if any
  try { rmSync(dbPath, { recursive: true, force: true }); } catch (e) {}

  // Create a database instance with JSON value encoding
  const db = new ClassicLevel(dbPath, { valueEncoding: 'json' });

  try {
    // Open the database
    await db.open();
    console.log('Database opened.');

    // Add a single entry
    await db.put('user:1', { name: 'Alice', age: 30 });
    console.log('Added user:1');

    // Add multiple entries using batch operation
    await db.batch([
      { type: 'put', key: 'user:2', value: { name: 'Bob', age: 25 } },
      { type: 'put', key: 'user:3', value: { name: 'Charlie', age: 35 } }
    ]);
    console.log('Added user:2 and user:3 via batch');

    // Retrieve a value
    const user1 = await db.get('user:1');
    console.log('Retrieved user:1:', user1);

    // Iterate over entries with keys greater than 'user:1'
    console.log('Iterating users > user:1:');
    for await (const [key, value] of db.iterator({ gt: 'user:1' })) {
      console.log(`  Key: ${key}, Value:`, value);
    }

  } catch (error) {
    console.error('An error occurred:', error);
  } finally {
    // Close the database
    await db.close();
    console.log('Database closed.');
  }
}

runExample();

view raw JSON →