Level Universal Database

10.0.0 · active · verified Tue Apr 21

The `level` package provides a universal, `abstract-level`-compliant key-value database interface for both Node.js and browser environments. It acts as a convenience wrapper, dynamically exporting `classic-level` for Node.js and `browser-level` for browsers. This design allows developers to write platform-agnostic code for lexicographically sorted key-value stores. The current stable version is 10.0.0, which incorporates `abstract-level` v3, enhancing its foundational API. The project maintains a frequent release cadence, often introducing breaking changes with major version bumps to align with upstream `abstract-level`, `classic-level`, or `browser-level` updates, emphasizing modernization and API consistency across its ecosystem. It is designed to be highly extensible and supports various encoding options, including `json` for complex data structures and `utf8` for strings.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates creating a database, performing `put` and `batch` operations, retrieving data with `get`, iterating over entries, and using `sublevel` for nested data, all with TypeScript type safety. It also includes database opening and closing.

import { Level } from 'level';

// Specify types of keys and values for better type safety.
// The generic type parameters default to Level<string, string>.
// Use { valueEncoding: 'json' } for objects, otherwise 'utf8' is common.
const db = new Level<string, any>('./my-database', { valueEncoding: 'json' });

async function runExample() {
  try {
    // Ensure the database is open before operations
    if (db.status === 'closed') {
      await db.open();
    }

    // Add an entry with key 'user:1' and a JSON object value
    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: 24 } },
      { type: 'put', key: 'user:3', value: { name: 'Charlie', age: 35 } }
    ]);
    console.log('Added user:2 and user:3');

    // Get value of key 'user:1'
    const user1 = await db.get('user:1');
    console.log('Value of user:1:', user1);

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

    // Example of a sublevel
    const userEmails = db.sublevel<string, string>('emails', { valueEncoding: 'utf8' });
    await userEmails.put('alice', 'alice@example.com');
    const aliceEmail = await userEmails.get('alice');
    console.log('Alice email:', aliceEmail);

  } catch (error) {
    console.error('Database operation failed:', error);
  } finally {
    // Always close the database when done
    if (db.status === 'open') {
      await db.close();
      console.log('Database closed.');
    }
  }
}

runExample();

view raw JSON →