Level / memory-level

In-memory abstract-level database for Node.js and browsers.
MIT License
25 stars 4 forks source link
abstract-level database javascript level memory

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: Which module should I use? What is abstract-level? Head over to the FAQ.

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
}

With callbacks:

db.put('example', { hello: 'world' }, (err) => {
  if (err) throw err

  db.get('example', (err, value) => {
    if (err) throw err
    console.log(value) // { hello: 'world' }
  })
})

API

The API of memory-level follows that of abstract-level with a one additional constructor option (see below). The createIfMissing and errorIfExists options of abstract-level are not relevant here. 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:

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