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 levelup = var db = net
and connect to it from the client:
var multilevel = var net = var db = multilevelvar con = netcon // asynchronous methodsdb // streamsdb
Compatibility
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 websocket-stream, which treats binary data well.
plugins
You can also expose custom methods and sublevels
with multilevel
!
When using plugins, you must generate a manifest with level-manifest and require it in the client.
Here's an example:
// server.js// create `db`var levelup = ;var db = ;// extend `db` with a foo(cb) methoddbmethods = dbmethods || {};dbmethods'foo' = type: 'async' ;db {;};// now write the manifest to a filevar fs = ;var createManifest = ;fs;// then expose `db` via shoe or any other streaming transport.var shoe = ;var sock =;sock;
level-manifest 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, yet, you may want to provide some read-only access, or something.
Auth controls may be injected when creating the server stream.
Allow read only access, unless logged in as root.
//server.jsvar db = //all your database customizationsvar fs =var createManifest =//write out manifestfs
The client authorizes by calling the auth method.
var stream =var db = multilevelstreamdb
API
The exposed DB has the exact same API as
levelUp.
Except that close
closes the connection, instead of the database.
isOpen
and isClose
tell you if you currently have a connection
to the remote db.
multilevel.server(db, authOpts?)
Returns a server-stream that exposes db
, an instance of levelUp.
authOpts
is optional, it should match this:
var authOpts ={//call back an error, if the user is not authorized.}{//throw if this user is not authorized for this action.}
var db = multilevel.client(manifest?)
Return a new client db.
manifest
may be optionally be provided,
which will allow client access to extensions.
db.createRpcStream()
Pipe this into a server stream.
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
npm install multilevel
Contributing
$ npm install$ npm test
Contributors
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.