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

kappa-view-list

v1.2.0

Published

general purpose sorted list view for kappa-core

Downloads

11

Readme

kappa-view-list

General purpose sorted list view for kappa-core.

This view models a sorted list materialized view, on top of a set of append-only logs. It allows for reading subsets of entries from that sorted list, and tailing the end of it.

Usage

Let's build a view that orders messages by their ISO timestamp:

var memdb = require('memdb')
var kappa = require('kappa-core')
var ram = require('random-access-memory')
var list = require('kappa-view-list')

var core = kappa(ram, { valueEncoding: 'json' })
var idx = memdb()

var listIdx = list(idx, function (msg, next) {
  if (!msg.value.timestamp) return next()
  next(null, [msg.value.timestamp])
})
core.use('timestamp', listIdx)

core.writer('local', function (err, feed) {
  var docs = [
    { timestamp: '2018-11-04T17:45:55.524Z', id: 'foo' },
    { timestamp: '2017-11-04T12:15:00.524Z', id: 'foo', n: 3 },
    { timestamp: '2018-12-04T07:00:00.524Z', id: 'foo', n: 12 }
  ]
  feed.append(docs, function (err, seq) {
    core.api.timestamp.read({ limit: 2 }, function (err, values) {
      console.log('values', values)
    })
  })

  core.api.timestamp.onInsert(function (msg) {
    console.log('update', msg.seq)
  })
})

outputs

update 0
update 1
update 2
values [ { key: 'c068a25f1579c6094b41849df3e11bf0db0a36c0c9567605f5f60a4af3ad121a',
    seq: 1,
    value: { timestamp: '2017-11-04T12:15:00.524Z', id: 'foo', n: 3 } },
  { key: 'c068a25f1579c6094b41849df3e11bf0db0a36c0c9567605f5f60a4af3ad121a',
    seq: 0,
    value: { timestamp: '2018-11-04T17:45:55.524Z', id: 'foo' } } ]

The list is in ascending order by default.

API

var list = require('kappa-view-list')

var view = list(lvl, mapFn)

Creates a new kappa view, view, using the LevelUP/LevelDOWN storage lvl and a mapping function mapFn.

Here's how mapFn works:

// here, msg.value.timestamp is the field to sort on
function mapMsgToSortFields (msg, next) {
  next(null, [msg.value.timestamp])
}

The callback next is called as next(error?, [field]).

From here, you can use core.use(name, view) to install it into a kappa-core. What follows are the APIs that get exposed if you did core.use('list', view):

[var rs = ]core.list.read([opts,] [cb])

Returns a subset of the list. If no callback cb is given, a Readable stream rs is returned. Otherwise, results are given as a list via the callback cb(err, [msgs]).

Each result has the form

{
  key: 'hexadecimal_feed_key',
  seq: Number,
  value: { user: 'data' }
}

opts can be Level-like parameters for controlling the results. Some examples are:

  • limit (Number): maximum number of results to return
  • gt: the value of the lowest field to return
  • lt: the value of the highest field to return

Fetching all entries with timestamps between 2017 and 2018 could look like this:

var stream = core.api.timestamp.read({gte: '2017-00-00T00:00:000Z', lt: '2018-00-00T00:00:000Z'})

core.list.tail(size, fn)

Listen for updates to the upper end of the list, a window of size size. fn is called with a list of messages, sorted from low to high.

core.list.onInsert(fn)

Subscribe to updates to every list insertion. The function fn is called as fn(key, value).

Install

With npm installed, run

$ npm install kappa-view-list

License

ISC