I'm setting up many to many relations in my loopback 4 application. Currently I am using this answer as a guide but after the repository and controller creation I don't know how to continue.
Currently I have three tables for the relation: Course
, Language
, and LanguageCourse
. This means a Course
can have many languages and a Language
can belong to many courses.
My language-course.model.ts
looks like this:
import {Course} from './course.model';
import {Language} from './language.model';
@model({settings: {}})
export class LanguageCourse extends Entity {
@property({
type: 'number',
id: true,
})
id?: number;
@belongsTo(() => Course)
courseId?: number;
@belongsTo(() => Language)
languageId?: number;
constructor(data?: Partial<LanguageCourse>) {
super(data);
}
}
export interface LanguageCourseRelations {
// describe navigational properties here
}
export type LanguageCourseWithRelations = LanguageCourse &
LanguageCourseRelations;
My course.model.ts looks like this (I have already set up a one to many relation in this model):
import {User, UserWithRelations} from './user.model';
@model({settings: {}})
export class Course extends Entity {
@property({
type: 'number',
id: true,
})
id?: number;
@property({
type: 'string',
})
name?: string;
@property({
type: 'string',
})
description?: string;
@belongsTo(() => User)
userId?: number;
@property({
type: 'string',
default: 'active',
})
state?: string;
@property({
type: 'string',
default: 'rookie',
})
level?: string;
@property({
type: 'string',
})
course_photo?: string;
constructor(data?: Partial<Course>) {
super(data);
}
}
export interface CourseRelations {
user?: UserWithRelations;
}
export type CourseWithRelations = Course & CourseRelations;
And my language.model.ts looks like this:
@model({settings: {}})
export class Language extends Entity {
@property({
type: 'number',
id: true,
})
id?: number;
@property({
type: 'string',
})
name?: string;
constructor(data?: Partial<Language>) {
super(data);
}
}
export interface LanguageRelations {
// describe navigational properties here
}
export type LanguageWithRelations = Language & LanguageRelations;
I would like to do a GET
request to, for example /courses/{id}
endpoint (and /courses
as well) and have in the response all the languages that course has but I don't know how to make it work. Also I would like this to work in /languages
endpoint.
Thanks for your time!