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 ?