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

foreign-key

v0.1.0

Published

group and reduce rows for foreign key relations for sorted key-value stores like leveldb

Downloads

17

Readme

foreign-key

generate keys for sorted key-value stores to group and reduce rows by a foreign key

build status

example

Given some data about hackerspaces, hackers, and items:

[
  { "type": "hackerspace", "name": "sudoroom" },
  { "type": "hacker", "name": "substack", "space": "sudoroom"},
  { "type": "hackerspace", "name": "noisebridge" },
  { "type": "hacker", "name": "mitch", "space": "nosiebridge" },
  { "type": "item", "name": "3d printer", "space": "sudoroom" },
  { "type": "item", "name": "piano", "space": "sudoroom" },
  { "type": "hacker", "name": "mk30", "space": "sudoroom" },
  { "type": "hacker", "name": "rwolfe", "space": "i3" },
  { "type": "item", "name": "3d printer", "space": "noisebridge" },
  { "type": "hacker", "name": "ioerror", "space": "noisebridge" },
  { "type": "hacker", "name": "wrought", "space": "sudoroom" },
  { "type": "hacker", "name": "nbritsky", "space": "i3" },
  { "type": "item", "name": "laser cutter", "space": "noisebridge" },
  { "type": "hackerspace", "name": "i3" }
]

we can generate key names to efficiently group hackers and items with their hackerspace using "space" as a foreign key refering to the "name" of a hackerspace:

var through = require('through');
var foreign = require('foreign-key');

var g = foreign([ 'type', 'hackerspace' ]);
g.add('hackers', [ 'type', 'hacker' ], 'space');
g.add('equipment', [ 'type', 'item' ], 'space');

var db = require('level')('/tmp/foreign-test', { valueEncoding: 'json' });
db.batch(require('./hackers.json').map(function (row) {
    return { type: 'put', key: g.key(row.name, row), value: row };
}));

db.createReadStream()
    .pipe(g.createStream())
    .pipe(through(console.log))
;

output:

{ type: 'hackerspace',
  name: 'i3',
  hackers: 
   [ { type: 'hacker', name: 'nbritsky', space: 'i3' },
     { type: 'hacker', name: 'rwolfe', space: 'i3' } ] }
{ type: 'hackerspace',
  name: 'noisebridge',
  equipment: 
   [ { type: 'item', name: '3d printer', space: 'noisebridge' },
     { type: 'item', name: 'laser cutter', space: 'noisebridge' },
     { type: 'item', name: 'makerbot', space: 'noisebridge' } ],
  hackers: 
   [ { type: 'hacker', name: 'ioerror', space: 'noisebridge' },
     { type: 'hacker', name: 'mitch', space: 'nosiebridge' } ] }
{ type: 'hackerspace',
  name: 'sudoroom',
  equipment: 
   [ { type: 'item', name: '3d printer', space: 'sudoroom' },
     { type: 'item', name: 'makerbot', space: 'sudoroom' },
     { type: 'item', name: 'piano', space: 'sudoroom' } ],
  hackers: 
   [ { type: 'hacker', name: 'mk30', space: 'sudoroom' },
     { type: 'hacker', name: 'substack', space: 'sudoroom' },
     { type: 'hacker', name: 'wrought', space: 'sudoroom' } ] }

methods

var foreign = require('foreign-key')

var g = foreign(primaryFilter)

Create a new foreign key instance g that pivots on the rows matching primaryFilter.

g.add(targetKey, filter, foreignKey)

Insert rows matching filter into rows matching primaryFilter at the location into primary rows targetKey.

g.key(key, row)

Return the new key to use for the ordinary unique identifier key given the foreign key rules applicable to row.

g.createStream()

Create a through stream that takes row objects as input and combines results from added secondary rows into primary rows.

install

With npm dO;

npm install foreign-key

license

MIT