Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
This repository was archived by the owner on Dec 1, 2024. It is now read-only.
/levelupPublic archive

Superseded by abstract-level. A wrapper for abstract-leveldown compliant stores, for Node.js and browsers.

License

NotificationsYou must be signed in to change notification settings

Level/levelup

Repository files navigation

Superseded byabstract-level. Please seeFrequently Asked Questions.

Table of Contents

Click to expand

Introduction

Fast and simple storage. A Node.js wrapper forabstract-leveldown compliant stores, which follow the characteristics ofLevelDB.

LevelDB is a simple key-value store built by Google. It's used in Google Chrome and many other products. LevelDB supports arbitrary byte arrays as both keys and values, singularget,put anddelete operations,batched put and delete, bi-directional iterators and simple compression using the very fastSnappy algorithm.

LevelDB stores entries sorted lexicographically by keys. This makes the streaming interface oflevelup - which exposes LevelDB iterators asReadable Streams - a very powerful query mechanism.

The most common store isleveldown which provides a pure C++ binding to LevelDB.Many alternative stores are available such aslevel.js in the browser ormemdown for an in-memory store. They typically support strings and Buffers for both keys and values. For a richer set of data types you can wrap the store withencoding-down.

Thelevel package is the recommended way to get started. It conveniently bundleslevelup,leveldown andencoding-down. Its main export islevelup - i.e. you can dovar db = require('level').

Supported Platforms

We aim to support Active LTS and Current Node.js releases as well as browsers. For support of the underlying store, please see the respective documentation.

Usage

If you are upgrading: please seeUPGRADING.md.

First you need to installlevelup! No stores are included so you must also installleveldown (for example).

$ npm install levelup leveldown

All operations are asynchronous. If you do not provide a callback,a Promise is returned.

varlevelup=require('levelup')varleveldown=require('leveldown')// 1) Create our storevardb=levelup(leveldown('./mydb'))// 2) Put a key & valuedb.put('name','levelup',function(err){if(err)returnconsole.log('Ooops!',err)// some kind of I/O error// 3) Fetch by keydb.get('name',function(err,value){if(err)returnconsole.log('Ooops!',err)// likely the key was not found// Ta da!console.log('name='+value)})})

API

levelup(db[, options[, callback]])

The main entry point for creating a newlevelup instance.

  • db must be anabstract-leveldown compliant store.
  • options is passed on to the underlying store when opened and is specific to the type of store being used

