SQLite-backed Abstract LevelDB

1.2.1 · active · verified Wed Apr 22

sqlite-level is a Node.js library that provides an `abstract-level` compliant interface, backed by a SQLite database. It leverages `better-sqlite3` for its underlying SQLite operations, offering a synchronous-like API while maintaining `abstract-level` compatibility. This allows developers familiar with the LevelDB API to utilize a durable, file-based store without needing to manage separate database processes or complex setups typical of other embedded databases. Currently stable at version 1.2.1, its release cadence is tied to the needs of the TinaCMS project, ensuring ongoing maintenance and updates. A key differentiator is its use of SQLite, offering ACID properties and wide tooling support, contrasting with LevelDB's LSM-tree design. It's particularly useful for server-side applications requiring a lightweight, transactional key-value store with data persistence for development or smaller deployments.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates opening a SQLite-backed LevelDB, performing basic put, get, delete operations, and iterating over stored keys and values, ensuring proper database closure.

import { Level } from 'sqlite-level';
import { join } from 'path';
import { tmpdir } from 'os';

async function runExample() {
  const dbPath = join(tmpdir(), 'my_sqlite_level_db.sqlite');
  const db = new Level(dbPath);

  try {
    console.log(`Opening database at: ${dbPath}`);
    await db.open();
    console.log('Database opened.');

    await db.put('name', 'Alice');
    await db.put('age', '30');

    const name = await db.get('name');
    const age = await db.get('age');
    console.log(`Name: ${name}, Age: ${age}`);

    // Iterate over entries
    console.log('Iterating over entries:');
    for await (const [key, value] of db.iterator({ gte: 'a', lt: 'z' })) {
      console.log(`  Key: ${key}, Value: ${value}`);
    }

    await db.del('age');
    const newAge = await db.get('age');
    console.log(`Age after deletion: ${newAge}`); // Should be undefined
  } catch (error) {
    console.error('An error occurred:', error);
  } finally {
    if (!db.isClosed()) {
      await db.close();
      console.log('Database closed.');
    }
  }
}

runExample();

view raw JSON →