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 🙏

© 2024 – Pkg Stats / Ryan Hefner

level-assoc

v0.13.0

Published

relational foreign key associations (hasMany, belongsTo) for leveldb

Downloads

37

Readme

level-assoc

foreign key associations (hasMany, belongsTo, ...) for leveldb

build status

example

var sub = require('level-sublevel');
var level = require('level-test')();
var db = sub(level('test', { valueEncoding: 'json' }));

var assoc = require('level-assoc')(db);
assoc.add('hackerspace')
    .hasMany('hackers', [ 'type', 'hacker' ])
    .hasMany('tools', [ 'type', 'tool' ])
;
assoc.add('hacker').belongsTo('hackerspace');
assoc.add('tool')
    .hasMany('usage', [ 'type', 'usage' ])
    .belongsTo('hackerspace')
;

db.batch(require('./data.json').map(function (row) {
    return { type: 'put', key: row.key, value: row.value };
}), ready);

function ready () {
    assoc.get('sudoroom', function (err, room) {
        console.log('SUDOROOM=', room);
        room.hackers().on('data', function (r) {
            console.log('HACKER', r.value)
        });
    });

    assoc.get('8d9a83', function (err, tool) {
        console.log('TOOL=', tool);
        tool.usage().on('data', function (r) {
            console.log('USAGE', r.value)
        });
    });
}

given this data.json in the database:

[
  {"key":"sudoroom","value":{"type":"hackerspace","name":"sudoroom"}},
  {"key":"noisebridge","value":{"type":"hackerspace","name":"noisebridge"}},
  {"key":"substack","value":{"type":"hacker","name":"substack","hackerspace":"sudoroom"}},
  {"key":"maxogden","value":{"type":"hacker","name":"maxogden","hackerspace":"sudoroom"}},
  {"key":"ioerror","value":{"type":"hacker","name":"ioerror","hackerspace":"noisebridge"}},
  {"key":"mitch","value":{"type":"hacker","name":"mitch","hackerspace":"noisebridge"}},
  {"key":"wrought","value":{"type":"hacker","name":"wrought","hackerspace":"sudoroom"}},
  {"key":"mk30","value":{"type":"hacker","name":"mk30","hackerspace":"sudoroom"}},
  {"key":"8d9a83","value":{"type":"tool","name":"3d printer","hackerspace":"sudoroom"}},
  {"key":"ea7e66","value":{"type":"tool","name":"piano","hackerspace":"sudoroom"}},
  {"key":"025452","value":{"type":"tool","name":"laser cutter","hackerspace":"noisebridge"}},
  {"key":"yardena","value":{"type":"hacker","name":"yardena","hackerspace":"sudoroom"}},
  {"key":"5cc709","value":{"type":"tool","name":"3d printer","hackerspace":"noisebridge"}},
  {"key":"d06ab1","value":{"type":"usage","tool":"8d9a83","minutes":"45","user":"maxogden"}},
  {"key":"2454c1","value":{"type":"usage","tool":"8d9a83","minutes":"20","user":"yardena"}},
  {"key":"ec08ed","value":{"type":"usage","tool":"ea7e66","minutes":"14","user":"substack"}},
  {"key":"61baab","value":{"type":"usage","tool":"025452","minutes":"8","user":"mitch"}}
]

the program prints:

SUDOROOM= { type: 'hackerspace',
  name: 'sudoroom',
  hackers: [Function],
  tools: [Function] }
TOOL= { type: 'tool',
  name: '3d printer',
  hackerspace: 'sudoroom',
  usage: [Function] }
HACKER { type: 'hacker', name: 'maxogden', hackerspace: 'sudoroom' }
USAGE { type: 'usage', tool: '8d9a83', minutes: '20', user: 'yardena' }
HACKER { type: 'hacker', name: 'mk30', hackerspace: 'sudoroom' }
USAGE { type: 'usage', tool: '8d9a83', minutes: '45', user: 'maxogden' }
HACKER { type: 'hacker', name: 'substack', hackerspace: 'sudoroom' }
HACKER { type: 'hacker', name: 'wrought', hackerspace: 'sudoroom' }
HACKER { type: 'hacker', name: 'yardena', hackerspace: 'sudoroom' }

methods

var levelAssoc = require('level-assoc')

var assoc = levelAssoc(db)

Create a new assoc instance from a sublevel-enabled leveldb instance db.

assoc.get(key, cb)

Fetch a key from the database with cb(err, row). row contains the underlying db.get() result but augmented with functions to return streams for the has-many collections.

var t = assoc.add(name)

Create a new type association t for rows with a "type" field set to name.

t.hasMany(key, filterKey)

Create a streaming collection at key for foreign rows matching the string array path filterKey.

t.belongsTo(key)

TODO. Currently a no-op.

install

With npm do:

npm install level-assoc

license

MIT