kwhitley / treeize

Converts row data (in JSON/associative array format) to tree structure based on column naming conventions.
MIT License
189 stars 24 forks source link

null value results in two identical rows not being merged #35

Open arenddeboer opened 8 years ago

arenddeboer commented 8 years ago

When rows contain a field with a null value, they are treated as different values and even though the rows should be merged, they are not.

// Treeize turns flat associative data (as from SQL queries) like this:
var peopleData = [
    {
        'name': 'John Doe',
        'age': null,
        'pets:name': 'Rex',
        'pets:type': 'dog',
        'pets:toys:type': 'bone'
    },
    {
        'name': 'John Doe',
        'age': null,
        'pets:name': 'Rex',
        'pets:type': 'dog',
        'pets:toys:type': 'ball'
    },
    {
        'name': 'Mary Jane',
        'age': 19,
        'pets:name': 'Mittens',
        'pets:type': 'kitten',
        'pets:toys:type': 'yarn'
    },
    {
        'name': 'Mary Jane',
        'age': 19,
        'pets:name': 'Fluffy',
        'pets:type': 'cat'
    }
];

// ...via a dead-simple implementation:
var Treeize   = require('treeize');
var people    = new Treeize();

people.grow(peopleData);

// ...into deep API-ready object graphs like this:
console.log(people.getData());

[ { name: 'John Doe', pets: [ [Object] ] },
  { name: 'John Doe', pets: [ [Object] ] },
  { name: 'Mary Jane', age: 19, pets: [ [Object], [Object] ] } ]

Or am I missing something ?

mario-jerkovic commented 8 years ago

You can set the option object on treeize instance with prune:true, that should fiex the null problem

arenddeboer commented 7 years ago

Unfortunately the prune option has no effect. (Prune seems to be on by default)

for the moment I try setting as much columns to a default value of "" But this is poor database design and will not work with foreign key references. I wonder why this is not a huge issue for working with this library as it specifically mentions sql joins as a use case where NULL columns are more rule than the exception.

arenddeboer commented 7 years ago

For anyone running into this, the treeize inspired flatToTrees seems handle null values better.

pom421 commented 4 years ago

I had the same problem with the null column value (for a deleted date which is null 99% of the time). It is fixable with the workaround to suffix the column name with '*', it is documented here https://github.com/kwhitley/treeize#specifying-your-own-keyblueprint-for-collections

labarilem commented 2 years ago

I got it working with the same approach @pom421 used.