UNPKG

33.6 kBMarkdownView Raw
1LevelUP
2=======
3
4<img alt="LevelDB Logo" height="100" src="http://leveldb.org/img/logo.svg">
5
6**Fast & simple storage - a Node.js-style LevelDB wrapper**
7
8[![Build Status](https://secure.travis-ci.org/Level/levelup.svg?branch=master)](http://travis-ci.org/Level/levelup)
9[![dependencies](https://david-dm.org/Level/levelup.svg)](https://david-dm.org/level/levelup)
10
11[![NPM](https://nodei.co/npm/levelup.png?stars&downloads&downloadRank)](https://nodei.co/npm/levelup/) [![NPM](https://nodei.co/npm-dl/levelup.png?months=6&height=3)](https://nodei.co/npm/levelup/)
12
13
14 * <a href="#intro">Introduction</a>
15 * <a href="#leveldown">Relationship to LevelDOWN</a>
16 * <a href="#platforms">Tested &amp; supported platforms</a>
17 * <a href="#basic">Basic usage</a>
18 * <a href="#api">API</a>
19 * <a href="#events">Events</a>
20 * <a href="#json">JSON data</a>
21 * <a href="#custom_encodings">Custom encodings</a>
22 * <a href="#extending">Extending LevelUP</a>
23 * <a href="#multiproc">Multi-process access</a>
24 * <a href="#support">Getting support</a>
25 * <a href="#contributing">Contributing</a>
26 * <a href="#license">Licence &amp; copyright</a>
27
28<a name="intro"></a>
29Introduction
30------------
31
32**[LevelDB](https://github.com/google/leveldb)** is a simple key/value data store built by Google, inspired by BigTable. It's used in Google Chrome and many other products. LevelDB supports arbitrary byte arrays as both keys and values, singular *get*, *put* and *delete* operations, *batched put and delete*, bi-directional iterators and simple compression using the very fast [Snappy](http://google.github.io/snappy/) algorithm.
33
34**LevelUP** aims to expose the features of LevelDB in a **Node.js-friendly way**. All standard `Buffer` encoding types are supported, as is a special JSON encoding. LevelDB's iterators are exposed as a Node.js-style **readable stream**.
35
36LevelDB stores entries **sorted lexicographically by keys**. This makes LevelUP's <a href="#createReadStream"><code>ReadStream</code></a> interface a very powerful query mechanism.
37
38**LevelUP** is an **OPEN Open Source Project**, see the <a href="#contributing">Contributing</a> section to find out what this means.
39
40<a name="leveldown"></a>
41Relationship to LevelDOWN
42-------------------------
43
44LevelUP is designed to be backed by **[LevelDOWN](https://github.com/level/leveldown/)** which provides a pure C++ binding to LevelDB and can be used as a stand-alone package if required.
45
46**As of version 0.9, LevelUP no longer requires LevelDOWN as a dependency so you must `npm install leveldown` when you install LevelUP.**
47
48LevelDOWN is now optional because LevelUP can be used with alternative backends, such as **[level.js](https://github.com/maxogden/level.js)** in the browser or [MemDOWN](https://github.com/level/memdown) for a pure in-memory store.
49
50LevelUP will look for LevelDOWN and throw an error if it can't find it in its Node `require()` path. It will also tell you if the installed version of LevelDOWN is incompatible.
51
52**The [level](https://github.com/level/level) package is available as an alternative installation mechanism.** Install it instead to automatically get both LevelUP & LevelDOWN. It exposes LevelUP on its export (i.e. you can `var leveldb = require('level')`).
53
54
55<a name="platforms"></a>
56Tested & supported platforms
57----------------------------
58
59 * **Linux**: including ARM platforms such as Raspberry Pi *and Kindle!*
60 * **Mac OS**
61 * **Solaris**: including Joyent's SmartOS & Nodejitsu
62 * **Windows**: Node 0.10 and above only. See installation instructions for *node-gyp's* dependencies [here](https://github.com/TooTallNate/node-gyp#installation), you'll need these (free) components from Microsoft to compile and run any native Node add-on in Windows.
63
64<a name="basic"></a>
65Basic usage
66-----------
67
68First you need to install LevelUP!
69
70```sh
71$ npm install levelup leveldown
72```
73
74Or
75
76```sh
77$ npm install level
78```
79
80*(this second option requires you to use LevelUP by calling `var levelup = require('level')`)*
81
82
83All operations are asynchronous although they don't necessarily require a callback if you don't need to know when the operation was performed.
84
85```js
86var levelup = require('levelup')
87
88// 1) Create our database, supply location and options.
89// This will create or open the underlying LevelDB store.
90var db = levelup('./mydb')
91
92// 2) put a key & value
93db.put('name', 'LevelUP', function (err) {
94 if (err) return console.log('Ooops!', err) // some kind of I/O error
95
96 // 3) fetch by key
97 db.get('name', function (err, value) {
98 if (err) return console.log('Ooops!', err) // likely the key was not found
99
100 // ta da!
101 console.log('name=' + value)
102 })
103})
104```
105
106<a name="api"></a>
107## API
108
109 * <a href="#ctor"><code><b>levelup()</b></code></a>
110 * <a href="#open"><code>db.<b>open()</b></code></a>
111 * <a href="#close"><code>db.<b>close()</b></code></a>
112 * <a href="#put"><code>db.<b>put()</b></code></a>
113 * <a href="#get"><code>db.<b>get()</b></code></a>
114 * <a href="#del"><code>db.<b>del()</b></code></a>
115 * <a href="#batch"><code>db.<b>batch()</b></code> *(array form)*</a>
116 * <a href="#batch_chained"><code>db.<b>batch()</b></code> *(chained form)*</a>
117 * <a href="#isOpen"><code>db.<b>isOpen()</b></code></a>
118 * <a href="#isClosed"><code>db.<b>isClosed()</b></code></a>
119 * <a href="#createReadStream"><code>db.<b>createReadStream()</b></code></a>
120 * <a href="#createKeyStream"><code>db.<b>createKeyStream()</b></code></a>
121 * <a href="#createValueStream"><code>db.<b>createValueStream()</b></code></a>
122
123### Special operations exposed by LevelDOWN
124
125 * <a href="#approximateSize"><code>db.db.<b>approximateSize()</b></code></a>
126 * <a href="#getProperty"><code>db.db.<b>getProperty()</b></code></a>
127 * <a href="#destroy"><code><b>leveldown.destroy()</b></code></a>
128 * <a href="#repair"><code><b>leveldown.repair()</b></code></a>
129
130### Special Notes
131 * <a href="#writeStreams">What happened to <code><b>db.createWriteStream()</b></code></a>
132
133
134--------------------------------------------------------
135<a name="ctor"></a>
136### levelup(location[, options[, callback]])
137### levelup(options[, callback ])
138### levelup(db[, callback ])
139<code>levelup()</code> is the main entry point for creating a new LevelUP instance and opening the underlying store with LevelDB.
140
141This function returns a new instance of LevelUP and will also initiate an <a href="#open"><code>open()</code></a> operation. Opening the database is an asynchronous operation which will trigger your callback if you provide one. The callback should take the form: `function (err, db) {}` where the `db` is the LevelUP instance. If you don't provide a callback, any read & write operations are simply queued internally until the database is fully opened.
142
143This leads to two alternative ways of managing a new LevelUP instance:
144
145```js
146levelup(location, options, function (err, db) {
147 if (err) throw err
148 db.get('foo', function (err, value) {
149 if (err) return console.log('foo does not exist')
150 console.log('got foo =', value)
151 })
152})
153
154// vs the equivalent:
155
156var db = levelup(location, options) // will throw if an error occurs
157db.get('foo', function (err, value) {
158 if (err) return console.log('foo does not exist')
159 console.log('got foo =', value)
160})
161```
162
163The `location` argument is available as a read-only property on the returned LevelUP instance.
164
165The `levelup(options, callback)` form (with optional `callback`) is only available where you provide a valid `'db'` property on the options object (see below). Only for back-ends that don't require a `location` argument, such as [MemDOWN](https://github.com/level/memdown).
166
167For example:
168
169```js
170var levelup = require('levelup')
171var memdown = require('memdown')
172var db = levelup({ db: memdown })
173```
174
175The `levelup(db, callback)` form (with optional `callback`) is only available where `db` is a factory function, as would be provided as a `'db'` property on an `options` object (see below). Only for back-ends that don't require a `location` argument, such as [MemDOWN](https://github.com/level/memdown).
176
177For example:
178
179```js
180var levelup = require('levelup')
181var memdown = require('memdown')
182var db = levelup(memdown)
183```
184
185#### `options`
186
187`levelup()` takes an optional options object as its second argument; the following properties are accepted:
188
189* `'createIfMissing'` *(boolean, default: `true`)*: If `true`, will initialise an empty database at the specified location if one doesn't already exist. If `false` and a database doesn't exist you will receive an error in your `open()` callback and your database won't open.
190
191* `'errorIfExists'` *(boolean, default: `false`)*: If `true`, you will receive an error in your `open()` callback if the database exists at the specified location.
192
193* `'compression'` *(boolean, default: `true`)*: If `true`, all *compressible* data will be run through the Snappy compression algorithm before being stored. Snappy is very fast and shouldn't gain much speed by disabling so leave this on unless you have good reason to turn it off.
194
195* `'cacheSize'` *(number, default: `8 * 1024 * 1024`)*: The size (in bytes) of the in-memory [LRU](http://en.wikipedia.org/wiki/Cache_algorithms#Least_Recently_Used) cache with frequently used uncompressed block contents.
196
197* `'keyEncoding'` and `'valueEncoding'` *(string, default: `'utf8'`)*: The encoding of the keys and values passed through Node.js' `Buffer` implementation (see [Buffer#toString()](http://nodejs.org/docs/latest/api/buffer.html#buffer_buf_tostring_encoding_start_end)).
198 <p><code>'utf8'</code> is the default encoding for both keys and values so you can simply pass in strings and expect strings from your <code>get()</code> operations. You can also pass <code>Buffer</code> objects as keys and/or values and conversion will be performed.</p>
199 <p>Supported encodings are: hex, utf8, ascii, binary, base64, ucs2, utf16le.</p>
200 <p><code>'json'</code> encoding is also supported, see below.</p>
201
202* `'db'` *(object, default: LevelDOWN)*: LevelUP is backed by [LevelDOWN](https://github.com/level/leveldown/) to provide an interface to LevelDB. You can completely replace the use of LevelDOWN by providing a "factory" function that will return a LevelDOWN API compatible object given a `location` argument. For further information, see [MemDOWN](https://github.com/level/memdown), a fully LevelDOWN API compatible replacement that uses a memory store rather than LevelDB. Also see [Abstract LevelDOWN](http://github.com/level/abstract-leveldown), a partial implementation of the LevelDOWN API that can be used as a base prototype for a LevelDOWN substitute.
203
204Additionally, each of the main interface methods accept an optional options object that can be used to override `'keyEncoding'` and `'valueEncoding'`.
205
206--------------------------------------------------------
207<a name="open"></a>
208### db.open([callback])
209<code>open()</code> opens the underlying LevelDB store. In general **you should never need to call this method directly** as it's automatically called by <a href="#ctor"><code>levelup()</code></a>.
210
211However, it is possible to *reopen* a database after it has been closed with <a href="#close"><code>close()</code></a>, although this is not generally advised.
212
213--------------------------------------------------------
214<a name="close"></a>
215### db.close([callback])
216<code>close()</code> closes the underlying LevelDB store. The callback will receive any error encountered during closing as the first argument.
217
218You should always clean up your LevelUP instance by calling `close()` when you no longer need it to free up resources. A LevelDB store cannot be opened by multiple instances of LevelDB/LevelUP simultaneously.
219
220--------------------------------------------------------
221<a name="put"></a>
222### db.put(key, value[, options][, callback])
223<code>put()</code> is the primary method for inserting data into the store. Both the `key` and `value` can be arbitrary data objects.
224
225The callback argument is optional but if you don't provide one and an error occurs then expect the error to be thrown.
226
227#### `options`
228
229Encoding of the `key` and `value` objects will adhere to `'keyEncoding'` and `'valueEncoding'` options provided to <a href="#ctor"><code>levelup()</code></a>, although you can provide alternative encoding settings in the options for `put()` (it's recommended that you stay consistent in your encoding of keys and values in a single store).
230
231If you provide a `'sync'` value of `true` in your `options` object, LevelDB will perform a synchronous write of the data; although the operation will be asynchronous as far as Node is concerned. Normally, LevelDB passes the data to the operating system for writing and returns immediately, however a synchronous write will use `fsync()` or equivalent so your callback won't be triggered until the data is actually on disk. Synchronous filesystem writes are **significantly** slower than asynchronous writes but if you want to be absolutely sure that the data is flushed then you can use `'sync': true`.
232
233--------------------------------------------------------
234<a name="get"></a>
235### db.get(key[, options][, callback])
236<code>get()</code> is the primary method for fetching data from the store. The `key` can be an arbitrary data object. If it doesn't exist in the store then the callback will receive an error as its first argument. 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`.
237
238```js
239db.get('foo', function (err, value) {
240 if (err) {
241 if (err.notFound) {
242 // handle a 'NotFoundError' here
243 return
244 }
245 // I/O or other error, pass it up the callback chain
246 return callback(err)
247 }
248
249 // .. handle `value` here
250})
251```
252
253#### `options`
254
255Encoding of the `key` and `value` objects is the same as in <a href="#put"><code>put</code></a>.
256
257LevelDB will by default fill the in-memory LRU Cache with data from a call to get. Disabling this is done by setting `fillCache` to `false`.
258
259--------------------------------------------------------
260<a name="del"></a>
261### db.del(key[, options][, callback])
262<code>del()</code> is the primary method for removing data from the store.
263```js
264db.del('foo', function (err) {
265 if (err)
266 // handle I/O or other error
267});
268```
269
270#### `options`
271
272Encoding of the `key` object will adhere to the `'keyEncoding'` option provided to <a href="#ctor"><code>levelup()</code></a>, although you can provide alternative encoding settings in the options for `del()` (it's recommended that you stay consistent in your encoding of keys and values in a single store).
273
274A `'sync'` option can also be passed, see <a href="#put"><code>put()</code></a> for details on how this works.
275
276--------------------------------------------------------
277<a name="batch"></a>
278### db.batch(array[, options][, callback]) *(array form)*
279<code>batch()</code> 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 LevelDB.
280
281Each 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.
282
283If `key` and `value` are defined but `type` is not, it will default to `'put'`.
284
285```js
286var ops = [
287 { type: 'del', key: 'father' }
288 , { type: 'put', key: 'name', value: 'Yuri Irsenovich Kim' }
289 , { type: 'put', key: 'dob', value: '16 February 1941' }
290 , { type: 'put', key: 'spouse', value: 'Kim Young-sook' }
291 , { type: 'put', key: 'occupation', value: 'Clown' }
292]
293
294db.batch(ops, function (err) {
295 if (err) return console.log('Ooops!', err)
296 console.log('Great success dear leader!')
297})
298```
299
300#### `options`
301
302See <a href="#put"><code>put()</code></a> for a discussion on the `options` object. You can overwrite default `'keyEncoding'` and `'valueEncoding'` and also specify the use of `sync` filesystem operations.
303
304In addition to encoding options for the whole batch you can also overwrite the encoding per operation, like:
305
306```js
307var ops = [{
308 type : 'put'
309 , key : new Buffer([1, 2, 3])
310 , value : { some: 'json' }
311 , keyEncoding : 'binary'
312 , valueEncoding : 'json'
313}]
314```
315
316--------------------------------------------------------
317<a name="batch_chained"></a>
318### db.batch() *(chained form)*
319<code>batch()</code>, when called with no arguments will return a `Batch` object which can be used to build, and eventually commit, an atomic LevelDB 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.
320
321```js
322db.batch()
323 .del('father')
324 .put('name', 'Yuri Irsenovich Kim')
325 .put('dob', '16 February 1941')
326 .put('spouse', 'Kim Young-sook')
327 .put('occupation', 'Clown')
328 .write(function () { console.log('Done!') })
329```
330
331<b><code>batch.put(key, value[, options])</code></b>
332
333Queue a *put* operation on the current batch, not committed until a `write()` is called on the batch.
334
335The optional `options` argument can be used to override the default `'keyEncoding'` and/or `'valueEncoding'`.
336
337This method may `throw` a `WriteError` if there is a problem with your put (such as the `value` being `null` or `undefined`).
338
339<b><code>batch.del(key[, options])</code></b>
340
341Queue a *del* operation on the current batch, not committed until a `write()` is called on the batch.
342
343The optional `options` argument can be used to override the default `'keyEncoding'`.
344
345This method may `throw` a `WriteError` if there is a problem with your delete.
346
347<b><code>batch.clear()</code></b>
348
349Clear all queued operations on the current batch, any previous operations will be discarded.
350
351<b><code>batch.length</code></b>
352
353The number of queued operations on the current batch.
354
355<b><code>batch.write([callback])</code></b>
356
357Commit the queued operations for this batch. All operations not *cleared* will be written to the database atomically, that is, they will either all succeed or fail with no partial commits. The optional `callback` will be called when the operation has completed with an *error* argument if an error has occurred; if no `callback` is supplied and an error occurs then this method will `throw` a `WriteError`.
358
359
360--------------------------------------------------------
361<a name="isOpen"></a>
362### db.isOpen()
363
364A LevelUP object can be in one of the following states:
365
366 * *"new"* - newly created, not opened or closed
367 * *"opening"* - waiting for the database to be opened
368 * *"open"* - successfully opened the database, available for use
369 * *"closing"* - waiting for the database to be closed
370 * *"closed"* - database has been successfully closed, should not be used
371
372`isOpen()` will return `true` only when the state is "open".
373
374--------------------------------------------------------
375<a name="isClosed"></a>
376### db.isClosed()
377
378*See <a href="#put"><code>isOpen()</code></a>*
379
380`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.
381
382--------------------------------------------------------
383<a name="createReadStream"></a>
384### db.createReadStream([options])
385
386You can obtain a **ReadStream** of the full database by calling the `createReadStream()` method. The resulting stream is a complete Node.js-style [Readable Stream](http://nodejs.org/docs/latest/api/stream.html#stream_readable_stream) where `'data'` events emit objects with `'key'` and `'value'` pairs. You can also use the `gt`, `lt` and `limit` options to control the range of keys that are streamed.
387
388```js
389db.createReadStream()
390 .on('data', function (data) {
391 console.log(data.key, '=', data.value)
392 })
393 .on('error', function (err) {
394 console.log('Oh my!', err)
395 })
396 .on('close', function () {
397 console.log('Stream closed')
398 })
399 .on('end', function () {
400 console.log('Stream ended')
401 })
402```
403
404The standard `pause()`, `resume()` and `destroy()` methods are implemented on the ReadStream, as is `pipe()` (see below). `'data'`, '`error'`, `'end'` and `'close'` events are emitted.
405
406Additionally, you can supply an options object as the first parameter to `createReadStream()` with the following options:
407
408* `'gt'` (greater than), `'gte'` (greater than or equal) define the lower bound of the range to be streamed. Only records 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 records streamed will be the same.
409
410* `'lt'` (less than), `'lte'` (less than or equal) define the higher bound of the range to be streamed. Only key/value pairs 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 records streamed will be the same.
411
412* `'start', 'end'` legacy ranges - instead use `'gte', 'lte'`
413
414* `'reverse'` *(boolean, default: `false`)*: a boolean, set true and the stream output will be reversed. Beware that due to the way LevelDB works, a reverse seek will be slower than a forward seek.
415
416* `'keys'` *(boolean, default: `true`)*: whether the `'data'` event should contain keys. If set to `true` and `'values'` set to `false` then `'data'` events will simply be keys, rather than objects with a `'key'` property. Used internally by the `createKeyStream()` method.
417
418* `'values'` *(boolean, default: `true`)*: whether the `'data'` event should contain values. If set to `true` and `'keys'` set to `false` then `'data'` events will simply be values, rather than objects with a `'value'` property. Used internally by the `createValueStream()` method.
419
420* `'limit'` *(number, default: `-1`)*: limit the number of results collected by this stream. This number represents a *maximum* number of results and may not be reached if you get to the end of the data first. A value of `-1` means there is no limit. When `reverse=true` the highest keys will be returned instead of the lowest keys.
421
422* `'fillCache'` *(boolean, default: `false`)*: whether LevelDB's LRU-cache should be filled with data read.
423
424* `'keyEncoding'` / `'valueEncoding'` *(string)*: the encoding applied to each read piece of data.
425
426--------------------------------------------------------
427<a name="createKeyStream"></a>
428### db.createKeyStream([options])
429
430A **KeyStream** is a **ReadStream** where the `'data'` events are simply the keys from the database so it can be used like a traditional stream rather than an object stream.
431
432You can obtain a KeyStream either by calling the `createKeyStream()` method on a LevelUP object or by passing an options object to `createReadStream()` with `keys` set to `true` and `values` set to `false`.
433
434```js
435db.createKeyStream()
436 .on('data', function (data) {
437 console.log('key=', data)
438 })
439
440// same as:
441db.createReadStream({ keys: true, values: false })
442 .on('data', function (data) {
443 console.log('key=', data)
444 })
445```
446
447--------------------------------------------------------
448<a name="createValueStream"></a>
449### db.createValueStream([options])
450
451A **ValueStream** is a **ReadStream** where the `'data'` events are simply the values from the database so it can be used like a traditional stream rather than an object stream.
452
453You can obtain a ValueStream either by calling the `createValueStream()` method on a LevelUP object or by passing an options object to `createReadStream()` with `values` set to `true` and `keys` set to `false`.
454
455```js
456db.createValueStream()
457 .on('data', function (data) {
458 console.log('value=', data)
459 })
460
461// same as:
462db.createReadStream({ keys: false, values: true })
463 .on('data', function (data) {
464 console.log('value=', data)
465 })
466```
467
468--------------------------------------------------------
469<a name="writeStreams"></a>
470#### What happened to `db.createWriteStream`?
471
472`db.createWriteStream()` has been removed in order to provide a smaller and more maintainable core. It primarily existed to create symmetry with `db.createReadStream()` but through much [discussion](https://github.com/level/levelup/issues/199), removing it was the best course of action.
473
474The main driver for this was performance. While `db.createReadStream()` performs well under most use cases, `db.createWriteStream()` was highly dependent on the application keys and values. Thus we can't provide a standard implementation and encourage more `write-stream` implementations to be created to solve the broad spectrum of use cases.
475
476Check out the implementations that the community has already produced [here](https://github.com/level/levelup/wiki/Modules#write-streams).
477
478--------------------------------------------------------
479<a name='approximateSize'></a>
480### db.db.approximateSize(start, end, callback)
481<code>approximateSize()</code> can used to get the approximate number of bytes of file system space used by the range `[start..end)`. The result may not include recently written data.
482
483```js
484var db = require('level')('./huge.db')
485
486db.db.approximateSize('a', 'c', function (err, size) {
487 if (err) return console.error('Ooops!', err)
488 console.log('Approximate size of range is %d', size)
489})
490```
491
492**Note:** `approximateSize()` is available via [LevelDOWN](https://github.com/level/leveldown/), which by default is accessible as the `db` property of your LevelUP instance. This is a specific LevelDB operation and is not likely to be available where you replace LevelDOWN with an alternative back-end via the `'db'` option.
493
494
495--------------------------------------------------------
496<a name='getProperty'></a>
497### db.db.getProperty(property)
498<code>getProperty</code> can be used to get internal details from LevelDB. When issued with a valid property string, a readable string will be returned (this method is synchronous).
499
500Currently, the only valid properties are:
501
502* <b><code>'leveldb.num-files-at-levelN'</code></b>: returns the number of files at level *N*, where N is an integer representing a valid level (e.g. "0").
503
504* <b><code>'leveldb.stats'</code></b>: returns a multi-line string describing statistics about LevelDB's internal operation.
505
506* <b><code>'leveldb.sstables'</code></b>: returns a multi-line string describing all of the *sstables* that make up contents of the current database.
507
508
509```js
510var db = require('level')('./huge.db')
511console.log(db.db.getProperty('leveldb.num-files-at-level3'))
512// → '243'
513```
514
515**Note:** `getProperty()` is available via [LevelDOWN](https://github.com/level/leveldown/), which by default is accessible as the `db` property of your LevelUP instance. This is a specific LevelDB operation and is not likely to be available where you replace LevelDOWN with an alternative back-end via the `'db'` option.
516
517
518--------------------------------------------------------
519<a name="destroy"></a>
520### leveldown.destroy(location, callback)
521<code>destroy()</code> is used to completely remove an existing LevelDB database directory. You can use this function in place of a full directory *rm* if you want to be sure to only remove LevelDB-related files. If the directory only contains LevelDB files, the directory itself will be removed as well. If there are additional, non-LevelDB files in the directory, those files, and the directory, will be left alone.
522
523The callback will be called when the destroy operation is complete, with a possible `error` argument.
524
525**Note:** `destroy()` is available via [LevelDOWN](https://github.com/level/leveldown/) which you will have to install seperately, e.g.:
526
527```js
528require('leveldown').destroy('./huge.db', function (err) { console.log('done!') })
529```
530
531--------------------------------------------------------
532<a name="repair"></a>
533### leveldown.repair(location, callback)
534<code>repair()</code> can be used to attempt a restoration of a damaged LevelDB store. From the LevelDB documentation:
535
536> If a DB cannot be opened, you may attempt to call this method to resurrect as much of the contents of the database as possible. Some data may be lost, so be careful when calling this function on a database that contains important information.
537
538You will find information on the *repair* operation in the *LOG* file inside the store directory.
539
540A `repair()` can also be used to perform a compaction of the LevelDB log into table files.
541
542The callback will be called when the repair operation is complete, with a possible `error` argument.
543
544**Note:** `repair()` is available via [LevelDOWN](https://github.com/level/leveldown/) which you will have to install seperately, e.g.:
545
546```js
547require('leveldown').repair('./huge.db', function (err) { console.log('done!') })
548```
549
550--------------------------------------------------------
551
552<a name="events"></a>
553Events
554------
555
556LevelUP emits events when the callbacks to the corresponding methods are called.
557
558* `db.emit('put', key, value)` emitted when a new value is `'put'`
559* `db.emit('del', key)` emitted when a value is deleted
560* `db.emit('batch', ary)` emitted when a batch operation has executed
561* `db.emit('ready')` emitted when the database has opened (`'open'` is synonym)
562* `db.emit('closed')` emitted when the database has closed
563* `db.emit('opening')` emitted when the database is opening
564* `db.emit('closing')` emitted when the database is closing
565
566If you do not pass a callback to an async function, and there is an error, LevelUP will `emit('error', err)` instead.
567
568<a name="json"></a>
569JSON data
570---------
571
572You specify `'json'` encoding for both keys and/or values, you can then supply JavaScript objects to LevelUP and receive them from all fetch operations, including ReadStreams. LevelUP will automatically *stringify* your objects and store them as *utf8* and parse the strings back into objects before passing them back to you.
573
574<a name="custom_encodings"></a>
575Custom encodings
576----------------
577
578A custom encoding may be provided by passing in an object as a value for `keyEncoding` or `valueEncoding` (wherever accepted), it must have the following properties:
579
580```js
581{
582 encode : function (val) { ... }
583 , decode : function (val) { ... }
584 , buffer : boolean // encode returns a buffer and decode accepts a buffer
585 , type : String // name of this encoding type.
586}
587```
588
589<a name="extending"></a>
590Extending LevelUP
591-----------------
592
593A list of <a href="https://github.com/level/levelup/wiki/Modules"><b>Node.js LevelDB modules and projects</b></a> can be found in the wiki.
594
595When attempting to extend the functionality of LevelUP, it is recommended that you consider using [level-hooks](https://github.com/dominictarr/level-hooks) and/or [level-sublevel](https://github.com/dominictarr/level-sublevel). **level-sublevel** is particularly helpful for keeping additional, extension-specific, data in a LevelDB store. It allows you to partition a LevelUP instance into multiple sub-instances that each correspond to discrete namespaced key ranges.
596
597<a name="multiproc"></a>
598Multi-process access
599--------------------
600
601LevelDB is thread-safe but is **not** suitable for accessing with multiple processes. You should only ever have a LevelDB database open from a single Node.js process. Node.js clusters are made up of multiple processes so a LevelUP instance cannot be shared between them either.
602
603See the <a href="https://github.com/level/levelup/wiki/Modules"><b>wiki</b></a> for some LevelUP extensions, including [multilevel](https://github.com/juliangruber/multilevel), that may help if you require a single data store to be shared across processes.
604
605<a name="support"></a>
606Getting support
607---------------
608
609There are multiple ways you can find help in using LevelDB in Node.js:
610
611 * **IRC:** you'll find an active group of LevelUP users in the **##leveldb** channel on Freenode, including most of the contributors to this project.
612 * **Mailing list:** there is an active [Node.js LevelDB](https://groups.google.com/forum/#!forum/node-levelup) Google Group.
613 * **GitHub:** you're welcome to open an issue here on this GitHub repository if you have a question.
614
615<a name="contributing"></a>
616Contributing
617------------
618
619LevelUP is an **OPEN Open Source Project**. This means that:
620
621> 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.
622
623See the [contribution guide](https://github.com/Level/community/blob/master/CONTRIBUTING.md) for more details.
624
625### Windows
626
627A large portion of the Windows support comes from code by [Krzysztof Kowalczyk](http://blog.kowalczyk.info/) [@kjk](https://twitter.com/kjk), see his Windows LevelDB port [here](http://code.google.com/r/kkowalczyk-leveldb/). If you're using LevelUP on Windows, you should give him your thanks!
628
629
630<a name="license"></a>
631License &amp; copyright
632-------------------
633
634Copyright &copy; 2012-2016 **LevelUP** [contributors](https://github.com/level/community#contributors).
635
636**LevelUP** is licensed under the MIT license. All rights not explicitly granted in the MIT license are reserved. See the included `LICENSE.md` file for more details.
637
638=======
639*LevelUP builds on the excellent work of the LevelDB and Snappy teams from Google and additional contributors. LevelDB and Snappy are both issued under the [New BSD Licence](http://opensource.org/licenses/BSD-3-Clause).*