Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

nanograph

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nanograph

minimalistic in-memory graph for metadata management

latest
Source
npmnpm
Version
1.0.3
Version published
Weekly downloads
13
Maintainers
1
Weekly downloads
 
Created
Source

Nanograph

Minimalistic in-memory graph for metadata management. Allows to create directions graphs to store entities and connect them. Connections, called "edges" may also contain data.

Installation

Just run npm i nanograph. Types for Typescript are provided and must not be installed separately.

Usage

Initialisation

import { Nanograph } from 'nanograph';

const nano: Nanograph = new Nanograph();

Insert vertices and edges

const { _id: vertex1Id, error: error1 } = nano.createVertex('PERSON', { name: 'John Doe' });
const { _id: vertex2Id, error: error2 } = nano.createVertex('PERSON', { name: 'Jane Doe' });

const { _id: friendshipId } = nano.createEdge('FRIENDSHIP', vertex1Id, vertex2Id, {
    type: 'platonic', since: Date.now(),
});

Retrieve vertices and edges

Retrieve the first edge matching provided criteria. If an ID is given, it can simply be passed as string.

const vertex = nano.findVertices('PERSON', vertex1Id ).getFirst();			
const edge = nano.findEdges('FRIENDSHIP', { _id: { equals: friendshipId } }).getFirst();

For filtering, instead of an id, parameters can be provided. For this version, only equals is possible. Result of getAll is a list of vertices or edges

const vertex = nano.findVertices('PERSON', { name: { equals: 'John Doe' } }).getAll();
const edges = nano.findEdges('FRIENDSHIP', { type: { equals: 'platonic' } }).getAll();

Traversal is also possible using labels and properties to filter

const vertices = nano
    .findVertices('PERSON', { name: { equals: 'John Doe' } }) // a person called John Doe
    .over('CHILDOF').to('PERSON') // searching for John Does Parents
    .over('CHILDOF').to('PERSON', { gender: { equals: 'm' } }) // searching for their father, John Doe's grandfather
    .over('MARRIED').to('PERSON') // searching for his wife, John Doe's grandmother
    .getAll();

Update Entities

Both edges and vertices can be updated using update methods. Old values will be kept. If a value to update is already exiting, it will be overwritten.

nano.updateVertex(vertexId, { weight: 6000, hasFur: false });
nano.updateEdge(edgeId, { isShareHolder: true, holdings: 15 });

Delete Entities

Vertices and edges can be deleted by their id.

nano.deleteEdge(edgeId);
nano.deleteVertex(vertexid);

Keywords

graph

FAQs

Package last updated on 29 Jan 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts