Package detail

memory-level

Level358.1kMIT3.1.0

In-memory abstract-level database for Node.js and browsers

level, leveldb, leveldown, levelup

readme

memory-level

In-memory abstract-level database for Node.js and browsers, backed by a fully persistent red-black tree. The successor to memdown and level-mem.

:pushpin: Wondering what happened to levelup? Visit Frequently Asked Questions.

level badge npm Node version Test Coverage Standard Common Changelog Donate

Usage

If you are upgrading: please see UPGRADING.md.

const { MemoryLevel } = require('memory-level')

// Create a database
const db = new MemoryLevel({ valueEncoding: 'json' })

// Add an entry with key 'a' and value 1
await db.put('a', 1)

// Add multiple entries
await db.batch([{ type: 'put', key: 'b', value: 2 }])

// Get value of key 'a': 1
const value = await db.get('a')

// Iterate entries with keys that are greater than 'a'
for await (const [key, value] of db.iterator({ gt: 'a' })) {
  console.log(value) // 2
}

API

The API of memory-level follows that of abstract-level with one additional constructor option (see below). The createIfMissing and errorIfExists options of abstract-level are not relevant here. Both implicit and explicit snapshots are supported. Data is discarded when the last reference to the database is released (i.e. db = null). Closing or reopening the database has no effect on the data. Data is not copied: when storing a Buffer value for example, subsequent mutations to that Buffer will affect the stored data too.

db = new MemoryLevel([options])

Besides abstract-level options, the optional options object may contain:

  • storeEncoding (string): one of 'buffer', 'view', 'utf8'. How to store data internally. This affects which data types can be stored non-destructively. The default is 'buffer' (that means Buffer) which is non-destructive. In browsers it may be preferable to use 'view' (Uint8Array) to be able to exclude the buffer shim. Or if there's no need to store binary data, then 'utf8' (String). Regardless of the storeEncoding, memory-level supports input that is of any of the aforementioned types, but internally converts it to one type in order to provide a consistent sort order.

Install

With npm do:

npm install memory-level

Contributing

Level/memory-level 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.

License

MIT

changelog

Changelog

3.1.0 - 2025-04-20

Added

  • Implement db.getSync() (#21) (b5f5c52) (Vincent Weevers)

3.0.0 - 2025-01-26

Would you mind voting in this community poll? Thank you! If you are upgrading: please see UPGRADING.md.

Changed

  • Breaking: upgrade to abstract-level 3 (#17) (946a57f) (Vincent Weevers)
  • Refactor: split index.js into lib files (#19) (2c24c62) (Vincent Weevers)

Added

  • Implement has() and hasMany() (#18) (fbad527) (Vincent Weevers)
  • Implement explicit snapshots (#16) (f832018) (Vincent Weevers)

Fixed

  • Increase code coverage to 100% (#20) (c5abd4a) (Vincent Weevers)

2.0.0 - 2024-10-22

If you are upgrading: please see UPGRADING.md.

Changed

  • Breaking: upgrade to abstract-level 2 (77dac64) (Vincent Weevers)

Removed

  • Breaking: drop support of Node.js < 18 (caff07d) (Vincent Weevers)
  • Remove level-mem compatibility checks (8552de5) (Vincent Weevers)

1.0.0 - 2022-01-30

:seedling: Initial release. If you are upgrading from memdown: please see UPGRADING.md.