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-hooks

v4.5.0

Published

pre/post hooks for leveldb

Downloads

506,834

Readme

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.

unsafe mode

normally, pre hooks prevent you from inserting into the hooked range when the hook is triggered. However, sometimes you do need to do this. In those cases, pass in a range with {start: START, end: END, safe: false} and level-hooks will not error. If you use this option, your hook must avoid triggering in a loop itself.

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