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

gson-relationship

v2.0.1

Published

Transform json-data using relational concepts

Downloads

6

Readme

JSON Relationship

This is a basic json relationship implementation following the minimal specification.

  • Quick transformation of nested json-data
  • reusable json-objects within a json-document (dry)

install via npm i gson-relationship --save

Examples

1:1 relationship

Normalize nested json-objects, like

{
  server: {
    serverA: {
      id: "serverA",
      service: { name: "A" }
    },
    serverB: {
      id: "serverB",
      services: { name: "B" }
    }
  }
}

to the following normalized representation

{
  server: {
    serverA: { id: "serverA" },
    serverB: { id: "serverB" }
  },
  server_services: {
    serverA: "serviceA"
    serverB: "serviceB"
  },
  services: {
    serviceA: { name: "A" },
    serviceB: { name: "B" },
  }
}

and vice versa

1:n relationship

Normalize nested json-objects, like

{
  server: {
    serverA: {
      id: "serverA",
      services: {
        serviceA: { name: "A" },
        serviceB: { name: "B" }
      }
    },
    serverB: {
      id: "serverB",
      services: {
        serviceB: { name: "B" }
      }
    }
  }
}

to the following normalized representation

{
  server: {
    serverA: { id: "serverA" },
    serverB: { id: "serverB" }
  },
  server_services: {
    serverA: ["serviceA", "serviceB"],
    serverB: ["serviceB"]
  },
  services: {
    serviceA: { name: "A" },
    serviceB: { name: "B" }
  }
}

and vice versa

Usage example

Transforms the above example having a server-service relationship to service-server relationship

const { join, normalize, invertPivot } = require('json-relationship');

const data = {
  server: {
    serverA: {
      id: "serverA",
      services: {
        serviceA: { name: "A" },
        serviceB: { name: "B" }
      }
    },
    serverB: {
      id: "serverB",
      services: {
        serviceB: { name: "B" }
      }
    }
  }
}

// normalize the table as described in 'examples'
const normalized = normalize(data, {
    type: "1:n",
    model: "server",
    alias: "services",
    pivot: "server_services",
    reference: "services"
});

// we need to invert the mapping table for our purpose
const inverted = invertPivot(normalized, "server_services", "services_server");

// then rebuild the data with the inverted relationship
const services = join(inverted, {
    type: "1:n",
    model: "services",
    alias: "server",
    pivot: "services_server",
    reference: "server"
});

which results in services structured as

{
  services: {
    serviceA: {
      name: "A",
      server: {
        serverA: { id: "serverA" }
      }
    },
    serviceB: {
      name: "B",
      server: {
        serverA: { id: "serverA" },
        serverB: { id: "serverB" }
      }
    }
  }
}

API

methods

| method | description | ------------------------------------- | ------------------------------------------------------------- | normalize(data, rel) : object | build a unlinked json-data model containing pivot-table and references | join(data, rel) : object | build a linked json-data, resolving pivot and reference-model | invertPivot(data, from, to) : object | invert a pivot table (1:1, 1:n). May change relationtype from 1:1 to 1:n

relation description

| property | description | ---------------------- | ------------------------------------------------------------- | model:string | json-pointer to parent tupels, e.g. /data/server | reference:string | json-pointer to related tupels e.g. /data/services | pivot:string | json-pointer to pivot table, mapping model-reference e.g. /data/pivot | alias:string | json-pointer within model tupel to related tupel, e.g. /services | move:string | Removes associated relationships and pivots from model. defaults to true. | referenceId:string | optional: change foreign key of a related tupel (property within tupel), e.g. /id

normalize

normalize(data:object, relationship:object) : object

join

join(data:object, relationship:object) : object

invertPivot

invertPivot(data:object, from:string, to:string = from) : object