Memory-Level Database
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
-
TypeError: MemoryLevel is not a constructor
cause Attempting to import MemoryLevel as a default export in ESM (e.g., `import MemoryLevel from 'memory-level'`) or incorrectly requiring in CommonJS (e.g., `const MemoryLevel = require('memory-level')`).fixUse named imports for ESM: `import { MemoryLevel } from 'memory-level'`. For CommonJS, use destructuring: `const { MemoryLevel } = require('memory-level')`. -
Error [ERR_REQUIRE_ESM]: require() of ES Module .../memory-level/index.js from ... not supported.
cause Attempting to `require()` memory-level in a CommonJS context when Node.js treats the package as an ES module, or vice-versa with `import` in a CommonJS file. This often occurs in mixed module environments.fixEnsure your project's module configuration (`package.json` `type` field or file extensions like `.mjs`/`.cjs`) aligns with how you are importing `memory-level`. For newer projects, consider using ESM throughout. If forced to mix, ensure correct imports for each module system. If using TypeScript, check `tsconfig.json`'s `moduleResolution` and `module` settings. -
db.get().on is not a function (or similar error on async methods)
cause Using older `abstract-level` callback/event-based patterns (e.g., from `leveldown` or `levelup`) with `memory-level` versions that have upgraded to a Promise-based `abstract-level` API (v2+).fixUpdate your code to use the modern Promise-based `async/await` syntax for `get`, `put`, `del`, `batch`, and `iterator` operations as demonstrated in the quickstart, in line with `abstract-level` v2 and v3.
Warnings
- breaking `memory-level` v3.0.0 upgraded to `abstract-level` v3, introducing breaking changes to the `abstract-level` API. Consult the `abstract-level` migration guide.
- breaking `memory-level` v2.0.0 upgraded to `abstract-level` v2, bringing breaking changes to the `abstract-level` API.
- breaking `memory-level` v2.0.0 dropped support for Node.js versions older than 18. Running on unsupported Node.js versions may lead to unexpected behavior or errors.
- gotcha When storing Buffer values, `memory-level` does not copy the Buffer data. Subsequent mutations to the original Buffer object will directly affect the data stored in the database.
- gotcha The `createIfMissing` and `errorIfExists` options, common in disk-backed `abstract-level` implementations, are not relevant for `memory-level` and will have no effect.
Install
-
npm install memory-level -
yarn add memory-level -
pnpm add memory-level
Imports
- MemoryLevel
import MemoryLevel from 'memory-level'
import { MemoryLevel } from 'memory-level' - MemoryLevel
const MemoryLevel = require('memory-level')const { MemoryLevel } = require('memory-level') - MemoryLevelOptions
import type { MemoryLevelOptions } from 'memory-level'
Quickstart
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);