What I want to have is a relation between a user profile and the languages this user speaks with each language having a specific proficiency, I've managed to do so via the following schema
export class Profile extends Model {
public languages?: Language[];
declare setLanguages: BelongsToManySetAssociationsMixin<Language, number>;
declare addLanguage: BelongsToManyAddAssociationMixin<Language, number>;
declare addLanguages: BelongsToManyAddAssociationsMixin<Language, number>;
declare getLanguges: BelongsToManyGetAssociationsMixin<Language>;
static associate(models: any) {
Profile.belongsToMany(models.Language, {
through: models.ProfileLanguage,
as: "languages"
})
}
}
class Language extends Model {
public id!: number;
public label!: string;
public properties?: ProfileLanguage;
static associate(models: any) {
Language.belongsToMany(models.Profile, { through: models.ProfileLanguage })
}
}
class ProfileLanguage extends Model {
public proficiency!: number;
}
now to add multiple languages at once to the profile one can use the setLanguages mixin however I cannot find anywhere in the documentation how I can add multiple proficiencies since the through property doesn't accept and array in typescript. I achieved what I want using the following
for(let lang of info.known_languages) {
await profile.addLanguage(lang.language, {through: {proficiency: lang.proficiency}})
}
however this results in multiple queries which is not optimal, can anyone tell me how to achieve this in one query? thanks in advance.