level-rpc-stream
Control a level db over a duplex stream. If you're looking for a full network server/client leveldb solution check out level-rpc-server.
Installation
npm i --save level-rpc-stream
Usage
Examples
Create a LevelRPCStream
const db = ... // any level db like require('level')('foo') // create a duplex rpc stream for interacting with levelconst stream =
Perform Level Operations
get
request
Send a To perform a get operation on level db using level-rpc-stream, you have to write an operation object (containing an id, operation, and args) to the stream. This will invoke the equivalent method on the level db instance, with the passed args.
// Get key data using stream, equivalent to `db.get('key')`stream
get
request full example
Send a To receive a response from the level-rpc-stream you have to pipe to demux
. demux
splits the level-rpc-stream's readable stream into multiple substreams. All responses from leveldb promise operations are sent over the "response substream".
const db = ... // any level dbconst stream = // perform a `get` operation and recieve a responseconst requestId = // receive response data over the level-rpc-stream substreamsstream // Get key data using stream, equivalent to `db.get('key')`stream
createReadStream
request full example
Send a LevelDB supports streaming operations like createReadStream
, createKeyStream
, and createValueStream
. level-rpc-stream supports streaming responses by using substreams. Each streaming response will create a new substream over the level-rpc-stream readable stream.
const db = ... // any level dbconst stream = // perform a `createReadStream` operation and recieve it's stream responseconst requestId = // receive response data over the level-rpc-stream substreamsstream // Write `createReadStream` operationstream
Documentation
createLevelStream(leveldb)
createLevelStream
is the default export of this module. It accepts a leveldb instance and returns a level-rpc-stream. levelStream
(level-rpc-stream instance), is a duplex stream. The writable stream is an object stream that accepts operations as objects, that invoke methods on leveldb instance. The readable stream is the multiplexed stream (has substreams) where responses and streams from level are read.
levelStream.write
levelStream.write
is used to send operations to leveldb. The supported operations are PUT, GET, DEL, BATCH, RSTREAM (createReadStream), KSTREAM (createKeyStream), VSTREAM (createValueStream), and DSTREAM (allows you to destroy a level stream by id). All of these operations are formatted like { id, op, args }
. id
is the request id, op
is the operation (use OPERATIONS), and args
are the arguments passed to the leveldb associated method.
Operation Requests Examples
// PUT operationlevelStream // GET operationlevelStream // DEL operationlevelStream // BATCH operationlevelStream // BATCH operationlevelStream
Stream Requests Examples
For stream options checkout: levelup's docs
// RSTREAM operationlevelStream // KSTREAM operationlevelStream // VSTREAM operationlevelStream
demux
levelStreams
readable stream is a multiplexed stream (has substreams). All operation request responses are can be read from the "response substream". The response substream's id is exported as RESPONSE_SUBSTREAM_ID
. Stream operations (like RSTREAM) will create new substreams on the readable stream. Check out the examples above "Send a get
request and recieve the response" and "Send a createReadStream
request full example".
License
MIT