0

I have a sequelize schema using postgre DB

    export const Commune = sq.define("commune",{
    codeCommune: {
        type: DataTypes.STRING(5),
        allowNull: false,
        primaryKey: true
    },
    libelleCommune: {
        type: DataTypes.STRING,
        allowNull:false
    },
    actif: {
        type: DataTypes.BOOLEAN,
        allowNull:true
    }
    },{timestamps: false,modelName:"Commune"});

export const CodePostal = sq.define("codePostal",{
    codePostal: {
        type: DataTypes.STRING(5),
        allowNull: false,
        primaryKey: true
    }
},{timestamps: false,modelName:"CodePostal"});

export const R_Commune_CodePostal = sq.define("R_Commune_CodePostal",{},{timestamps:false});

CodePostal.belongsToMany(Commune,{through: R_Commune_CodePostal});
Commune.belongsToMany(CodePostal,{through: R_Commune_CodePostal});

And I have successfully created this :

await CodePostal.create({
       codePostal: "37340",
       communes: [
           {
               codeCommune: "37002",
               libelleCommune:"Ambillou",
               actif: true
           },
           {
               codeCommune:"37013",
               libelleCommune: "Avrillé-les-Ponceaux",
               actif: true
           }
       ]
   }, {include: Commune}).then ...

Now I want to list all the data like this :

CodePostal.findAll({raw:true,include:[{model: Commune},nest:true}).then ...

Expected output :

{codePostal: "37340",
communes: [
   {codeCommune: "37002",libelleCommune: "Ambillou",actif: true},
   {codeCommune: "37013",libelleCommune: "Avrillé-les-Ponceaux",actif: true}
]}

But I have this :

[ { codepostal: '37340',
    communes: { 
       codeCommune: '37002',
       libelleCommune: 'Ambillou',
       actif: true,
       R_Commune_CodePostal: [Object] } },
  { codepostal: '37340',
    communes: {
       codeCommune: '37013',
       libelleCommune: 'Avrillé-les-Ponceaux',
       actif: true,
       R_Commune_CodePostal: [Object] }
     }
]

For each commune, sequelize joins the code postal, insted of listing the communes in a array for each codePostal.

Can someone help me to achieve this result ?

Wapax
  • 37
  • 8

1 Answers1

1

raw: true will return the data as DB returns in a plain response. That means it will return each association as 1 row. However, your expected output is compounded into 1 array for the same codepostal.

By default (without raw: true), findAll should return as your expected output and nest: true is not necessary either.

CodePostal.findAll({
    include:[{model: Commune}]
})

Also, if you don't need attributes from the through table, you can add these option to get cleaner response.

CodePostal.findAll({
    include:[{
        model: Commune,
        through: {
            attributes: []
        }
    }]
})
Emma
  • 8,518
  • 1
  • 18
  • 35
  • Thanks a lot ! I just processed the result into a forEach loop to get result as JSON object. – Wapax Nov 17 '21 at 13:18
  • to convert sequelize instance to JSON object for findAll, check out here. `map` is usually better than `forEach` in terms of immutability. https://stackoverflow.com/a/21982117/2956135 – Emma Nov 17 '21 at 15:06