question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Find & include data structure shape

See original GitHub issue

I 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:closed
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
dafflcommented, Jul 19, 2018

What I mean is that all you have to do to get the nested models is updated you attachAssociation (which is a before hook) to:

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 = {
    raw: false,
    include: [permissions]
  }
  return context;
}
0reactions
dafflcommented, Jul 23, 2018

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found