What is deferred-leveldown?
The deferred-leveldown npm package is a wrapper for LevelDOWN that defers operations until the underlying store is ready. This is particularly useful for scenarios where you need to queue operations while waiting for the database to be initialized or opened.
What are deferred-leveldown's main functionalities?
Deferred Operations
This feature allows you to queue operations like 'put' and 'get' until the underlying LevelDOWN store is ready. The operations will be executed once the store is initialized.
const levelup = require('levelup');
const leveldown = require('leveldown');
const deferredLeveldown = require('deferred-leveldown');
const db = levelup(deferredLeveldown(leveldown('./mydb')));
db.put('key', 'value', (err) => {
if (err) return console.error('Error:', err);
console.log('Put operation completed');
});
db.get('key', (err, value) => {
if (err) return console.error('Error:', err);
console.log('Got value:', value);
});
Error Handling
This feature demonstrates how deferred-leveldown handles errors. If an operation fails, the error is passed to the callback, allowing you to handle it appropriately.
const levelup = require('levelup');
const leveldown = require('leveldown');
const deferredLeveldown = require('deferred-leveldown');
const db = levelup(deferredLeveldown(leveldown('./mydb')));
db.put('key', 'value', (err) => {
if (err) return console.error('Error:', err);
console.log('Put operation completed');
});
db.get('nonexistentKey', (err, value) => {
if (err) return console.error('Error:', err);
console.log('Got value:', value);
});
Other packages similar to deferred-leveldown
levelup
LevelUP is a higher-level module for working with LevelDB, providing a more user-friendly API. Unlike deferred-leveldown, LevelUP does not defer operations but can be used in conjunction with deferred-leveldown to achieve similar functionality.
subleveldown
SublevelDOWN is a package that provides namespaced sublevels for LevelDB. It allows you to create isolated sub-databases within a LevelDB instance. While it does not defer operations, it can be used alongside deferred-leveldown for more complex database structures.
memdown
MemDOWN is an in-memory backend for LevelUP, useful for testing and temporary data storage. It does not provide deferred operations but can be used as a fast, in-memory alternative to LevelDOWN.
deferred-leveldown
A mock abstract-leveldown
implementation that queues operations while a real abstract-leveldown
instance is being opened.
Usage
If you are upgrading: please see UPGRADING.md.
deferred-leveldown
implements the abstract-leveldown
API so it can be used as a drop-in replacement where leveldown
is needed.
put()
, get()
, getMany()
, del()
, batch()
and clear()
operations are all queued and kept in memory until the abstract-leveldown
-compatible object has been opened through deferred-leveldown
's open()
method.
batch()
operations will all be replayed as the array form. Chained-batch operations are converted before being stored.
const deferred = require('deferred-leveldown')
const leveldown = require('leveldown')
const db = deferred(leveldown('location'))
db.open(function (err) {
})
db.put('foo', 'bar', function (err) {
})
Contributing
Level/deferred-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 Contribution Guide for more details.
Donate
Support us with a monthly donation on Open Collective and help us continue our work.
License
MIT