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

can't serialize nested associations #7

Open
aphavichitr opened this issue Jan 22, 2018 · 8 comments
Open

can't serialize nested associations #7

aphavichitr opened this issue Jan 22, 2018 · 8 comments

Comments

@aphavichitr
Copy link

does this lib work with nested associations? i'm working with 2 level deep nested associations. for example models Order -> User -> UserLocation

json { order: { user: {userLocation:{ // i want data from here to be included in the json }}}

@hauru
Copy link
Owner

hauru commented Jan 22, 2018

Yes, it was made with nested associations in mind. I think you should include assoc fields explicitely (either by their names or via @assoc) as they're not added by default.

@aphavichitr
Copy link
Author

aphavichitr commented Jan 22, 2018

Do you have to add the field names with the association in front? For example user.id or just id? If userLocation also has an id field called id, wouldn't there be a conflict?

The error i'm getting is Error: [object Object] is not a valid Sequelize model

By any chance do you have an example of a scheme that shows this?

@hauru
Copy link
Owner

hauru commented Jan 22, 2018

Well, there's an example in the readme. Nested serialization should work regardless of the depth, as long as the schemes involved are set up properly.

@aphavichitr
Copy link
Author

I think my issue has to do with having nested associations that return Arrays (one to many relationship) inside the outer models. I noticed in the example it only shows how to deal with one (.serialize) or many (.serializeMany) but not both if it's contained together in the nested layers.

@hauru
Copy link
Owner

hauru commented Jan 26, 2018

Can you provide a sample of code that doesn't work?

@davesag
Copy link

davesag commented May 9, 2018

I'm trying to serialise objects created using https://github.com/overlookmotel/sequelize-hierarchy

It works if I manually specify in the serialiser how deep to nest the hierarchy but I was hoping that just being able to do something like

const hierarchicalScheme = {
  include: ['@all', 'children'],
  exclude: ['@pk', '@fk', '@auto', 'hierarchyLevel'],
  assoc: {
    children: {
      include: ['@all', 'children'],
      exclude: ['@pk', '@fk', '@auto', 'hierarchyLevel']
    }
  }
}

would be enough to recurse the serialisation all the way down. Alas if doesn't, it only does the first and second tiers.

@hauru
Copy link
Owner

hauru commented May 10, 2018

@davesag Seems like useful capability. I'll think about it.

@davesag
Copy link

davesag commented May 11, 2018

maybe something like

const hierarchicalScheme = {
  include: ['@all', 'children'],
  exclude: ['@pk', '@fk', '@auto', 'hierarchyLevel'],
  assoc: {
    children: {
      include: ['@parent']
    }
  }
}

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