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

pull-level

v2.0.4

Published

pull-stream interface to levelup

Downloads

163,040

Readme

pull-level

pull-stream interface to levelup

Example - reading

read items in database.

var pl = require('pull-level')
var pull = require('pull-stream')

var db = require('levelup')('/tmp/pull-level-example')

pull(pl.read(db), pull.collect(console.log))

read items in database, plus realtime changes

pull(
  pl.read(db, {live: true}),
  //log data as it comes,
  //because tail will keep the connection open
  //so we'll never see the end otherwise.
  pull.through(console.log),
  //note, pull-streams will not drain unless something is
  //pulling the data through, so we have to add drain
  //even though the data we want is coming from pull.through()
  pull.drain()
)

If you just want the realtime inserts, use live

pull(
  pl.live(db, {live: true}),
  pull.through(console.log),
  pull.drain()
)

Example - writing

To write, pipe batch changes into write

pull(
  pull.values([
    {key: 0, value: 'zero', type: 'put'},
    {key: 1, value: 'one',  type: 'put'},
    {key: 2, value: 'two',  type: 'put'},
  ]),
  pl.write(db)
)

If you are lazy/busy, you can leave off type. In that case, if value is non-null, the change is considered a put else, a del.

pull(
  pull.values([
    {key: 0, value: 'zero'},
    {key: 1, value: 'one'},
    {key: 2, value: 'two'},
  ]), 
  pl.write(db)
)

Example - indexes!

With pull-level it's easy to create indexes. just save a pointer to the key.

like this:

pull(
  pull.values([
    {key: key, value: VALUE, type: 'put'},
    {key: '~INDEX~' + VALUE.prop, value: key,  type: 'put'},
  ]),
  pl.write(db)
)

then, when you want to do a read, use asyncMap

pull(
  pl.read(db, {min: '~INDEX~', max: '~INDEX~~'})
  pull.asyncMap(function (e, cb) {
    db.get(e.value, function (value) {
      cb(null, {key: e.value, value: value})
    })
  }),
  pull.collect(console.log)
)

Example realtime aggregation

We want to keep a realtime count of everything in the database. When ever something is inserted, we increment. But, we need to check the records that are currently in the database.

Since it takes some time to scan the database, we need to make sure we have done that before giving an answer. We can read it all with one stream, using {sync: true} to be notified of when we have read out all the old records.

First all the old records are read from the non-live stream, then you get one {sync: true} element, then all the new item.

var sum = 0, ready = false, waiting = []

//call get count to know s
function getSum (cb) {
  if(!ready) waiting.push(cb)
  else cb(null, sum)
}

pull(
  pl.read(db, {sync: true}),
  pull.drain(function (op) {
    if(op.sync) {
      //if we see a data element with this it means
      ready = true
      while(waiting.length) waiting.shift()(null, count)
    }
    //increment our counter!
    if(Number.isFinite(+op.value.amount)) //filter out non numbers & NaN.
      sum += op.value.amount
  })
)

License

MIT