{"id":15497513,"url":"https://github.com/bcomnes/level-auto-index","last_synced_at":"2026-03-12T16:03:56.541Z","repository":{"id":51797244,"uuid":"78295492","full_name":"bcomnes/level-auto-index","owner":"bcomnes","description":"Automatic secondary indexing for leveldb and subleveldown","archived":false,"fork":false,"pushed_at":"2020-05-25T04:10:37.000Z","size":84,"stargazers_count":19,"open_issues_count":4,"forks_count":3,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-15T16:25:59.939Z","etag":null,"topics":["autoindex","indexing","leveldb","levelup","nodejs"],"latest_commit_sha":null,"homepage":"https://npmjs.com/level-auto-index","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bcomnes.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-01-07T18:15:24.000Z","updated_at":"2024-06-10T20:41:10.000Z","dependencies_parsed_at":"2022-08-17T15:10:54.744Z","dependency_job_id":null,"html_url":"https://github.com/bcomnes/level-auto-index","commit_stats":null,"previous_names":["hypermodules/level-auto-index"],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bcomnes%2Flevel-auto-index","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bcomnes%2Flevel-auto-index/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bcomnes%2Flevel-auto-index/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bcomnes%2Flevel-auto-index/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bcomnes","download_url":"https://codeload.github.com/bcomnes/level-auto-index/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250333267,"owners_count":21413356,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["autoindex","indexing","leveldb","levelup","nodejs"],"created_at":"2024-10-02T08:39:07.936Z","updated_at":"2026-01-12T02:44:55.596Z","avatar_url":"https://github.com/bcomnes.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# level-auto-index\n\nAutomatic secondary indexing for leveldb and subleveldown.\n\n\n```bash\nnpm install level-auto-index\n```\n\n[![level badge][level-badge]](https://github.com/level/awesome)\n[![npm][npm-image]][npm-url]\n[![Build Status](https://travis-ci.org/hypermodules/level-auto-index.svg?branch=master)](https://travis-ci.org/hypermodules/level-auto-index)\n[![dependencies Status](https://david-dm.org/hypermodules/level-auto-index/status.svg)](https://david-dm.org/hypermodules/level-auto-index)\n[![devDependencies Status](https://david-dm.org/hypermodules/level-auto-index/dev-status.svg)](https://david-dm.org/hypermodules/level-auto-index?type=dev)\n\n\u003cimg height=\"100\" src=\"index.png\"\u003e\n\n[level-badge]: https://camo.githubusercontent.com/1bd15320a5fad1db168bba8bcedb098735f82464/68747470733a2f2f6c6576656c6a732e6f72672f696d672f62616467652e737667\n[npm-image]: https://img.shields.io/npm/v/level-auto-index.svg\n[npm-url]: https://www.npmjs.com/package/level-auto-index\n\n## Usage\n\n```js\nvar level = require('level')\nvar AutoIndex = require('level-auto-index')\nvar sub = require('subleveldown')\nvar keyReducer = AutoIndex.keyReducer\n\nvar db = level()\n\nvar posts = sub(db, 'posts', {valueEncoding: 'json'})\nvar idx = {\n  title: sub(db, 'title'),\n  length: sub(db, 'length'),\n  tag: sub(db, 'tag')\n}\n\n// add a title index\nposts.byTitle = AutoIndex(posts, idx.title, keyReducer('title'))\n\n// add a length index\n// append the post.id for unique indexes with possibly overlapping values\nposts.byLength = AutoIndex(posts, idx.length, function (post) {\n  return post.body.length + '!' + post.id\n})\n\n// Create multiple index keys on an index\nposts.byTag = AutoIndex(posts, idx.tag, function (post) {\n    if (!post || !post.tags || !Array.isArray(post.tags)) return\n    return post.tags.map(function (tag) {\n      return [tag, post.id].join('!')\n    })\n  }, { multi: true })\n\nposts.put('1337', {\n  id: '1337',\n  title: 'a title',\n  body: 'lorem ipsum',\n  tags: [ 'foo', 'bar', 'baz' ]\n}, function (err) {\n  if (err) throw err\n\n  posts.byTitle.get('a title', function (err, post) {\n    if (err) throw err\n    console.log('get', post)\n    // =\u003e get: { id: '1337', title: 'a title', body: 'lorem ipsum' }\n\n    posts.del('1337', function (err) {\n      if (err) throw err\n      posts.byTitle.get('a title', function (err) {\n        console.log(err.name)\n        // =\u003e NotFoundError\n      })\n    })\n  })\n\n  posts.byLength.createReadStream({\n    start: 10,\n    end: 20\n  }).on('data', console.log.bind(console, 'read'))\n  // =\u003e read { key: '1337', value: { id: '1337', title: 'a title', body: 'lorem ipsum' } }\n\n  posts.byLength.createKeyStream({\n    start: 10,\n    end: 20\n  }).on('data', console.log.bind(console, 'key'))\n  // =\u003e key 1337\n\n  posts.byLength.createValueStream({\n    start: 10,\n    end: 20\n  }).on('data', console.log.bind(console, 'value'))\n  // =\u003e value { id: '1337', title: 'a title', body: 'lorem ipsum' }\n})\n```\n\n## API\n\n### AutoIndex(db, idb, reduce, opts)\n\nAutomatically index a `db` level into the `idb` level using a `reduce` function that creates the index key.  The `db` and `idb` levels should be in isolated key namespaces, either by being two different levels or [`mafintosh/subleveldown`](https://github.com/mafintosh/subleveldown) partitions.  The `db` hook is mutated by [`hypermodules/level-hookdown`](https://github.com/hypermodules/level-hookdown) to set up the prehooks used for indexing.  Only `db` keys are stored as values to save space and reduce data redundancy.\n\nSecondary returns an `AutoIndex` level that helps prune old index values, and automatically looks up source documents from `db` as you access keys on the `AutoIndex` level.\n\nThe `reduce` functions get the `value` of the `put` or `batch` operations.  Make sure that this `value` has everything you need to create your index keys.\n\n```js\nfunction reducer (value) {\n  var idxKey = value.foo + '!' + value.bar\n  return idxKey\n}\n```\n\nAvailable opts:\n\n```js\n{\n  multi: false // Reducer returns an array of keys to associate with the primary key\n}\n```\n\nMulti-key index's are for when you you want to write multiple index entries into an index.  This is useful for 'tag' fields, where a document may have `n` tags per document, and you would like to index documents by 'tag'.  When creating a multi-key index, your reducer must return an array of keys to index by.\n\n### AutoIndex#get(key, opts, cb)\n\nGet the value that has been indexed with `key`.\n\n### AutoIndex#create{Key,Value,Read}Stream(opts)\n\nCreate a readable stream that has indexes as keys and indexed data as values.\n\n### AutoIndex#manifest\n\nA [level manifest](https://github.com/dominictarr/level-manifest) that you can pass to [multilevel](https://github.com/juliangruber/multilevel).\n\n### AutoIndex.keyReducer(string)\n\nA shortcut reducer for simplistic key indexing.  You might need more than this.\n\n```js\nfunction keyReducer (reducerString) {\n  function keyRdc (value) {\n    return value[reducerString]\n  }\n  return keyRdc\n}\n```\n\nFor a higher level api for creating secondary indexes see [hypermodules/level-idx](https://github.com/hypermodules/level-idx).\n\n### AutoIndex.keyReducer(string)\n\nA shortcut reducer for simplistic multi-key indexing.  You might need more than this.\n\n```js\nfunction multiKeyReducer (multiFieldName, primaryKeyFieldName) {\n  return function multiKeyrdc (document) {\n    if (!document || !document[multiFieldName] || !Array.isArray(document[multiFieldName])) return\n    return document[multiFieldName].map(function (tag) {\n      return [tag, document[primaryKeyFieldName]].join('!')\n    })\n  }\n```\n\n### AutoIndex#db\n\nThe level instance that we are indexing.\n\n### AutoIndex#idb\n\nThe level instance that we are using for the index.\n\n## See Also\n\n- [hypermodules/level-hookdown](https://github.com/hypermodules/level-hookdown) - Simple levelup hooks implemented using instance method override of arbitrary levelups.\n- [hypermodules/level-idx](https://github.com/hypermodules/level-idx) - Another high-level API for creating secondary leveldb indexes using `level-auto-index` and `level-hookdown`.\n\nThis module is a variant of\n\n- [juliangruber/level-secondary](https://github.com/juliangruber/level-secondary)\n\nbut aimed at decoupling the index storage fromt the indexd db and also being compatable with subleveldown.  It came out of the work trying to make `level-secondary` compatable with subleveldown and level-sublevel.  That work lives here: [github.com/bcomnes/level-secondary/commit/9b2f914e53](https://github.com/bcomnes/level-secondary/commit/9b2f914e5304c791813b39abf892c32ee7616abf).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbcomnes%2Flevel-auto-index","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbcomnes%2Flevel-auto-index","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbcomnes%2Flevel-auto-index/lists"}