DEV Community

ShinJustinHolly3317
ShinJustinHolly3317

Posted on • Edited on

Difference between toJSON() & { raw: true }

Both of them are ways to get a plain object from Sequelize DAO.

Main difference is that if we pass a { raw: true } option when using findOne() or findAll() we get a flattened data.

While we use Dao.toJSON() we get a nested object.

Let's see one example from Sequelize doc.

We gonna create 2 many to many tables.
And we use raw: true option to see the result.

const Foo = sequelize.define('Foo', { name: DataTypes.TEXT });
const Bar = sequelize.define('Bar', { name: DataTypes.TEXT });
Foo.belongsToMany(Bar, { through: 'Foo_Bar' });
Bar.belongsToMany(Foo, { through: 'Foo_Bar' });

await sequelize.sync();
const amy = await User.create({ name: 'Amy' });
const faveFood1 = await FavoriteFood.create({ name: 'pizza' });
const faveFood2 = await FavoriteFood.create({ name: 'Dacquoise' });
await amy.addFavoriteFood(faveFood1);
await amy.addFavoriteFood(faveFood2);
const amyFaveFood = await amy.findAll({ 
  include: FavoriteFood,
  raw: true,
});
console.log(amyFaveFood);
Enter fullscreen mode Exit fullscreen mode

Basically it's nearly the direct result from raw SQL.
In query result we definitely get seperete rows.

[
  {
    "id": 1,
    "name": "Amy",
    "FavoriteFood": {
      "id": 1,
      "name": "pizza",
      "User_FavoriteFood": {
        "UserId": 1,
        "FavoriteFoodId": 1
      }
    }
  },
  {
    "id": 1,
    "name": "Amy",
    "FavoriteFood": {
      "id": 2,
      "name": "Dacquoise",
      "User_FavoriteFood": {
        "UserId": 1,
        "FavoriteFoodId": 2
      }
    }
  }
]
Enter fullscreen mode Exit fullscreen mode

However with that result we have more work handling data formatting.
We can simply use toJSON() to aggregate the parts that we want.

Take this as example, I want all FavoriteFood aggregated as an array under one key of the Dao.

const Foo = sequelize.define('Foo', { name: DataTypes.TEXT });
const Bar = sequelize.define('Bar', { name: DataTypes.TEXT });
Foo.belongsToMany(Bar, { through: 'Foo_Bar' });
Bar.belongsToMany(Foo, { through: 'Foo_Bar' });

await sequelize.sync();
const amy = await User.create({ name: 'Amy' });
const faveFood1 = await FavoriteFood.create({ name: 'pizza' });
const faveFood2 = await FavoriteFood.create({ name: 'Dacquoise' });
await amy.addFavoriteFood(faveFood1);
await amy.addFavoriteFood(faveFood2);
const amyFaveFood = await amy.findAll({ 
  include: FavoriteFood,
});
console.log(amyFaveFood.toJSON());
Enter fullscreen mode Exit fullscreen mode
[
  {
    "id": 1,
    "name": "amy",
    "FavoriteFood": [
      {
        "id": 1,
        "name": "pizza",
        "User_FavoriteFood": {
          "UserId": 1,
          "FavoriteFoodId": 1
        }
      },
      {
        "id": 2,
        "name": "Dacquoise",
        "User_FavoriteFood": {
          "UserId": 1,
          "FavoriteFoodId": 2
        }
      },
    ]
  }
]
Enter fullscreen mode Exit fullscreen mode

easy come easy go

Top comments (0)