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-index-update

v0.4.2

Published

A module to save a document into leveldb where the old indexes are removed in the same batch as the new ones are inserted

Downloads

13

Readme

level-index-update

Build status

A module to save a document into leveldb where the old indexes are removed in the same batch as the new ones are inserted

It loads the old document and creates a single batch of 'del' and 'put' commands from the mapper function you provide.

It can also expand a batch of multiple documents into a single larger batch containing the index 'put' and 'del' commands for each document.

This is a cumbersome way to do it and there are limitations but hey - life is short and suggestions (like how to manage an immutable collection of indexes for changing values) are welcome : )

example

var level = require('level');
var sub = require('level-sublevel');
var indexupdate = require('level-index-update');

var db = sub(level(__dirname + '/pathdb', {
	 valueEncoding: 'json'
}))

var indexer = indexupdate(db.sublevel('docs'), '_indexes', function(key, value, emit){

	// you can emit multiple indexes per value
	emit(['color', value.color])
	emit(['heightcolor', value.height, value.color])

})

indexer.save('/my/path', {
	height:50,
	color:'red'
}, function(err, batch){

	console.dir(batch);
	// [{type:'put', key:'/my/path', value:'{color:"red",height:50}'},
	// {type:'put', key:'color~red~/my/path'},
	// {type:'put', key:'heightcolor~red~50~/my/path'}]

	// the index for red is inserted

	indexer.save('/my/path', {
		height:45,
		color:'blue'
	}, function(err, batch){

		console.dir(batch);
		// {type:'put', key:'/my/path', value:'{color:"blue",height:45}'},
		// {type:'del', key:'color~red~/my/path'},
		// {type:'del', key:'heightcolor~50~red~/my/path'},
		// {type:'put', key:'color~blue~/my/path'},
		// {type:'put', key:'heightcolor~45~blue~/my/path'}]

		// the index for red is deleted and the index for blue is inserted
	})
	
})

You can also insert a batch of documents and it will auto-expand the batch for the indexes:

indexer.batch([{
	key:'/my/path',
	color:'red',
	height:50
},{
	key:'/my/otherpath',
	color:'blue',
	height:45
}], function(err, batch){
	console.dir(batch);

	// [{type:'put', key:'/my/path', value:'{color:"blue",height:45}'},
	// {type:'put', key:'color~red~/my/path'},
	// {type:'put', key:'heightcolor~50~red~/my/path'},
	// {type:'put', key:'/my/otherpath', value:'{color:"blue",height:45}'},
	// {type:'put', key:'color~blue~/my/otherpath'},
	// {type:'put', key:'heightcolor~45~blue~/my/otherpath'}],

})

api

var indexer = indexupdate(db, [indexpath], mapper)

Create an updater function by passing a leveldb, an optional path to write the indexes (defaults to 'updateindex') and a mapper function that will emit the index fields and values for each value

indexer.save(key, value, callback(err, batch))

Run the updater function by passing the key and the value for the update.

The callback will be passed the batch that was inserted into the database.

indexer.expandBatch(arr, callback(err, batch))

Pass a batch of commands and it will convert into a larger batch with all the indexes for each 'put' and 'del' resolved.

indexer.batch(arr, callback(err, batch))

Uses expandBatch and then actually runs the batch

mapper(key, value, emit([values], manualkey))

The mapper function is called for each update - once for the old values and once for the new values

Call emit with an array of values to index based on the passed object

You can call emit many times per object - the key will be added to the index entry automatically

Pass an array of values to the indexer - the key will automatically be appended.

If you want to manually manage the index but still want the batch behavious, pass true as the second argument to emit:


var indexer = indexupdate(db.sublevel('docs'), '_indexes', function(key, value, emit){

	// manually manage the index structure
	emit(['color', value.color, key, '__', new Date().getTime()], true)

})

todo

  • handle if a save request for a key arrives whilst another is resolving

license

MIT