Security News
RubyGems.org Adds New Maintainer Role
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
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.
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');
});
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 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 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.
In-memory
abstract-leveldown
store for Node.js and browsers.
If you are upgrading: please see the upgrade guide.
const levelup = require('levelup')
const memdown = require('memdown')
const db = levelup(memdown())
db.put('hey', 'you', (err) => {
if (err) throw err
db.get('hey', { asBuffer: false }, (err, value) => {
if (err) throw err
console.log(value) // 'you'
})
})
Your data is discarded when the process ends or you release a reference to the store. Note as well, though the internals of memdown
operate synchronously - levelup
does not.
Unlike leveldown
, memdown
does not stringify keys or values. This means that in addition to Buffers, you can store any JS type without the need for encoding-down
. For keys for example, you could use Buffers or strings, which sort lexicographically, or numbers, even Dates, which sort naturally. The only exceptions are null
and undefined
. Keys and values of that type are rejected.
const db = levelup(memdown())
db.put(12, true, (err) => {
if (err) throw err
db.createReadStream({
keyAsBuffer: false,
valueAsBuffer: false
}).on('data', (entry) => {
console.log(typeof entry.key) // 'number'
console.log(typeof entry.value) // 'boolean'
})
})
If you desire normalization for keys and values (e.g. to stringify numbers), wrap memdown
with encoding-down
. Alternatively install level-mem
which conveniently bundles levelup
, memdown
and encoding-down
. Such an approach is also recommended if you want to achieve universal (isomorphic) behavior. For example, you could have leveldown
in a backend and memdown
in the frontend.
const encode = require('encoding-down')
const db = levelup(encode(memdown()))
db.put(12, true, (err) => {
if (err) throw err
db.createReadStream({
keyAsBuffer: false,
valueAsBuffer: false
}).on('data', (entry) => {
console.log(typeof entry.key) // 'string'
console.log(typeof entry.value) // 'string'
})
})
A memdown
store is backed by a fully persistent data structure and thus has snapshot guarantees. Meaning that reads operate on a snapshot in time, unaffected by simultaneous writes. Do note memdown
cannot uphold this guarantee for (copies of) object references. If you store object values, be mindful of mutating referenced objects:
const db = levelup(memdown())
const obj = { thing: 'original' }
db.put('key', obj, (err) => {
obj.thing = 'modified'
db.get('key', { asBuffer: false }, (err, value) => {
console.log(value === obj) // true
console.log(value.thing) // 'modified'
})
})
Conversely, when memdown
is wrapped with encoding-down
it stores representations rather than references.
const encode = require('encoding-down')
const db = levelup(encode(memdown(), { valueEncoding: 'json' }))
const obj = { thing: 'original' }
db.put('key', obj, (err) => {
obj.thing = 'modified'
db.get('key', { asBuffer: false }, (err, value) => {
console.log(value === obj) // false
console.log(value.thing) // 'original'
})
})
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
Level/memdown
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.
Cross-browser Testing Platform and Open Source ♥ Provided by Sauce Labs.
To sustain Level
and its activities, become a backer or sponsor on Open Collective. Your logo or avatar will be displayed on our 28+ GitHub repositories and npm packages. 💖
MIT © 2013-present Rod Vagg and Contributors.
FAQs
An drop-in replacement for LevelDOWN that works in memory only
The npm package memdown receives a total of 155,725 weekly downloads. As such, memdown popularity was classified as popular.
We found that memdown 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
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.