0.2.6 • Published 11 years ago

levelidb v0.2.6

Weekly downloads
3
License
-
Repository
github
Last release
11 years ago

levelidb

A levelup interface on top of indexeddb

Example

The exact same api as levelup

var levelidb = require("levelidb")
    , toArray = require("write-stream").toArray
    , assert = require("assert")

var db = levelidb("/tmp/some/uri", {
    encoding: "json"
})

db.put("foo", { hello: "world" }, function (err) {
    if (err) {
        throw err
    }

    db.get("foo", function (err, value) {
        if (err) {
            throw err
        }

        assert.equal(value.hello, "world")

        console.log("value", value)
    })
})

Batch example

var ten = makeTen()

db.batch(ten, function (err) {
    if (err) {
        throw err
    }

    var stream = db.readStream({
        start: "batch:"
        , end: "batch;"
    })

    stream.pipe(toArray(function (list) {
        list.forEach(function (item, index) {
            assert.equal(item.key, ten[index].key)
            assert.equal(item.value, ten[index].value)
        })

        console.log("list", list)
    }))

    var keyStream = db.keyStream({
        start: "batch:"
        , end: "batch;"
    })

    keyStream.pipe(toArray(function (list) {
        list.forEach(function (item, index) {
            assert.equal(item, ten[index].key)
        })

        console.log("keys", list)
    }))

    var valueStream = db.valueStream({
        start: "batch:"
        , end: "batch;"
    })

    valueStream.pipe(toArray(function (list) {
        list.forEach(function (item, index) {
            assert.equal(item, ten[index].value)
        })

        console.log("values", list)
    }))
})

Stream example

var writable = db.writeStream()

writable.write({ key: "write:05", value: "5" })
writable.write({ key: "write:20", value: "20" })

writable.end()
writable.on("finish", function () {
    console.log("finished writing")

    var readable = db.valueStream({
        start: "write:"
        , end: "write;"
    })

    readable.pipe(toArray(function (list) {
        assert.equal(list[0], "5")
        assert.equal(list[1], "20")
        assert.equal(list.length, 2)

        console.log("values from writeStream", list)
    }))
})

function makeTen() {
    var list = []
    for (var i = 0; i < 10; i++) {
        list.push({
            type: "put"
            , key: "batch:" + i
            , value: i
        })
    }
    return list
}

Installation

npm install levelidb

Contributors

  • Raynos

MIT Licenced

0.2.6

11 years ago

0.2.5

11 years ago

0.2.4

11 years ago

0.2.3

11 years ago

0.2.2

11 years ago

0.2.1

11 years ago

0.2.0

11 years ago

0.1.8

11 years ago

0.1.7

11 years ago

0.1.6

11 years ago

0.1.5

11 years ago

0.1.4

11 years ago

0.1.3

11 years ago

0.1.2

11 years ago

0.1.1

11 years ago

0.1.0

11 years ago