{"id":16433,"library":"memory-level","title":"Memory-Level Database","description":"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.","status":"active","version":"3.1.0","language":"javascript","source_language":"en","source_url":"https://github.com/Level/memory-level","tags":["javascript","level","leveldb","leveldown","levelup","memory","typescript"],"install":[{"cmd":"npm install memory-level","lang":"bash","label":"npm"},{"cmd":"yarn add memory-level","lang":"bash","label":"yarn"},{"cmd":"pnpm add memory-level","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Core API interface that memory-level implements. Understanding abstract-level's API is crucial for using memory-level.","package":"abstract-level","optional":false},{"reason":"Underlying data structure used for efficient in-memory storage.","package":"functional-red-black-tree","optional":false}],"imports":[{"note":"MemoryLevel is a named export. Attempting to import it as a default export (without curly braces) will result in an error. The package ships TypeScript types.","wrong":"import MemoryLevel from 'memory-level'","symbol":"MemoryLevel","correct":"import { MemoryLevel } from 'memory-level'"},{"note":"For CommonJS, MemoryLevel is a named export from the module object. Directly requiring the module and expecting the class to be the default export will fail.","wrong":"const MemoryLevel = require('memory-level')","symbol":"MemoryLevel","correct":"const { MemoryLevel } = require('memory-level')"},{"note":"Type import for configuration options when using TypeScript.","symbol":"MemoryLevelOptions","correct":"import type { MemoryLevelOptions } from 'memory-level'"}],"quickstart":{"code":"import { MemoryLevel } from 'memory-level'\n\nasync function runDbExample() {\n  // Create a database with JSON value encoding\n  const db = new MemoryLevel({ valueEncoding: 'json' })\n\n  // Add an entry with key 'a' and value 1\n  await db.put('a', 1)\n\n  // Add multiple entries using a batch operation\n  await db.batch([{ type: 'put', key: 'b', value: 2 }])\n\n  // Get value of key 'a': 1\n  const valueA = await db.get('a')\n  console.log('Value of key \"a\":', valueA) // Expected: 1\n\n  // Iterate entries with keys that are greater than 'a'\n  console.log('Entries greater than \"a\":')\n  for await (const [key, value] of db.iterator({ gt: 'a' })) {\n    console.log('Key:', key, 'Value:', value) // Expected: Key: b Value: 2\n  }\n\n  // Clean up resources (optional for in-memory, but good practice)\n  await db.close()\n}\n\nrunDbExample().catch(console.error);","lang":"typescript","description":"Demonstrates initializing an in-memory `MemoryLevel` database, performing basic `put`, `batch`, `get`, and `iterator` operations, and closing the database."},"warnings":[{"fix":"Review the `abstract-level` v3 migration guide for updated API signatures and behaviors. Update your database operations accordingly.","message":"`memory-level` v3.0.0 upgraded to `abstract-level` v3, introducing breaking changes to the `abstract-level` API. Consult the `abstract-level` migration guide.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Refer to the `abstract-level` v2 migration guide for necessary changes in your application's database interactions.","message":"`memory-level` v2.0.0 upgraded to `abstract-level` v2, bringing breaking changes to the `abstract-level` API.","severity":"breaking","affected_versions":">=2.0.0 <3.0.0"},{"fix":"Upgrade your Node.js environment to version 18 or newer to ensure compatibility and stability.","message":"`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.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"If immutability is required for Buffer values, explicitly clone the Buffer before storing it (e.g., `db.put(key, Buffer.from(myBuffer))`).","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"These options can be safely omitted or ignored when creating a `MemoryLevel` instance, as the in-memory nature makes them superfluous.","message":"The `createIfMissing` and `errorIfExists` options, common in disk-backed `abstract-level` implementations, are not relevant for `memory-level` and will have no effect.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"fix":"Use named imports for ESM: `import { MemoryLevel } from 'memory-level'`. For CommonJS, use destructuring: `const { MemoryLevel } = require('memory-level')`.","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')`).","error":"TypeError: MemoryLevel is not a constructor"},{"fix":"Ensure 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.","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.","error":"Error [ERR_REQUIRE_ESM]: require() of ES Module .../memory-level/index.js from ... not supported."},{"fix":"Update 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.","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+).","error":"db.get().on is not a function (or similar error on async methods)"}],"ecosystem":"npm"}