title: sequelize V6 typescript: get data as plain object with findall
body: <p>I'm testing sequelize (using typescript) to replace typeorm.
  However, I'm fighting to get data from the database, on a proper format. &quot;proper format&quot; means a record by row as a plain object, which represents bascially an instance of the class.(check the picture with the expected result )
  Below is a simple code that is self explicit. Team has several players and a player belongs to a team. In the postgres table, there are 2 teams, the first team as 3 players, the second one has 2 players.
  The connection's options to postgres are trivial.</p>
  <pre><code>    &quot;pg&quot;: &quot;^8.3.3&quot;,
      &quot;pg-hstore&quot;: &quot;^2.3.3&quot;,
      &quot;sequelize&quot;: &quot;^6.3.5&quot;,
      &quot;typescript&quot;: &quot;^4.0.2&quot;
  
  
  import { DataTypes, Model, Sequelize } from &quot;sequelize&quot;;
  import { createDbConnection } from &quot;./DbConnections&quot;;
  
  interface ITeamAttributes {
      id: number;
      teamName: string;
  }
  
  type ITeamCreationAttributes = Partial&lt;ITeamAttributes&gt; &amp; { id: number };
  
  class Team
      extends Model&lt;ITeamAttributes, ITeamCreationAttributes&gt;
      implements ITeamAttributes {
      public id: number;
      public teamName: string;
      public teamPlayers: Array&lt;Player&gt;;
  
      static defineModel(
          dbConnection: Sequelize
      ): Model&lt;ITeamAttributes, ITeamCreationAttributes&gt; {
          const table = this.init&lt;Team&gt;(
              {
                  id: {
                      type: DataTypes.INTEGER,
                      allowNull: false,
                      autoIncrementIdentity: true,
                      primaryKey: true,
                  },
                  teamName: {
                      type: DataTypes.STRING(255),
                      allowNull: false,
                      defaultValue: &quot;&quot;,
                  },
              },
              {
                  sequelize: dbConnection,
                  tableName: &quot;team&quot;,
              }
          );
  
          return table;
      }
  
      static defineRelations(): void {
          Team.hasMany(Player, {
              sourceKey: &quot;id&quot;,
              foreignKey: &quot;teamId&quot;,
              as: &quot;teamPlayers&quot;,
          });
      }
  }
  
  interface IPlayerAttributes {
      id: number;
      playerName: string;
  }
  
  type IPlayerCreationAttributes = Partial&lt;IPlayerAttributes&gt; &amp; { id: number };
  
  class Player
      extends Model&lt;IPlayerAttributes, IPlayerCreationAttributes&gt;
      implements IPlayerAttributes {
      public id: number;
      public playerName: string;
      public playerTeam: Team;
  
      public static defineModel(
          dbConnection: Sequelize
      ): Model&lt;IPlayerAttributes, IPlayerCreationAttributes&gt; {
          const table = this.init&lt;Player&gt;(
              {
                  id: {
                      type: DataTypes.INTEGER,
                      allowNull: false,
                      autoIncrementIdentity: true,
                      primaryKey: true,
                  },
                  playerName: {
                      type: DataTypes.STRING(255),
                      allowNull: false,
                      defaultValue: &quot;&quot;,
                  },
              },
              {
                  sequelize: dbConnection,
                  tableName: &quot;player&quot;,
              }
          );
  
          return table;
      }
      public static defineRelations(): void {
          Player.belongsTo&lt;Player, Team&gt;(Team, {
              foreignKey: &quot;teamId&quot;,
              targetKey: &quot;id&quot;,
              as: &quot;playerTeam&quot;,
          });
      }
  }
  
  const main = async (): Promise&lt;void&gt; =&gt; {
      const dbConnection: Sequelize = createDbConnection();
  
      if (dbConnection.authenticate()) {
          Player.defineModel(dbConnection);
          Team.defineModel(dbConnection);
          // relations to be defined after all Model definition
          Player.defineRelations();
          Team.defineRelations();
  
          await dbConnection.sync();
  
          const teams: Array&lt;Team&gt; = await Team.findAll&lt;Team&gt;({
              include: [{ association: &quot;teamPlayers&quot; }],
          });
          const players: Array&lt;Player&gt; = await Player.findAll&lt;Player&gt;({
              include: [{ association: &quot;playerTeam&quot; }],
          });
  
          console.log(teams);
          console.log(players);
  
          console.log(teams.map((team) =&gt; team.get()));
          console.log(players.map((player) =&gt; player.get()));
  
          const tt = JSON.parse(JSON.stringify(teams, null, 2)) as Team[];
          const pp = JSON.parse(JSON.stringify(players, null, 2)) as Player[];
  
          console.log(tt, pp);
  
          tt.map((t) =&gt; t.teamPlayers.map((p) =&gt; console.log(p.playerName)));
      } else {
          console.log(&quot;not connected to database&quot;);
      }
  };
  
  main();
  </code></pre>
  <p>the issue is that the only whay to get data as a real instance if the table is doing this :</p>
  <pre><code>    const tt = JSON.parse(JSON.stringify(teams, null, 2)) as Team[];
      const pp = JSON.parse(JSON.stringify(players, null, 2)) as Player[];
  </code></pre>
  <p>the others console.log are not returning the records as a plain object.
  I'be tried to add the options <code>{raw:true}</code> to the <code>findAll</code> call but this is even worst. When getting the Team, instead of receiving a record per team with an array of players(so 2 records), the result is flatterned. There is one reacord for each couple Team/Player, so 2*3 = 6 records.</p>
  <p>What is the solution ? Because applying the <code>JSON.STRINGIFY</code> and the <code>JSON.PARSE</code> is a crappy way to go.</p>
  <p>Regards</p>
  <p>[Edit]
  <strong>here the result of <code>findAll&lt;Team&gt;()</code>, this is not an array of Team</strong>
  <a href="https://i.stack.imgur.com/Q47J7.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Q47J7.png" alt="enter image description here" /></a></p>
  <p><strong>here is the result of <code>findAll&lt;Team&gt;({raw:true})</code>. There is one record per Team/player. Expected result is one record per team with an array of Player</strong></p>
  <p><a href="https://i.stack.imgur.com/0VdZG.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0VdZG.png" alt="enter image description here" /></a></p>
  <p><strong>here is the expected result of <code>findAll&lt;Team&gt;()</code> and <code>findAll&lt;Player&gt;()</code>. There is one record per Team with the array of Player, and each Player has his team information. This result is getting by applying <code>JSON.parse(JSON.stringify(teams, null, 2)) as Team[]</code> and <code>JSON.parse(JSON.stringify(players, null, 2)) as Player[]</code></strong></p>
  <p><a href="https://i.stack.imgur.com/5RSd6.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5RSd6.png" alt="enter image description here" /></a></p>