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

fifinet

v0.1.7

Published

A tiny graph database and query engine

Downloads

77

Readme

fifinet

Tiny in-memory graph database and query engine implementation.

The query engine looks a lot like the Gremlin traversal engine, and this module is heavily based upon Dagoba by Dann Toliver.

What's this?

This is a tiny implementation of an in-memory graph database, with a non-optimized but functional query engine based on a fluent API.

The graph is a property graph, which means that vertices and edges can have arbitrary properties which you can use to store information in.

You can then use the query functionality to query the database.

Creating a graph

Define your own types to hold the properties of vertices and edges.

On vertices, the _id property is special: it will hold the ID of the vertex. You can provide one, otherwise one will be assigned automatically.

On edges, the _label property is special: it is the type of edge. It is optional, in case you only have one type of edge.

Creating a graph looks like this:

import * as fn from 'fifinet';

interface GreekProps {
  species: 'god' | 'titan' | 'halfgod' | 'mortal';
}

interface EdgeProps {
}

const graph = new fn.Graph<GreekProps, EdgeProps>(
  [
    { _id: 'zeus',       species: 'god' },
    { _id: 'herakles',   species: 'halfgod' },
    { _id: 'alcmene',    species: 'mortal' },
    { _id: 'amphitryon', species: 'mortal' },
  ], [
    { _out: 'amphitryon', _in: 'alcmene',    _label: 'marriedTo' },
    { _out: 'alcmene',    _in: 'amphitryon', _label: 'marriedTo' },
    { _out: 'zeus',       _in: 'amphitryon', _label: 'impersonates' },
    { _out: 'zeus',       _in: 'alcmene',    _label: 'rapes' },
    { _out: 'herakles',   _in: 'zeus',       _label: 'childOf' },
    { _out: 'herakles',   _in: 'alcmene',    _label: 'childOf' },
  ],
);

The graph object also has methods to mutate and inspect the graph:

graph.addVertices([
  { _id: 'perseus', species: 'mortal' },
  { _id: 'electryon', species: 'mortal' },
]);
graph.addEges([
  { _out: 'perseus',   _in: 'zeus',      _label: 'childOf' },
  { _out: 'electryon', _in: 'perseus',   _label: 'childOf' },
  { _out: 'alcmene',   _in: 'electryon', _label: 'childOf' },
]);

Querying

Querying starts with the graph.v() function, which selects one or more vertices from the graphs. You then chain a number of operations onto the query, before finally calling run() to execute the query:

const results = graph.v()./* any number of operations here */.run();

For example, to find all the parents of Herakles:

const results = graph.v('herakles').out('childOf').run();

Graph: v()

Returns a set of nodes to start the query. The following filter call patterns are accepted:

graph.v()                      // All nodes
graph.v('zeus')                // Single ID
graph.v(['zeus', 'herakles'])  // Multiple IDs
graph.v({ species: 'god' })    // Filter by example

Query: in()/out()/inAny()/outAny()

Traverse edges from the currently selected set of nodes:

  • in, out: traverse one edge either coming into or going out of the current node.
  • inAny, outAny: traverse one or more edges either coming into or going out of the current node.

Takes an argument filtering the edges:

graph.v().out()                         // All outgoing edges
graph.v().out('marriedTo')              // Single label
graph.v().out(['marriedTo', 'childOf']) // Multiple labels
graph.v().out({ _label: 'marriedTo' })  // Filter by example

Query: property()

For convenience, return a properties from each vertex in the query result set, instead of returning the whole query object.

graph.v('zeus').in('childOf').property('species').run()

Query: filter()

Filter the current set of nodes down by subsetting it.

query.filter({ species: 'mortal' })                 // Filter by example
query.filter(vertex => vertex.species === 'mortal') // Filter by callback

Example:

graph.v()
  .filter({ species: 'halfgod' })
  .run()

Query: unique()

If the query would yield duplicate nodes, return only the unique ones.

graph.v('zeus')
  .in('childOf')
  .out('childOf') // This would return Zeus as many times as he has children
  .unique()       // Suppress duplicate Zeuses
  .run()

Query: take()

Instead of returning all results, return only the next N.

You can run the query multiple times to obtain multiple subsets of results:

const query = graph.v().take(2);

query.run(); // Returns the first 2 nodes
query.run(); // Returns the next 2 nodes, etc.

Query: as()

Label the current set of vertices in the query with an alias. This can be used to refer back to the current vertex set later on, using other operators like merge(), except() and back().

g.v().as('root')

Query: merge()

Replace the current vertex set with those of one or more aliases from previous node sets.

To return Zeus' children and grandchildren:

g.v('zeus')
  .in('childOf').as('children')
  .in('childOf').as('grandchildren')
  .merge('children', 'grandchildren')
  .run()

Query: except()

Exclude the vertices from a previously named vertex set from the current set of vertices.

The following returns all of Herakles' siblings who aren't Herakles:

g.v('herakles').as('me')
  .out('childOf').in('childOf')
  .except('me')
  .run()

Query: back()/having()

Back returns to a previous vertex in the query, but only if the query so far has been successful.

The following returns all nodes that have mortal children:

g.v().as('me')
  .in('childOf').filter({ species: 'mortal' })
  .back('me')
  .unique()
  .run()

having() can be used as an alias for as()/back()/unique():

g.v()
  .having(me => me.in('childOf').filter({ species: 'mortal' ]))
  .run()