npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

level-proxy

v1.1.2

Published

proxy a leveldb reference to swap instances transparently

Downloads

14

Readme

level-proxy

proxy a leveldb reference so you can swap backend instances on the fly

build status

This is useful for modules that need to switch out references transparently, like automatically upgrading an ordinary vanilla reference into a multilevel handle. You could probably also use this module to implement even crazier things, like a transparent hash ring.

example

In this example, we'll create 2 db handles proxied by the level-proxy handle db: a and b. The handles will swap into being the active handle every 3 seconds.

var level = require('level');
var levelSwap = require('level-proxy');

var a = level('/tmp/db-a');
var b = level('/tmp/db-b');
var db = levelSwap(a);

var n = 0;
setInterval(function () { db.put('x', n++) }, 250);

setInterval(function () {
    a.get('x', function (err, x) { console.log('a.x=', x) });
    b.get('x', function (err, x) { console.log('b.x=', x) });
}, 1000);

var index = 0;
setInterval(function () {
    db.swap([ a, b ][++ index % 2]);
}, 3000);

output:

$ node example/swap.js
a.x= 2
b.x= 22
a.x= 6
b.x= 22
a.x= 10
b.x= 22
a.x= 10
b.x= 14
a.x= 10
b.x= 18
^C

You can proxy streaming methods with buffering too:

var level = require('level');
var levelProxy = require('level-proxy');

var a = level('/tmp/stream-a');
var b = level('/tmp/stream-b');

a.batch([
    { type: 'put', key: 'a', value: 3 },
    { type: 'put', key: 'b', value: 4 },
    { type: 'put', key: 'c', value: 5 },
]);

b.batch([
    { type: 'put', key: 'x', value: 7 },
    { type: 'put', key: 'y', value: 8 },
    { type: 'put', key: 'z', value: 9 },
]);

var db = levelProxy();

setInterval(function () {
    var s = db.createKeyStream();
    var keys = [];
    s.on('data', function (key) { keys.push(key) })
    s.on('end', function () { console.log(keys) });
}, 1000);

var index = 0;
setInterval(function () {
    db.swap([ a, b ][++ index % 2]);
}, 3000);

output:

$ node example/stream.js
[ 'x', 'y', 'z' ]
[ 'x', 'y', 'z' ]
[ 'x', 'y', 'z' ]
[ 'x', 'y', 'z' ]
[ 'x', 'y', 'z' ]
[ 'a', 'b', 'c' ]
[ 'a', 'b', 'c' ]
[ 'a', 'b', 'c' ]
[ 'x', 'y', 'z' ]
[ 'x', 'y', 'z' ]
[ 'x', 'y', 'z' ]
[ 'a', 'b', 'c' ]
^C

methods

var levelProxy = require('level-proxy');

var db = levelProxy(initDb)

Create a new proxied database handle db, including events. All the leveldown methods are available on the db instance and all method calls will be queued when a database handle isn't yet available so you can start using the handle immediately.

You can optionally give an initial initDb handle to use, which is the same as calling db.swap(initDb) after creating an instance.

db.swap(newDb)

Swap out the db's internal proxy for a leveldb handle newDb.

Any buffered method calls that have queued up will fire on newDb on the same tick.

This event triggers a 'swap' event with the newDb reference.

db.get(), db.put(), db.batch(), db.createReadStream(), ...

All of these methods behave the same as with an ordinary level db handle.

db.db.iterator(), ...

leveldown methods such as db.db.iterator() are also proxied.

install

With npm do:

npm install level-proxy

license

MIT