🎩 You're Invited:Meet the Socket team at Black Hat in Las Vegas, August 3-6.RSVP
Sign In

level-atomic-counter

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

level-atomic-counter

Accurately keep count of entries in a levelup store.

latest
Source
npmnpm
Version
0.1.0
Version published
Maintainers
1
Created
Source

level-atomic-counter

npm

Accurately keep count of entries in a levelup store with sublevel installed. It does this by monkey-patching put/del/batch to check whether a key already exists or not before appending the new count to the batch of operations being written. The count is stored in a sublevel named 'counter'.

Note: The chained form a batch() is not supported.

let level = require('level')
let sublevel = require('level-sublevel');
let db = sublevel( level('example.db') );

let Counter = require('level-atomic-counter');
Counter.install(db);

db.batch([
	{type: 'put', key: 1, value: 1},
	{type: 'put', key: 2, value: 2},
	{type: 'del', key: 1},
], (err) => {
	db.getCount((err, result) => {
		console.log(result); // 1
	});
});

Methods

let counter = require('level-atomic-counter')(db);

This does not alter the db object and instead returns an object of methods in the form of {put, del, batch, getCount, recount} which will update the counter only when used.

require('level-atomic-counter').install(db);

This will install the methods onto the db object directly instead of returning them. Do it this way if you want all writes to the database to update the counter so long as those writes use this this particular db object.

counter.getCount(callback(Error, Number))
counter.recount(callback(Error, Number))

License

MIT

Keywords

leveldb

FAQs

Package last updated on 10 Sep 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts