UNPKG

3.97 kBMarkdownView Raw
1# MemDOWN [![Travis](https://secure.travis-ci.org/Level/memdown.png)](http://travis-ci.org/Level/memdown) [![Coverage Status](https://coveralls.io/repos/Level/memdown/badge.svg?branch=master&service=github)](https://coveralls.io/github/Level/memdown?branch=master) [![npm](https://img.shields.io/npm/v/memdown.svg)](https://www.npmjs.com/package/memdown) [![npm](https://img.shields.io/npm/dm/memdown.svg)](https://www.npmjs.com/package/memdown) [![Greenkeeper badge](https://badges.greenkeeper.io/Level/memdown.svg)](https://greenkeeper.io/)
2
3<img alt="LevelDB Logo" height="100" src="http://leveldb.org/img/logo.svg">
4
5A drop-in replacement for [LevelDOWN](https://github.com/rvagg/node-leveldown) that works in-memory.
6Can be used as a backend for [LevelUP](https://github.com/rvagg/node-levelup) rather than an actual LevelDB store.
7
8## Example
9
10As of version 0.7, LevelUP allows you to pass a `'db'` option when you create a new instance. This will override the default LevelDOWN store with a LevelDOWN API compatible object. MemDOWN conforms exactly to the LevelDOWN API but only performs operations in memory, so your data is discarded when the process ends or you release a reference to the database.
11
12```js
13var levelup = require('levelup')
14 // note that if multiple instances point to the same location,
15 // the db will be shared, but only per process
16 , db = levelup('/some/location', { db: require('memdown') })
17
18db.put('name', 'Clint Eastwood')
19db.put('dob', 'May 31, 1930')
20db.put('occupation', 'Badass')
21
22db.readStream()
23 .on('data', console.log)
24 .on('close', function () { console.log('Go ahead, make my day!') })
25```
26
27Note in this example we're not even bothering to use callbacks on our `.put()` methods even though they are async. We know that MemDOWN operates immediately so the data will go straight into the store.
28
29Running our example gives:
30
31```
32{ key: 'dob', value: 'May 31, 1930' }
33{ key: 'name', value: 'Clint Eastwood' }
34{ key: 'occupation', value: 'Badass' }
35Go ahead, make my day!
36```
37
38Global Store
39---
40
41Even though it's in memory, the location parameter does do something. MemDOWN
42has a global cache, which it uses to save databases by the path string.
43
44So for instance if you create these two MemDOWNs:
45
46```js
47var db1 = levelup('foo', {db: require('memdown')});
48var db2 = levelup('foo', {db: require('memdown')});
49```
50
51...they will actually share the same data, because the `'foo'` string is the same.
52
53You may clear this global store via the `MemDOWN.clearGlobalStore()` function:
54
55```js
56require('memdown').clearGlobalStore();
57```
58
59By default, it doesn't delete the store but replaces it with a new one, so the open instance of MemDOWN will not be affected.
60
61`clearGlobalStore` takes a single parameter, which if truthy clears the store strictly by deleting each individual key:
62
63```js
64require('memdown').clearGlobalStore(true); // delete each individual key
65```
66
67If you are using MemDOWN somewhere else while simultaneously clearing the global store in this way, then it may throw an error or cause unexpected results.
68
69Browser support
70----
71
72See [.zuul.yml](https://github.com/Level/memdown/blob/master/.zuul.yml) for the full list of browsers that are tested in CI.
73
74But essentially, this module requires a valid ES5-capable browser, so if you're using one that's not ES5-capable (e.g. PhantomJS, Android < 4.4, IE < 10), then you will need [the es5-shim](https://github.com/es-shims/es5-shim).
75
76Testing
77----
78
79 npm install
80
81Then:
82
83 npm test
84
85Or to test in the browser using Saucelabs:
86
87 npm run test-browser
88
89Or to test locally in your browser of choice:
90
91 npm run test-browser-local
92
93To run the linter:
94
95 npm run lint
96
97To check code coverage:
98
99 npm run coverage
100
101Licence
102---
103
104MemDOWN is Copyright (c) 2013-2015 Rod Vagg [@rvagg](https://twitter.com/rvagg) and licensed under the MIT licence. All rights not explicitly granted in the MIT license are reserved. See the included LICENSE file for more details.
105
\No newline at end of file