leveldb-addon

A Node.js LevelDB binding


Keywords
leveldb, database, node db
License
MIT
Install
npm install leveldb-addon@2.0.5

Documentation

Leveldb-addon

Leveldb addon for Node.js, supports synchronous and asynchronous invocation, and supports event emitter

FEATURES

  • Support synchronization method call Leveldb, code logic more clear
  • Also supports asynchronous method call Leveldb to make running more efficient
  • Asynchronous calls use Promise to make the code clearer

INSTALLATION

npm install l-db --save

API

new Ldb([options])

const Ldb = require('l-db');
let options = {
  // base settings
  "location" : './test.db',
  "writeSync" : false,
  "readFillCache" : true,
  
  // Levedb default settings
  "createIfMissing" : true,
  "errorIfExists" : false,
  "compression" : true,
  "writeBufferSize" : 4194304,
  "blockSize" : 4096,
  "maxOpenFiles" : 1000,
  "blockRestartInterval" :16,
  "maxFileSize" : 2097152,
  "block_cache" : 8388608,
  "filterPolicy" : 10,
  
  // EventListener
  "onReady" : ()=>{},
  "onCreate" : ()=>{},
  "onChanged" : ()=> {},
  "onError" : ()=>{},
  "onClosed" : ()=>{}
};

let ldb = new Ldb(options);

For information about setting items, see the Leveldb documentation

Ldb.prototype.open(location, [options])

ldb.open('./test.db', {});

Ldb.prototype.close()

ldb.close();

Ldb.prototype.put( key, value ) or Ldb.prototype.put( json )

if (ldb.put("abc", 123)) {
    console.log('ok');
}
ldb.put({
  "abc": null, // delete abc
  "def": 456,
  "ghi": [7, 8, 9]
});

Ldb.prototype.del( key ) or Ldb.prototype.del( keys )

ldb.del("abc");
ldb.del(["abc", "def"]);

Ldb.prototype.get( key ) or Ldb.prototype.get( keys )

let value = ldb.get("abc");
let value_json = ldb.get(["abc", "def", "ghi"]);

Ldb.prototype.forEach( callback, [iteratorOptions] )

ldb.put({"a1": 1, "a2": 2, "b1": 3, "b2": 4, "c1": 5, "c2" : 6, "d.1": 7, "d.2": 8});

ldb.forEach((iter)=>{
  	if(!/b/.test(iter.key)){
        iter.break();
    }else{
       	console.log(iter.key, iter.value); 
    }
});

let callback = (iter) => {console.log(iter.key, ":", iter.value);}

ldb.forEach(callback, {start: "b"});
// print: b1:3, b2:4, c1:5, c2:6, d.1:7, d.2:8

ldb.forEach(callback, {start: "b", end:"c"});
// print: b1:3, b2:4

ldb.forEach(callback, {start: "c", end:"b", reverse:true});
// print: c1:5, c2:6

ldb.forEach(callback, {prefix: "d."});
// print: d.1:7, d.2:8

Ldb.prototype.asyncPut()

ldb.asyncPut("abc", 123).then(()=>{
    ...
}).catch((error)=>{
  	console.log(error);  
});
(async ()=>{
  await ldb.asyncPut("abc", 1234);
})()

Ldb.prototype.asyncDel()

ldb.asyncDel("abc").then(()=>{
    ...
});
(async ()=>{
  await ldb.asyncDel("abc")
})()

Ldb.prototype.asyncGet()

ldb.asyncGet("abc", 123).then((value)=>{
    console.log(value)
});
(async ()=>{
  let value = await ldb.asyncGet("abc");
})()

Ldb.prototype.asyncForEach( callback, [iterator_options] )

ldb.forEach((iter)=>{
    console.log(iter.key, iter.value);
});

Ldb.prototype.getIterator([iterator_options])

let iter = ldb.getIterator({start: "b"});
while(iter.read()){
    console.log(iter.key, iter.value);
}

Ldb.prototype.addListener( type, callback, once )

  • type :
    • ready Trigger event after database open
    • create Triggers an event when the database is created
    • changed Triggers an event when the put or del operation is executed
    • error Triggers an event when an error occurs
    • closed Trigger event after database shutdown
ldb.addEventListener("changed", (info)=>{
  	console.log( info.from, info.key );
  	// print: put "abc"
});

ldb.put("abc", 123);

Ldb.prototype.removeListener( type, callback )

ldb.removeEventListener("ready", func);

Ldb.open(location, options)

let ldb = Ldb.open("./test.db")

Ldb.destroy(location)

Ldb.destroy("./test.db")

Ldb.repair(location)

Ldb.repair("./test.db")

Iterator Options

let options = {
    "start":"a",
  	"end": "b",
  	"prefix": "c",
  	"reverse": true,
  	"limit":
}