import {Entity,Column,PrimaryGeneratedColumn,CreateDateColumn, OneToMany, OneToOne, JoinColumn} from "typeorm"
//导入实体副表
import {Works} from "../../works/entities/works.entity"
@Entity()
export class My{
//主键
@PrimaryGeneratedColumn({type:"int",name:"id"})
id:number
@Column({type:"varchar",length:100})
username:string
@Column({type:"varchar",length:150})
phone:string
@Column({type:"varchar",length:150,select:false})
password:string
@CreateDateColumn({type:"timestamp"})
createTime:Date
@Column({type:"varchar",length:150,default:"1675924792718.jpg"})
avator:string
@Column({type:"varchar",length:100})
class:string
@Column({type:"varchar",length:100})
team:string
//创建一对多的关系works.my是副表 my: My字段,也就是下面所示
// @ManyToOne((type)=>My,(my) => my.works,{eager:true,cascade:true})
//@JoinColumn({name:"userId",referencedColumnName:"id"})
//my: My
@OneToMany(()=>Works,works=>works.my)
works:Works[] //联表查询的时候会吧符合条件作品字段存入works中来展示
}
2、创建副本
import { Entity, Column, PrimaryGeneratedColumn, CreateDateColumn, ManyToMany, ManyToOne, JoinColumn, OneToOne, UpdateDateColumn, Generated, RelationId, JoinTable } from "typeorm"
//导入my实体表
import { My } from "../../my/entities/my.entity"
@Entity()
export class Works {
//本表的主键
@PrimaryGeneratedColumn({ type: "int" })
id: number
@Column({ type: "text" })
content: string
@Column({ type: "text" })
image: string
@Column({ type: "text" })
video: string
@Column({ type: "int" })
type: number
@CreateDateColumn({ type: "timestamp" })
createTime: Date
@Column({ type: "varchar", length: 200 })
site: string
@Column({ type: "int" })
worksLimit: number
@Column({ type: "int" })
examine: number
@Column({ type: "varchar", length: 50 })
sort: string
@Column({ type: "varchar", length: 200 })
taskId: string
@Column({ type: "varchar", length: 100 })
teamId: string
//外键id位外键的字段名
@Column({ type: "int",foreignKeyConstraintName:"id"})
userId: number
//创建多对一的关系my.works是主表 works:Works字段
//@OneToMany(()=>Works,works=>works.my)
//works:Works[]
@ManyToOne((type)=>My,(my) => my.works,{eager:true,cascade:true})
//是要创建与外表连接的键的: name的话是本表的外键userId名, id的话是主表的主键名
@JoinColumn({name:"userId",referencedColumnName:"id"})
my: My
}
3、把实体都引进各自的模块中
1、用户的模块中使用
import { Module } from '@nestjs/common';
import { MyService } from './my.service';
import { MyController } from './my.controller';
import {TypeOrmModule} from "@nestjs/typeorm"
import {ConfigModule} from "../config/config.module"
import { JwtModule } from '@nestjs/jwt';
import { Works } from '../works/entities/works.entity';
import {My} from "./entities/my.entity"
@Module({
imports:[TypeOrmModule.forFeature([My,Works]),ConfigModule,JwtModule.register({
secret:"yaohf",
signOptions:{
expiresIn:"1h"
}
})],
providers: [MyService],
controllers: [MyController]
})
export class MyModule {}
2、works模块中使用
import { Module } from '@nestjs/common';
import { WorksController } from './works.controller';
import {WorksService} from "./works.service"
import { TypeOrmModule } from '@nestjs/typeorm';
import {ConfigModule} from "../config/config.module"
import { My } from '../my/entities/my.entity';
import {Works} from "./entities/works.entity"
@Module({
imports:[TypeOrmModule.forFeature([Works,My]),ConfigModule],
controllers: [WorksController],
providers:[WorksService]
})
export class WorksModule {}
4、创建查询逻辑
import { Injectable, Inject } from '@nestjs/common';
import { Repository} from 'typeorm';
import { InjectRepository } from "@nestjs/typeorm"
import { worksInte } from "./interface/index"
import { utils } from '../config/config.utils';
import { My } from '../my/entities/my.entity';
import { Works } from "./entities/works.entity"
@Injectable()
export class WorksService {
constructor(
@InjectRepository(Works) private works: Repository<Works>,
@InjectRepository(My) private my:Repository<My>,
@Inject("utils") private utils:utils
) { }
//获取团队任务中的数据 user.works 就是主表中的定义的works:Works[]字段
//Works是works实体对外暴露的名称,typeOrm底层框架会帮我们处理的,就是把它连两个表做一个连接处理而已
async getTeamData(teamId:string){
let res = await this.my.createQueryBuilder("user")
.innerJoinAndSelect("user.works","Works",).where("Works.teamId=:teamId",{teamId})
.getMany()
if(res.length>0){
res.forEach(item=>{
item.createTime = this.utils.dateConversion( item.createTime) as unknown as Date
})
return {
code:200,
mes:"获取数据成功",
data:res
}
}else{
return {
code:200,
mes:"没有数据哟",
data:[]
}
}
// return {
// }
}
}
原文地址:https://blog.csdn.net/qq_53244180/article/details/129106855
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_19339.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。