{"id":16102,"library":"level","title":"Level Universal Database","description":"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.","status":"active","version":"10.0.0","language":"javascript","source_language":"en","source_url":"https://github.com/Level/level","tags":["javascript","level","leveldb","stream","database","db","store","storage","json","typescript"],"install":[{"cmd":"npm install level","lang":"bash","label":"npm"},{"cmd":"yarn add level","lang":"bash","label":"yarn"},{"cmd":"pnpm add level","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Provides the underlying LevelDB binding for Node.js environments. Automatically used when `level` is run in Node.js.","package":"classic-level","optional":false},{"reason":"Provides the underlying IndexedDB/Web SQL binding for browser environments. Automatically used when `level` is run in a browser.","package":"browser-level","optional":false},{"reason":"Defines the abstract interface that `level` implements and exposes, ensuring consistent API across different backend implementations.","package":"abstract-level","optional":false}],"imports":[{"note":"`Level` is a named export, not a default export. For TypeScript, remember to specify generic parameters, e.g., `Level<string, any>`.","wrong":"import Level from 'level'","symbol":"Level","correct":"import { Level } from 'level'"},{"note":"Even in CommonJS, `Level` is destructured from the module export.","wrong":"const Level = require('level')","symbol":"Level (CommonJS)","correct":"const { Level } = require('level')"},{"note":"While `level` implements `AbstractLevel`, the type definition itself is exported by `abstract-level`.","wrong":"import { AbstractLevel } from 'level'","symbol":"AbstractLevel (type)","correct":"import type { AbstractLevel } from 'abstract-level'"}],"quickstart":{"code":"import { Level } from 'level';\n\n// Specify types of keys and values for better type safety.\n// The generic type parameters default to Level<string, string>.\n// Use { valueEncoding: 'json' } for objects, otherwise 'utf8' is common.\nconst db = new Level<string, any>('./my-database', { valueEncoding: 'json' });\n\nasync function runExample() {\n  try {\n    // Ensure the database is open before operations\n    if (db.status === 'closed') {\n      await db.open();\n    }\n\n    // Add an entry with key 'user:1' and a JSON object value\n    await db.put('user:1', { name: 'Alice', age: 30 });\n    console.log('Added user:1');\n\n    // Add multiple entries using batch operation\n    await db.batch([\n      { type: 'put', key: 'user:2', value: { name: 'Bob', age: 24 } },\n      { type: 'put', key: 'user:3', value: { name: 'Charlie', age: 35 } }\n    ]);\n    console.log('Added user:2 and user:3');\n\n    // Get value of key 'user:1'\n    const user1 = await db.get('user:1');\n    console.log('Value of user:1:', user1);\n\n    // Iterate entries with keys greater than 'user:1'\n    console.log('Users greater than user:1:');\n    for await (const [key, value] of db.iterator({ gt: 'user:1' })) {\n      console.log(`  Key: ${key}, Value:`, value);\n    }\n\n    // Example of a sublevel\n    const userEmails = db.sublevel<string, string>('emails', { valueEncoding: 'utf8' });\n    await userEmails.put('alice', 'alice@example.com');\n    const aliceEmail = await userEmails.get('alice');\n    console.log('Alice email:', aliceEmail);\n\n  } catch (error) {\n    console.error('Database operation failed:', error);\n  } finally {\n    // Always close the database when done\n    if (db.status === 'open') {\n      await db.close();\n      console.log('Database closed.');\n    }\n  }\n}\n\nrunExample();","lang":"typescript","description":"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."},"warnings":[{"fix":"Consult the `UPGRADING.md` file in the `level` repository and the `abstract-level` documentation for v3 changes. Update `abstract-level` specific code or options.","message":"Version 10.0.0 introduced a breaking change by upgrading to `abstract-level` v3. This may require changes to code interacting directly with the `abstract-level` API or relying on specific behaviors of earlier versions.","severity":"breaking","affected_versions":">=10.0.0"},{"fix":"Refer to the `UPGRADING.md` for v9 and the `abstract-level` v2 documentation. Key changes involved database options and some method signatures.","message":"Version 9.0.0 included a breaking change by upgrading to `abstract-level` v2. This involved significant API changes in how `abstract-level` databases operate and are instantiated.","severity":"breaking","affected_versions":">=9.0.0 <10.0.0"},{"fix":"Migrate any direct `leveldown` interactions to the `abstract-level` interface provided by `level`. Ensure `valueEncoding` and other options are compatible with the new backend implementations. Review `UPGRADING.md` for v8.","message":"Version 8.0.0 fundamentally changed `level` to use `classic-level` in Node.js and `browser-level` in browsers. If your application previously relied on specific `leveldown` behaviors or direct `leveldown` imports, this will be a breaking change.","severity":"breaking","affected_versions":">=8.0.0"},{"fix":"Review the `UPGRADING.md` for v7. Ensure your Node.js environment meets the minimum requirement (Node.js 18+ for current versions). Check for API changes in `level-js` if targeting browsers.","message":"Version 7.0.0 included multiple breaking changes, including bumps to `leveldown` and `level-packager`, and a major upgrade of `level-js` from 5.x to 6.x. It also modernized syntax, potentially affecting older Node.js environments.","severity":"breaking","affected_versions":">=7.0.0 <8.0.0"},{"fix":"Always use `const { Level } = require('level')` or `import { Level } from 'level'`.","message":"Incorrectly using `require('level')` without destructuring `Level` can lead to runtime errors, as `level` exports `Level` as a named property.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Explicitly define key and value types, e.g., `new Level<string, any>('./db', { valueEncoding: 'json' })` or `db.sublevel<string, number>('counts')`.","message":"When using TypeScript, omitting generic type parameters for `Level` or `sublevel` will default to `Level<string, string>`, leading to type errors if you store or retrieve non-string values (e.g., objects with `valueEncoding: 'json'`).","severity":"gotcha","affected_versions":"All versions with TypeScript"}],"env_vars":null,"last_verified":"2026-04-21T00:00:00.000Z","next_check":"2026-07-20T00:00:00.000Z","problems":[{"fix":"Ensure you are destructuring the `Level` class from the `level` module: `const { Level } = require('level')` or `import { Level } from 'level'`.","cause":"Attempting to instantiate `level` with `new require('level')()` instead of `new Level()` after destructuring.","error":"TypeError: Level is not a constructor"},{"fix":"Always call `await db.open()` before performing any operations. Check `db.status` to ensure it's `open`.","cause":"Attempting database operations (like `put`, `get`, `iterator`) before the database has been successfully opened.","error":"Error: Database is not open"},{"fix":"Explicitly define the value type when creating the `Level` instance: `new Level<string, any>('./db', { valueEncoding: 'json' })` if storing objects, or `new Level<string, Buffer>('./db', { valueEncoding: 'binary' })` for binary data.","cause":"In TypeScript, the `Level` instance was inferred or explicitly typed as `Level<string, string>`, but you are attempting to store values that are not strings (e.g., objects with `valueEncoding: 'json'`).","error":"Property 'put' does not exist on type 'Level<string, string>'"}],"ecosystem":"npm"}