├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── example.js ├── index.js ├── package.json └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "0.10" 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Mathias Buus 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # levelup-defaults 2 | 3 | Change the defaults settings on a levelup instance 4 | by returning a new levelup instance that uses the same leveldown but different options. 5 | 6 | ``` 7 | npm install levelup-defaults 8 | ``` 9 | 10 | [![build status](http://img.shields.io/travis/mafintosh/levelup-defaults.svg?style=flat)](http://travis-ci.org/mafintosh/levelup-defaults) 11 | 12 | ## Usage 13 | 14 | ``` js 15 | var defaults = require('levelup-defaults') 16 | var level = require('level') 17 | var db = level('db') 18 | 19 | // create a new levelup that uses binary encoding as default 20 | var binaryDb = defaults(db, {valueEncoding:'binary'}) 21 | 22 | // create a new levelup that uses bytewise and json 23 | var anotherDb = defaults(db, {keyEncoding:require('bytewise'), valueEncoding:'json'}) 24 | ``` 25 | 26 | The original `db` is not mutated so any encoding settings on that are left unchanged. 27 | 28 | ## License 29 | 30 | MIT 31 | -------------------------------------------------------------------------------- /example.js: -------------------------------------------------------------------------------- 1 | var memdb = require('memdb') 2 | var defaults = require('levelup-defaults') 3 | var db = memdb() 4 | 5 | db = defaults(db, {valueEncoding:'binary'}) 6 | 7 | db.put('hello', 'world', function() { 8 | db.get('hello', function(err, buf) { 9 | console.log('"world" as a buffer:', buf) 10 | }) 11 | }) 12 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var levelup = require('levelup') 2 | 3 | var noop = function() {} 4 | 5 | var Proxy = function(db) { 6 | this.db = db 7 | this.leveldown = null 8 | } 9 | 10 | Proxy.prototype.open = function(opts, cb) { 11 | if (typeof opts === 'function') return this.open(null, opts) 12 | if (!cb) cb = noop 13 | var self = this 14 | this.db.open(function(err) { 15 | if (err) return cb(err) 16 | self.leveldown = self.db.db 17 | cb() 18 | }) 19 | } 20 | 21 | 'put get del batch approximateSize iterator close'.split(' ').forEach(function(m) { 22 | Proxy.prototype[m] = function() { 23 | return this.leveldown[m].apply(this.leveldown, arguments) 24 | } 25 | }) 26 | 27 | module.exports = function(db, defaults) { 28 | if (!defaults) defaults = {} 29 | 30 | defaults.db = function(l) { 31 | return new Proxy(db) 32 | } 33 | 34 | var up = levelup(db.location || 'no-location', defaults) 35 | 36 | up.on('ready', function() { // remove the proxy 37 | if (up.db instanceof Proxy) up.db = up.db.leveldown 38 | }) 39 | 40 | return up 41 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "levelup-defaults", 3 | "version": "2.0.0", 4 | "description": "Change the defaults settings on a levelup instance by returning a new levelup instance that uses the same leveldown but different options", 5 | "main": "index.js", 6 | "dependencies": { 7 | "levelup": "^1.3.2" 8 | }, 9 | "devDependencies": { 10 | "memdb": "^0.2.0", 11 | "tape": "^3.0.3" 12 | }, 13 | "scripts": { 14 | "test": "tape test.js" 15 | }, 16 | "repository": { 17 | "type": "git", 18 | "url": "https://github.com/mafintosh/levelup-defaults.git" 19 | }, 20 | "author": "Mathias Buus (@mafintosh)", 21 | "license": "MIT", 22 | "bugs": { 23 | "url": "https://github.com/mafintosh/levelup-defaults/issues" 24 | }, 25 | "homepage": "https://github.com/mafintosh/levelup-defaults" 26 | } 27 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | var defaults = require('./') 2 | var tape = require('tape') 3 | var memdb = require('memdb') 4 | 5 | tape('valueEncoding', function(t) { 6 | var db = memdb() 7 | 8 | db.put('hello', JSON.stringify({hello:'world'}), function() { 9 | db.get('hello', function(err, val) { 10 | t.same(val, '{"hello":"world"}', 'not json') 11 | var jdb = defaults(db, {valueEncoding:'json'}) 12 | jdb.get('hello', function(err, val) { 13 | t.same(val, {hello:'world'}, 'is json') 14 | db.get('hello', function(err, val) { 15 | t.same(val, '{"hello":"world"}', 'db not changed') 16 | t.end() 17 | }) 18 | }) 19 | }) 20 | }) 21 | }) 22 | 23 | tape('keyEncoding', function(t) { 24 | var db = memdb() 25 | 26 | db.put('"hello"', 'world', function() { 27 | db.get('"hello"', function(err, val) { 28 | t.same(val, 'world') 29 | var jdb = defaults(db, {keyEncoding:'json'}) 30 | jdb.get('hello', function(err, val) { 31 | t.same(val, 'world') 32 | db.get('"hello"', function(err, val) { 33 | t.same(val, 'world', 'db not changed') 34 | t.end() 35 | }) 36 | }) 37 | }) 38 | }) 39 | }) --------------------------------------------------------------------------------