Skip to content
In-memory abstract-leveldown store for Node.js and browsers.
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.airtap.yml use airtap instead of zuul (#139) Feb 18, 2018
.dntrc
.gitignore Add nyc and browser code coverage (#169) Jul 4, 2018
.travis.yml Remove node 9 Jul 12, 2018
CHANGELOG.md
CONTRIBUTORS.md Apply common project tweaks (#178) Jan 5, 2019
LICENSE.md Apply common project tweaks (#178) Jan 5, 2019
README.md Apply common project tweaks (round 2) (#179) Jan 7, 2019
UPGRADING.md
immediate-browser.js Use immediate in browsers, setImmediate in Node (#61) Nov 19, 2016
immediate.js Use immediate in browsers, setImmediate in Node (#61) Nov 19, 2016
memdown.js rename iterator#_end to fix conflict with abstract-leveldown Feb 9, 2018
package.json chore(package): update nyc to version 13.2.0 (#180) Feb 5, 2019
sauce-labs.svg Add Sauce Labs logo to README (#165) May 28, 2018
test.js remove typescript tests (#154) May 13, 2018

README.md

memdown

In-memory abstract-leveldown store for Node.js and browsers.

level badge npm Node version Travis Coverage Status JavaScript Style Guide npm Backers on Open Collective Sponsors on Open Collective

Example

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.

Browser support

Sauce Test Status

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.

Data types

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 of that type are rejected; values of that type are converted to empty strings.

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'
  })
})

Snapshot guarantees

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'
  })
})

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

Contributing

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.

Big Thanks

Cross-browser Testing Platform and Open Source ♥ Provided by Sauce Labs.

Sauce Labs logo

Donate

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, npm packages and (soon) our website. 💖

Backers

Open Collective backers

Sponsors

Open Collective sponsors

License

MIT © 2013-present Rod Vagg and Contributors.

You can’t perform that action at this time.