Coder Social home page Coder Social logo

level-onion's Introduction

level-onion

NPM

Extend a levelup instance in layers, like an onion.

Each layer you add can expose new functionality or modify functionality of inner layers, or you can bypass the layers by using the inner unmodified layers.

WARNING This is currently experimental, and primarily a proof-of-concept. There are no guarantees that this will be a best practice for working with levelup, or be adopted by any significant portion of the levelup ecosystem.

Pros: Simple & low impact.

Cons: I'm not exactly thrilled with the convention it requires wrapping modules to comply with.


Example: case-insensitive keys:

Create a wrapping layer:

  function Upper() {
    // Required
    this.type = "upper"
    // Default false, if true it will prevent you from double-wrapping with this wrapper.
    this.unique = false
  }

  // Requires an install method that takes the wrapped db and the parent it is wrapping
  Upper.prototype.install = function (db, parent) {
    db.put = function (key, value, cb) {
      parent.put(key.toUpperCase(), value, cb)
    }

    db.get = function (key, cb) {
      parent.get(key.toUpperCase(), cb)
    }

    // ... etc.
  }

Wrap a levelup instance with your wrapping layer:

var level = require("level")
var wrap = require("level-onion")

var ldb = level("/tmp/db")
var db = wrap(db, new Upper())
// ldb remains intact and unmodified
// db now has the wrapper installed

level-onion's People

Contributors

brycebaril avatar

Stargazers

 avatar Chris Jares avatar Matteo Collina avatar Tim Kevin Oxley avatar

Watchers

 avatar James Cloos avatar

Forkers

redorion

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.