Security News
NIST Misses 2024 Deadline to Clear NVD Backlog
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.
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.
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);
}
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 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 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.
A Low-level, LevelDOWN-compatible, Node.js LMDB binding
"LMDB" is Symas Lightning Memory-Mapped Database.
LMDB is an ultra-fast, ultra-compact key-value embedded data store developed by Symas for the OpenLDAP Project. It uses memory-mapped files, so it has the read performance of a pure in-memory database while still offering the persistence of standard disk-based databases, and is only limited to the size of the virtual address space, (it is not limited to the size of physical RAM). Note: LMDB was originally called MDB, but was renamed to avoid confusion with other software associated with the name MDB
LMDB for Node.js, is primarily designed to serve as a back-end to LevelUP, it is strongly recommended that you use LevelUP in preference to this library directly.
lmdb()
lmdb#open()
lmdb#close()
lmdb#put()
lmdb#get()
lmdb#del()
lmdb#batch()
lmdb#approximateSize()
lmdb#getProperty()
lmdb#iterator()
iterator#next()
iterator#end()
lmdb.destroy()
lmdb.repair()
lmdb()
returns a new Node.js LMDB instance. location
is a String pointing to the LMDB store to be opened or created.
open()
is an instance method on an existing database object.
The callback
function will be called with no arguments when the database has been successfully opened, or with a single error
argument if the open operation failed for any reason.
options
The optional options
argument may contain:
'createIfMissing'
(boolean, default: true
): If true
, will initialise an empty data store at the specified location if one doesn't already exist. If false
and a database doesn't exist you will receive an error in your open()
callback and your database won't open.
'errorIfExists'
(boolean, default: false
): If true
, you will receive an error in your open()
callback if the database exists at the specified location.
'mapSize'
(boolean, default: 10485760
(10MB)): Specify the size of the memory map, which is also the maximum size of the database. The value should be chosen as large as possible, to accommodate future growth of the database. The size may be changed by closing and reopening the environment. Any attempt to set a size smaller than the space already consumed by the environment will be silently changed to the current size of the used space. The size should be a multiple of the OS page size.
'sync'
(boolean, default: true
): By default, system buffers are flushed to disk after committing transactions (which are performed on every operation). Use this option to turn off this behaviour to speed up writes at the risk of losing writes upon system crash. Note that setting 'sync'
to false
and 'writeMap'
to true
leaves the system with no hint for when to write transactions to disk. Using 'mapAsync'
with 'writeMap'
may be preferable.
'readOnly'
(boolean, default: false
): Open the environment in read-only mode. No write operations will be allowed. LMDB will still modify the lock file - except on read-only filesystems, where LMDB does not use locks.
'writeMap'
(boolean, default: false
): Use a writeable memory map (unless 'readOnly'
is set). This is faster and uses fewer malloc
operations. Note that setting 'writeMap'
to true
will involve the pre-allocation of the data store, as a single file, of 'mapSize'
size.
'metaSync'
(boolean, default: true
): System buffers will be flushed to disk once per transaction (unless 'sync'
is set to false
). Setting 'metaSync'
to false
will prevent a metadata flush, deferring it until the system flushes files to disk, or the next (non-read-only) write. A 'metaSync'
set to false
will improve performance and maintain database integrity, but a system crash may undo the last committed transaction.
'mapAsync'
(boolean, default: false
): When using a 'writeMap'
, use asynchronous flushes to disk. As with 'sync'
set to false
, a system crash can then corrupt the database or lose the last transactions.
close()
is an instance method on an existing database object. The underlying LMDB database will be closed and the callback
function will be called with no arguments if the operation is successful or with a single error
argument if the operation failed for any reason.
put()
is an instance method on an existing database object, used to store new entries, or overwrite existing entries in the LMDB store.
The key
and value
objects may either be String
s or Node.js Buffer
objects and cannot be undefined
or null
. Other object types are converted to JavaScript String
s with the toString()
method and the resulting String
may not be a zero-length. A richer set of data-types are catered for in LevelUP.
The callback
function will be called with no arguments if the operation is successful or with a single error
argument if the operation failed for any reason.
get()
is an instance method on an existing database object, used to fetch individual entries from the LMDB store.
The key
object may either be a String
or a Node.js Buffer
object and cannot be undefined
or null
. Other object types are converted to JavaScript String
s with the toString()
method and the resulting String
may not be a zero-length. A richer set of data-types are catered for in LevelUP.
options
'asBuffer'
(boolean, default: true
): Used to determine whether to return the value
of the entry as a String
or a Node.js Buffer
object. Note that converting from a Buffer
to a String
incurs a cost so if you need a String
(and the value
can legitimately become a UFT8 string) then you should fetch it as one with asBuffer: true
and you'll avoid this conversion cost.The callback
function will be called with a single error
if the operation failed for any reason. If successful the first argument will be null
and the second argument will be the value
as a String
or Buffer
depending on the asBuffer
option.
del()
is an instance method on an existing database object, used to delete entries from the LMDB store.
The key
object may either be a String
or a Node.js Buffer
object and cannot be undefined
or null
. Other object types are converted to JavaScript String
s with the toString()
method and the resulting String
may not be a zero-length. A richer set of data-types are catered for in LevelUP.
The callback
function will be called with no arguments if the operation is successful or with a single error
argument if the operation failed for any reason.
batch()
is an instance method on an existing database object. Used for very fast bulk-write operations (both put and delete). The operations
argument should be an Array
containing a list of operations to be executed sequentially, although as a whole they are executed within a single transaction on LMDB. Each operation is contained in an object having the following properties: type
, key
, value
, where the type is either 'put'
or 'del'
. In the case of 'del'
the 'value'
property is ignored. Any entries with a 'key'
of null
or undefined
will cause an error to be returned on the callback
and any 'type': 'put'
entry with a 'value'
of null
or undefined
will return an error. See LevelUP for full documentation on how this works in practice.
The callback
function will be called with no arguments if the operation is successful or with a single error
argument if the operation failed for any reason.
iterator()
is an instance method on an existing database object. It returns a new Iterator instance which abstracts an LMDB "cursor".
options
The optional options
object may contain:
'start'
: the key you wish to start the read at. By default it will start at the beginning of the store. Note that the start doesn't have to be an actual key that exists, LMDB will simply jump to the next key, greater than the key you provide.
'end'
: the key you wish to end the read on. By default it will continue until the end of the store. Again, the end doesn't have to be an actual key as an (inclusive) <=
-type operation is performed to detect the end. You can also use the destroy()
method instead of supplying an 'end'
parameter to achieve the same effect.
'reverse'
(boolean, default: false
): a boolean, set to true if you want the stream to go in reverse order.
'keys'
(boolean, default: true
): whether the callback to the next()
method should receive a non-null key
. There is a small efficiency gain if you ultimately don't care what the keys are as they don't need to be converted and copied into JavaScript.
'values'
(boolean, default: true
): whether the callback to the next()
method should receive a non-null value
. There is a small efficiency gain if you ultimately don't care what the values are as they don't need to be converted and copied into JavaScript.
'limit'
(number, default: -1
): limit the number of results collected by this iterator. This number represents a maximum number of results and may not be reached if you get to the end of the store or your 'end'
value first. A value of -1
means there is no limit.
'keyAsBuffer'
(boolean, default: true
): Used to determine whether to return the key
of each entry as a String
or a Node.js Buffer
object. Note that converting from a Buffer
to a String
incurs a cost so if you need a String
(and the value
can legitimately become a UFT8 string) then you should fetch it as one.
'valueAsBuffer'
(boolean, default: true
): Used to determine whether to return the value
of each entry as a String
or a Node.js Buffer
object.
next()
is an instance method on an existing iterator object, used to increment the underlying LMDB cursor and return the entry at that location.
the callback
function will be called with no arguments in any of the following situations:
end
key has been reached; orlimit
has been reachedOtherwise, the callback
function will be called with the following 3 arguments:
error
- any error that occurs while incrementing the iterator.key
- either a String
or a Node.js Buffer
object depending on the keyAsBuffer
argument when the createIterator()
was called.value
- either a String
or a Node.js Buffer
object depending on the valueAsBuffer
argument when the createIterator()
was called.end()
is an instance method on an existing iterator object. The underlying LMDB cursor will be deleted and the callback
function will be called with no arguments if the operation is successful or with a single error
argument if the operation failed for any reason.
There are multiple ways you can find help in using LMDB / LevelUP / LevelDB in Node.js:
Node.js LMDB is an OPEN Open Source Project. This means that:
Individuals making significant and valuable contributions are given commit-access to the project to contribute as they see fit. This project is more like an open wiki than a standard guarded open source project.
See the CONTRIBUTING.md file for more details.
Copyright (c) 2012-2013 Node.js LMDB contributors.
Node.js LMDB is licensed under an MIT +no-false-attribs license. All rights not explicitly granted in the MIT license are reserved. See the included LICENSE file for more details.
Node.js LMDB builds on the excellent work of the Howard Chu of Symas Corp and additional contributors. LMDB are both issued under the The OpenLDAP Public License.
FAQs
Simple, efficient, scalable, high-performance LMDB interface
The npm package lmdb receives a total of 1,273,578 weekly downloads. As such, lmdb popularity was classified as popular.
We found that lmdb demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.
Security News
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
Security News
The Socket Research team breaks down a malicious npm package targeting the legitimate DOMPurify library. It uses obfuscated code to hide that it is exfiltrating browser and crypto wallet data.