1.0.1 • Published 7 years ago
l-db v1.0.1
Ldb
对 leveldb 的 node.js 封装,支持同步与异步调用,并支持事件监听
Install
npm install l-db --save
Documentation
Features
- 支持同步方法调用 leveldb api,代码逻更清析
- 也支持异步方法调用 leveldb api,使运行更高效
- 异步调用使用 Promise 对象,尽量使代码更清析
- 支持 Javascript ES6 中的
async
await
方法
API
- Ldb.prototype.open(location, [options])
- Ldb.prototype.close()
Ldb.prototype.put( key, value ) or Ldb.prototype.put( json )
- Ldb.prototype.get( key ) or Ldb.prototype.get( keys ) return values
- Ldb.prototype.forEach( callback, [iterator_options] )
- Ldb.prototype.asyncPut() return Promise object
- Ldb.prototype.asyncDel() return Promise object
- Ldb.prototype.asyncGet() return Promise object
- Ldb.prototype.asyncForEach( callback, [iterator_options] )
- Ldb.prototype.getIterator([iterator_options]) return LdbIterator
- Ldb.prototype.addEventListener( type, callback )
- Ldb.prototype.addEventListenerOnce( type, callback )
- Ldb.prototype.removeEventListener( type, callback )
Ldb.open(location, options) return Ldb instance
- Ldb.destroy(location)
- Ldb.repair(location)
new Ldb(options)
!! 如果参数中含有 location
, 那么 ldb 实例即为打开状态
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);
想了解设置项的含义,请参考 Leveldb 文档
Ldb.prototype.open(location, options)
具体的参数设置同上
ldb.open('./test.db', {});
Ldb.prototype.close()
ldb.close();
Ldb.prototype.put( key, value ) or Ldb.prototype.put( json )
ldb.put("abc", 123);
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, iterator_options )
ldb.forEach((iter)=>{
console.log(iter.key, iter.value);
});
通过设置 iterator_options
可筛选返回结果
ldb.put({"a1": 1, "a2": 2, "b1": 3, "b2": 4, "c1": 5, "c2" : 6, "d.1": 7, "d.2": 8});
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, {prefix: "d."});
// print: d.1:7, d.2:8
Ldb.prototype.asyncPut()
异步调用 put 方法
ldb.asyncPut("abc", 123).then(()=>{
...
}).catch((error)=>{
console.log(error);
});
(async ()=>{
await ldb.asyncPut("abc", 1234);
})()
Ldb.prototype.asyncDel()
ldb.asyncDel("abc", 123).then(()=>{
...
});
(async ()=>{
await ldb.asyncPut("abc", 1234);
})()
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.isValid){
console.log(iter.key, iter.value);
iter.next();
}
Ldb.prototype.addEventListener( type, callback )
ready
数据库打开后,会触发事件create
当数据库不存在被创建后,会触发事件changed
当执行put
或del
操作后,会触发事件error
当有错误产生后,会触发事件closed
当数据库关闭后,会触发事件
ldb.addEventListener("changed", (info)=>{
console.log( info.from, info.key );
// print: put "abc"
});
ldb.put("abc", 123);
Ldb.prototype.addEventListenerOnce( type, callback )
ldb.addEventListenerOnce("ready", (info)=>{
console.log("ok")
});
Ldb.prototype.removeEventListener( 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")