Home

Awesome

level-sec

High-level API for creating secondary indexes using level-secondary. Also works over multilevel.

build status

Example

Index posts by title and body length, then query for them:

var Index = require('level-sec');
var level = require('level');
var sub = require('level-sublevel');

var db = sub(level('db', { valueEncoding: 'json' }));

var posts = Index(db.sublevel('posts'))
  .by('Title', 'title')
  .by('Length', ['body.length', 'title'])
  .by('Author', ['author', 'title'])
  .db;

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', console.log);
  posts.byAuthor.get('julian!a title', console.log);
});

API

Index(db)

Index db.

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. 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-secondary.

Multilevel

Populate db.methods with the manifests of each indexed db.object:

var index = require('level-sec');
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 
};

Installation

With npm do:

npm install level-sec

License

(MIT)

Copyright (c) 2013 Julian Gruber <julian@juliangruber.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.