Level Read Stream
level-read-stream provides Node.js-style readable streams for abstract-level compatible databases. It allows developers to consume data from a LevelDB-like database as a stream of entries, keys, or values. The current stable version is 2.0.0, which notably supports `abstract-level` v2 and Node.js >=18. The project maintains an active development pace with incremental releases addressing compatibility and adding features like TypeScript types. Key differentiators include its tight integration with the `abstract-level` API, offering `EntryStream`, `KeyStream`, and `ValueStream` for optimized data retrieval. For Web Streams, users are directed to `level-web-stream`.
Common errors
-
TypeError: db.iterator is not a function
cause Attempting to use `level-read-stream` v2.x with an `abstract-level` v1.x compatible database instance.fixUpgrade your database package (e.g., `level`, `memdown`) to a version that implements `abstract-level` v2 or higher. -
Error [ERR_REQUIRE_ESM]: require() of ES Module .../node_modules/level-read-stream/index.js from ... not supported.
cause Attempting to use `require()` to import `level-read-stream` in an environment configured for pure ESM, or when `level-read-stream` itself becomes pure ESM in a future major version (not currently true for v2).fixChange your import statement to `import { EntryStream } from 'level-read-stream';` and ensure your project is configured for ESM (e.g., `"type": "module"` in `package.json`). -
TypeError: EntryStream is not a constructor
cause Incorrect import statement when using CommonJS, attempting `const EntryStream = require('level-read-stream')` instead of destructuring.fixUse `const { EntryStream } = require('level-read-stream');` to correctly destructure the named export. -
TS2307: Cannot find module '@types/readable-stream' or its corresponding type declarations.
cause Missing TypeScript type definitions for the `readable-stream` package when using `level-read-stream` in a TypeScript project.fixInstall the types package: `npm install --save-dev @types/readable-stream`.
Warnings
- breaking Version 2.0.0 drops support for `abstract-level` v1. Users must upgrade their underlying `abstract-level` implementation to `>=2.0.0`.
- breaking Version 2.0.0 updated its internal `readable-stream` dependency from v3 to v4. While largely compatible, this might introduce subtle behavioral changes or require adjustments if your project relies on specific `readable-stream` v3 internals.
- gotcha For TypeScript users, in addition to installing `level-read-stream`, you must also install `@types/readable-stream` to get proper type definitions for Node.js's stream API.
- gotcha If migrating from older `levelup` or `level <= 7` versions, consult the `UPGRADING.md` file in the package's GitHub repository for specific migration steps and considerations.
- gotcha While `EntryStream` provides a stream interface, for consuming database entries using `for await...of` syntax, it is generally recommended to directly use `db.iterator()` as it offers a more idiomatic async iteration pattern.
Install
-
npm install level-read-stream -
yarn add level-read-stream -
pnpm add level-read-stream
Imports
- EntryStream
const EntryStream = require('level-read-stream').EntryStreamimport { EntryStream } from 'level-read-stream' - KeyStream
import KeyStream from 'level-read-stream'
import { KeyStream } from 'level-read-stream' - ValueStream
const { ValueStream } = require('level-read-stream')import { ValueStream } from 'level-read-stream'
Quickstart
import { EntryStream } from 'level-read-stream';
import { Writable, pipeline } from 'readable-stream';
import { Level } from 'level'; // Assuming 'level' or similar abstract-level implementation
async function runExample() {
const db = new Level('./mydb');
await db.put('a', '1');
await db.put('b', '2');
await db.put('c', '3');
const src = new EntryStream(db, { gte: 'b' });
const dst = new Writable({
objectMode: true,
write(entry, _, next) {
console.log('Entry: %s: %s', entry.key, entry.value);
next();
}
});
// Using pipeline with a callback for error handling and stream completion
pipeline(src, dst, (err) => {
if (err) {
console.error('Pipeline failed.', err);
} else {
console.log('Pipeline succeeded.');
}
db.close(); // Close the database after stream operations
});
}
runExample().catch(console.error);