Security News
Cloudflare Adds Security.txt Setup Wizard
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
leveldown
Advanced tools
LevelDOWN is a Node.js binding for LevelDB, a fast key-value storage library written at Google that provides an ordered mapping from string keys to string values. It is used as a backend for the LevelUP module, which provides a more user-friendly API.
Open a Database
This feature allows you to open a LevelDB database. The code sample demonstrates how to open a database located at './mydb'.
const leveldown = require('leveldown');
const db = leveldown('./mydb');
db.open(function (err) {
if (err) throw err;
console.log('Database opened');
});
Put Data
This feature allows you to store a key-value pair in the database. The code sample shows how to store the value 'value' under the key 'key'.
db.put('key', 'value', function (err) {
if (err) throw err;
console.log('Data written');
});
Get Data
This feature allows you to retrieve a value by its key from the database. The code sample demonstrates how to get the value associated with the key 'key'.
db.get('key', function (err, value) {
if (err) throw err;
console.log('Data retrieved:', value);
});
Delete Data
This feature allows you to delete a key-value pair from the database. The code sample shows how to delete the key 'key' from the database.
db.del('key', function (err) {
if (err) throw err;
console.log('Data deleted');
});
Batch Operations
This feature allows you to perform multiple operations in a single batch. The code sample demonstrates how to perform a batch operation that puts and deletes multiple key-value pairs.
db.batch()
.put('key1', 'value1')
.del('key2')
.put('key3', 'value3')
.write(function (err) {
if (err) throw err;
console.log('Batch operations completed');
});
LevelUP is a higher-level module that provides a more user-friendly API for LevelDB. It uses LevelDOWN as its default backend. Compared to LevelDOWN, LevelUP offers a more convenient and feature-rich interface for interacting with LevelDB.
RocksDB is a high-performance key-value store developed by Facebook. It is similar to LevelDB but offers better performance and more features, such as column families and transactions. The 'rocksdb' npm package provides Node.js bindings for RocksDB.
LMDB (Lightning Memory-Mapped Database) is a fast memory-mapped key-value store. The 'lmdb' npm package provides Node.js bindings for LMDB. Compared to LevelDOWN, LMDB offers better performance for read-heavy workloads and supports ACID transactions.
LevelDOWN is currently undergoing API development to prepare it for general use. Version 0.0.0 represents the API as it has been used internally within LevelUP but this API is not ideal for public consumption.
If you wish to contribute towards improving this API, please submit your suggestions in the form of a pull request against this README.md file, highlighting changes you believe ought to be made; discussion will then ensue!
LevelDOWN is stable for general use as long as you are careful what arguments you provide (LevelUP sanitises most inputs prior to feeing them to the binding). You also need to be aware of the ongoing work on the API. Each time a breaking change is made to the API, the Major version will be incremented (i.e. where semver specifies Major.Minor.Patch), so be careful with your npm dependencies!
In general, the current API is fairly brittle and each method must be fed the right number and type of arguments; failing to adhere to the requirements of each method call may result in process death. Making the API more resilient will be a top priority.
The other major priority for API-change will be to make it as easy and natural to use without unnecessary fluff (the fluff can go in LevelUP if it's really needed). The API will be as minimal as possible while still providing enough async access to LevelDB for efficient use.
createDatabase()
createIterator()
leveldown#open()
leveldown#close()
leveldown#put()
leveldown#get()
leveldown#del()
leveldown#batch()
leveldown#approximateSize()
iterator#next()
iterator#end()
createDatabase()
returns a new LevelDOWN instance.
createIterator()
returns a new Iterator instance for the given database
instance. Both arguments are required.
options
The 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, LevelDB will simply find 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. Beware that due to the way LevelDB works, a reverse seek will be slower than a forward seek.
'keys'
(boolean, default: true
): whether the 'data'
event should contain keys. If set to true
and 'values'
set to false
then 'data'
events will simply be keys, rather than objects with a 'key'
property. Used internally by the keyStream()
method.
'values'
(boolean, default: true
): whether the 'data'
event should contain values. If set to true
and 'keys'
set to false
then 'data'
events will simply be values, rather than objects with a 'value'
property. Used internally by the valueStream()
method.
'limit'
(number, default: -1
): limit the number of results collected by this stream. 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.
'fillCache'
(boolean, default: false
): wheather LevelDB's LRU-cache should be filled with data read.
'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.
open()
is an instance method on an existing database object. location
is a String pointing to the LevelDB location to be opened.
options
The options
object may contain:
'createIfMissing'
(boolean, default: true
): If true
, will initialise an empty database 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.
'compression'
(boolean, default: true
): If true
, all compressible data will be run through the Snappy compression algorithm before being stored. Snappy is very fast and shouldn't gain much speed by disabling so leave this on unless you have good reason to turn it off.
'cacheSize'
(number, default: 8 * 1024 * 1024
): The size (in bytes) of the in-memory LRU cache with frequently used uncompressed block contents.
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.
All 3 arguments are required.
close()
is an instance method on an existing database object. The underlying LevelDB 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.
The callback
argument is required.
put()
is an instance method on an existing database object, used to store new entries, or overwrite existing entries in the LevelDB store.
The key
and value
objects may either be String
s or Node.js Buffer
objects and cannot be undefined
or null
.
options
The only property currently available on the options
object is 'sync'
(boolean, default: false
). If you provide a 'sync'
value of true
in your options
object, LevelDB will perform a synchronous write of the data; although the operation will be asynchronous as far as Node is concerned. Normally, LevelDB passes the data to the operating system for writing and returns immediately, however a synchronous write will use fsync()
or equivalent so your callback won't be triggered until the data is actually on disk. Synchronous filesystem writes are significantly slower than asynchronous writes but if you want to be absolutely sure that the data is flushed then you can use 'sync': true
.
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.
All 4 arguments are required.
get()
is an instance method on an existing database object, used to fetch individual entries from the LevelDB store.
The key
object may either be a String
or a Node.js Buffer
object and cannot be undefined
or null
.
options
The options
object may contain:
'fillCache'
(boolean, default: true
): LevelDB will by default fill the in-memory LRU Cache with data from a call to get. Disabling this is done by setting fillCache
to false
.
'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.
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.
All 3 arguments are required.
del()
is an instance method on an existing database object, used to delete entries from the LevelDB store.
The key
object may either be a String
or a Node.js Buffer
object and cannot be undefined
or null
.
options
The only property currently available on the options
object is 'sync'
(boolean, default: false
). See leveldown#put() for details about this option.
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.
All 3 arguments are required.
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 contain a list of operations to be executed sequentially. 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. See LevelUP for full documentation on how this works in practice.
options
The only property currently available on the options
object is 'sync'
(boolean, default: false
). See leveldown#put() for details about this option.
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.
All 3 arguments are required.
approximateSize()
is an instance method on an existing database object. Used to get the approximate number of bytes of file system space used by the range [start..end)
. The result may not include recently written data.
The start
and end
parameters may be either String
or Node.js Buffer
objects representing keys in the LevelDB store.
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.
All 3 arguments are required.
next()
is an instance method on an existing iterator object, used to increment the underlying LevelDB iterator and return the entry at that location.
The endCallback
function will be called with no arguments in any of the following situations:
end
key has been reached; orlimit
has been reachedOtherwise, nextCallback
will be called with the following 3 arguments:
null
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.Both endCallback
and nextCallback
arguments are required.
end()
is an instance method on an existing iterator object. The underlying LevelDB iterator 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.
The callback
argument is required.
LevelDOWN 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.
LevelDOWN is only possible due to the excellent work of the following contributors:
Rod Vagg | GitHub/rvagg | Twitter/@rvagg |
---|---|---|
John Chesley | GitHub/chesles | Twitter/@chesles |
Jake Verbaten | GitHub/raynos | Twitter/@raynos2 |
Dominic Tarr | GitHub/dominictarr | Twitter/@dominictarr |
Max Ogden | GitHub/maxogden | Twitter/@maxogden |
Lars-Magnus Skog | GitHub/ralphtheninja | Twitter/@ralphtheninja |
David Björklund | GitHub/kesla | Twitter/@david_bjorklund |
Julian Gruber | GitHub/juliangruber | Twitter/@juliangruber |
Paolo Fragomeni | GitHub/hij1nx | Twitter/@hij1nx |
Copyright (c) 2012-2013 LevelDOWN contributors (listed above).
LevelDOWN 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.
LevelDOWN builds on the excellent work of the LevelDB and Snappy teams from Google and additional contributors. LevelDB and Snappy are both issued under the New BSD Licence.
FAQs
A low-level Node.js LevelDB binding
We found that leveldown demonstrated a not healthy version release cadence and project activity because the last version was released 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
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.
Security News
ENISA’s 2024 report highlights the EU’s top cybersecurity threats, including rising DDoS attacks, ransomware, supply chain vulnerabilities, and weaponized AI.