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

property-graph

v3.0.0

Published

Base for creating objects that behave like a Property Graph.

Downloads

73,515

Readme

property-graph

Latest NPM release npm bundle size License Build Status Coverage

Extensible base for creating objects that behave like a Property Graph.

Overview

The property-graph package is intended as a foundation for libraries requiring many custom types of compatible parts, which can be represented as a Property Graph. The Property Graph representation is useful for dependency chains, resource references, node-based art workflows, and a broader class of applications where Graph databases are common.

Conceptually, a Property Graph is a labeled, directed multigraph, in which entities ("nodes") may have named relationships ("edges") with other nodes on the graph. Both nodes and edges may also be associated with key/value attributes. Beyond that, property-graph is intended to be small and practical, rather than providing a large standard library for graph theory — if you need something more comprehensive, I'd suggest graphology.

Typically, you'll define several classes inheriting from the base GraphNode. When using TypeScript, an interface should be provided defining the kinds of connections that each type of graph node allows. Then, .set and .get methods may be used to set key/value attributes (strings, numbers, booleans, ...), and .getRef and .setRef methods may be used to create edges (or relationships) to other nodes of a compatible type. All references have names, and support compile-time type-checking.

Features

In a codebase with many distinct types of entities and relationships among them (e.g. "Client has N Projects", "Project has N Tasks"), this project can make management of entities and their relationships considerably easier than writing plain getters/setters for each case.

  • Traversal: GraphEdges are tracked and can be traversed up or down
  • Disposal: GraphNode disposal automatically cleans up incoming references from other nodes
  • Finding dependents: Efficiently locate all GraphNodes that refer to a given GraphNode, or that have references from a given GraphNode
  • Change detection: GraphNodes dispatch events when changed, which can be optionally propagated throughout the graph
  • Extensibility: Operations like .copy(), .equals(), and .swap(a, b) can be implemented abstractly

Usage

Definitions:

import { GraphNode, RefSet } from 'property-graph';

interface IPerson {
  name: string;
  age: number;
  friends: RefSet<Person>;
  pet: Pet;
}

interface IPet {
  type: 'dog' | 'cat';
  name: string;
}

class Person extends GraphNode<IPerson> {
	getDefaults(): Nullable<IPerson> {
		return {name: '', age: 0, friends: new RefSet(), pet: null};
	}
}
class Pet extends GraphNode<IPet> {
	getDefaults(): Nullable<IPet> {
		return {type: 'dog', name: ''};
	}
}

Basic usage:

const graph = new Graph();

const spot = new Pet(graph)
  .set('type', 'dog')
  .set('name', 'Spot');

const jo = new Person(graph)
  .set('name', 'Jo')
  .set('age', 41)
  .setRef('pet', spot);

const sam = new Person(graph)
  .set('name', 'Sam')
  .set('age', 45)
  .addRef('friends', jo);

Lifecycles:

jo.equals(sam); // recursive equality → false

console.log(sam.listRefs('friends')); // → [jo]

jo.dispose();

console.log(sam.listRefs('friends')); // → []

API

Literal Attributes

Literal attributes (string, number, boolean, ...) are modified with two methods:

  • node.get('key'): Literal
  • node.set('key', value: Literal): this

References

References support one named connection to a single graph node of a given type:

  • node.getRef('key'): GraphNode
  • node.setRef('key', node: GraphNode): this

Reference Lists

Reference Lists support a named list of connections to graph nodes of a given type:

  • node.addRef('key', node: GraphNode): this
  • node.removeRef('key', node: GraphNode): this
  • node.listRefs('key'): GraphNode[]

Reference Maps

Reference Maps support a named map having any number of subkeys, where each subkey points to a graph node of a given type:

  • node.getRefMap('key', 'subkey'): GraphNode
  • node.setRefMap('key', 'subkey', node: GraphNode): this
  • node.listRefMapKeys('key'): string[]
  • node.listRefMapValues('key'): GraphNode[]

References