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 | ### Summary
|
8 |
|
9 | There has been quite some work done for this new major version:
|
10 |
|
11 | 1. Make `levelup` more generic by reducing focus on [`leveldown`](https://github.com/Level/leveldown) and [`LevelDB`](https://github.com/google/leveldb).
|
12 | 2. Make `levelup` more generic by removing code related to encodings, which would allow \*down implementations to manage encodings themselves.
|
13 | 3. Use [`standard`](https://github.com/standard/standard) as linter to avoid bikeshedding.
|
14 | 4. 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 |
|
16 | Point `1` and `2` also helps out with reducing complexity.
|
17 |
|
18 | ### Upgrade Guide
|
19 |
|
20 | Since `levelup` no longer tries to load `leveldown` as a default backend you have to provide a backend instance yourself.
|
21 |
|
22 | So if you previously did:
|
23 |
|
24 | ```
|
25 | $ npm i levelup leveldown --save
|
26 | ```
|
27 |
|
28 | And in your code you did something like:
|
29 |
|
30 | ```js
|
31 | const levelup = require('levelup')
|
32 | const db = levelup('/path/to/db')
|
33 | ```
|
34 |
|
35 | You should now do (for identical functionality):
|
36 |
|
37 | ```js
|
38 | const levelup = require('levelup')
|
39 | const encode = require('encoding-down')
|
40 | const leveldown = require('leveldown')
|
41 | const db = levelup(encode(leveldown('/path/to/db')))
|
42 | ```
|
43 |
|
44 | Note 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 |
|
46 | This 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 |
|
48 | Switching from `levelup` and `leveldown` combo to only using `level` you would do:
|
49 |
|
50 | ```js
|
51 | const level = require('level')
|
52 | const db = level('/path/to/db')
|
53 | ```
|
54 |
|
55 | Also, we aim to simplify using `memdown` in the same way by updating `level-mem`.
|
56 |
|
57 | For 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 |
|
59 | So if you previously did:
|
60 |
|
61 | ```js
|
62 | const db = levelup('/path/to/db', {
|
63 | db: function (location) {
|
64 | return new CustomLevelDOWN(location)
|
65 | }
|
66 | })
|
67 | ```
|
68 |
|
69 | You should now do (for identical functionality):
|
70 |
|
71 | ```js
|
72 | const encode = require('encoding-down')
|
73 | const db = levelup(encode(new CustomLevelDOWN('/path/to/db')))
|
74 | ```
|