level-hookdown
Simple levelup hooks implemented using instance method override of arbitrary levelups.
npm install level-hookdown
Usage
var hook = var mem = // or 'level' or other levelup factoryvar mdb = var db = { console console } { console console } dbprehooksdbposthooks db
API
hookdb = hook(db, [options])
Returns a levelup instance that executes a series of pre and post hook functions before put
, del
, and batch
method calls. Composes well with mafintosh/subleveldown. Conflicts with dominictarr/level-sublevel.
The following options
can be set when wrapping a level
with hook
:
type: 'parallel' | 'series' | 'limit' limit: 5 protectHook: false
- The
type
determines if the hook functions are run in series, parallel or parallel-limit. Default isparallel
. - The limit option is passed to
run-parallel-limit
whentype
is set tolimit
. The default is 5. protectHook
performs a deep copy on the operation array in batches to preserve values if the levelup mutates them (likesubleveldown
does).
Hooks
hookdb.prehooks
An array of hook functions that are run before put
, del
, and batch
method calls to the hookdb
instance. If a hook function in the prehook array returns an err
object to its callback, the originating put
, del
or batch
operation will not be run on the contained db
that hookdb
is wrapping. A prehook veto convetion could be built on top of this behavior via error handling and retry.
hookdb.posthooks
An array of functions that are run after sucessful put
, del
, and batch
method calls on the wrapped db
instance inside a hookdb
instance.
hookFn(operation, callback)
Hook functions receive an operation
object that describes the level operation and a callback
function.
hookdb.prehooks
and hookdb.posthooks
are arrays that you can add, rearrange, and delete hook functions from using standard array methods like hookdb.prehooks.push(fn)
and hookdb.posthooks.pop(fn)
.
operation
The operation
argument can contain an object that looks like:
{type:'put', key: 'key', value: 'value', opts}
{type: 'del', key: 'key', opts}
{type: 'batch', array: [operationArray], opts}
The opts
object in the level operation object are the options that get passed through to the wrapped level.
See Also
- hypermodules/level-auto-index - Automatic secondary indexing for leveldb and subleveldown leveraging
level-hookdown
. - hypermodules/level-idx - Another high-level API for creating secondary leveldb indexes using
level-auto-index
andlevel-hookdown
.
This module is basically an alternative implementation of:
but aimed at a subleveldown workflow. These were also influential: