I have PublicationsEntity with a OneToMany relation to LikesEntity, i'm trying to sort my publications by likes, and return all of the likes with the user associated in likes, and all the comments to display them front-side
@Entity('publications')
export class PublicationsEntity {
@PrimaryGeneratedColumn()
id: number;
@Column({ type: 'longtext' })
publication: string;
@ManyToOne(type => UsersEntity, users => users.publications, {
onDelete: "CASCADE" })
user: UsersEntity;
@OneToMany(type => CommentsEntity, comments =>
comments.publication, { onDelete: "CASCADE" })
comments: CommentsEntity[];
@OneToMany(type => LikesEntity, likes => likes.publication, { onDelete: "CASCADE" })
likes: LikesEntity[];
}
there is my likes entity who contains user and i'm trying to return each user on each like after with the sorted publications :
@Entity('likes')
export class LikesEntity {
@PrimaryGeneratedColumn()
id: number;
@Column()
like: number;
@ManyToOne(type => UsersEntity, user => user.likes, { eager: true })
user: UsersEntity;
@ManyToOne(type => PublicationsEntity, publication => publication.likes)
publication: PublicationsEntity;
}
my users entity :
@Entity('users')
export class UsersEntity {
@PrimaryGeneratedColumn()
id: number;
@Column({ nullable: true })
userName: string;
@OneToMany(type => PublicationsEntity, publications =>
publications.user, { onDelete: "CASCADE" })
publications: PublicationsEntity[];
@Column()
email: string;
@Column({ select: false })
password: string;
@OneToMany(type => CommentsEntity, comment => comment.user, { onDelete: "CASCADE" })
comments: CommentsEntity[];
@OneToMany(type => LikesEntity, like => like.user, { onDelete: "CASCADE" })
likes: LikesEntity[];
@OneToMany(type => PublicationsEntity, publications => publications.user, { onDelete: "CASCADE" })
publications: PublicationsEntity[];
@CreateDateColumn({ type: "timestamp", default: () => "CURRENT_TIMESTAMP(6)" })
createdAt: Date;
@UpdateDateColumn({ type: "timestamp", default: () => "CURRENT_TIMESTAMP(6)", onUpdate: "CURRENT_TIMESTAMP(6)" })
updatedAt: Date;
}
and there is my publicationsService
async getPublications() {
return await getConnection()
.createQueryBuilder(PublicationsEntity, 'publications')
.leftJoinAndSelect('publications.likes', 'likes')
.leftJoinAndSelect('publications.user', 'user')
.leftJoinAndSelect('publications.comments', 'comments')
.leftJoinAndSelect('comments.children', 'children')
//.orderBy()
.getMany()
}
some data on my likes table : https://i.stack.imgur.com/wJ0l5.png