UNPKG

3.28 kBMarkdownView Raw
1# memdown <img alt="LevelDB Logo" height="20" src="http://leveldb.org/img/logo.svg" />
2
3> In-memory `abstract-leveldown` store for Node.js and browsers.
4
5[![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/)
6
7## Example
8
9`levelup` allows you to pass a `db` option to its constructor. This overrides the default `leveldown` store.
10
11```js
12// Note that if multiple instances point to the same location,
13// the db will be shared, but only per process.
14var levelup = require('levelup')
15var db = levelup('/some/location', { db: require('memdown') })
16
17db.put('hey', 'you', function (err) {
18 if (err) throw err
19
20 db.createReadStream()
21 .on('data', function (kv) {
22 console.log('%s: %s', kv.key, kv.value)
23 })
24 .on('end', function () {
25 console.log('done')
26 })
27})
28```
29
30Your data is discarded when the process ends or you release a reference to the database. Note as well, though the internals of `memdown` operate synchronously - `levelup` does not.
31
32Running our example gives:
33
34```
35hey: you
36done
37```
38
39Browser support
40----
41
42[![Sauce Test Status](https://saucelabs.com/browser-matrix/level-ci.svg)](https://saucelabs.com/u/level-ci)
43
44`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](https://github.com/es-shims/es5-shim).
45
46Global Store
47---
48
49Even though it's in memory, the location parameter does do something. `memdown`
50has a global cache, which it uses to save databases by the path string.
51
52So for instance if you create these two instances:
53
54```js
55var db1 = levelup('foo', {db: require('memdown')});
56var db2 = levelup('foo', {db: require('memdown')});
57```
58
59Then they will actually share the same data, because the `'foo'` string is the same.
60
61You may clear this global store using `memdown.clearGlobalStore()`:
62
63```js
64require('memdown').clearGlobalStore();
65```
66
67By default, it doesn't delete the store but replaces it with a new one, so the open instance of `memdown` will not be affected.
68
69`clearGlobalStore` takes a single parameter, which if truthy clears the store strictly by deleting each individual key:
70
71```js
72require('memdown').clearGlobalStore(true); // delete each individual key
73```
74
75If 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.
76
77Test
78----
79
80In addition to the regular `npm test`, you can test `memdown` in a browser of choice with:
81
82 npm run test-browser-local
83
84To check code coverage:
85
86 npm run coverage
87
88Licence
89---
90
91`memdown` is Copyright (c) 2013-2017 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.
92
\No newline at end of file