Skip to content
This repository has been archived by the owner on Dec 2, 2024. It is now read-only.

Commit

Permalink
Clarify scope of keys in README examples
Browse files Browse the repository at this point in the history
Closes #94, closes #95
  • Loading branch information
vweevers committed Sep 25, 2021
1 parent b87eab7 commit 58a628a
Showing 1 changed file with 29 additions and 8 deletions.
37 changes: 29 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,46 @@
**If you are upgrading:** please see [UPGRADING.md](UPGRADING.md).

```js
var sub = require('subleveldown')
var level = require('level')
const sub = require('subleveldown')
const level = require('level')

var db = level('db')
var example = sub(db, 'example')
var nested = sub(example, 'nested')
const db = level('db')
const example = sub(db, 'example')
const nested = sub(example, 'nested')
```

The `example` and `nested` db's are just regular [`levelup`][levelup] instances:

```js
example.put('hello', 'world', function () {
nested.put('hi', 'welt', function () {
// will print {key:'hello', value:'world'}
example.createReadStream().on('data', console.log)
// Prints { key: 'hi', value: 'welt' }
nested.createReadStream().on('data', console.log)
})
})
```

Or with promises and iterators:

```js
await example.put('hello', 'world')
await nested.put('hi', 'welt')

for await (const [key, value] of nested.iterator()) {
// Prints ['hi', 'welt']
console.log([key, value])
}
```

Sublevels see their own keys as well as keys of any nested sublevels:

```js
// Prints:
// { key: '!nested!hi', value: 'welt' }
// { key: 'hello', value: 'world' }
example.createReadStream().on('data', console.log)
```

They also support `db.clear()` which is very useful to empty a bucket of stuff:

```js
Expand All @@ -61,7 +82,7 @@ example.clear(function (err) {})
// Or delete a range within `example`
example.clear({ gt: 'hello' }, function (err) {})

// For believers
// With promises
await example.clear()
```

Expand Down

0 comments on commit 58a628a

Please sign in to comment.