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

v2.0.1

Published

Generic indexer for leveldb. Only stores document keys for space efficiency.

Downloads

13

Readme

level-indexer

Generic indexer for leveldb. Only stores document keys for space efficiency.

npm install level-indexer

build status

Usage

var indexer = require('level-indexer')

// create a index (by country)
var country = indexer(db, ['country']) // index by country

country.add({
  key: 'mafintosh',
  name: 'mathias',
  country: 'denmark'
})

country.add({
  key: 'maxogden',
  name: 'max',
  country: 'united states'
})

var stream = country.find({
  gte:{country:'denmark'},
  lte:{country:'denmark'}
})

// or using the shorthand syntax

var stream = country.find('denmark')

stream.on('data', function(key) {
  console.log(key) // will print mafintosh
})

The stored index is prefix with the index key names which means you can use the same levelup instance to store multiple indexes.

API

index = indexer(db, [prop1, prop2, ...], [options])

Creates a new index using the given properties. Options include

{
  map: function(key, cb) {
    // map find results to another value
    db.get(key, cb)
  }
}

index.add(doc, [key], [cb])

Add a document to the index. The document needs to have a key or provide one. Only the key will be stored in the index.

index.remove(doc, [key], [cb])

Remove a document from the index.

index.key(doc, [key])

Returns the used leveldb key. Useful if you want to batch multiple index updates together yourself

var batch = [{type:'put', key:index.key(doc), value:doc.key}, ...]

stream = index.find(options, [cb])

Search the index. Use options.{gt,gte,lt,lte} to scope your search.

// find everyone in the age range 20-50 in denmark

var index = indexer(db, ['country', 'age'])

...
var stream = index.find({
  gt: {
    country: 'denmark',
    age: 20
  },
  lt: {
    country: 'denmark',
    age: 50
  }
})

Optionally you can specify the ranges using arrays

var stream = index.find({
  gt: ['denmark', 20],
  lt: ['denmark', 50]
})

Or if you do not care about ranges

var stream = index.find(['denmark', 20])

// equivalent to

var stream = index.find({
  gte: ['denmark', 20],
  lte: ['denmark', 20]
})

The stream will contain the keys of the documents that where found in the index. Use options.map to map the to the document values.

Options also include the regular levelup db.createReadStream options.

If you set cb the stream will be buffered and passed as an array.

index.findOne(options, cb)

Only find the first match in the index and pass that to the callbck

License

MIT