{
  "name": "level-mapped-index",
  "description": "Simple indexing for LevelUP",
  "version": "0.2.3",
  "homepage": "https://github.com/rvagg/node-level-mapped-index",
  "authors": [
    "Rod Vagg <rod@vagg.org> (https://github.com/rvagg)"
  ],
  "keywords": [
    "leveldb",
    "levelup",
    "index",
    "indexing"
  ],
  "main": "./level-mapped-index.js",
  "repository": {
    "type": "git",
    "url": "https://github.com/rvagg/node-level-mapped-index.git"
  },
  "dependencies": {
    "map-reduce": "~3.1.3",
    "xtend": "~2.0.3",
    "transform-stream": "~0.2.2"
  },
  "peerDependencies": {},
  "devDependencies": {
    "tap": "*",
    "levelup": "~0.7.0",
    "level-sublevel": "~3.3.1",
    "rimraf": "~2.1.4",
    "after": "~0.7.0",
    "delayed": "~0.0.0"
  },
  "scripts": {
    "test": "node ./test.js"
  },
  "license": "MIT",
  "readme": "# Mapped Index for LevelDB [![Build Status](https://secure.travis-ci.org/rvagg/node-level-mapped-index.png)](http://travis-ci.org/rvagg/node-level-mapped-index)\n\n![LevelDB Logo](https://twimg0-a.akamaihd.net/profile_images/3360574989/92fc472928b444980408147e5e5db2fa_bigger.png)\n\nA simple and flexible indexer for LevelDB, built on [LevelUP](https://github.com/rvagg/node-levelup) and [Map Reduce](https://github.com/dominictarr/map-reduce/).\n\nAfter initialising Mapped Index, your LevelUP instance will have some new methods that let you register new indexes and fetch values from them.\n\n```js\n// requires levelup and level-sublevel packages\nconst levelup     = require('levelup')\n    , mappedIndex = require('level-mapped-index')\n    , subLevel    = require('level-sublevel')\n\nlevelup('/tmp/foo.db', function (err, db) {\n\n  // set up our LevelUP instance\n  db = subLevel(db)\n  db = mappedIndex(db)\n\n  // register 2 indexes:\n\n  // first index is named 'id' and indexes the 'id' property\n  // of each entry\n  db.registerIndex('id', function (key, value, emit) {\n    value = JSON.parse(value)\n    // if the value has a property 'id', register this entry\n    // by calling emit() with just the indexable value\n    if (value.id) emit(value.id)\n  })\n\n  // second index is named 'bleh' and indexes the 'bleh' property\n  db.registerIndex('bleh', function (key, value, emit) {\n    value = JSON.parse(value)\n    // in this case we're just going to index any entries that have a\n    // 'boom' property equal to 'bam!'\n    if (value.boom == 'bam!') emit(String(value.boom))\n  })\n\n  // ... use the database\n})\n```\n\nIn this example we're using the `registerIndex()` method to register two indexes. You must supply an index name (String) and a function that will parse and register individual entries for this index. Your function receives the key and the value of the entry and an `emit()` function. You call `emit()` with a single argument, the property for this entry that you are indexing on. The `emit()` function *does not need to be called* for each entry, only entries relevant to your index.\n\nNow we put some values into our database:\n\n```js\ndb.put('foo1', JSON.stringify({ one   : 'ONE'   , id : '1' }))\ndb.put('foo2', JSON.stringify({ two   : 'TWO'   , id : '2' , boom: 'bam!' }))\ndb.put('foo3', JSON.stringify({ three : 'THREE' , id : '3' , boom: 'bam!' }))\ndb.put('foo4', JSON.stringify({ four  : 'FOUR'  , id : '4' , boom : 'fizzle...' }))\n```\n\n*Map Reduce* processes these entries and passes them each to our index functions that we registered earlier. Our index references are stored in the same database, namespaced, so that they can be efficiently retrieved when required:\n\n```js\ndb.getBy('id', '1', function (err, data) {\n  // `data` will equal:\n  //    [{ key: 'foo1', value: '{\"one\":\"ONE\",\"key\":\"1\"}' }]\n})\n\ndb.getBy('bleh', 'bam!', function (err, data) {\n  // `data` will equal:\n  // [\n  //     { key: 'foo2', value: '{\"two\":\"TWO\",\"key\":\"2\",\"boom\":\"bam!\"}' }\n  //   , { key: 'foo3', value: '{\"three\":\"THREE\",\"key\":\"3\",\"boom\":\"bam!\"}' }\n  // ]\n})\n```\n\nOur LevelUP instance has been augmented with a `getBy()` method that takes 3 arguments: the index name, the value on that index we are looking for and a callback function. Our callback will receive two arguments, an error and an array of objects containing `'key'` and `'value'` properties for each indexed entry. You will receive empty arrays where your indexed value finds no corresponding entries.\n\nIt is **important to note** that your entries are not stored in duplicate, only the primary keys are stored for each index entry so an additional look-up is required to fetch each complete entry.\n\nYou can also ask for a stream of your indexed entries in a similar manner:\n\n```js\ndb.createIndexedStream('id', '1')\n  .on('data', function (data) {\n    // this will be called once, and data will equal:\n    // { key: 'foo1', value: '{\"one\":\"ONE\",\"key\":\"1\"}' }\n  })\n  .on('error', function () {\n    // ...\n  })\n  .on('end', function () {\n    // ...\n  })\n\ndb.createIndexedStream('key', '1')\n  .on('data', function (data) {\n    // this will be called twise, and data will equal:\n    // { key: 'foo2', value: '{\"two\":\"TWO\",\"key\":\"2\",\"boom\":\"bam!\"}' }\n    // { key: 'foo3', value: '{\"three\":\"THREE\",\"key\":\"3\",\"boom\":\"bam!\"}' }\n  })\n  .on('error', function () {\n    // ...\n  })\n  .on('end', function () {\n    // ...\n  })\n```\n\nOf course this method is preferable if you are likely to have a large number of entries for each index value, otherwise `getBy()` will buffer each entry before returning them to you on the callback.\n\n## Licence\n\nlevel-mapped-index is Copyright (c) 2013 Rod Vagg [@rvagg](https://twitter.com/rvagg) and licensed under the MIT licence. All rights not explicitly granted in the MIT license are reserved. See the included LICENSE file for more details.\n",
  "readmeFilename": "README.md",
  "_id": "level-mapped-index@0.2.3",
  "_from": "level-mapped-index@~0.2.3"
}
