Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to handle array of string? #13

Open
Saviio opened this issue Dec 18, 2016 · 4 comments
Open

How to handle array of string? #13

Saviio opened this issue Dec 18, 2016 · 4 comments

Comments

@Saviio
Copy link
Contributor

Saviio commented Dec 18, 2016

It looks like this module cannot to convert the data as string[]? Otherwise am I just miss something?

let's say I have some data like this:

let data = [
  {
    id: 1,
    foo: 'foo'
  },
  {
    id: 1,
    foo: 'bar'
  },
  {
    id: 2,
    foo: 'baz'
  }
]

my question is how to make a definition so that it can be used to convert the data like this below.

[
  {
    id: 1,
    foo: ['foo', 'bar']
  },
  {
   id: 2,
   foo: ['baz']
  }
]
@dvalentiate
Copy link
Member

hmm, I hadn't considered doing that when I designed the library. I like the idea but haven't fully considered its needs and consequences.

I suspect there will be trouble with implementing a design like this because a set of unique keys is needed for each row of input.

It isn't clear to me how that a mapping would be possible if the source data had two properties that differed per id. In the following example if we wanted foo and bar to be arrays.

let data = [
  {
    id: 1,
    foo: 'foo1',
    bar: 'bar1'
  },
  {
    id: 1,
    foo: 'foo1',
    bar: 'bar2'
  },
  {
    id: 2,
    foo: 'foo2',
    bar: 'bar1'
  }
];

I'm not sure there would be a logical output.

The closest the NestHydration can do at the moment would be to have the input modified to something like the following:

let data = [
  {
    id: 1,
    foo__id: 1,
    foo__value: 'foo'
  },
  {
    id: 1,
    foo__id: 2,
    foo__value: 'bar'
  },
  {
    id: 2,
    foo__id: 3,
    foo__value: 'baz'
  }
];

which could be mapped by nestHydration to this:

let result = NestHydrationJS.nest(data);

// [
  {
    id: 1,
    foo: [
      {id: 1, value: 'foo'},
      {id: 2, value: 'bar'}
    ]
  },
  {
    id: 2,
    foo: [
      {id: 3, value: 'baz'}
    ]
  }
]

which using js array map could get the output you want:

for (let obj of result) {
  obj.foo = obj.foo.map((foo) => foo.value);
}

which would result in the desired output of

// [
  {
    id: 1,
    foo: ['foo', 'bar']
  },
  {
   id: 2,
   foo: ['baz']
  }
];

I still kinda like your idea though, and I think I will have time in the next 2 weeks to consider this further and maybe make a change that would allow for what you proposed. If you want to put a pull request forward yourself I'd certainly appreciate it.

@Saviio
Copy link
Contributor Author

Saviio commented Dec 20, 2016

Hi,

Thanks for your reply, after I post my issue I realized nestHydration can only support the structured data mapping for now. And I just found this problem when I try to write test cases, so this is not a big deal for me currently.

And the solution you mentioned I think it can only help the simple object mapping, if I have a complex object then I have to write a lot of code to make the output right.

P.S:
here's a strange output I got when I try to figure out the problem. Maybe you can use it as this format rule of new feature.

qq20161220-0

@dvalentiate
Copy link
Member

Update

I've come to the conclusion that this array is possible, a good idea, and can be implemented in a consistent manner.

I've implemented a filter that can run after the structure is built but came across a hiccup that makes it not a great option.

I've decided on implementing this array building option inside the core algorithm but it will take a bit of time to get to it.

@jordansoltman
Copy link

jordansoltman commented Mar 8, 2019

I'm a little late to the party, but for anyone else who has this issue, I have a fork that handles arrays.

https://github.com/jordansoltman/NestHydrationJS

It also handles composite keys as well. I've made a pull request to bring in the changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants