can't serialize nested associations
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 }}}
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.
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?
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.
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.
Can you provide a sample of code that doesn't work?
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.
@davesag Seems like useful capability. I'll think about it.
maybe something like
const hierarchicalScheme = {
include: ['@all', 'children'],
exclude: ['@pk', '@fk', '@auto', 'hierarchyLevel'],
assoc: {
children: {
include: ['@parent']
}
}
}