Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

q-level

Package Overview
Dependencies
Maintainers
3
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

q-level

Promise'd LevelUp with Q, originally a fork of nathan7/level-promise.

  • 1.1.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2
increased by100%
Maintainers
3
Weekly downloads
 
Created
Source

q-level

originally a fork of nathan7/level-promise

Promisified level API with Q

Installation

$ npm install q-level

Warning

At the application level, promises vs callbacks is yours to choose. If you use this in a LevelUp extension that isn't explicitly about promises, nathan7 will find you and destroy you. Play nice with the rest, use callbacks for your extension. If your extension works with level-manifest, it'll work with this.

What it does

Every method marked as async by level-manifest will now return a promise when you don't pass it a callback. The methods that return readable streams will return streams augmented with Q's promise interface.

It recurses into sublevels.

Usage

Promises

var level = require('level')
var promisify = require('q-level')
var db = promisify(level('my.db', { valueEncoding: 'json' }))
var contents = {}

db.put('a', 1)
  .then(function() {
    return db.batch([
      { type: 'put', key: 'b', value: 2 },
      { type: 'put', key: 'c', value: 3 }
    ])
  })
  .then(function() {
    return db.createReadStream()
  })
  .progress(function(data) {
    contents[data.key] = data.value
  })
  .catch(function(err) {
    console.log('Error reading stream', err)
  })
  .then(function() {
    console.log('Contents:', contents)
    return db.close()
  })
  .done(function() {
    console.log('Closed')
  })

Callbacks

same example as above, but with callbacks

var level = require('level')
var promisify = require('q-level')
var db = promisify(level('my.db'), { valueEncoding: 'json' })
var contents = {}

db.put('a', 1, function(err) {
  db.batch([
    { type: 'put', key: 'b', value: 2 },
    { type: 'put', key: 'c', value: 3 }
  ], function(err) {
    return db.createReadStream()
      .on('data', function(data) {
        contents[data.key] = data.value
      })
      .on('error', function(err) {
        console.log('Error reading stream', err)
      })
      .on('close', function(err) {
        console.log('Contents:', contents)
        db.close(function(err) {
          console.log('Closed')
        })
      })
  })
})

Query engine support

var level = require('level')
var levelQuery = require('level-queryengine')
var jsonQueryEngine = require('jsonquery-engine')
var promisify = require('q-level')

db = levelQuery(level('my.db', { valueEncoding: 'json' }))
db.query.use(jsonQueryEngine())
// IMPORTANT TO PROMISIFY LAST!
promisify(db)
promisify(db, 'query', { type: 'readable' })

db.query({ happy: { $in: ['very', 'not so much'] }})
  .progress(function(val) {
    // do stuff
  })
  .done(function() {
    // do other stuff
  })

License

MIT

Keywords

FAQs

Package last updated on 03 Feb 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc