https://github.com/Level/levelup
A node.js wrapper for abstract-leveldown compliant stores
一个为实现抽象leveldown兼容存储器的node.js封装器
levelup
Introduction
Fast and simple storage. A Node.js wrapper for abstract-leveldown
compliant stores, which follow the characteristics of LevelDB.
快速简单存储。一个为实现抽象leveldown兼容存储器的node.js封装器,遵循了LevelDB的特性
LevelDB is a simple key-value store built by Google. It's used in Google Chrome and many other products. LevelDB supports arbitrary byte arrays as both keys and values, singular get, put and delete operations, batched put and delete, bi-directional iterators and simple compression using the very fast Snappy algorithm.
LevelDB是一个Google构建的简单的键值存储。在Google Chrome和其他产品中被使用。LevelDB支持抽象字节数组作为键和值,单一get, put 和delete操作,批处理的put 和delete操作,双向迭代和使用快速Snappy
算法进行简单压缩
LevelDB stores entries sorted lexicographically by keys. This makes the streaming interface of levelup
- which exposes LevelDB iterators as Readable Streams - a very powerful query mechanism.
LevelDB存储按照键的字母顺序进行排序的条目。这构造了levelup
的流接口-暴露了levelup
作为可读流的迭代器-一个十分有效的查询机制
The most common store is leveldown
which provides a pure C++ binding to LevelDB. Many alternative stores are availablesuch as level.js
in the browser or memdown
for an in-memory store. They typically support strings and Buffers for both keys and values. For a richer set of data types you can wrap the store with encoding-down
.
最普遍的存储器是提供了存C++链接到LevelDB的leveldown
。有很多交替存储是可用的,如在浏览器中的level.js,或者是内存存储的memdown.他们的键和值基本上之处字符串和Buffers类型。对于更丰富的数据类型集,你可以使用encoding-down来封装该存储
The level
package is the recommended way to get started. It conveniently bundles levelup
, leveldown
and encoding-down
. Its main export is levelup
- i.e. you can do var db = require('level')
.
level包是推荐的入门方法。它有着levelup
, leveldown
和
encoding-down三部分。
主要的接口是level-比如使用var db = require('level')
Supported Platforms支持平台
We aim to support Active LTS and Current Node.js releases as well as browsers. For support of the underlying store, please see the respective documentation.
我们的目的是支持可用的LTS和当前的node.js以及浏览器。为了支持下面的存储,请查看相应的文档
Usage使用
If you are upgrading: please see UPGRADING.md
.
如果更新:请看UPGRADING.md
First you need to install levelup
! No stores are included so you must also install leveldown
(for example).
首先安装levelup
!没有包含的存储器,所有还要安装leveldown
$ npm install levelup leveldown
All operations are asynchronous. If you do not provide a callback, a Promise is returned.
所有操作都是异步的。如果你么有提供回调函数,那么将返回一个Promise
var levelup = require('levelup') var leveldown = require('leveldown') // 1) Create our store,创建存储器 var db = levelup(leveldown('./mydb')) // 2) Put a key & value,存储键name&值levelup db.put('name', 'levelup', function (err) { if (err) return console.log('Ooops!', err) // some kind of I/O error // 3) Fetch by key,通过键name获取值levelup db.get('name', function (err, value) { if (err) return console.log('Ooops!', err) // likely the key was not found // Ta da! console.log('name=' + value) }) })
API
详细内容看本博客Level/levelup-2-API
Promise Support
levelup
ships with native Promise
support out of the box.
带有本地Promise的
levelup
支持开箱即用
Each function accepting a callback returns a promise if the callback is omitted. This applies for:
每一个接受回调函数的函数,在回调函数被省略时返回promise。其应用在:
db.get(key[, options])
db.put(key, value[, options])
db.del(key[, options])
db.batch(ops[, options])
db.batch().write()
The only exception is the levelup
constructor itself, which if no callback is passed will lazily open the underlying store in the background.
唯一的特例是levelup
自己的构造函数,如果没有回调函数将懒得在后台打开底层的存储器
Example:
var db = levelup(leveldown('./my-db'))
db.put('foo', 'bar') .then(function () { return db.get('foo') }) .then(function (value) { console.log(value) }) .catch(function (err) { console.error(err) })
Or using async/await
:
const main = async () => {
const db = levelup(leveldown('./my-db')) await db.put('foo', 'bar') console.log(await db.get('foo')) }
Events事件
levelup
is an EventEmitter
and emits the following events.
Event | Description | Arguments |
---|---|---|
put | Key has been updated | key, value (any) |
del | Key has been deleted | key (any) |
batch | Batch has executed | operations (array) |
opening | Underlying store is opening | - |
open | Store has opened | - |
ready | Alias of open | - |
closing | Store is closing | - |
closed | Store has closed. | - |
For example you can do:
db.on('put', function (key, value) { console.log('inserted', { key, value }) })
Extending
A list of Level modules and projects can be found in the wiki. We are in the process of moving all this to awesome
.
在wiki中可以找到一个级别模块和项目列表。我们正在把这一切变得更棒
Multi-process Access
Stores like LevelDB are thread-safe but they are not suitable for accessing with multiple processes. You should only ever have a store open from a single Node.js process. Node.js clusters are made up of multiple processes so a levelup
instance cannot be shared between them either.
像LevelDB的存储器是线程安全的,但是当访问多进程时它是不适用的。你应该只从单Node.js进程打开该存储。Node.js集群由多进程组成,所以levelup
实例也不能够在他们之间共享
See the aformentioned wiki for modules like multilevel, that may help if you require a single store to be shared across processes.
有关multilevel之类的模块,请参阅规范的wiki,如果您需要跨进程共享单个存储,这可能会有所帮助。