Socket
Socket
Sign inDemoInstall

level-sublevel

Package Overview
Dependencies
10
Maintainers
1
Versions
102
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    level-sublevel

partition levelup databases


Version published
Weekly downloads
144K
increased by9.32%
Maintainers
1
Install size
166 kB
Created
Weekly downloads
 

Readme

Source

level-sublevel

Separate sections of levelup, with hooks!

build status

This module allows you to create seperate sections of a levelup database, kinda like tables in an sql database, but evented, and ranged, for real-time changing data.

Stability

Unstable: Expect patches and features, possible api changes.

This is module is working well, but may change in the future as it's use is futher explored.

Example

var LevelUp = require('levelup')
var Sublevel = require('level-sublevel')

var db = Sublevel(LevelUp('/tmp/sublevel-example'))
var sub = db.sublevel('stuff')

//put a key into the main levelup
db.put(key, value, function () {
  
})

//put a key into the sub-section!
sub.put(key2, value, function () {

})

Sublevel prefixes each subsection so that it will not collide with the outer db when saving or reading!

Hooks

Hooks are specially built into Sublevel so that you can do all sorts of clever stuff, like generating views or logs when records are inserted!

Records added via hooks will be atomically with the triggering change.

Hooks Example

Whenever a record is inserted, save an index to it by the time it was inserted.

var sub = db.sublevel('SEQ')

db.pre(function (ch, add) {
  add({
    key: ''+Date.now(), 
    value: ch.key, 
    type: 'put',
    prefix: sub //NOTE pass the destination db to add
               //and the value will end up in that subsection!
  })
})

db.put('key', 'VALUE', function (err) {

  //read all the records inserted by the hook!

  sub.createReadStream()
    .on('data', console.log)

})

Notice that sub is the second argument to add, which tells the hook to save the new record in the sub section.

Batches

In sublevel batches also support a prefix: subdb property, if set, this row will be inserted into that database section, instead of the current section.

var sub1 = db.sublevel('SUB_1')
var sub2 = db.sublevel('SUM_2')

sub.batch([
  {key: 'key', value: 'Value', type: 'put'},
  {key: 'key', value: 'Value', type: 'put', prefix: sub2},
], function (err) {...})

License

MIT

FAQs

Last updated on 01 Sep 2013

Did you know?

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc