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

Ability to "collapse" data #32

Open astrotars opened 9 years ago

astrotars commented 9 years ago

For POST and PUTS, I feel it would be a great value add to allow for collapse functionality. That would mean taking a tree like data structure and returning it to its original form. Is this on the roadmap and/or do you have any recommendations on how to implementation for the time being?

kwhitley commented 9 years ago

To be honest Nick, I hadn't considered that... if you're talking about collapsing for the sake of space savings, the tree version should in theory be smaller than any other version. It "looks" bigger, having added structure fluff around the data, but removes all data duplication. Given that treeize was originally designed to remove the duplication from say... a SQL join type dataset (where data from one or more records is repeated for each child row), there can be a lot of duplication without it.

Mind giving me a couple line example of what you're proposing so I understand it better?

astrotars commented 9 years ago

@kwhitley definitely. The benefit behind having a collapse function would be to put grown data (in tree form) back to a flat data structure. For example, if I was updating a single row of data, it would allow me to take something like:

[
    {
      'director': 'Christopher Nolan',
      'title': 'The Prestige',
      'actors': [
        {
          'as': 'Alfred Borden',
          'name': 'Christian Bale'
        },
        {
          'as': 'Robert Angier',
          'name': 'Hugh Jackman'
        }
      ]
    }
]

to a flat data structure such as:

[
  {
    'title':             'The Prestige',
    'director':          'Christopher Nolan',
    'actors:name':       'Christian Bale',
    'actors:as':         'Alfred Borden'
  },
  {
    'title':             'The Prestige',
    'director':          'Christopher Nolan',
    'actors:name':       'Hugh Jackman',
    'actors:as':         'Robert Angier'
  }
]

This would allow for a super simple create and update on a row. As it stands right now, it's extremely difficult to reverse the grown data into a flat structure. Figured it would be a great value add.

kwhitley commented 9 years ago

Ah, gotcha. Shouldn't be too difficult to implement... I believe I store the original data that's inserted but if you mean to reverse a tree that may have been modified, it'll take a reversing function. Should be easier than the tree mapping though... I'll check it out!

On Tue, Dec 9, 2014 at 11:53 AM, Nick Parsons notifications@github.com wrote:

@kwhitley https://github.com/kwhitley definitely. The benefit behind having a collapse function would be to put grown data (in tree form) back to a flat data structure. For example, if I was updating a single row of data, it would allow me to take something like:

[ { 'director': 'Christopher Nolan', 'title': 'The Prestige', 'actors': [ { 'as': 'Alfred Borden', 'name': 'Christian Bale' }, { 'as': 'Robert Angier', 'name': 'Hugh Jackman' } ] } ]

to a flat data structure such as:

[ { 'title': 'The Prestige', 'director': 'Christopher Nolan', 'actors:name': 'Christian Bale', 'actors:as': 'Alfred Borden' }, { 'title': 'The Prestige', 'director': 'Christopher Nolan', 'actors:name': 'Hugh Jackman', 'actors:as': 'Robert Angier' } ]

This would allow for a super simple create and update on a row. As it stands right now, it's extremely difficult to reverse the grown data into a flat structure. Figured it would be a great value add.

— Reply to this email directly or view it on GitHub https://github.com/kwhitley/treeize/issues/32#issuecomment-66325983.

astrotars commented 9 years ago

@kwhitley Awesome! It would be a game changer for me. I look forward to seeing what you come up with.

kwhitley commented 9 years ago

Very sorry for the delay @nparsons08 - I've added in a simple .getSeedData() function that should return the original flat rows, but actually creating a complete reversal (opposite of .grow()) to collapse expanded data will have to be a challenge to save for another day. It should certainly be possible though...

cscalfani commented 8 years ago

I wrote an untreeize library you might find useful: https://www.npmjs.com/package/untreeize

kwhitley commented 8 years ago

@cscalfani So awesome!!! I'll do a few tests on it and include your lib in the docs 👍 :)