level indexing and finding
JavaScript
Switch branches/tags
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
test update tests to latest sublevel Nov 21, 2013
.gitignore initial Nov 19, 2013
History.md
Makefile initial Nov 19, 2013
Readme.md initial Nov 19, 2013
example.js initial Nov 19, 2013
index.js
package.json version 0.3.0 Nov 22, 2013

Readme.md

level-indexing

level indexing and finding

Installing

npm install level-indexing

Example

var assert = require('assert');

var level = require('level');
var sublevel = require('sublevel');
var indexing = require('level-indexing');

// sublevel instance
var db = sublevel(level('./level-test'));

// users sublevel (needs json value encoding)
var users = db.sublevel('users', { valueEncoding: 'json' });

// use indexing
indexing(users);

// index some properties
users
.index('username')
.index('email');

// a user object
var user = {
  id: 1,
  username: 'foobar',
  email: 'foo@bar'
};

users.put(user.id, user, function(err){
  // properties are now indexed
  users.by('username', 'foobar', function(err, value){
    assert.deepEqual(user, value);
  });

  users.byEmail('foo@bar', function(err, value){
    assert.deepEqual(user, value);
  });

  users.find(1, function(err, value){
    assert.deepEqual(user, value);
  });

  users.find('foobar', function(err, value){
    assert.deepEqual(user, value);
  });

  users.find('foo@bar', function(err, value){
    assert.deepEqual(user, value);
  });
});

API

indexing(sublevel)

Enable indexing on a sublevel db instance.

index(name)

Indexes a property name.

by(index, key, fn)

Gets a value from index by key. Key can be an object with a property of index.

find(key, fn)

Finds key in all indexes. Key can be an object.

License

MIT