Callinglevelup(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 formfunction (err, db) {} wheredb is thelevelup instance. If you don't provide a callback, any read & write operations are simply queued internally until the store is fully opened, unless it fails to open, in which case anerror event will be emitted.

This leads to two alternative ways of managing alevelup instance:

levelup(leveldown(location),options,function(err,db){if(err)throwerrdb.get('foo',function(err,value){if(err)returnconsole.log('foo does not exist')console.log('got foo =',value)})})

Versus the equivalent:

// Will throw if an error occursvardb=levelup(leveldown(location),options)db.get('foo',function(err,value){if(err)returnconsole.log('foo does not exist')console.log('got foo =',value)})

db.supports

A read-onlymanifest. Might be used like so:

if(!db.supports.permanence){thrownewError('Persistent storage is required')}if(db.supports.bufferKeys&&db.supports.promises){awaitdb.put(Buffer.from('key'),'value')}

db.open([options][, callback])

Opens the underlying store. In general you shouldn't need to call this method directly as it's automatically called bylevelup(). However, it is possible to reopen the store after it has been closed withclose().

If no callback is passed, a promise is returned.

db.close([callback])

close() closes the underlying store. The callback will receive any error encountered during closing as the first argument.

You should always clean up yourlevelup instance by callingclose() when you no longer need it to free up resources. A store cannot be opened by multiple instances oflevelup simultaneously.

If no callback is passed, a promise is returned.

db.put(key, value[, options][, callback])

put() is the primary method for inserting data into the store. Bothkey andvalue can be of any type as far aslevelup is concerned.

options is passed on to the underlying store.

If no callback is passed, a promise is returned.

db.get(key[, options][, callback])

Get a value from the store bykey. Thekey 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 canerr.type == 'NotFoundError' or you can perform a truthy test on the propertyerr.notFound.

db.get('foo',function(err,value){if(err){if(err.notFound){// handle a 'NotFoundError' herereturn}// I/O or other error, pass it up the callback chainreturncallback(err)}// .. handle `value` here})

The optionaloptions object is passed on to the underlying store.

If no callback is passed, a promise is returned.

db.getMany(keys[, options][, callback])

Get multiple values from the store by an array ofkeys. The optionaloptions object is passed on to the underlying store.

Thecallback function will be called with anError if the operation failed for any reason. If successful the first argument will benull and the second argument will be an array of values with the same order askeys. If a key was not found, the relevant value will beundefined.

If no callback is provided, a promise is returned.

db.del(key[, options][, callback])

del() is the primary method for removing data from the store.

db.del('foo',function(err){if(err)// handle I/O or other error});

options is passed on to the underlying store.

If no callback is passed, a promise is returned.

db.batch(array[, options][, callback])(array form)

batch() can be used for very fast bulk-write operations (bothput anddelete). Thearray 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.

Each operation is contained in an object having the following properties:type,key,value, where thetype is either'put' or'del'. In the case of'del' thevalue property is ignored. Any entries with akey ofnull orundefined will cause an error to be returned on thecallback and anytype: 'put' entry with avalue ofnull orundefined will return an error.

constops=[{type:'del',key:'father'},{type:'put',key:'name',value:'Yuri Irsenovich Kim'},{type:'put',key:'dob',value:'16 February 1941'},{type:'put',key:'spouse',value:'Kim Young-sook'},{type:'put',key:'occupation',value:'Clown'}]db.batch(ops,function(err){if(err)returnconsole.log('Ooops!',err)console.log('Great success dear leader!')})

options is passed on to the underlying store.

If no callback is passed, a promise is returned.

db.batch()(chained form)

batch(), when called with no arguments will return aBatch 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 ofbatch() over the array form.

db.batch().del('father').put('name','Yuri Irsenovich Kim').put('dob','16 February 1941').put('spouse','Kim Young-sook').put('occupation','Clown').write(function(){console.log('Done!')})

batch.put(key, value[, options])

Queue aput operation on the current batch, not committed until awrite() is called on the batch. Theoptions argument, if provided, must be an object and is passed on to the underlying store.

This method maythrow aWriteError if there is a problem with your put (such as thevalue beingnull orundefined).

batch.del(key[, options])

Queue adel operation on the current batch, not committed until awrite() is called on the batch. Theoptions argument, if provided, must be an object and is passed on to the underlying store.

This method maythrow aWriteError if there is a problem with your delete.

batch.clear()

Clear all queued operations on the current batch, any previous operations will be discarded.

batch.length

The number of queued operations on the current batch.

batch.write([options][, callback])

Commit the queued operations for this batch. All operations notcleared will be written to the underlying store atomically, that is, they will either all succeed or fail with no partial commits.

The optionaloptions object is passed to the.write() operation of the underlying batch object.

If no callback is passed, a promise is returned.

db.status

A readonly string that is one of:

  • new - newly created, not opened or closed
  • opening - waiting for the underlying store to be opened
  • open - successfully opened the store, available for use
  • closing - waiting for the store to be closed
  • closed - store has been successfully closed.

db.isOperational()

Returnstrue if the store accepts operations, which in the case oflevelup means thatstatus is eitheropening oropen, because it opens itself and queues up operations until opened.

db.createReadStream([options])

Returns aReadable Stream of key-value pairs. A pair is an object withkey andvalue 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.

db.createReadStream().on('data',function(data){console.log(data.key,'=',data.value)}).on('error',function(err){console.log('Oh my!',err)}).on('close',function(){console.log('Stream closed')}).on('end',function(){console.log('Stream ended')})

You can supply an options object as the first parameter tocreateReadStream() with the following properties:

  • 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. Whenreverse=true the order will be reversed, but the entries streamed will be the same.

  • 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. Whenreverse=true the order will be reversed, but the entries streamed will be the same.

  • 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.

  • limit(number, default:-1): limit the number of entries collected by this stream. This number represents amaximum 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. Whenreverse=true the entries with the highest keys will be returned instead of the lowest keys.

  • keys(boolean, default:true): whether the results should contain keys. If set totrue andvalues set tofalse then results will simply be keys, rather than objects with akey property. Used internally by thecreateKeyStream() method.

  • values(boolean, default:true): whether the results should contain values. If set totrue andkeys set tofalse then results will simply be values, rather than objects with avalue property. Used internally by thecreateValueStream() method.

db.createKeyStream([options])

Returns aReadable Stream of keys rather than key-value pairs. Use the same options as described forcreateReadStream() to control the range and direction.

You can also obtain this stream by passing an options object tocreateReadStream() withkeys set totrue andvalues set tofalse. The result is equivalent; both streams operate inobject mode.

db.createKeyStream().on('data',function(data){console.log('key=',data)})// same as:db.createReadStream({keys:true,values:false}).on('data',function(data){console.log('key=',data)})

db.createValueStream([options])

Returns aReadable Stream of values rather than key-value pairs. Use the same options as described forcreateReadStream() to control the range and direction.

You can also obtain this stream by passing an options object tocreateReadStream() withvalues set totrue andkeys set tofalse. The result is equivalent; both streams operate inobject mode.

db.createValueStream().on('data',function(data){console.log('value=',data)})// same as:db.createReadStream({keys:false,values:true}).on('data',function(data){console.log('value=',data)})

db.iterator([options])

Returns anabstract-leveldown iterator, which is what powers the readable streams above. Options are the same as the range options ofcreateReadStream() and are passed to the underlying store.

These iterators supportfor await...of:

forawait(const[key,value]ofdb.iterator()){console.log(value)}

db.clear([options][, callback])

Delete all entries or a range. Not guaranteed to be atomic. Accepts the following range options (with the same rules as on iterators):

  • gt (greater than),gte (greater than or equal) define the lower bound of the range to be deleted. Only entries where the key is greater than (or equal to) this option will be included in the range. Whenreverse=true the order will be reversed, but the entries deleted will be the same.
  • lt (less than),lte (less than or equal) define the higher bound of the range to be deleted. Only entries where the key is less than (or equal to) this option will be included in the range. Whenreverse=true the order will be reversed, but the entries deleted will be the same.
  • reverse(boolean, default:false): delete entries in reverse order. Only effective in combination withlimit, to remove the last N records.
  • limit(number, default:-1): limit the number of entries to be deleted. This number represents amaximum 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. Whenreverse=true the entries with the highest keys will be deleted instead of the lowest keys.

If no options are provided, all entries will be deleted. Thecallback function will be called with no arguments if the operation was successful or with anWriteError if it failed for any reason.

If no callback is passed, a promise is returned.

What happened todb.createWriteStream?

db.createWriteStream() has been removed in order to provide a smaller and more maintainable core. It primarily existed to create symmetry withdb.createReadStream() but through muchdiscussion, removing it was the best course of action.

The main driver for this was performance. Whiledb.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 morewrite-stream implementations to be created to solve the broad spectrum of use cases.

Check out the implementations that the community has producedhere.

Promise Support

Each function accepting a callback returns a promise if the callback is omitted. The only exception is thelevelup constructor itself, which if no callback is passed will lazily open the underlying store in the background.

Example:

constdb=levelup(leveldown('./my-db'))awaitdb.put('foo','bar')console.log(awaitdb.get('foo'))

Events

levelup is anEventEmitter and emits the following events.

EventDescriptionArguments
putKey has been updatedkey, value (any)
delKey has been deletedkey (any)
batchBatch has executedoperations (array)
clearEntries were deletedoptions (object)
openingUnderlying store is opening-
openStore has opened-
readyAlias ofopen-
closingStore is closing-
closedStore has closed.-
errorAn error occurrederror (Error)

For example you can do:

db.on('put',function(key,value){console.log('inserted',{ key, value})})

Multi-process Access

Stores like LevelDB are thread-safe but they arenot suitable for accessing with multiple processes. You should only ever have a store open from a single Node.js process. Node.js clusters are made up of multiple processes so alevelup instance cannot be shared between them either.

SeeLevel/awesome for modules likemultileveldown that may help if you require a single store to be shared across processes.

Contributing

Level/levelup is anOPEN Open Source Project. This means that:

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.

See theContribution Guide for more details.

Big Thanks

Cross-browser Testing Platform and Open Source ♥ Provided bySauce Labs.

Sauce Labs logo

Donate

Support us with a monthly donation onOpen Collective and help us continue our work.

License

MIT

About

Superseded by abstract-level. A wrapper for abstract-leveldown compliant stores, for Node.js and browsers.

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Sponsor this project


    [8]ページ先頭

    ©2009-2025 Movatter.jp