What is level-ws?
The level-ws npm package is a WriteStream implementation for LevelDB, which allows for efficient batch writing of data to a LevelDB database. It is particularly useful for handling large amounts of data that need to be written to the database in a streaming fashion.
What are level-ws's main functionalities?
Batch Writing
This feature allows you to write multiple key-value pairs to a LevelDB database in a batch. The code sample demonstrates how to create a WriteStream and write multiple entries to the database.
const level = require('level');
const levelws = require('level-ws');
const db = level('./mydb');
const ws = levelws(db);
ws.write({ key: 'name', value: 'Alice' });
ws.write({ key: 'age', value: '30' });
ws.end();
Stream Handling
This feature allows you to handle streams of data and pipe them directly into the LevelDB database. The code sample demonstrates how to create a readable stream and pipe it into the WriteStream.
const level = require('level');
const levelws = require('level-ws');
const { Readable } = require('stream');
const db = level('./mydb');
const ws = levelws(db);
const data = [
{ key: 'name', value: 'Alice' },
{ key: 'age', value: '30' }
];
const readable = Readable.from(data);
readable.pipe(ws);
Other packages similar to level-ws
levelup
LevelUP is a Node.js module that provides a higher-level API for interacting with LevelDB. It offers a more user-friendly interface compared to the lower-level LevelDB bindings. While level-ws focuses on WriteStream functionality, LevelUP provides a broader range of database operations including read, write, delete, and batch operations.
leveldown
LevelDOWN is the backend store for LevelDB in Node.js. It provides the low-level bindings to the LevelDB library. While level-ws is specifically for streaming writes, LevelDOWN offers the foundational operations for interacting with LevelDB, such as put, get, and del.
level
The level package is a convenience package that bundles LevelUP and LevelDOWN, providing a simple and easy-to-use interface for working with LevelDB. It offers a comprehensive set of features for database operations, including streaming, but with a more integrated approach compared to the specialized level-ws.
level-ws
A basic writable stream for abstract-level
databases, using Node.js core streams. This is not a high-performance stream. If benchmarking shows that your particular usage does not fit then try one of the alternative writable streams that are optimized for different use cases.
:pushpin: To instead write data using Web Streams, see level-web-stream
.
Usage
If you are upgrading: please see UPGRADING.md
.
const { Level } = require('level')
const WriteStream = require('level-ws')
const db = new Level('./db', { valueEncoding: 'json' })
const ws = new WriteStream(db)
ws.on('close', function () {
console.log('Done!')
})
ws.write({ key: 'alice', value: 42 })
ws.write({ key: 'bob', value: 7 })
ws.write({ type: 'del', key: 'tomas' })
ws.write({ type: 'put', key: 'sara', value: 16 })
ws.end()
API
ws = new WriteStream(db[, options])
Create a writable stream that operates in object mode, accepting batch operations to be committed with db.batch()
on each tick of the Node.js event loop. The optional options
argument may contain:
type
(string, default: 'put'
): default batch operation type if not set on indididual operations.maxBufferLength
(number, default Infinity
): limit the size of batches. When exceeded, the stream will stop processing writes until the current batch has been committed.highWaterMark
(number, default 16
): buffer level when stream.write()
starts returning false.
Contributing
Level/level-ws
is an OPEN Open Source Project. This means that:
Individuals making significant and valuable contributions are given commit-access to the project to contribute as they see fit. This project is more like an open wiki than a standard guarded open source project.
See the Contribution Guide for more details.
License
MIT