0%

数据库相关且必要的知识补充

参考

typeorm 中的 N+1 问题 (有吗?似乎没有)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import { Entity, PrimaryGeneratedColumn, Column, ManyToOne, OneToMany } from 'typeorm'

@Entity()
export class User {
@PrimaryGeneratedColumn()
id: number
@Column()
name: string
@OneToMany(type => Post, post => post.user)
posts: Post[]
}

@Entity()
export class Post {
@Column({ length: 255 })
title: string
@Column()
content: string
@ManyToOne(type => User, user => user.posts)
user: User;
}
1
2
3
4
5
6
7
8
import { getRepository } from "typeorm"
import { Post } from "./entity/Post"

const postRepository = getRepository(Post)
const posts = postRepository.find({
select: ['title'],
relations: ['user', 'user.name']
})