# graph-denormalizer

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

Latest version **1.0.20** (published 2017-02-28) · ISC license · 0 weekly downloads

## Install

```sh
npm install graph-denormalizer
pnpm add graph-denormalizer
yarn add graph-denormalizer
bun add graph-denormalizer
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.20 |
| Published | 2017-02-28 |
| First published | 2017-02-25 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Marcello Sachs |
| Maintainers | marcello.sachs |
| Keywords | denormalize, graph, habtm, has_and_belongs_to_many |

## Links

- npm: https://www.npmjs.com/package/graph-denormalizer
- Repository: https://github.com/marcellosachs/graph-denormalizer
- Homepage: https://github.com/marcellosachs/graph-denormalizer#readme
- Issues: https://github.com/marcellosachs/graph-denormalizer/issues
- npm.io page: https://npm.io/package/graph-denormalizer

## Alternatives

- [apollo-link-http-common](https://npm.io/package/apollo-link-http-common.md) — 879.0K weekly downloads
- [react-relay](https://npm.io/package/react-relay.md) — 336.8K weekly downloads
- [relay-test-utils](https://npm.io/package/relay-test-utils.md) — 181.6K weekly downloads
- [@vendure/core](https://npm.io/package/@vendure/core.md) — 14.8K weekly downloads
- [@pnpm/deps.graph-sequencer](https://npm.io/package/@pnpm/deps.graph-sequencer.md) — 13.4K weekly downloads

## Recent versions

- 1.0.20 (latest) — 2017-02-28
- 1.0.19 — 2017-02-26
- 1.0.18 — 2017-02-26
- 1.0.17 — 2017-02-26
- 1.0.16 — 2017-02-26
- 1.0.15 — 2017-02-25
- 1.0.14 — 2017-02-25
- 1.0.13 — 2017-02-25

## README

# 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: []
        }
      ]
    }
  ]
}
```

---
_Source: https://npm.io/package/graph-denormalizer · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
