nest mysql多对多练习

Tags
import {
    Entity,
    PrimaryGeneratedColumn,
    Column,
    ManyToMany,
    JoinTable
} from "typeorm"
import { Posts } from "./Posts"

@Entity({ name: "tags" })
export class Tags {
    @PrimaryGeneratedColumn({
        type: "int",
        name: "id",
        comment: "主键id"
    })
    id: number

    @Column({
        type: "varchar",
        name: "name",
        nullable: false,
        unique: true,
        comment: "标签名称"
    })
    name: string

    @ManyToMany(type => Posts, posts => posts.tags)
    @JoinTable({ name: "tags_posts" })
    posts: Posts[]
}
Posts
import {
    Entity,
    PrimaryGeneratedColumn,
    Column,
    CreateDateColumn,
    UpdateDateColumn,
    ManyToOne,
    ManyToMany
} from "typeorm"
import { Tags } from "./Tags"
import { User } from "./User"

@Entity({ name: "posts" })
export class Posts {
    @PrimaryGeneratedColumn({
        type: "int",
        name: "id",
        comment: "主键id"
    })
    id: number

    @Column({
        type: "varchar",
        nullable: false,
        length: 50,
        unique: true,
        name: "title",
        comment: "标题"
    })
    title: string

    @Column({
        type: "text",
        nullable: false,
        name: "content",
        comment: "内容"
    })
    content: string

    @Column("tinyint", {
        nullable: false,
        default: () => 0,
        name: "is_del",
        comment: "是否删除, 1表示删除, 0表示正常"
    })
    isDel: number

    @CreateDateColumn({
        type: "timestamp",
        nullable: false,
        name: "created_at",
        comment: "创建时间"
    })
    createdAt: Date

    @UpdateDateColumn({
        type: "timestamp",
        nullable: false,
        name: "update_at",
        comment: "更新时间"
    })
    updateAt: Date

    @ManyToOne(type => User, user => user.posts)
    user: User

    @ManyToMany(type => Tags, tag => tag.posts)
    tags: Tags[]
}
数据操作
import "reflect-metadata";
import { createConnection } from "typeorm";
// 引入刚刚定义的实体类
import { User } from "./entity/User";
import { UserExtend } from "./entity/UserExtend"
import { Posts } from "./entity/Posts"
import { Tags } from "./entity/Tags"

createConnection()
    .then(async (connection) => {
        const tag1 = new Tags()
        tag1.name = "mysql"

        const tag2 = new Tags()
        tag2.name = "node"

        const posts1 = new Posts()
        posts1.title = "文章一"
        posts1.content = "文章一内容"
        posts1.tags = [tag1, tag2]

        const posts2 = new Posts()
        posts2.title = "文章二"
        posts2.content = "文章二内容"
        posts2.tags = [tag1, tag2]

        const user = new User()
        user.username = "张龙"
        user.password = "654321"
        user.posts = [posts1, posts2]

        const userRepository = connection.getRepository(User)
        const postsRepository = connection.getRepository(Posts)
        const tagRepository = connection.getRepository(Tags)

        await tagRepository.save(tag1)
        await tagRepository.save(tag2)

        await postsRepository.save(posts1)
        await postsRepository.save(posts2)

        await userRepository.save(user)

        console.log("添加数据成功")

        // 查询帖子  拥有的tag及其用户信息
        const result = await postsRepository.find({ relations: ["tags", "user"]})
        console.log(result);

    })
    .catch((error) => console.log(error));
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值