npm

npm’s 2019 JavaScript ecosystem survey analysis is now available!Get your copy here »

level-option-wrap

1.1.0 • Public • Published

level-option-wrap

wrap opts.{{g,l}{t,e,te},start,end} arguments with functions

With this package you can expose familiar db.createReadStream() options like from leveldb but without leaking information about your internal key representations.

build status

example

For example, suppose you have a list of users using a bytewise keyEncoding and you want to let api consumers constrain the output. If you pass through opts.gt/opts.lt arguments directly, you must communicate to api consumers the internal structure of your keys, which breaks encapsulation.

However, implementing opts.{{g,l}{t,e,te},start,end} yourself inside your library or application code is madness! This library lets you do that:

var wrap = require('level-option-wrap');
var minimist = require('minimist');
var defined = require('defined');
 
var argv = minimist(process.argv.slice(2));
var opts = wrap(argv, {
    gt: function (x) { return [ 'user', defined(x, null) ] },
    lt: function (x) { return [ 'user', defined(x, undefined) ] }
});
console.log(opts);

Now to get all users >= 'mafintosh' and < 'maxogden' we can just do:

$ node example/prefix.js --gte mafintosh --lt maxogden
{ gte: [ 'user', 'mafintosh' ], lt: [ 'user', 'maxogden' ] }

This works even though we only specified gt and lt as our prefix functions, because narrower ranges take precedence.

methods

var wrap = require('level-option-wrap')

var newOpts = wrap(opts, fns)

Generate newOpts given some leveldb createReadStream-style options opts and an object fns mapping key contraints {g,l}{t,e,te} to functions. Each function gets the relevant constraint (whether exclusive or not) and should return the new key bound. Exclusivity for each key is determined by the corresponding opts key.

opts.limit values will be passed through and can be modified by defining an fns.limit(n) function.

install

With npm do:

npm install level-option-wrap

license

MIT

install

npm i level-option-wrap

Downloadsweekly downloads

5,089

version

1.1.0

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability