UNPKG

2.87 kBMarkdownView Raw
1# Upgrade Guide
2
3This 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### Summary
8
9There has been quite some work done for this new major version:
10
111. Make `levelup` more generic by reducing focus on [`leveldown`](https://github.com/Level/leveldown) and [`LevelDB`](https://github.com/google/leveldb).
122. Make `levelup` more generic by removing code related to encodings, which would allow \*down implementations to manage encodings themselves.
133. Use [`standard`](https://github.com/standard/standard) as linter to avoid bikeshedding.
144. Add a native `Promise` API for promise using geeks. Many have been asking for this. Also `async/await` is awesome. Breaking change: previously, if you did not pass a callback to an async function and there was an error, `levelup` would emit an `error` event instead. This is no longer true.
15
16Point `1` and `2` also helps out with reducing complexity.
17
18### Upgrade Guide
19
20Since `levelup` no longer tries to load `leveldown` as a default backend you have to provide a backend instance yourself.
21
22So if you previously did:
23
24```
25$ npm i levelup leveldown --save
26```
27
28And in your code you did something like:
29
30```js
31const levelup = require('levelup')
32const db = levelup('/path/to/db')
33```
34
35You should now do (for identical functionality):
36
37```js
38const levelup = require('levelup')
39const encode = require('encoding-down')
40const leveldown = require('leveldown')
41const db = levelup(encode(leveldown('/path/to/db')))
42```
43
44Note that we have moved out encodings into [`encoding-down`](https://github.com/level/encoding-down), which in itself is a \*down that wraps a \*down (meta ftw). It basically just sits in between `levelup` and the _actual_ backend to operate on encodings for keys and values. Default encoding is `'utf8'` like before.
45
46This obviously means everyone has to do a lot of code rewrite which is bad. So we aim to fix this by putting that code into [`level@2.0.0`](https://github.com/level/level), which already is used as a convenience package.
47
48Switching from `levelup` and `leveldown` combo to only using `level` you would do:
49
50```js
51const level = require('level')
52const db = level('/path/to/db')
53```
54
55Also, we aim to simplify using `memdown` in the same way by updating `level-mem`.
56
57For more advanced usage with custom versions of `abstract-leveldown`, the first parameter to `levelup()` should be an `abstract-leveldown` instead of passing a factory function via `options.db`.
58
59So if you previously did:
60
61```js
62const db = levelup('/path/to/db', {
63 db: function (location) {
64 return new CustomLevelDOWN(location)
65 }
66})
67```
68
69You should now do (for identical functionality):
70
71```js
72const encode = require('encoding-down')
73const db = levelup(encode(new CustomLevelDOWN('/path/to/db')))
74```