1.1.3 • Published 7 years ago

deep-object-graph v1.1.3

Weekly downloads
28
License
MIT
Repository
github
Last release
7 years ago

Deep Object Graph

Build Status

An alternative to treeize.

Converts flat JSON objects into deep object graph.

Purpose

Results that comes back from a SQL join query usually have many duplicated data (depending on how complex the joins are).

deep-object-graph converts this flat data into a clean and readable JSON by removing these duplicated data and merging data that shares the same parent entity.

Usage

Install it.

npm install deep-object-graph --save

To use.

Note: Each object needs to have an unique field to identify them with other objects that share the same unique field for merging. By Default this field name is 'id', it is configurable with options.primaryKeyField.

import deepObjectGraph from 'deep-object-graph';
// const deepObjectGraph = require('deep-object-graph').default; // es5-syntax.

// Create new instance with default options shown.
const dogInstance = new deepObjectGraph({
	primaryKeyField: 'id', // The key name of the unique field to identify an object.
    delimiter: '.', // The delimiter char to determine nesting.
    specialKeyArray: [] // The key name of fields where values will be in an array even if there is only one object.
}); 

const data = [{
    "id": "1",
    "name": "Cat",
    "tags.id": "t2",
    "tags.tag": "animal",
    "tags.description": null,
    "tags.metadata": {
        "isStray": true
    }
}];

let results = dogInstance.convert(data);

// 'results' will be...
[{
    "id": "1",
    "name": "Cat",
    "tags": {
      "id": "t2",
      "tag": "animal",
      "description": null,
      "metadata": {
      	"isStray": true
      }
    }
}]

Examples

// Input...
[
	{
        "id": "1",
        "name": "Cat",
        "tags.id": "t2",
        "tags.tag": "animal",
        "tags.description": null,
        "tags.meta": {
            "fieldA": true
        }
    },{
        "id": "1",
        "name": "Cat",
        "tags.id": "t1",
        "tags.tag": "colour",
        "tags.description": null,
        "tags.meta": {
            "fieldB": true
        },
    },{
        "id": "2",
        "name": "Dog",
        "tags.id": "t2",
        "tags.tag": "animal",
        "tags.description": null,
        "tags.meta": {
            "fieldA": true
        }
    },{
        "id": "2",
        "name": "Dog",
        "tags.id": "t1",
        "tags.tag": "colour",
        "tags.description": null,
        "tags.meta": {
            "fieldB": true
        }
	}
]

// Output...
[
	{
        "id": "1",
        "name": "Cat",
        "tags": [{
            "id": "t2",
            "tag": "animal",
            "description": null,
            "meta": {
                "fieldA": true
            }
        },{
            "id": "t1",
            "tag": "colour",
            "description": null,
            "meta": {
                "fieldB": true
            }
        }]
    },{
        "id": "2",
        "name": "Dog",
        "tags": [{
            "id": "t2",
            "tag": "animal",
            "description": null,            
            "meta": {
                "fieldA": true   
            }
        },{
            "id": "t1",
            "tag": "colour",
            "description": null,
            "meta": {
                "fieldB": true
            }
        }]
	}
]

Convert to array if value type is non-object.

// Input...
[
	{
        "id": "1",
        "name": "Cat",
        "metaData": 'valueA'
    },{
        "id": "1",
        "name": "Cat",
        "metaData": 'valueB'
    },{
        "id": "1",
        "name": "Cat",
        "metaData": 'valueC'
    }
]

// Output...
[
	{
        "id": "1",
        "name": "Cat",
        "metaData": ['valueA', 'valueB', 'valueC']
    }
]

primaryKeyField option.

let thisDogInstance = new DeepObjectGraph({
    primaryKeyField: 'uuid'
});

// Input...
[
	{
        "uuid": "1",
        "name": "Cat",
        "tags.uuid": "t2",
        "tags.tag": "animal",
        "tags.description": null,
        "tags.metadata": {
            "isStray": true
        }
    }
]

// Output...
[
	{
        "uuid": "1",
        "name": "Cat",
        "tags": {
            "uuid": "t2",
            "tag": "animal",
            "description": null,
            "metadata": {
                "isStray": true
            }
        }
    }
]

delimiter option.

let dogInstance = new DeepObjectGraph({
    delimiter: ':'
});

// Input...
[
	{
        "id": "1",
        "name": "Cat",
        "tags:id": "t2",
        "tags:tag": "animal",
        "tags:description": null,
        "tags:meta:isHungry": true,
        "tags:meta:isCute": false,
        "tags:meta:isNotStray:hasName": true
    },{
        "id": "1",
        "name": "Cat",
        "tags:id": "t2",
        "tags:tag": "animal",
        "tags:description": null,
        "tags:meta:isHungry": true,
        "tags:meta:isCute": false,
        "tags:meta:isNotStray:isSpayed": true
	}
]

// Output...
[
	{
        "id": "1",
        "name": "Cat",
        "tags": {
            "id": "t2",
            "tag": "animal",
            "description": null,
            "meta": {
                "isHungry": true,
                "isCute": false,
                "isNotStray": {
                    "hasName": true,
                    "isSpayed": true
                }
            }
        }
    }
]

specialKeyArray option to force value into an array.

let dogInstance = new DeepObjectGraph({
    specialKeyArray: ['tags']
});

// Input...
[
	{
        "id": "1",
        "name": "Cat",
        "tags.id": "t2",
        "tags.tag": "animal",
        "tags.description": null,
        "tags.metadata": {
            "isStray": true
        }
	}
];

// Output...
[
	{
        "id": "1",
        "name": "Cat",
        "tags": [{
            "id": "t2",
            "tag": "animal",
            "description": null,
            "metadata": {
                "isStray": true
            }
        }]
	}
]

Refer to test.js for more examples.

1.1.3

7 years ago

1.1.2

7 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago