https://github.com/bcomnes/level-idx
Another high-level API for creating secondary leveldb indexes
https://github.com/bcomnes/level-idx
indexing leveldb nodejs
Last synced: 6 months ago
JSON representation
Another high-level API for creating secondary leveldb indexes
- Host: GitHub
- URL: https://github.com/bcomnes/level-idx
- Owner: bcomnes
- License: mit
- Created: 2017-01-07T18:11:54.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2020-07-17T18:59:36.000Z (almost 6 years ago)
- Last Synced: 2025-03-29T19:34:28.451Z (about 1 year ago)
- Topics: indexing, leveldb, nodejs
- Language: JavaScript
- Homepage:
- Size: 36.1 KB
- Stars: 14
- Watchers: 3
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# level-idx
High Level leveldb indexing API using [level-auto-index](https://github.com/hypermodules/level-auto-index).
```bash
npm install level-idx
```
[![level badge][level-badge]](https://github.com/level/awesome)
[![npm][npm-image]][npm-url]
[](https://travis-ci.org/hypermodules/level-idx)
[](https://david-dm.org/hypermodules/level-idx)
[](https://david-dm.org/hypermodules/level-idx?type=dev)
[level-badge]: https://camo.githubusercontent.com/1bd15320a5fad1db168bba8bcedb098735f82464/68747470733a2f2f6c6576656c6a732e6f72672f696d672f62616467652e737667
[npm-image]: https://img.shields.io/npm/v/level-idx.svg
[npm-url]: https://www.npmjs.com/package/level-idx
## Usage
Index posts by title and body length, then query for them:
```js
var Index = require('level-idx')
var level = require('memdb')
var sub = require('subleveldown')
var db = level()
var posts = sub(db, 'posts', {valueEncoding: 'json'})
var idx = sub(db, 'idx')
Index(posts, idx)
.by('Title', 'title')
.by('Length', ['body.length', 'title'])
.by('Author', ['author', 'title'])
var post = {
title: 'a title',
body: 'lorem ipsum',
author: 'julian'
}
posts.put('1337', post, function (err) {
if (err) throw err
posts.byTitle.get('a title', console.log)
posts.byLength.get('11!a title', console.log)
posts.byAuthor.get('julian!a title', console.log)
})
```
## API
### Index(db, idb)
Index `db` into `idb`.
### Index#by(name, props)
Create an index called `name` and index by `props`.
`props` should be a string or an array of strings that each name a property.
Deep object access is enabled via
[deep-access](https://github.com/juliangruber/deep-access). Use multiple
properties if you can't guarantee the uniqueness of the first property's
value.
If a property doesn't exist, e.g. if you want to index by `body.length` but there is no key `body`, it will be ignored.
### Index.db
The underlying `db`.
### Index.db.by{Name}.get(key[, opts], fn)
### Index.db.by{Name}.create{Key,Value,Read}Stream([opts])
See [level-auto-index](https://github.com/hypermodules/level-auto-index).
## Multilevel
Populate `db.methods` with the manifests of each indexed db.`object`:
```js
var index = require('level-idx');
var createManifest = require('level-manifest');
db.methods = {};
db.posts = index(db.sublevel('posts'))
.by('Slug', ['slug'])
.db;
db.methods.posts = {
type: 'object',
methods: createManifest(db.posts).methods
};
```
## See Also
- [hypermodules/level-hookdown](https://github.com/hypermodules/level-hookdown) - Simple levelup hooks implemented using instance method override of arbitrary levelups.
- [hypermodules/level-auto-index](https://github.com/hypermodules/level-auto-index) - Automatic secondary indexing for leveldb and subleveldown leveraging `level-hookdown`.
This module is a port of [juliangruber/level-sec](https://github.com/juliangruber/level-sec) that works/uses [subleveldown](http://ghub.io/subleveldown) and [level-auto-index](http://ghub.io/level-auto-index).