Giter Club home page Giter Club logo

level-hooks's Introduction

Pre/Post hooks for leveldb

Intercept put/delete/batch operations on levelup.

Warning - Breaking Changes

The API for implementing pre hooks has changed. Instead of mutating an array at once, the prehook is called on each change hook(change, add) and may call add(_change) to add a new item into the batch.

Also, attaching hooks to leveldb is now simpler

var Hooks = require('level-hooks')
Hooks(db) //previously: Hooks()(db)

Example

var levelup   = require('levelup')
var timestamp = require('monotonic-timestamp')
var hooks     = require('level-hooks')

levelup(file, {createIfMissing: true}, function (err, db) {

  //install hooks onto db.
  hooks(db)

  db.hooks.pre({start: '', end: '~'}, function (change, add) {
    //change is same pattern as the an element in the batch array.
    //add a log to record every put operation.
    add({type: 'put', key: '~log-'+timestamp()+'-'+change.type, value: change.key})
  })

  //add a hook that responds after an operation has completed.
  db.hooks.post(function (ch) {
    //{type: 'put'|'del', key: ..., value: ...}
  })

})

Used by map-reduce to make map-reduce durable across crashes!

API

rm = db.hooks.pre (range?, hook(change, add(op), batch))

If prefix is a string or object that defines the range the pre-hook triggers on. If prefix' is a string, then the hook only triggers on keys that _start_ with that string. If the hook is an object it must be of form {start: START, end: END}`

hook is a function, and will be called on each item in the batch (if it was a put or del, it will be called on the change) op is always of the form {key: key, value: value, type:'put' | 'del'}

Pass additional changes to add to add them to the batch. If add is passed a string as the second argument it will prepend that prefix to any keys you add.

You can check what opperations are currently in the batch with the third argument. Do not modify the batch directly, instead use add

To veto (remove) the current change call add(false).

db.hooks.pre returns a function that will remove the hook when called.

rm = db.hooks.post (range?, hook)

Post hooks do not offer any chance to change the value. but do take a range option, just like pre

db.hooks.post returns a function that will remove the hook when called.

License

MIT

level-hooks's People

Contributors

dominictarr avatar heapwolf avatar alfredc avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.