Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

graph-denormalizer

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

graph-denormalizer

denormalize a graph (handles has_and_belongs_to_many relationships, which normalizr/denormalizr (https://github.com/paularmstrong/normalizr) doesn't)

  • 1.0.20
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

Install

npm install --save graph-denormalizer

Test

npm run test

Usage

Example 1

import graphDenormalizerHof from 'graph-denormalizer'

const typeFn = (typeInput, ele) => typeInput === ele.category

const config = {
  typeFn,
  nodeIdAttr: 'this_id',
  node1IdAttrOnEdge: 'parentId',
  node2IdAttrOnEdge: 'childId',
  isDirectedGraph: false,
}

const configuredGraphDenormalizerHof = graphDenormalizer(config)

const nodes = [
    {this_id: 1, category: 'a'},
    {this_id: 2, category: 'b'},
    {this_id: 3, category: 'b'},
    {this_id: 4, category: 'c'},
    {this_id: 5, category: 'd'}
]

const edges = [
  {parentId: 1, childId: 2},
  {parentId: 1, childId: 3},
  {parentId: 1, childId: 4},
  {parentId: 2, childId: 5}
]

const graphDenormalizer = configuredGraphDenormalizerHof({nodes, edges})

Input

const nestSpec = {
  a: {
    b: {
      d: null,
    },
    c: {
      d: null,
    },
  },
}

const result = graphDenormalizer(nestSpec)

Output


{
  a: [
    {
      this_id: 1,
      category: 'a',
      b: [
        {
          this_id: 2,
          category: 'b',
          d: [
            {
              this_id: 5,
              category: 'd',
            }
          ]
        },
        {
          this_id: 3,
          category: 'b',
          d: [],
        },
      ],
      c: [
        {
          this_id: 4,
          category: 'c',
          d: []
        }
      ]
    }
  ]
}

Example 2

import graphDenormalizerHof from 'graph-denormalizer'

const typeFn = (typeInput, ele) => typeInput === ele.assignmentType.name

const config = {
  typeFn,
  nodeIdAttr: 'id',
  node1IdAttrOnEdge: 'assignment1Id',
  node2IdAttrOnEdge: 'assignment2Id',
  isDirectedGraph: false,
}

const configuredGraphDenormalizerHof = graphDenormalizerHof(config)

const nodes = [
    {id: 1, name: 'Hostos', assignmentType: {id: 1, name: 'site'}},
    {id: 2, name: '101', assignmentType: {id: 2, name: 'route'}},
    {id: 3, name: '102', assignmentType: {id: 2, name: 'route'}},
    {id: 4, name: 'Team 1', assignmentType: {id: 3, name: 'team'}},    
]

const edges = [
  {assignment1Id: 1, assignment2Id: 2},
  {assignment1Id: 1, assignment2Id: 3},
  {assignment1Id: 4, assignment2Id: 2},  
]

const graphDenormalizer = configuredGraphDenormalizerHof({nodes, edges})

Input

const nestSpec = {
  site: {
    route: {
      team: null,
    },
  }
}

const result = graphDenormalizer(nestSpec)

Output

{
  site: [
    {
      id: 1,
      name: 'Hostos',
      assignmentType: {id: 1, name: 'site'},
      route: [
        {
          id: 2,
          name: '101',
          assignmentType: {id: 2, name: 'route'},
          team: [
            {
              id: 4,
              name: 'Team 1',
              assignmentType: {id: 4, name: 'team'},
            }
          ]
        },
        {
          id: 3,
          name: '102',
          assignmentType: {id: 3, name: 'route'},
          team: []
        }
      ]
    }
  ]
}

Keywords

FAQs

Package last updated on 28 Feb 2017

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc