I am working with two models:
@Entity('user')
class UserModel {
@PrimaryColumn({
name: 'id',
type: 'varchar',
})
id: Ulid = generate()
...
@OneToMany(() => CourseModel, (course) => course.author)
courses?: CourseModel[]
}
@Entity('course')
class CourseModel {
@PrimaryColumn({
name: 'id',
type: 'varchar',
})
id: Ulid = generate()
...
@ManyToOne(() => UserModel, (user) => user.courses)
user?: UserModel
}
My goal is to query users and receive the following structure:
{
id: '...',
...
courseIds: ['...', '...', etc...]
}
Although in TypeORM, I could not find a direct option to load courseIds
, only courses
. Is there a way to achieve this without using the query builder?
I attempted to create a getter function in the User class but it required loading all users. Additionally, the queryBuilder approach does work, however, it may not be the most efficient solution. There must be an alternative method to accomplish this without relying on the query builder.