level-ws
Version:
A basic writable stream for abstract-level databases
64 lines (41 loc) • 3.13 kB
Markdown
# level-ws
**A basic writable stream for [`abstract-level`](https://github.com/Level/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`](https://github.com/Level/web-stream).
[![level badge][level-badge]](https://github.com/Level/awesome)
[](https://www.npmjs.com/package/level-ws)
[](https://www.npmjs.com/package/level-ws)
[](https://github.com/Level/level-ws/actions/workflows/test.yml)
[](https://codecov.io/gh/Level/level-ws)
[](https://standardjs.com)
[](https://common-changelog.org)
[](https://opencollective.com/level)
## Usage
_If you are upgrading: please see [`UPGRADING.md`](UPGRADING.md)._
```js
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 })
// To delete entries, specify an explicit type
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](https://nodejs.org/dist/latest-v18.x/docs/api/stream.html#stream_class_stream_writable) 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`](https://github.com/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](https://github.com/Level/community/blob/master/CONTRIBUTING.md) for more details.
## License
[MIT](LICENSE)
[level-badge]: https://leveljs.org/img/badge.svg