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

radgraph

v0.1.13

Published

Simple, opinionated, object association API

Downloads

294

Readme

Radgraph

Based loosely on Facebook's TAO spec.

Radgraph is a simple object association service designed primarily to complement Radredis. However, the only assumption Radgraph makes about your object store is that each object has a unique primary key, and can be used for arbitrary key associations.

Radgraph allows you to store associations between integers, automatically handles inverse association, and can optionally attach data to associations.

Setup

const Radgraph = require('radgraph')
const redisOpts = { db: 15, keyPrefix: 'your-app:' }
const transforms = {} // not yet implemented

const schema =
  { name: "Authored"
  , inverse: "AuthoredBy"
  , from: "User"
  , to: "Post"
  , properties:
    { status: { type: "integer" }
    , category: { type: "string" }
    , tags: { type: "array" }
    }
  }

const Authored = Radgraph(schema, transforms, redisOpts)
const AuthoredBy = Authored.inv

Note that if no inverse name is specified, Radgraph will assume that you do not want an inverse edge and Authored.inv will be undefined. Ensure that the inverse field is specified if you intend to use both directions.

See ioredis for more information on redis connection options.

Response type

All Radgraph methods return promises. Edges are of the format:

const Edge = 
  { type: "Authored"
  , from: 1
  , to: 2
  , time: 1455486968000
  , data:
    { status: 0
    , category: "filthy, disgusting mfa trash"
    , tags: [ "wiwt", "pickup", "frugal" ]
    }
  }

.from(from, { limit = 30, offset = 0 })

Queries all attributes pointing away from a specified node, using limit and offset for pagination

Authored.from(1)
  .then(console.log)
// => [ Edge, Edge, ..., Edge ]

Authored.inv.from(5)
  .then(console.log)
// => [ Edge ]

Authored.from(2, { limit: 3, offset: 2 })
  .then(console.log)
// => [ Edge, Edge, Edge ]

The .of(from) shorthand is provided to get the head of the array returned from a .from(from, { limit: 1, offset: 0 }) query. This is primarily a convenience method for inverse queries on one-to-many relationships.

Authored.inv.of(5)
  .then(console.log)
// => { type: 'AuthoredBy'
//    , from: 5
//    , to: 3
//    , time: 1455487422000
//    , data: { ... }
//    }

.get(from, to, { time })

Retrieves an edge between two nodes. If no { time } parameter is specified, the latest edge will be retrieved.

Authored.get(1, 2)
  .then(console.log)
// => { type: 'Authored'
//    , from: 1
//    , to: 2
//    , time: 1455487876000
//    , data: { status: 0 }
//    }
Authored.inv.get(2, 1)
// => { type: 'AuthoredBy'
//    , from: 2
//    , to: 1
//    , time: 1455487876000
//    , data: { status: 0 }
//    }
Authored.get(1, 2, { time: 1455487844400 })
  .then(console.log)
// => { type: 'Authored'
//    , from: 1
//    , to: 2
//    , time: 1455487844400
//    , data: { status: 1 }
//    }

.find(from, to, { limit = 30, offset = 0})

Retrieves all edges between two nodes. For most applications, edges will be unique and .get(from, to) should suffice.

Authored.find(1, 2)
  .then(console.log)
// => [ Edge, Edge, ... ]

Authored.inv.find(2, 1, { limit: 2, offset: 1 })
// => [ Edge, Edge ]

.add(from, to, [attributes])

Creates a new edge between two nodes. If the edge has data parameters, specify them in the third argument

Returns the new edge.

Authored.add(4, 5)
  .then(console.log)
// => { type: 'Authored'
//    , from: 4
//    , to: 4
//    , time: 1455488127000
//    }

Authored.add(6, 7, { status: 4, tags: [ "WizWearsCoolPants", "waves" ] })
  .then(console.log)
// => { type: 'Authored'
//    , from: 6
//    , to: 7
//    , time: 1455488198000
//    , data:
//      { status: 4
//      , tags: [ "WizWearsCoolPants", "waves" ]
//      }
//    }

Authored.inv.add(10, 1)
  .then(console.log)
// => { type: 'AuthoredBy'
//    , from: 10
//    , to: 1
//    , time: 1455488252000
//    }

.set(from, to, [attributes])

Updates attributes of an edge. If no time attribute is specified, the most recent edge will be updated.

Returns the updated edge, or null if no such edge exists to update.

Authored.set(1, 3, { status: 1 })
  .then(console.log)
// => { type: 'Authored'
//    , from: 1
//    , to: 3
//    , time: 1455487479000
//    , data:
//      { status: 1
//      , category: "a$ap"
//      , tags: ["rick owens", "cdg heart meme"]
//      }
//    }

Authored.inv.set(2, 1, { status: 2, time: 1455487401000 })
  .then(console.log)
// =>  { type: 'AuthoredBy'
//     , from: 2
//     , to: 1
//     , time: 1455487401000
//     , data: { status: 2 }
//     }

Authored.set(9999, 9999, { time: -1 })
  .then(console.log)
// => null

.delete(from, to, [time])

Deletes an edge. If no time attribute is provided, the most recent edge will be deleted.

Returns timestamp of deleted edge, or null if the edge does not exist

Authored.delete(1, 2)
  .then(console.log)
// => 1455486761000

Authored.delete(1, 2, 1455486751000)
  .then(console.log)
// => 1455486751000

Authored.delete(1, 2, -1)
  .then(console.log)
// => null (edge doesn't exist)

Authored.inv.delete(2, 1)

.deleteAll(from)

Delete all edges leading to and from a node, useful for deleting records.

Returns the number of edges deleted.

Authored.deleteAll(8)
  .then(console.log)
// => 5

Authored.deleteAll(1)
  .then(() => Authored.from(1))
  .then(console.log)
// => []

Authored.inv.deleteAll(1)
  .then(() => Authored.inv.of(1))
  .then(console.log)
// => undefined