What is memdown?
memdown is an in-memory backend for the LevelUP library, which provides a simple and efficient way to store and retrieve data in memory. It is particularly useful for testing and development purposes where persistence is not required.
What are memdown's main functionalities?
Basic Operations
This feature allows you to perform basic operations such as putting, getting, and deleting key-value pairs in the in-memory database.
const levelup = require('levelup');
const memdown = require('memdown');
const db = levelup(memdown());
// Put a value
await db.put('key', 'value');
// Get a value
const value = await db.get('key');
console.log(value); // 'value'
// Delete a value
await db.del('key');
Batch Operations
This feature allows you to perform multiple operations in a single batch, which can be more efficient than performing them individually.
const levelup = require('levelup');
const memdown = require('memdown');
const db = levelup(memdown());
// Perform batch operations
await db.batch()
.put('key1', 'value1')
.put('key2', 'value2')
.del('key1')
.write();
// Get a value
const value = await db.get('key2');
console.log(value); // 'value2'
Stream Operations
This feature allows you to create streams to read data from the database, which can be useful for processing large amounts of data efficiently.
const levelup = require('levelup');
const memdown = require('memdown');
const db = levelup(memdown());
// Put some values
await db.put('key1', 'value1');
await db.put('key2', 'value2');
await db.put('key3', 'value3');
// Create a read stream
const stream = db.createReadStream();
stream.on('data', ({ key, value }) => {
console.log(`${key}: ${value}`);
});
stream.on('end', () => {
console.log('Stream ended');
});
Other packages similar to memdown
leveldown
leveldown is a LevelDB backend for the LevelUP library. Unlike memdown, which stores data in memory, leveldown stores data on disk using Google's LevelDB. This makes it suitable for applications that require persistent storage.
rocksdb
rocksdb is a backend for the LevelUP library that uses Facebook's RocksDB, a high-performance, persistent key-value store. It offers better performance and more features compared to LevelDB, making it suitable for high-throughput applications.
sqlite3
sqlite3 is a backend for the LevelUP library that uses SQLite, a self-contained, serverless, and zero-configuration SQL database engine. It provides a more feature-rich and flexible storage solution compared to memdown, with support for complex queries and transactions.
memdown
In-memory abstract-leveldown
store for Node.js and browsers.
Example
levelup
allows you to pass a db
option to its constructor. This overrides the default leveldown
store.
var levelup = require('levelup')
var db = levelup('/some/location', { db: require('memdown') })
db.put('hey', 'you', function (err) {
if (err) throw err
db.createReadStream()
.on('data', function (kv) {
console.log('%s: %s', kv.key, kv.value)
})
.on('end', function () {
console.log('done')
})
})
Your data is discarded when the process ends or you release a reference to the database. Note as well, though the internals of memdown
operate synchronously - levelup
does not.
Running our example gives:
hey: you
done
Browser support
memdown
requires a ES5-capable browser. If you're using one that's isn't (e.g. PhantomJS, Android < 4.4, IE < 10) then you will need es5-shim.
Global Store
Even though it's in memory, the location parameter does do something. memdown
has a global cache, which it uses to save databases by the path string.
So for instance if you create these two instances:
var db1 = levelup('foo', {db: require('memdown')});
var db2 = levelup('foo', {db: require('memdown')});
Then they will actually share the same data, because the 'foo'
string is the same.
You may clear this global store using memdown.clearGlobalStore()
:
require('memdown').clearGlobalStore();
By default, it doesn't delete the store but replaces it with a new one, so the open instance of memdown
will not be affected.
clearGlobalStore
takes a single parameter, which if truthy clears the store strictly by deleting each individual key:
require('memdown').clearGlobalStore(true);
If you are using memdown
somewhere else while simultaneously clearing the global store in this way, then it may throw an error or cause unexpected results.
Test
In addition to the regular npm test
, you can test memdown
in a browser of choice with:
npm run test-browser-local
To check code coverage:
npm run coverage
Licence
memdown
is Copyright (c) 2013-2017 Rod Vagg @rvagg and licensed under the MIT licence. All rights not explicitly granted in the MIT license are reserved. See the included LICENSE file for more details.