I have this query that I tested in the query tool, and I now wish to apply this into Typeorm syntax
SELECT menu_entry.*, "user_menu".is_bookmark FROM menu_entry LEFT JOIN "user_menu" ON user_menu.menu_id = menu_entry.id LEFT JOIN "user" ON "user".id = user_menu.user_id WHERE "user".id = 1
I have tried this approach but it’s not 100% correct:
async findUserBookmarks(request): Promise<any>{ const user: any = await createQueryBuilder(User, 'user') .leftJoinAndSelect('menu_id', 'menu_entry') .leftJoinAndSelect('user.id', 'roles') .where('user.id = :id', {id:1 }) .getMany() return console.log('user: ', user); }
Advertisement
Answer
Assuming your class defined for the entity menu_entry
is MenuEntry
, typeorm query statement would be as follows-
async findUserBookmarks(request): Promise<any>{ return await getRepository(MenuEntry). .createQueryBuilder("menuEntry") .leftJoinAndSelect("user_menu","userMenu","userMenu.menu_id = menuEntry.id") .leftJoinAndSelect("user","user","user.id = userMenu.user_id") .where("user.id = 1" ) .getMany() }