Find & include data structure shape
See original GitHub issueI have a roles model and a permissions model. In the roles model I have: roles.belongsToMany(models.permissions, { through: ‘rolePermissions’ });
Then in the roles hook I have the following function that runs in the Before.Find Hook
const attachAssociations = (context) => {
// Get the Sequelize instance. In the generated application via:
const sequelize = context.app.get('sequelizeClient');
const { permissions } = sequelize.models;
context.params.sequelize = {
include: [permissions]
}
return context;
}
When I do a request then to users I get the following output:
{ total: 1,
limit: 10,
skip: 0,
data:
[ { id: 1,
name: 'anonymous',
createdAt: 2018-07-18T06:13:29.971Z,
updatedAt: 2018-07-18T06:13:29.971Z,
'permissions.id': 2,
'permissions.name': 'create-user',
'permissions.createdAt': 2018-07-18T06:13:29.895Z,
'permissions.updatedAt': 2018-07-18T06:13:29.895Z,
'permissions.rolePermissions.createdAt': 2018-07-18T06:13:29.985Z,
'permissions.rolePermissions.updatedAt': 2018-07-18T06:13:29.985Z,
'permissions.rolePermissions.roleId': 1,
'permissions.rolePermissions.permissionId': 2 },
{ id: 1,
name: 'anonymous',
createdAt: 2018-07-18T06:13:29.971Z,
updatedAt: 2018-07-18T06:13:29.971Z,
'permissions.id': 10,
'permissions.name': 'read-orgs',
'permissions.createdAt': 2018-07-18T06:13:29.897Z,
'permissions.updatedAt': 2018-07-18T06:13:29.897Z,
'permissions.rolePermissions.createdAt': 2018-07-18T06:13:29.985Z,
'permissions.rolePermissions.updatedAt': 2018-07-18T06:13:29.985Z,
'permissions.rolePermissions.roleId': 1,
'permissions.rolePermissions.permissionId': 10 },
{ id: 1,
name: 'anonymous',
createdAt: 2018-07-18T06:13:29.971Z,
updatedAt: 2018-07-18T06:13:29.971Z,
'permissions.id': 12,
'permissions.name': 'read-supplies',
'permissions.createdAt': 2018-07-18T06:13:29.898Z,
'permissions.updatedAt': 2018-07-18T06:13:29.898Z,
'permissions.rolePermissions.createdAt': 2018-07-18T06:13:29.985Z,
'permissions.rolePermissions.updatedAt': 2018-07-18T06:13:29.985Z,
'permissions.rolePermissions.roleId': 1,
'permissions.rolePermissions.permissionId': 12 } ] }
So the request works but the structure of the output to me seems a little strange, I would expact that I would get back 1 array entry per role with an array of permissions inside it, instead I am getting a duplication of roles decided by the number of permissions that the user has.
I would expect / prefer a structure more like this:
{ total: 1,
limit: 10,
skip: 0,
data:
[ { id: 1,
name: 'anonymous',
createdAt: 2018-07-18T06:13:29.971Z,
updatedAt: 2018-07-18T06:13:29.971Z,
permissionIds: [2, 10, 12] ] }
Are there any ways of doing this / suggestions on the best way to do this?
Thank you and regards, Emir
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Right data structure for containing shapes and spatial ...
Use Python's classes for this. A simple example. Let's first define a Point data structure that will hold information about a point in ......
Read more >Python shape() method - All you need to know!
With NumPy data structure, we store data elements in the form of an array. When we associate the shape() method with the NumPy...
Read more >The shape of data
Mostly focused on how application-level data is represented, manipulated, viewed, stored and transmitted, as opposed to how new types and data- ...
Read more >Find Similarly-Shaped Structures with Advanced Search
Use the Advanced Search>Structure Similarity option to find proteins with similar 3D protein shapes using either a PDB ID or a URL to...
Read more >Data structure - for soft objects
whose shape changes in response to their ... revision concerns the data structure for the field ... To find the cubes intersected by...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
What I mean is that all you have to do to get the nested models is updated you
attachAssociation
(which is abefore
hook) to:If you have an idea how it could be improved, a pull request is definitely welcome. Although the docs for params.sequelize does mention that it should be done in a
before
hook. Maybe an example that shows the hook being registered might make it more clear.Closing this issue since the problem has been solved.