What is lmdb?
The lmdb npm package is a Node.js binding for the Lightning Memory-Mapped Database (LMDB). LMDB is a fast, compact, and powerful embedded data store. The lmdb package allows Node.js applications to store and retrieve data in a high-performance and transactional database environment.
What are lmdb's main functionalities?
Key-Value Store
LMDB provides a simple key-value store where you can save and retrieve data using keys.
const { open } = require('lmdb');
const db = open({ path: './mydata' });
db.put('key', 'value');
const value = db.get('key');
console.log(value); // 'value'
Transactions
LMDB supports transactions, allowing multiple operations to be performed atomically.
const { open } = require('lmdb');
const db = open({ path: './mydata' });
db.transaction(() => {
db.put('key1', 'value1');
db.put('key2', 'value2');
});
Multiple Databases
LMDB allows you to create multiple databases within the same environment.
const { open } = require('lmdb');
const env = open({ path: './mydata' });
const db1 = env.openDbi({ name: 'db1', create: true });
const db2 = env.openDbi({ name: 'db2', create: true });
db1.put('key', 'value1');
db2.put('key', 'value2');
Dupsort
LMDB supports sorted duplicate keys, allowing multiple values per key, sorted by value.
const { open } = require('lmdb');
const db = open({ path: './mydata', dupSort: true });
db.put('key', 'value1');
db.put('key', 'value2');
const values = db.getRange({ start: 'key' });
for (const { key, value } of values) {
console.log(key, value);
}
Other packages similar to lmdb
level
Level is a fast and simple storage. A Node.js-style LevelDB wrapper that is used for persistent or ephemeral key-value data. It differs from lmdb in that it's built on top of LevelDB and has a different API, but it provides similar key-value store capabilities.
rocksdb
RocksDB is a high performance embedded database for key-value data. It is a fork of LevelDB by Facebook optimized for fast storage. RocksDB provides similar functionality to lmdb but with different performance characteristics and additional features like column families.
nedb
NeDB is a lightweight JavaScript database using a subset of MongoDB's API. It's an in-memory database with persistence to disk. Unlike lmdb, NeDB is not a key-value store but a document database, which can be more suitable for complex data structures.