npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

level-foreignkeys

v2.1.2

Published

Adds foreign key indexes to level.

Downloads

13

Readme

level-foreignkeys

Manage relationships between keys, and stream the results.

Deleting keys automatically cleans up their relationships.

##Full Use Example

var level = require('level');
var LevelForeignKeys = require('level-foreignkeys');
var ConcatStream = require('concat-stream');

var db = LevelForeignKeys(level('./some-path.db'));

function put1() {
    db.put('greeting1', 'hello', put2);
}
function put2() {
    db.put('greeting2', 'hi', put3);
}
function put3() {
    db.put('signoff1', 'bye', link1);
}
function link1() {
    db.addForeignKey('greeting1', 'response', 'signoff1', link2);
}
function link2() {
    db.addForeignKey('greeting2', 'response', 'signoff1', get1);
}
function get1() {
    var results = readForeignKeys('greeting1', 'response', {});
    results.pipe(new ConcatStream(function (entries) {
        console.log(entries);
        /*
        [{key: 'signoff1', value: 'bye'}]
        */
        get2();
    }));
}
function get2() {
    var results = readReverseForeignKeys('signoff1', 'response', {});
    results.pipe(new ConcatStream(function (entries) {
        console.log(entries);
        /*
        [{key: 'greeting1', value: 'hello'}, {key: 'greeting2': value: 'hi'}]
        */
    }));

}

put1();

##Installing

npm install --save level-foreignkeys

##Running Tests

cd node_modules/level-foreignkeys
npm i
npm test

##Wrapping LevelUp to "Mix-in" Foreign Key Methods

var level = require('level'); //by default, uses leveldown+levelup
var LevelForeignKeys = require('level-foreignkeys');

var db = LevelForeignKeys(level('./some-path.db'));

##Adding Foreign Key

addForeignKey(key, field, foreignKey, putOptions, callback)

Arguments:

  • key: The key you want the relationship to start from.
  • field: The name of the relationship type. This may represent the field in your value object. It might not. It is the name that categorizes the relationship.
  • foreignKey: The key you want to link the first key to.
  • putOptions: In running this function, it does several puts. Pass in the options you want to pass to those put operations.
  • callback: Function called when done.
db.addForeignKey(key, relationship, foreignkey, {}, function(err) {
    console.log("added link from %s to %s with the field %s", key, foreignkey, relationship);
});

##Removing a Foreign Key

In the same way you added the foreign key, you may remove it.

delForeignKey(key, field, foreignKey, delOoptions, callback)

Arguments:

  • key: The key you want the relationship to start from.
  • field: The name of the relationship type. This may represent the field in your value object. It might not. It is the name that categorizes the relationship.
  • foreignKey: The key you want to link the first key to.
  • delOptions: In running this function, it does several del calls. Pass in the options you want to pass to those del operations.
  • callback: Function called when done.
db.delForeignKey(key, relationship, foreignkey, {}, function(err) {
    if (!err) {
        console.log("deleted link from %s to %s with the field %s", key, foreignkey, relationship);
    }
});

##Streaming Foreign Keys

readForeignKeys(key, field, opts)

Arguments:

  • key: The key you want to get the foreign keys of.
  • field: The relationship category that you want.
  • opts: The extra options Object that you want to merge into the createReadStream call that this function makes.

Returns:

A node stream of key/value entries that have the queried relationship.

var resultStream = db.readForeinKeys('somekey', 'somerelationship', {some: 'options'});

##Streaming Reversed Relationships

level-foreignkeys also keeps track of the relationships in reverse with separate indexes. Retreiving them similarly involves starting at the other end of the relationship and using readReverseForeignKey.

readReverseForeignKeys(key, field, opts)

Arguments:

  • key: The foreign key from which you want to get the keys related.
  • field: The relationship category that you want.
  • opts: The extra options Object that you want to merge into the createReadStream call that this function makes.

Returns:

A node stream of key/value entries that have the queried relationship.

var resultStream = db.readReverseForeignKeys('someforeignkey', 'somerelationship', {some: 'options'});