Socket
Socket
Sign inDemoInstall

memdown

Package Overview
Dependencies
6
Maintainers
6
Versions
42
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    memdown

An drop-in replacement for LevelDOWN that works in memory only


Version published
Maintainers
6
Install size
272 kB
Created

Changelog

Source

[2.0.0] - 2018-02-11

If you are upgrading: please see UPGRADING.md.

Added

Changed

  • Update abstract-leveldown to 4.0.0 (@vweevers)
  • Perform serialization through idiomatic _serializeKey and _serializeValue (@vweevers)
  • Don't stringify anything except nullish values (@vweevers)
  • Use Buffer.isBuffer() instead of AbstractLevelDOWN#isBuffer (@vweevers)
  • README: update instantiation instructions for latest levelup (@kumavis)
  • README: rename "database" to "store" (@ralphtheninja)
  • README: simplify example and prefer ES6 (@vweevers)
  • Configure Greenkeeper to ignore updates to @types/node (@ralphtheninja)

Fixed

  • Don't clone Buffer in iterator (@vweevers)
  • Stringify Buffer.from() argument in iterator (@vweevers)
  • README: use SVG rather than PNG badge for Travis (@ralphtheninja)
  • README: link to abstract-leveldown (@vweevers)
  • README: normalize markdown headers (@ralphtheninja)
  • README: fix license typos (@ralphtheninja)
  • README: fix code example (@ralphtheninja)
  • Rename iterator#_end to fix conflict with abstract-leveldown (@vweevers)
  • Set zuul --concurrency to 1 to avoid hitting Sauce Labs limit (@vweevers)
  • Test on Android 6.0 instead of latest (7.1) due to Sauce Labs issue (@vweevers)

Removed

  • Remove global store (@vweevers)
  • Remove skipping of falsy elements in MemDOWN#batch (@vweevers)
  • Remove obsolete benchmarks (@vweevers)
  • Remove obsolete testBuffer from test.js (@vweevers)
  • Remove redundant testCommon parameter from most tests (@vweevers)
  • Remove unnecessary rimraf replacement for Browserify (@vweevers)
  • README: remove Greenkeeper badge (@ralphtheninja)

Readme

Source

memdown

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

level badge Node version Travis Coverage Status npm npm

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

License

memdown is Copyright (c) 2013-2017 Rod Vagg @rvagg and licensed under the MIT license. All rights not explicitly granted in the MIT license are reserved. See the included LICENSE file for more details.

Keywords

FAQs

Last updated on 11 Feb 2018

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc