UNPKG

5.11 kBMarkdownView Raw
1# memdown
2
3> In-memory [`abstract-leveldown`] store for Node.js and browsers.
4
5[![level badge][level-badge]](https://github.com/Level/awesome)
6[![npm](https://img.shields.io/npm/v/memdown.svg?label=&logo=npm)](https://www.npmjs.com/package/memdown)
7[![Node version](https://img.shields.io/node/v/memdown.svg)](https://www.npmjs.com/package/memdown)
8[![Travis](https://img.shields.io/travis/Level/memdown.svg?logo=travis&label=)](https://travis-ci.org/Level/memdown)
9[![Coverage Status](https://coveralls.io/repos/Level/memdown/badge.svg?branch=master&service=github)](https://coveralls.io/github/Level/memdown?branch=master)
10[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
11[![npm](https://img.shields.io/npm/dm/memdown.svg?label=dl)](https://www.npmjs.com/package/memdown)
12[![Backers on Open Collective](https://opencollective.com/level/backers/badge.svg?color=orange)](#backers)
13[![Sponsors on Open Collective](https://opencollective.com/level/sponsors/badge.svg?color=orange)](#sponsors)
14
15## Example
16
17**If you are upgrading:** please see the [upgrade guide](./UPGRADING.md).
18
19```js
20const levelup = require('levelup')
21const memdown = require('memdown')
22
23const db = levelup(memdown())
24
25db.put('hey', 'you', (err) => {
26 if (err) throw err
27
28 db.get('hey', { asBuffer: false }, (err, value) => {
29 if (err) throw err
30 console.log(value) // 'you'
31 })
32})
33```
34
35Your 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.
36
37## Browser support
38
39[![Sauce Test Status](https://saucelabs.com/browser-matrix/level-ci.svg)](https://saucelabs.com/u/level-ci)
40
41## Data types
42
43Keys and values can be strings or Buffers. Any other key type will be irreversibly stringified. The only exceptions are `null` and `undefined`. Keys and values of that type are rejected.
44
45```js
46const db = levelup(memdown())
47
48db.put('example', 123, (err) => {
49 if (err) throw err
50
51 db.createReadStream({
52 keyAsBuffer: false,
53 valueAsBuffer: false
54 }).on('data', (entry) => {
55 console.log(typeof entry.key) // 'string'
56 console.log(typeof entry.value) // 'string'
57 })
58})
59```
60
61If you desire non-destructive encoding (e.g. to store and retrieve numbers as-is), 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.
62
63```js
64const encode = require('encoding-down')
65const db = levelup(encode(memdown(), { valueEncoding: 'json' }))
66
67db.put('example', 123, (err) => {
68 if (err) throw err
69
70 db.createReadStream({
71 keyAsBuffer: false,
72 valueAsBuffer: false
73 }).on('data', (entry) => {
74 console.log(typeof entry.key) // 'string'
75 console.log(typeof entry.value) // 'number'
76 })
77})
78```
79
80## Snapshot guarantees
81
82A `memdown` store is backed by [a fully persistent data structure](https://www.npmjs.com/package/functional-red-black-tree) and thus has snapshot guarantees. Meaning that reads operate on a snapshot in time, unaffected by simultaneous writes.
83
84## Test
85
86In addition to the regular `npm test`, you can test `memdown` in a browser of choice with:
87
88```
89npm run test-browser-local
90```
91
92To check code coverage:
93
94```
95npm run coverage
96```
97
98## Contributing
99
100[`Level/memdown`](https://github.com/Level/memdown) is an **OPEN Open Source Project**. This means that:
101
102> 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.
103
104See the [Contribution Guide](https://github.com/Level/community/blob/master/CONTRIBUTING.md) for more details.
105
106## Big Thanks
107
108Cross-browser Testing Platform and Open Source ♥ Provided by [Sauce Labs](https://saucelabs.com).
109
110[![Sauce Labs logo](./sauce-labs.svg)](https://saucelabs.com)
111
112## Donate
113
114To sustain [`Level`](https://github.com/Level) and its activities, become a backer or sponsor on [Open Collective](https://opencollective.com/level). Your logo or avatar will be displayed on our 28+ [GitHub repositories](https://github.com/Level) and [npm](https://www.npmjs.com/) packages. 💖
115
116### Backers
117
118[![Open Collective backers](https://opencollective.com/level/backers.svg?width=890)](https://opencollective.com/level)
119
120### Sponsors
121
122[![Open Collective sponsors](https://opencollective.com/level/sponsors.svg?width=890)](https://opencollective.com/level)
123
124## License
125
126[MIT](LICENSE.md) © 2013-present Rod Vagg and [Contributors](CONTRIBUTORS.md).
127
128[`abstract-leveldown`]: https://github.com/Level/abstract-leveldown
129
130[`levelup`]: https://github.com/Level/levelup
131
132[`encoding-down`]: https://github.com/Level/encoding-down
133
134[`leveldown`]: https://github.com/Level/leveldown
135
136[`level-mem`]: https://github.com/Level/mem
137
138[level-badge]: https://leveljs.org/img/badge.svg