Answer the question
In order to leave comments, you need to log in
typeorm. How to make this request?
There is a project on nest.js and a database on postgreSQL.
The entire code can be viewed here: https://github.com/Fblfbl/backend-movies/
There are 3 entities - users, movies and movie statuses for the user (status below).
Users cannot create films, they are just a database of films that users can add to favorites, to the list of views for the future and to history.
Entities have this form:
@Entity()
export class User {
@PrimaryGeneratedColumn()
id: number;
@Column()
name: string;
@Column()
email: string;
@Column()
password: string;
@Column()
login: string;
@OneToMany((type) => UserDetail, (userDetail) => userDetail.user)
userDetails: UserDetail[];
}
@Entity()
export class Movie {
@PrimaryGeneratedColumn()
id: number;
@Column()
title: string;
@Column()
genre: string;
@OneToOne((type) => UserDetail, (userDetail) => userDetail.movie)
userDetails: UserDetail;
}
@Entity()
export class UserDetail {
@PrimaryGeneratedColumn()
id: number;
@Column({ default: false })
isFavorite: boolean;
@Column({ default: false })
isWatched: boolean;
@Column({ default: false })
isInWatchlist: boolean;
@Column({ default: null })
watchingDate: Date | null;
@ManyToOne((type) => User, (user) => user.userDetails)
user: User;
@Column()
userId: number;
@ManyToOne((type) => Movie, (movie) => movie.userDetails)
movie: Movie;
@Column()
movieId: number;
@BeforeUpdate()
updateDate() {
if (this.isWatched && !this.watchingDate) {
this.watchingDate = new Date();
} else if (!this.isWatched) {
this.watchingDate = null;
}
}
@BeforeInsert()
createDate() {
this.updateDate();
}
}
[
{
"id": 1,
"title": "test1",
"genre": "test1",
"userDetails": {
"id": 1,
"isFavorite": true,
"isWatched": false,
"isInWatchlist": false,
"watchingDate": null,
"userId": 1,
"movieId": 1
}
},
{
"id": 2,
"title": "test2",
"genre": "test2",
},
{
"id": 3,
"title": "test3",
"genre": "test3",
}
]
[
{
"id": 1,
"title": "test1",
"genre": "test1",
},
{
"id": 2,
"title": "test2",
"genre": "test2",
},
{
"id": 3,
"title": "test3",
"genre": "test3",
}
]
Answer the question
In order to leave comments, you need to log in
What's the problem with doing it via leftJoin ?
this.lfMovieER.createQueryBuilder('m')
.setParameter('userID', 82)
.leftJoin(
LfUserDetailEntity,
'uD',
'm.id = uD.movie_id AND uD.user_id = :userID'
)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question