TypeORM - findAll method to find all articles of an user
17,524
You can join if the relationship was defined in model
export class Article {
/// ... other columns
@ManyToOne(type => Author, author => author.articles)
author: Author;
}
There are two way to query: find*
method or QueryBuilder
// find*
createConnection(/*...*/).then(async connection => {
/*...*/
let articleRepository = connection.getRepository(Article);
let articles = await articleRepository.find({ relations: ["author"] });
}).catch(error => console.log(error));
// Query Builder
const articles = await connection
.getRepository(Article)
.createQueryBuilder("article")
.leftJoinAndSelect("article.author", "user")
.getMany();
You can read the documentation for more detail
Author by
pirmax
Updated on June 16, 2022Comments
-
pirmax almost 2 years
I want find all articles of one user from TypeORM package.
On Sequelize, I have this:
async findAllByUser(userUuid: string, findOptions: object): Promise<Article[]> { return await Article.findAll<Article>({ include: [{ model: User, where: { uuid: userUuid } }] }); }
I'd like an alternative for TypeORM.