Memory-Level Database

3.1.0 · active · verified Wed Apr 22

memory-level is an in-memory database implementation that adheres to the `abstract-level` API specification, designed for both Node.js and browser environments. It is currently at version 3.1.0 and is actively maintained, with releases often coinciding with upgrades to its underlying `abstract-level` interface. This package serves as the modern successor to earlier in-memory Level implementations like `memdown` and `level-mem`. A key differentiator is its backing by a fully persistent red-black tree, providing reliable in-memory data storage that supports implicit and explicit snapshots. Unlike disk-backed databases, closing or reopening a `memory-level` instance does not affect its stored data, which is only discarded when all references to the database are released. It offers flexible `storeEncoding` options to optimize for Buffer, Uint8Array, or UTF8 string storage, impacting how data types are handled internally for consistency and performance.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates initializing an in-memory `MemoryLevel` database, performing basic `put`, `batch`, `get`, and `iterator` operations, and closing the database.

import { MemoryLevel } from 'memory-level'

async function runDbExample() {
  // Create a database with JSON value encoding
  const db = new MemoryLevel({ valueEncoding: 'json' })

  // Add an entry with key 'a' and value 1
  await db.put('a', 1)

  // Add multiple entries using a batch operation
  await db.batch([{ type: 'put', key: 'b', value: 2 }])

  // Get value of key 'a': 1
  const valueA = await db.get('a')
  console.log('Value of key "a":', valueA) // Expected: 1

  // Iterate entries with keys that are greater than 'a'
  console.log('Entries greater than "a":')
  for await (const [key, value] of db.iterator({ gt: 'a' })) {
    console.log('Key:', key, 'Value:', value) // Expected: Key: b Value: 2
  }

  // Clean up resources (optional for in-memory, but good practice)
  await db.close()
}

runDbExample().catch(console.error);

view raw JSON →