├── .gitignore
├── .travis.yml
├── CHANGELOG.md
├── CONTRIBUTORS.md
├── LICENSE.md
├── README.md
├── UPGRADING.md
├── browser.js
├── level.js
├── package.json
├── test-browser.js
└── test-node.js
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules
2 | package-lock.json
3 | .nyc_output/
4 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | sudo: false
2 |
3 | language: node_js
4 |
5 | node_js:
6 | - 6
7 | - 8
8 | - 10
9 |
10 | after_success: npm run coverage
11 |
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | # Changelog
2 |
3 | ## [Unreleased][unreleased]
4 |
5 | ## [2.0.0] - 2018-06-21
6 |
7 | ### Changed
8 |
9 | - Upgrade `level-js` from `3.0.0-rc1` to `^3.0.0` ([**@ralphtheninja**](https://github.com/ralphtheninja))
10 | - Fix missing link to `encoding-down` ([**@ralphtheninja**](https://github.com/ralphtheninja))
11 | - Skip installing `g++-4.8` on Travis ([**@ralphtheninja**](https://github.com/ralphtheninja))
12 | - Remove copyright year for less maintenance ([**@ralphtheninja**](https://github.com/ralphtheninja))
13 |
14 | ### Added
15 |
16 | - Add `UPGRADING.md` ([**@ralphtheninja**](https://github.com/ralphtheninja))
17 | - Add `remark` tooling ([**@ralphtheninja**](https://github.com/ralphtheninja))
18 |
19 | ## [2.0.0-rc1] - 2018-05-29
20 |
21 | ### Changed
22 |
23 | - Upgrade `leveldown` from `^3.0.0` to `^4.0.0` ([**@greenkeeper**](https://github.com/greenkeeper))
24 | - Upgrade `level-packager` from `~1.2.0` to `~3.1.0` ([**@ralphtheninja**](https://github.com/ralphtheninja))
25 | - Upgrade `level-js` from `~2.1.6` to `3.0.0-rc1` ([**@ralphtheninja**](https://github.com/ralphtheninja))
26 | - Use proper version range for `tape` ([**@ralphtheninja**](https://github.com/ralphtheninja))
27 | - Homogenize README headers, add table of contents and document the api ([**@ralphtheninja**](https://github.com/ralphtheninja))
28 | - Separate tests into node and browser specific versions ([**@ralphtheninja**](https://github.com/ralphtheninja))
29 | - Import `levelup` integration tests from `level-js` ([**@ralphtheninja**](https://github.com/ralphtheninja))
30 | - Be explicit when running tests from `level-packager` ([**@ralphtheninja**](https://github.com/ralphtheninja))
31 |
32 | ### Added
33 |
34 | - Add node 9 and 10 to Travis ([**@ralphtheninja**](https://github.com/ralphtheninja))
35 | - Add `airtap` for local browser testing ([**@ralphtheninja**](https://github.com/ralphtheninja))
36 | - Add `standard` for linting ([**@ralphtheninja**](https://github.com/ralphtheninja))
37 |
38 | ### Removed
39 |
40 | - Remove node 0.12, 4, 5 and 7 from Travis ([**@ralphtheninja**](https://github.com/ralphtheninja))
41 |
42 | ## [1.1.2] - 2018-03-20
43 |
44 | ### Changed
45 |
46 | - Upgrade `leveldown` from `^1.4.1` to `^3.0.0` ([**@ralphtheninja**](https://github.com/ralphtheninja))
47 | - Simplify `level-packager` call for `level-js` ([**@rh0**](https://github.com/rh0))
48 | - Ignore `package-lock.json` ([**@ralphtheninja**](https://github.com/ralphtheninja))
49 |
50 | ### Added
51 |
52 | - Add Greenkeeper badge ([**@ralphtheninja**](https://github.com/ralphtheninja))
53 | - Add node 8 to Travis ([**@ralphtheninja**](https://github.com/ralphtheninja))
54 |
55 | ## [1.1.1] - 2017-05-19
56 |
57 | ### Added
58 |
59 | - Add gcc-4.8 to Travis ([**@ralphtheninja**](https://github.com/ralphtheninja))
60 | - Add node 6 and 7 to Travis ([**@ralphtheninja**](https://github.com/ralphtheninja))
61 | - Update copyright year ([**@ralphtheninja**](https://github.com/ralphtheninja))
62 |
63 | ### Removed
64 |
65 | - Remove node 0.10, 1.0, 1.8, 2 and 3 from Travis ([**@ralphtheninja**](https://github.com/ralphtheninja))
66 |
67 | ## [1.1.0] - 2015-12-10
68 |
69 | ### Changed
70 |
71 | - Upgrade `level-packager` from `~1.1.0` to `~1.2.0` ([**@ralphtheninja**](https://github.com/ralphtheninja))
72 |
73 | ### Added
74 |
75 | - Add node 1.0, 2, 3, 4 and 5 to Travis ([**@ralphtheninja**](https://github.com/ralphtheninja))
76 | - Add dependency badge ([**@ralphtheninja**](https://github.com/ralphtheninja))
77 |
78 | ### Removed
79 |
80 | - Remove `node-gyp-install` from Travis ([**@ralphtheninja**](https://github.com/ralphtheninja))
81 |
82 | ## [1.0.2] - 2015-12-10
83 |
84 | ### Changed
85 |
86 | - Use `^` to track `level-js` ([**@mafintosh**](https://github.com/mafintosh))
87 |
88 | ## [1.0.1] - 2015-09-20
89 |
90 | ### Changed
91 |
92 | - Upgrade `leveldown` from `~1.2.2` to `^1.4.1` ([**@jimkang**](https://github.com/jimkang))
93 |
94 | ## [1.0.0] - 2015-06-09
95 |
96 | ### Changed
97 |
98 | - Upgrade `level-packager` from `~0.19.0` to `~1.1.0` ([**@mcollina**](https://github.com/mcollina))
99 | - Upgrade `leveldown` from `~0.10.0` to `~1.2.2` ([**@mcollina**](https://github.com/mcollina))
100 | - Make sure dependencies use `~` range ([**@mcollina**](https://github.com/mcollina))
101 | - Update nodei.co links in README ([**@mcollina**](https://github.com/mcollina))
102 | - Put back `nvm` on Travis and use `node-gyp-install` to get correct headers ([**@ralphtheninja**](https://github.com/ralphtheninja))
103 |
104 | ## [0.19.1] - 2015-05-05
105 |
106 | ### Changed
107 |
108 | - Change license from MIT +no-false-attribs to MIT ([**@ralphtheninja**](https://github.com/ralphtheninja))
109 | - Update `Level` -> `Level-browserify` in README ([**@ralphtheninja**](https://github.com/ralphtheninja))
110 | - Use `n` instead of `nvm` for iojs support and native modules ([**@ralphtheninja**](https://github.com/ralphtheninja))
111 |
112 | ### Added
113 |
114 | - Add node 0.12 and iojs to Travis ([**@ralphtheninja**](https://github.com/ralphtheninja))
115 |
116 | ## [0.19.0] - 2015-05-04
117 |
118 | ### Changed
119 |
120 | - Update logo and copyright ([**@ralphtheninja**](https://github.com/ralphtheninja))
121 | - Upgrade `level-js` from `~1.1.2` to `^2.1.6` ([**@mafintosh**](https://github.com/mafintosh))
122 | - Upgrade `level-packager` from `~0.18.0` to `~0.19.0` ([**@ralphtheninja**](https://github.com/ralphtheninja))
123 |
124 | ### Added
125 |
126 | - Add support for node 0.8 ([**@mafintosh**](https://github.com/mafintosh))
127 |
128 | ## [0.18.1] - 2014-04-17
129 |
130 | ### Changed
131 |
132 | - Update README ([**@maxogden**](https://github.com/maxogden), [**@mcollina**](https://github.com/mcollina))
133 | - Skip tests not runnable in the browser ([**@mcollina**](https://github.com/mcollina))
134 |
135 | ### Fixed
136 |
137 | - Make `browserify` work properly ([**@JamesKyburz**](https://github.com/JamesKyburz))
138 |
139 | ## 0.18.0 - 2013-11-18
140 |
141 | :seedling: Initial release.
142 |
143 | [unreleased]: https://github.com/level/level-browserify/compare/v2.0.0...HEAD
144 |
145 | [2.0.0]: https://github.com/level/level-browserify/compare/v2.0.0-rc1...v2.0.0
146 |
147 | [2.0.0-rc1]: https://github.com/level/level-browserify/compare/v1.1.2...v2.0.0-rc1
148 |
149 | [1.1.2]: https://github.com/level/level-browserify/compare/v1.1.1...v1.1.2
150 |
151 | [1.1.1]: https://github.com/level/level-browserify/compare/v1.1.0...v1.1.1
152 |
153 | [1.1.0]: https://github.com/level/level-browserify/compare/v1.0.2...v1.1.0
154 |
155 | [1.0.2]: https://github.com/level/level-browserify/compare/v1.0.1...v1.0.2
156 |
157 | [1.0.1]: https://github.com/level/level-browserify/compare/v1.0.0...v1.0.1
158 |
159 | [1.0.0]: https://github.com/level/level-browserify/compare/v0.19.1...v1.0.0
160 |
161 | [0.19.1]: https://github.com/level/level-browserify/compare/v0.19.0...v0.19.1
162 |
163 | [0.19.0]: https://github.com/level/level-browserify/compare/v0.18.1...v0.19.0
164 |
165 | [0.18.1]: https://github.com/level/level-browserify/compare/v0.18.0...v0.18.1
166 |
--------------------------------------------------------------------------------
/CONTRIBUTORS.md:
--------------------------------------------------------------------------------
1 | # Contributors
2 |
3 | | Name | GitHub | Social |
4 | | :------------------- | :----------------------------------------------------- | :-------------------------------------------------------------- |
5 | | **Lars-Magnus Skog** | [**@ralphtheninja**](https://github.com/ralphtheninja) | [**@ralph@social.weho.st**](https://social.weho.st/@ralph) |
6 | | **Rod Vagg** | [**@rvagg**](https://github.com/rvagg) | [**@rvagg@twitter**](https://twitter.com/rvagg) |
7 | | **Matteo Collina** | [**@mcollina**](https://github.com/mcollina) | [**@matteocollina@twitter**](https://twitter.com/matteocollina) |
8 | | **Vincent Weevers** | [**@vweevers**](https://github.com/vweevers) | [**@vweevers@twitter**](https://twitter.com/vweevers) |
9 | | **Julian Gruber** | [**@juliangruber**](https://github.com/juliangruber) | [**@juliangruber@twitter**](https://twitter.com/juliangruber) |
10 | | **Mathias Buus** | [**@mafintosh**](https://github.com/mafintosh) | [**@mafintosh@twitter**](https://twitter.com/mafintosh) |
11 | | **Jim Kang** | | |
12 | | **rho** | | |
13 | | **James Kyburz** | [**@jameskyburz**](https://github.com/jameskyburz) | [**@jameskyburz@twitter**](https://twitter.com/jameskyburz) |
14 | | **Max Ogden** | [**@maxogden**](https://github.com/maxogden) | [**@maxogden@twitter**](https://twitter.com/maxogden) |
15 |
--------------------------------------------------------------------------------
/LICENSE.md:
--------------------------------------------------------------------------------
1 | # The MIT License (MIT)
2 |
3 | **Copyright © 2012-present [Contributors](CONTRIBUTORS.md).**
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # level-browserify
2 |
3 | **No longer maintained: superseded by [`level v5.0.0`](https://github.com/Level/level).**
4 |
5 | ---
6 |
7 | > Fast & simple storage. A Node.js-style `LevelDB` wrapper that works in the browser too!
8 |
9 | [![level badge][level-badge]](https://github.com/Level/awesome)
10 | [](https://www.npmjs.com/package/level-browserify)
11 | [](https://travis-ci.org/Level/level-browserify)
12 | [](https://coveralls.io/github/Level/level-browserify)
13 | [](https://standardjs.com)
14 | [](https://www.npmjs.com/package/level-browserify)
15 | [](#backers)
16 | [](#sponsors)
17 |
18 | A convenience package that:
19 |
20 | - exports a function that returns a [`levelup instance`](https://github.com/Level/levelup#ctor) when invoked
21 | - bundles the current release of [`levelup`][levelup] and [`leveldown`][leveldown]/[`level-js`][level-js]
22 | - leverages encodings using [`encoding-down`][encoding-down]
23 |
24 | Use this package to avoid having to explicitly install `leveldown`/`level-js` when you just want to use `levelup` in node and in the browser.
25 |
26 | In node.js you get `leveldown`, while in the browser you get `level-js` (through use of browserify's `browser` field setting in `package.json`).
27 |
28 | **If you are upgrading:** please see [`UPGRADING.md`](UPGRADING.md).
29 |
30 | ## Table of Contents
31 |
32 | Click to expand
33 |
34 | - [Usage](#usage)
35 | - [API](#api)
36 | - [Promise Support](#promise-support)
37 | - [Events](#events)
38 | - [Contributing](#contributing)
39 | - [Donate](#donate)
40 | - [License](#license)
41 |
42 |
43 |
44 | ## Usage
45 |
46 | ```js
47 | var level = require('level-browserify')
48 |
49 | // 1) Create our database, supply location and options.
50 | // This will create or open the underlying LevelDB store/Indexedb Database
51 | var db = level('./mydb')
52 |
53 | // 2) put a key & value
54 | db.put('name', 'Level', function (err) {
55 | if (err) return console.log('Ooops!', err) // some kind of I/O error
56 |
57 | // 3) fetch by key
58 | db.get('name', function (err, value) {
59 | if (err) return console.log('Ooops!', err) // likely the key was not found
60 |
61 | // ta da!
62 | console.log('name=' + value)
63 | })
64 | })
65 | ```
66 |
67 | ## API
68 |
69 | - level()
70 | - db.open()
71 | - db.close()
72 | - db.put()
73 | - db.get()
74 | - db.del()
75 | - db.batch()
_(array form)_
76 | - db.batch()
_(chained form)_
77 | - db.isOpen()
78 | - db.isClosed()
79 | - db.createReadStream()
80 | - db.createKeyStream()
81 | - db.createValueStream()
82 |
83 | See [`levelup`][levelup] and [`leveldown`][leveldown]/[`level-js`][level-js] for more details.
84 |
85 |
86 |
87 | ### `db = level(location[, options[, callback]])`
88 |
89 | The main entry point for creating a new `levelup` instance.
90 |
91 | - `location` path to the underlying `LevelDB`.
92 | - `options` is passed on to the underlying store.
93 | - `options.keyEncoding` and `options.valueEncoding` are passed to [`encoding-down`][encoding-down], default encoding is `'utf8'`
94 |
95 | Calling `level('./db')` will also open the underlying store. This is an asynchronous operation which will trigger your callback if you provide one. The callback should take the form `function (err, db) {}` where `db` is the `levelup` instance. If you don't provide a callback, any read & write operations are simply queued internally until the store is fully opened.
96 |
97 | This leads to two alternative ways of managing a `levelup` instance:
98 |
99 | ```js
100 | level(location, options, function (err, db) {
101 | if (err) throw err
102 |
103 | db.get('foo', function (err, value) {
104 | if (err) return console.log('foo does not exist')
105 | console.log('got foo =', value)
106 | })
107 | })
108 | ```
109 |
110 | Versus the equivalent:
111 |
112 | ```js
113 | // Will throw if an error occurs
114 | const db = level(location, options)
115 |
116 | db.get('foo', function (err, value) {
117 | if (err) return console.log('foo does not exist')
118 | console.log('got foo =', value)
119 | })
120 | ```
121 |
122 | The constructor function has a `.errors` property which provides access to the different error types from [`level-errors`](https://github.com/Level/errors#api). See example below on how to use it:
123 |
124 | ```js
125 | level('./db', { createIfMissing: false }, function (err, db) {
126 | if (err instanceof level.errors.OpenError) {
127 | console.log('failed to open database')
128 | }
129 | })
130 | ```
131 |
132 |
133 |
134 | ### `db.open([callback])`
135 |
136 | Opens the underlying store. In general you should never need to call this method directly as it's automatically called by levelup()
.
137 |
138 | However, it is possible to _reopen_ the store after it has been closed with close()
, although this is not generally advised.
139 |
140 | If no callback is passed, a promise is returned.
141 |
142 |
143 |
144 | ### `db.close([callback])`
145 |
146 | close()
closes the underlying store. The callback will receive any error encountered during closing as the first argument.
147 |
148 | You should always clean up your `levelup` instance by calling `close()` when you no longer need it to free up resources. A store cannot be opened by multiple instances of `levelup` simultaneously.
149 |
150 | If no callback is passed, a promise is returned.
151 |
152 |
153 |
154 | ### `db.put(key, value[, options][, callback])`
155 |
156 | put()
is the primary method for inserting data into the store. Both `key` and `value` can be of any type as far as `levelup` is concerned.
157 |
158 | `options` is passed on to the underlying store.
159 |
160 | If no callback is passed, a promise is returned.
161 |
162 |
163 |
164 | ### `db.get(key[, options][, callback])`
165 |
166 | get()
is the primary method for fetching data from the store. The `key` can be of any type. If it doesn't exist in the store then the callback or promise will receive an error. A not-found err object will be of type `'NotFoundError'` so you can `err.type == 'NotFoundError'` or you can perform a truthy test on the property `err.notFound`.
167 |
168 | ```js
169 | db.get('foo', function (err, value) {
170 | if (err) {
171 | if (err.notFound) {
172 | // handle a 'NotFoundError' here
173 | return
174 | }
175 | // I/O or other error, pass it up the callback chain
176 | return callback(err)
177 | }
178 |
179 | // .. handle `value` here
180 | })
181 | ```
182 |
183 | `options` is passed on to the underlying store.
184 |
185 | If no callback is passed, a promise is returned.
186 |
187 |
188 |
189 | ### `db.del(key[, options][, callback])`
190 |
191 | del()
is the primary method for removing data from the store.
192 |
193 | ```js
194 | db.del('foo', function (err) {
195 | if (err)
196 | // handle I/O or other error
197 | });
198 | ```
199 |
200 | `options` is passed on to the underlying store.
201 |
202 | If no callback is passed, a promise is returned.
203 |
204 |
205 |
206 | ### `db.batch(array[, options][, callback])` _(array form)_
207 |
208 | batch()
can be used for very fast bulk-write operations (both _put_ and _delete_). The `array` argument should contain a list of operations to be executed sequentially, although as a whole they are performed as an atomic operation inside the underlying store.
209 |
210 | Each operation is contained in an object having the following properties: `type`, `key`, `value`, where the _type_ is either `'put'` or `'del'`. In the case of `'del'` the `value` property is ignored. Any entries with a `key` of `null` or `undefined` will cause an error to be returned on the `callback` and any `type: 'put'` entry with a `value` of `null` or `undefined` will return an error.
211 |
212 | If `key` and `value` are defined but `type` is not, it will default to `'put'`.
213 |
214 | ```js
215 | var ops = [
216 | { type: 'del', key: 'father' },
217 | { type: 'put', key: 'name', value: 'Yuri Irsenovich Kim' },
218 | { type: 'put', key: 'dob', value: '16 February 1941' },
219 | { type: 'put', key: 'spouse', value: 'Kim Young-sook' },
220 | { type: 'put', key: 'occupation', value: 'Clown' }
221 | ]
222 |
223 | db.batch(ops, function (err) {
224 | if (err) return console.log('Ooops!', err)
225 | console.log('Great success dear leader!')
226 | })
227 | ```
228 |
229 | `options` is passed on to the underlying store.
230 |
231 | If no callback is passed, a promise is returned.
232 |
233 |
234 |
235 | ### `db.batch()` _(chained form)_
236 |
237 | batch()
, when called with no arguments will return a `Batch` object which can be used to build, and eventually commit, an atomic batch operation. Depending on how it's used, it is possible to obtain greater performance when using the chained form of `batch()` over the array form.
238 |
239 | ```js
240 | db.batch()
241 | .del('father')
242 | .put('name', 'Yuri Irsenovich Kim')
243 | .put('dob', '16 February 1941')
244 | .put('spouse', 'Kim Young-sook')
245 | .put('occupation', 'Clown')
246 | .write(function () { console.log('Done!') })
247 | ```
248 |
249 | **`batch.put(key, value)`**
250 |
251 | Queue a _put_ operation on the current batch, not committed until a `write()` is called on the batch.
252 |
253 | This method may `throw` a `WriteError` if there is a problem with your put (such as the `value` being `null` or `undefined`).
254 |
255 | **`batch.del(key)`**
256 |
257 | Queue a _del_ operation on the current batch, not committed until a `write()` is called on the batch.
258 |
259 | This method may `throw` a `WriteError` if there is a problem with your delete.
260 |
261 | **`batch.clear()`**
262 |
263 | Clear all queued operations on the current batch, any previous operations will be discarded.
264 |
265 | **`batch.length`**
266 |
267 | The number of queued operations on the current batch.
268 |
269 | **`batch.write([callback])`**
270 |
271 | Commit the queued operations for this batch. All operations not _cleared_ will be written to the underlying store atomically, that is, they will either all succeed or fail with no partial commits.
272 |
273 | If no callback is passed, a promise is returned.
274 |
275 |
276 |
277 | ### `db.isOpen()`
278 |
279 | A `levelup` instance can be in one of the following states:
280 |
281 | - _"new"_ - newly created, not opened or closed
282 | - _"opening"_ - waiting for the underlying store to be opened
283 | - _"open"_ - successfully opened the store, available for use
284 | - _"closing"_ - waiting for the store to be closed
285 | - _"closed"_ - store has been successfully closed, should not be used
286 |
287 | `isOpen()` will return `true` only when the state is "open".
288 |
289 |
290 |
291 | ### `db.isClosed()`
292 |
293 | _See isOpen()
_
294 |
295 | `isClosed()` will return `true` only when the state is "closing" _or_ "closed", it can be useful for determining if read and write operations are permissible.
296 |
297 |
298 |
299 | ### `db.createReadStream([options])`
300 |
301 | Returns a [Readable Stream](https://nodejs.org/docs/latest/api/stream.html#stream_readable_streams) of key-value pairs. A pair is an object with `key` and `value` properties. By default it will stream all entries in the underlying store from start to end. Use the options described below to control the range, direction and results.
302 |
303 | ```js
304 | db.createReadStream()
305 | .on('data', function (data) {
306 | console.log(data.key, '=', data.value)
307 | })
308 | .on('error', function (err) {
309 | console.log('Oh my!', err)
310 | })
311 | .on('close', function () {
312 | console.log('Stream closed')
313 | })
314 | .on('end', function () {
315 | console.log('Stream ended')
316 | })
317 | ```
318 |
319 | You can supply an options object as the first parameter to `createReadStream()` with the following properties:
320 |
321 | - `gt` (greater than), `gte` (greater than or equal) define the lower bound of the range to be streamed. Only entries where the key is greater than (or equal to) this option will be included in the range. When `reverse=true` the order will be reversed, but the entries streamed will be the same.
322 |
323 | - `lt` (less than), `lte` (less than or equal) define the higher bound of the range to be streamed. Only entries where the key is less than (or equal to) this option will be included in the range. When `reverse=true` the order will be reversed, but the entries streamed will be the same.
324 |
325 | - `reverse` _(boolean, default: `false`)_: stream entries in reverse order. Beware that due to the way that stores like LevelDB work, a reverse seek can be slower than a forward seek.
326 |
327 | - `limit` _(number, default: `-1`)_: limit the number of entries collected by this stream. This number represents a _maximum_ number of entries and may not be reached if you get to the end of the range first. A value of `-1` means there is no limit. When `reverse=true` the entries with the highest keys will be returned instead of the lowest keys.
328 |
329 | - `keys` _(boolean, default: `true`)_: whether the results should contain keys. If set to `true` and `values` set to `false` then results will simply be keys, rather than objects with a `key` property. Used internally by the `createKeyStream()` method.
330 |
331 | - `values` _(boolean, default: `true`)_: whether the results should contain values. If set to `true` and `keys` set to `false` then results will simply be values, rather than objects with a `value` property. Used internally by the `createValueStream()` method.
332 |
333 | Legacy options:
334 |
335 | - `start`: instead use `gte`
336 |
337 | - `end`: instead use `lte`
338 |
339 |
340 |
341 | ### `db.createKeyStream([options])`
342 |
343 | Returns a [Readable Stream](https://nodejs.org/docs/latest/api/stream.html#stream_readable_streams) of keys rather than key-value pairs. Use the same options as described for createReadStream
to control the range and direction.
344 |
345 | You can also obtain this stream by passing an options object to `createReadStream()` with `keys` set to `true` and `values` set to `false`. The result is equivalent; both streams operate in [object mode](https://nodejs.org/docs/latest/api/stream.html#stream_object_mode).
346 |
347 | ```js
348 | db.createKeyStream()
349 | .on('data', function (data) {
350 | console.log('key=', data)
351 | })
352 |
353 | // same as:
354 | db.createReadStream({ keys: true, values: false })
355 | .on('data', function (data) {
356 | console.log('key=', data)
357 | })
358 | ```
359 |
360 |
361 |
362 | ### `db.createValueStream([options])`
363 |
364 | Returns a [Readable Stream](https://nodejs.org/docs/latest/api/stream.html#stream_readable_streams) of values rather than key-value pairs. Use the same options as described for createReadStream
to control the range and direction.
365 |
366 | You can also obtain this stream by passing an options object to `createReadStream()` with `values` set to `true` and `keys` set to `false`. The result is equivalent; both streams operate in [object mode](https://nodejs.org/docs/latest/api/stream.html#stream_object_mode).
367 |
368 | ```js
369 | db.createValueStream()
370 | .on('data', function (data) {
371 | console.log('value=', data)
372 | })
373 |
374 | // same as:
375 | db.createReadStream({ keys: false, values: true })
376 | .on('data', function (data) {
377 | console.log('value=', data)
378 | })
379 | ```
380 |
381 | ## Promise Support
382 |
383 | `level-browserify` ships with native `Promise` support out of the box.
384 |
385 | Each function taking a callback also can be used as a promise, if the callback is omitted. This applies for:
386 |
387 | - `db.get(key[, options])`
388 | - `db.put(key, value[, options])`
389 | - `db.del(key[, options])`
390 | - `db.batch(ops[, options])`
391 | - `db.batch().write()`
392 |
393 | The only exception is the `level-browserify` constructor itself, which if no callback is passed will lazily open the underlying store in the background.
394 |
395 | Example:
396 |
397 | ```js
398 | var db = level('./my-db')
399 |
400 | db.put('foo', 'bar')
401 | .then(function () { return db.get('foo') })
402 | .then(function (value) { console.log(value) })
403 | .catch(function (err) { console.error(err) })
404 | ```
405 |
406 | Or using `async/await`:
407 |
408 | ```js
409 | var main = async () => {
410 | const db = level('./my-db')
411 |
412 | await db.put('foo', 'bar')
413 | console.log(await db.get('foo'))
414 | }
415 | ```
416 |
417 | ## Events
418 |
419 | `levelup` is an [`EventEmitter`](https://nodejs.org/api/events.html) and emits the following events.
420 |
421 | | Event | Description | Arguments |
422 | | :-------- | :-------------------------- | :------------------- |
423 | | `put` | Key has been updated | `key, value` (any) |
424 | | `del` | Key has been deleted | `key` (any) |
425 | | `batch` | Batch has executed | `operations` (array) |
426 | | `opening` | Underlying store is opening | - |
427 | | `open` | Store has opened | - |
428 | | `ready` | Alias of `open` | - |
429 | | `closing` | Store is closing | - |
430 | | `closed` | Store has closed. | - |
431 |
432 | For example you can do:
433 |
434 | ```js
435 | db.on('put', function (key, value) {
436 | console.log('inserted', { key, value })
437 | })
438 | ```
439 |
440 | ## Contributing
441 |
442 | [`Level/level-browserify`](https://github.com/Level/level-browserify) is an **OPEN Open Source Project**. This means that:
443 |
444 | > 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.
445 |
446 | See the [Contribution Guide](https://github.com/Level/community/blob/master/CONTRIBUTING.md) for more details.
447 |
448 | ## Donate
449 |
450 | To 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), [npm](https://www.npmjs.com/) packages and (soon) [our website](http://leveldb.org). 💖
451 |
452 | ### Backers
453 |
454 | [](https://opencollective.com/level)
455 |
456 | ### Sponsors
457 |
458 | [](https://opencollective.com/level)
459 |
460 | ## License
461 |
462 | [MIT](LICENSE.md) © 2012-present [Contributors](CONTRIBUTORS.md).
463 |
464 | [level-badge]: http://leveldb.org/img/badge.svg
465 |
466 | [levelup]: https://github.com/Level/levelup
467 |
468 | [leveldown]: https://github.com/Level/leveldown
469 |
470 | [encoding-down]: https://github.com/Level/encoding-down
471 |
472 | [level-js]: https://github.com/Level/level-js
473 |
--------------------------------------------------------------------------------
/UPGRADING.md:
--------------------------------------------------------------------------------
1 | # Upgrade Guide
2 |
3 | This document describes breaking changes and how to upgrade. For a complete list of changes including minor and patch releases, please refer to the [changelog](CHANGELOG.md).
4 |
5 | ## v2
6 |
7 | Dropped support for node 4.
8 |
9 | The parts that make up `level-browserify` have been refactored to increase modularity. This is an upgrade to `leveldown@^4.0.0`, `level-js@^3.0.0` and `level-packager@~3.1.0`, which in turn upgraded to `levelup@^3.0.0`. The responsibility of encoding keys and values moved from [`levelup`](https://github.com/Level/levelup) to [`encoding-down`](https://github.com/Level/encoding-down), which comes bundled with [`level-packager`](https://github.com/Level/packager).
10 |
11 | Also, upgrading `leveldown` and `level-js` means upgrading to `abstract-leveldown@~5.0.0` which in turn contains breaking changes to [`.batch()`](https://github.com/Level/abstract-leveldown/commit/a2621ad70571f6ade9d2be42632ece042e068805). Though this is negated by `levelup`, we decided to release a new major version in the event of dependents reaching down into `db.db`.
12 |
13 | Being a convenience package, `level-browserify` glues the parts back together to form a drop-in replacement for the users of `levelup@1`, while staying fully compatible with `level-browserify@1`. One thing we do get for free, is native Promise support.
14 |
15 | ```js
16 | const db = level('db')
17 | await db.put('foo', 'bar')
18 | console.log(await db.get('foo'))
19 | ```
20 |
21 | This does not affect the existing callback API, functionality-wise or performance-wise.
22 |
23 | For more information please check the corresponding `CHANGELOG.md` for:
24 |
25 | - [`levelup`](https://github.com/Level/levelup/blob/master/CHANGELOG.md)
26 | - [`leveldown`](https://github.com/Level/leveldown/blob/master/CHANGELOG.md)
27 | - [`level-js`](https://github.com/Level/level-js/blob/master/CHANGELOG.md)
28 | - [`level-packager`](https://github.com/Level/packager/blob/master/CHANGELOG.md)
29 |
--------------------------------------------------------------------------------
/browser.js:
--------------------------------------------------------------------------------
1 |
2 | module.exports = require('level-packager')(require('level-js'))
3 |
--------------------------------------------------------------------------------
/level.js:
--------------------------------------------------------------------------------
1 | module.exports = require('level-packager')(require('leveldown'))
2 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "level-browserify",
3 | "version": "2.0.0",
4 | "description": "Fast & simple storage - a Node.js-style LevelDB wrapper (a convenience package bundling LevelUP & LevelDOWN or Level.js)",
5 | "license": "MIT",
6 | "main": "level.js",
7 | "scripts": {
8 | "test": "standard && hallmark && nyc node test-node.js",
9 | "coverage": "nyc report --reporter=text-lcov | coveralls",
10 | "test-browser-local": "standard && airtap --local test-browser.js",
11 | "hallmark": "hallmark --fix",
12 | "dependency-check": "dependency-check -i levelup -i encoding-down . browser.js test*.js",
13 | "prepublishOnly": "npm run dependency-check"
14 | },
15 | "browser": "browser.js",
16 | "dependencies": {
17 | "level-js": "^3.0.0",
18 | "level-packager": "^4.0.0",
19 | "leveldown": "^4.0.0"
20 | },
21 | "devDependencies": {
22 | "airtap": "^2.0.0",
23 | "coveralls": "^3.0.2",
24 | "dependency-check": "^3.3.0",
25 | "hallmark": "^0.1.0",
26 | "level-community": "^3.0.0",
27 | "nyc": "^12.0.2",
28 | "standard": "^12.0.0",
29 | "tape": "^4.9.0"
30 | },
31 | "hallmark": {
32 | "community": "level-community"
33 | },
34 | "repository": {
35 | "type": "git",
36 | "url": "https://github.com/Level/level-browserify.git"
37 | },
38 | "homepage": "https://github.com/Level/level-browserify",
39 | "keywords": [
40 | "level",
41 | "leveldb",
42 | "stream",
43 | "database",
44 | "db",
45 | "store",
46 | "storage",
47 | "json",
48 | "indexeddb"
49 | ]
50 | }
51 |
--------------------------------------------------------------------------------
/test-browser.js:
--------------------------------------------------------------------------------
1 | 'use strict'
2 |
3 | var test = require('tape')
4 | var leveljs = require('level-js')
5 | var testCommon = require('level-js/test/util/test-common')
6 | var levelup = require('levelup')
7 | var level = require('./')
8 | var encoding = require('encoding-down')
9 |
10 | require('level-packager/abstract/base-test')(test, level)
11 | require('level-packager/abstract/db-values-test')(test, level)
12 |
13 | test('setup', testCommon.setUp)
14 |
15 | test('levelup put', function (t) {
16 | t.plan(4)
17 |
18 | var down = leveljs(testCommon.location())
19 | var db = levelup(down)
20 |
21 | db.put('name', 'LevelUP string', function (err) {
22 | t.ifError(err, 'no put error')
23 |
24 | db.get('name', { asBuffer: false }, function (err, value) {
25 | t.ifError(err, 'no get error')
26 | t.is(value, 'LevelUP string')
27 |
28 | db.close(function (err) {
29 | t.ifError(err, 'no close error')
30 | })
31 | })
32 | })
33 | })
34 |
35 | test('binary', function (t) {
36 | t.plan(9)
37 |
38 | var down = leveljs(testCommon.location())
39 | var db = levelup(encoding(down, { valueEncoding: 'binary' }))
40 | var buf = Buffer.from('00ff', 'hex')
41 |
42 | db.put('binary', buf, function (err) {
43 | t.ifError(err, 'no put error')
44 |
45 | db.get('binary', function (err, value) {
46 | t.ifError(err, 'no get error')
47 | t.ok(Buffer.isBuffer(value), 'is a buffer')
48 | t.same(value, buf)
49 |
50 | db.get('binary', { valueEncoding: 'id' }, function (err, value) {
51 | t.ifError(err, 'no get error')
52 | t.notOk(Buffer.isBuffer(value), 'is not a buffer')
53 | t.ok(value instanceof Uint8Array, 'is a Uint8Array')
54 | t.same(Buffer.from(value), buf)
55 |
56 | db.close(function (err) {
57 | t.ifError(err, 'no close error')
58 | })
59 | })
60 | })
61 | })
62 | })
63 |
64 | test('teardown', testCommon.tearDown)
65 |
--------------------------------------------------------------------------------
/test-node.js:
--------------------------------------------------------------------------------
1 | require('level-packager/abstract/test')(require('tape'), require('./'))
2 |
--------------------------------------------------------------------------------