Associated query of multiple tables

Defining entity Classes

The User entity class

/ / file directory: SRC/entities/user. The entity. The ts
import { Column, Entity, PrimaryColumn } from "typeorm";

@Entity('user')
export class User {
  @PrimaryColumn(a)id: string;

  @Column(a)nickname: string;

  @Column(a)username: string;

  @Column(a)password: string;

  @Column(a)avator: string;

  @Column(a)email: string;
}
Copy the code

The Article entity class

/ / file directory: article/SRC/entities. Entity. Ts
import { Column, Entity, PrimaryColumn } from "typeorm";

@Entity('article')
export class Article {
  @PrimaryColumn(a)id: string;

  @Column(a)title: string;

  @Column(a)link: string;

  @Column(a)fileId: string;

  @Column('text')
  content: string;

  @Column(a)categoryId: string;

  @Column(a)formatId: number;

  @Column(a)originId: number;

  @Column(a)createBy: string;
}
Copy the code

Attachment entity class

/ / file directory: SRC/entities/attachment entity. Ts
import { Column, Entity, PrimaryColumn } from "typeorm";

@Entity('attachment')
export class Attachment {
  @PrimaryColumn(a)id: string;

  @Column(a)originName: string;

  @Column(a)size: number;

  @Column(a)filePath: string;

  @Column(a)createBy: string;
}
Copy the code

correlation

user.id === article.createBy && user.id === attachment.createBy
Copy the code

UserModule Module file

import { Module } from "@nestjs/common";
import { TypeOrmModule } from "@nestjs/typeorm";
import { UserController } from "./user.controller";
import { User } from ".. /.. /entities/user.entity";
import { UserService } from "./user.service";
import { Article } from "src/entities/article.entity";
import { Attachment } from "src/entities/attachment.entity";

@Module({
  imports: [TypeOrmModule.forFeature([User, Article, Attachment])],
  controllers: [UserController],
  providers: [UserService]
})
export class UserModule {}
Copy the code

Note:

  1. The entity class used must be imported in the imports; otherwise, the NestJS framework will not be able to instantiate instance objects of the corresponding type through New

    @Module({
      imports: [TypeOrmModule.forFeature([User, Article, Attachment])],
      ...
    })
    Copy the code

UserService file

import { Injectable } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { Article } from ".. /.. /entities/article.entity";
import { Attachment } from ".. /.. /entities/attachment.entity";
import { Repository } from "typeorm";
import { User } from ".. /.. /entities/user.entity";

@Injectable(a)export class UserService {
  constructor(@InjectRepository(User) private readonly userRepository: Repository<User>){}
   
  // select * from table 1
  getAttachment(): Promise<any> {
    return this.userRepository.createQueryBuilder()
      .leftJoinAndSelect(Article, 'article'.'user.id = article.createBy')
      .leftJoinAndSelect(Attachment, 'attachment'.'user.id = attachment.createBy')
      .select(` article.id as id, article.title as title, article.content as content, user.id as userId, user.nickname as nickname, user.username as usernmae, user.avator as avator, attachment.id as attachmentId, attachment.originName as fileName, attachment.size as fileSize, attachment.filePath as filePath `) .getRawMany(); }}Copy the code

The test returns the following data

[{"id": "0fcb8310-9c4a-11ea-9427-017d0539b705"."title": "fe'f"."content": "

microtask

"
."userId": "16ffe4f0-98d0-11ea-adcb-cd4aa44d4464"."nickname": "Li Yunlong"."usernmae": "wanghailong"."avator": "http://192.168.1.101:8765/avator/f360e610-9d80-11ea-9008-019523360f5b.jpg"."attachmentId": "03eba231-9bed-11ea-8495-bd633b2536d8"."fileName": "Annex iv Health Commitment.jpg"."fileSize": 139981."filePath": "E:\\Practices\\workspace-koa2\\whl-blog\\admin\\attachment\\article\\03eba230-9bed-11ea-8495-bd633b2536d8.jpg"},... ]Copy the code