6.6.5 • Published 6 years ago

level-sublevel v6.6.5

Weekly downloads
97,905
License
MIT
Repository
github
Last release
6 years ago

level-sublevel

Separate sections of levelup, with hooks!

build status

testling badge

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.

level-sublevel@6 BREAKING CHANGES

The long awaited level-sublevel rewrite is out! You are hearby warned this is a significant breaking change. So it's good to use it with a new project, The user api is mostly the same as before, but the way that keys are encoded has changed, and this means you cannot run 6 on a database you created with 5.

Also, createWriteStream has been removed, in anticipation of this change use something like level-write-stream

Legacy Mode

Using leveldb with legacy mode is the simplest way to get the new sublevel on top of a database that used old sublevel. Simply require sublevel like this:

var level = require('level')
                                   //  V *** require legacy.js ***
var sublevel = require('level-sublevel/legacy')
var db = sublevel(level(path))

Migration Tool

@calvinmetcalf has created a migration tool: sublevel-migrate

This can be used to copy an old level-sublevel into the new format.

Stability

Unstable: Expect patches and features, possible api changes.

This module is working well, but may change in the future as its use is further 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 inserted 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',
    // NOTE: pass the destination db to add the value to that subsection!
    prefix: sub
  })
})

db.put('key', 'VALUE', function (err) {
  // read all the records inserted by the hook!
  sub.createReadStream().on('data', console.log)
})

Notice that the prefix property to add() is set to sub, 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, similar to the pre hook above.

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

6.6.5

6 years ago

6.6.4

6 years ago

6.6.3

6 years ago

6.6.2

6 years ago

6.6.1

8 years ago

6.6.0

8 years ago

6.5.4

8 years ago

6.5.3

8 years ago

6.5.2

9 years ago

6.5.1

9 years ago

6.5.0

9 years ago

6.4.6

9 years ago

6.4.5

9 years ago

6.4.4

9 years ago

6.4.3

9 years ago

6.4.2

9 years ago

6.4.1

9 years ago

6.4.0

9 years ago

6.3.17

9 years ago

6.3.16

9 years ago

6.3.15

10 years ago

6.3.14

10 years ago

6.3.13

10 years ago

6.3.12

10 years ago

6.3.11

10 years ago

6.3.8

10 years ago

6.3.7

10 years ago

6.3.6

10 years ago

6.3.4

10 years ago

5.2.3

10 years ago

5.2.2

10 years ago

6.3.2

10 years ago

6.3.1

10 years ago

6.3.0

10 years ago

6.2.3

10 years ago

6.2.2

10 years ago

6.2.1

10 years ago

6.2.0

10 years ago

6.1.8

10 years ago

6.1.7

10 years ago

6.1.6

10 years ago

6.1.5

10 years ago

6.1.4

10 years ago

6.1.2

10 years ago

6.1.1

10 years ago

6.1.0

10 years ago

6.0.0

10 years ago

5.2.0

10 years ago

5.1.1

11 years ago

5.1.0

11 years ago

5.0.1

11 years ago

5.0.0

11 years ago

4.8.3

11 years ago

4.8.2

11 years ago

4.8.1

11 years ago

4.8.0

11 years ago

4.7.1

11 years ago

4.7.0

11 years ago

4.6.13

11 years ago

4.6.12

11 years ago

4.6.11

11 years ago

4.6.10

11 years ago

4.6.9

11 years ago

4.6.8

11 years ago

4.6.7

11 years ago

4.6.6

11 years ago

4.6.5

11 years ago

4.6.4

11 years ago

4.6.3

11 years ago

4.6.2

11 years ago

4.6.1

11 years ago

4.6.0

11 years ago

4.5.0

11 years ago

4.4.0

11 years ago

4.3.1

11 years ago

4.3.0

11 years ago

4.2.0

11 years ago

4.1.3

11 years ago

4.1.2

11 years ago

4.1.1

11 years ago

4.1.0

11 years ago

4.0.5

11 years ago

4.0.4

11 years ago

4.0.3

11 years ago

4.0.2

11 years ago

4.0.0

11 years ago

3.4.0

11 years ago

3.3.1

11 years ago

3.3.0

11 years ago

3.2.1

11 years ago

3.2.0

11 years ago

3.1.0

11 years ago

3.0.1

11 years ago

3.0.0

11 years ago

2.1.1

11 years ago

2.1.0

11 years ago

2.0.1

11 years ago

2.0.0

11 years ago

1.0.4

11 years ago

1.0.3

11 years ago

1.0.2

11 years ago

1.0.1

11 years ago

1.0.0

11 years ago