前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >TypeORM 多对多查询实现

TypeORM 多对多查询实现

作者头像
狂奔滴小马
发布2021-11-15 15:03:42
3.3K0
发布2021-11-15 15:03:42
举报
文章被收录于专栏:前端专享前端专享

首先定义2个实体 ArticleCategory是多对多的关系,一篇文章可以有多个分类,一个分类可以包含多篇文章

实体定义

代码语言:javascript
复制
import { Entity, Column, PrimaryGeneratedColumn, ManyToMany, JoinTable, } from 'typeorm'
import { IsNotEmpty } from 'class-validator'
import { Category } from './category'

@Entity()
export class Article {
  @PrimaryGeneratedColumn()
  id: number
  
  @Column()
  @IsNotEmpty()
  title: string

  @Column({
    select: false,
    type: 'text',
  })
  content: string

  @ManyToMany((type) => Category, {
    cascade: true,
  })
  @JoinTable()
  categories: Category[]
}
代码语言:javascript
复制
import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm'
import { IsNotEmpty } from 'class-validator'

@Entity()
export class Category {
  @PrimaryGeneratedColumn()
  id: number

  @Column()
  @IsNotEmpty()
  name: string
}

表结构

根据上面2个实体会自动实现3张表,表结构如下:

表article

代码语言:javascript
复制
+-------------+--------------+------+-----+------------------+----------------+
| Field       | Type         | Null | Key | Default          | Extra          |
+-------------+--------------+------+-----+------------------+----------------+
| id          | int          | NO   | PRI | NULL             | auto_increment |
| title       | varchar(255) | NO   |     | NULL             |                |
| content     | text         | NO   |     | NULL             |                |
+-------------+--------------+------+-----+------------------+----------------+

表category

代码语言:javascript
复制
+-----------+--------------+------+-----+------------------+----------------+
| Field     | Type         | Null | Key | Default          | Extra          |
+-----------+--------------+------+-----+------------------+----------------+
| id        | int          | NO   | PRI | NULL             | auto_increment |
| name      | varchar(255) | NO   |     | NULL             |                |
+-----------+--------------+------+-----+------------------+----------------+

表article_categories_category

代码语言:javascript
复制
+------------+------+------+-----+---------+-------+
| Field      | Type | Null | Key | Default | Extra |
+------------+------+------+-----+---------+-------+
| articleId  | int  | NO   | PRI | NULL    |       |
| categoryId | int  | NO   | PRI | NULL    |       |
+------------+------+------+-----+---------+-------+

查询分页

如果查询的字段都在 Article 表内,可以使用Repository API 查询

代码语言:javascript
复制
import { getManager,  Repository, FindManyOptions, FindConditions, Like } from 'typeorm'
...
...
const { pageSize = 20, pageNum = 1, title, tag } = ctx.request.query
const articleRepository: Repository<Article> = getManager().getRepository(
    Article
)
    
const where: FindConditions<Article> | FindConditions<Article>[] = {}
if (title) {
    where.title = Like(`%${title}%`)
}

const options: FindManyOptions<Article> = {
    where,
    relations: ['categories'],
    skip: (pageNum - 1) * pageSize,
    take: pageSize,
}

const [data, total] = await articleRepository.findAndCount(options)

但是如果查询的字段需要连表查询,leftJoin、 innerJoin, 就需要用到typeORM 提供的QueryBuilder

代码语言:javascript
复制
import { getManager,  Repository, FindManyOptions, FindConditions, Like } from 'typeorm'
...

...
const { pageSize = 20, pageNum = 1, title, tag } = ctx.request.query
const articleRepository: Repository<Article> = getManager().getRepository(Article)
const sql = articleRepository
    .createQueryBuilder('article')
    .innerJoinAndSelect('article.categories', 'category')
if (title) {
    sql.where('article.title like :title ', { title: `%${title}%` })
}
if (tag) {
    sql.andWhere('category.name like :tag ', { tag: `%${tag}%` })
}
sql
    .skip((pageNum - 1) * pageSize)
    .take(pageSize)

const [data, total] = await sql.getManyAndCount()

ctx.status = 200
ctx.body = { data, total }
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 实体定义
  • 表结构
  • 查询分页
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档