multilevel
Expose a levelDB over the network, to be used by multiple processes, with levelUp's API.
Usage
Expose a db on the server:
var multilevel = ;var net = ;var level = ; var db = ; net;
And connect to it from the client:
var multilevel = ;var net = ; var db = multilevel;var con = net;con; // asynchronous methodsdb; // streamsdb;
Compatibility and binary data
multilevel works in the browser too - via browserify - and has full support for binary data. For getting a connection between browser and server I recommend either websocket-stream, which treats binary data well, or engine.io-stream, which has websocket fallbacks.
When using a binary capable transport, require multilevel like this:
var multilevel = ;
This way it uses msgpack-stream instead of json-buffer which uses way less bandwidth - but needs a binary capable transport.
Plugins
You can also expose custom methods and sublevels
with multilevel
!
When using plugins, you must generate a manifest and require it in the client.
Here's an example:
// server.js// create `db`var level = ;var multilevel = ;var db = ;// extend `db` with a foo(cb) methoddbmethods = dbmethods || {};dbmethods'foo' = type: 'async' ;db {;};// now write the manifest to a filemultilevel;// then expose `db` via shoe or any other streaming transport.var shoe = ;var sock =;sock;
Manifests are generated using level-manifest, which doesn't only support async functions but e.g. streams as well. For more, check its README.
Then require the manifest on the client when bundling with browserify or in any other nodejs compatible environment.
// client.js// instantiate a multilevel client with the `manifest.json` we just generatedvar multilevel = ;var manifest = ;var db = multilevel;// now pipe the db to the servervar stream = ;stream;// and you can call the custom `foo` method!db;
Authentication
You do not want to expose every database feature to every user, you might e.g. only want to provide read-only access to some users.
Auth controls may be injected when creating the server stream.
In this example, allow read only access, unless logged in as root.
//server.jsvar db = ; //all your database customizationsvar multilevel = ; //write out manifestmultilevel;
The client authorizes by calling the auth method.
var multilevel = ;var shoe = ;var stream = ;var db = multilevel;stream;db;
API
The exposed DB has the exact same API as levelUp, except
db#close()
closes the connection, instead of the database.- the synchronous versions of
db#isOpen()
anddb#isClose()
tell you if you currently have a connection to the remote db. - events, like
db.on("put", ...)
are not emitted. If you need updates, you can use level-live-stream.
multilevel.server(db[, authOpts])
Returns a server-stream that exposes db
, an instance of levelUp.
authOpts
is optional and should be of this form:
var authOpts ={//call back an error, if the user is not authorized.}{//throw if this user is not authorized for this action.}
It is necessary that you create one server stream per client.
multilevel.writeManifest(db, path)
Synchronoulsy write db
's manifest to path
.
Also returns the manifest as json.
var db = multilevel.client([manifest])
Return a new client db. manifest
may optionally be provided, which will
grant the client access to extensions and sublevels.
db.createRpcStream()
Pipe this into a server stream.
Listen for the error
event on this stream to handle / swallow reconnect events.
db.auth(data, cb)
Authorize with the server.
db.deauth (cb)
Deauthorize with the server.
Performance
On my macbook pro one multilevel server handles ~15k ops/s over a local tcp socket.
∴ : node indexjs writing "1234567890abcdef" 100 times native : multilevel direct : multilevel network : writing "1234567890abcdef" 1000 times native : multilevel direct : multilevel network : writing "1234567890abcdef" 10000 times native : multilevel direct : multilevel network : writing "1234567890abcdef" 100000 times native : multilevel direct : multilevel network :
Installation
With npm do:
$ npm install multilevel
Contributing
$ npm install$ npm test$ npm run test-browser
Contributors
Sponsors
This module is proudly supported by my Sponsors!
Do you want to support modules like this to improve their quality, stability and weigh in on new features? Then please consider donating to my Patreon. Not sure how much of my modules you're using? Try feross/thanks!
License
(MIT)
Copyright (c) 2013 Julian Gruber <julian@juliangruber.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.