typeorm 表名_TypeORM使用记录

基础使用

// 新增保存

let param = new CreatUserDto()

param.name = user.name

param.password = user.password

param.crateTime = formatDate()

param.updateTime = formatDate()

return await this.userRepository.save(param)

// 查找所有

const allUsers = await this.userRepository.find()

// 按id查找

const firstUser = await this.userRepository.findOne(id);

// 关系查找(查找和该表关联的值)

const questions = await this.userRepository.find({ relations: ["categories"] });

// 查找并计数

const count = await this.userRepository.findAndCount();

// 按条件查找一个

const timber = await this.userRepository.findOne({ name: "admin", crateTime: "2020-7-1" });

// 删除

await this.userRepository.remove(timber);

使用Query Builder

新增一条评论

// 评论

public async creatComment(params: CreatCommentDto){

try{

let publics: Public

try {

// 关联发布实体

publics = await this.publicRepository

.createQueryBuilder('public')

.where('public.id = :id', {id: params.publicId})

.getOne()

} catch (e) {

console.log(e)

}

try{

let res = await this.commentRepository

.createQueryBuilder('c')

.insert()

.into(Comment)

.values([{

crateTime: formatDate(),

content: params.content,

userId: params.userId, // 未关联的用户

publics // 关联的发布消息

}])

.execute();

return res

} catch(e){

console.log(e)

}

}catch(e){}

}

查询评论列表

// 列表

public async getList(query: QueryCommentDto) {

try{

const queryConditionList = []

if(query.content) { queryConditionList.push('c.content LIKE :content') }

if(query.userId) { queryConditionList.push('c.userId = :userId') }

if(query.isDisable) { queryConditionList.push('c.isDisable = :isDisable') }

const queryCondition = queryConditionList.join(' AND ')

const res = await this.commentRepository

.createQueryBuilder('c')

.leftJoinAndSelect('c.publics', 'publics') // 关联的实体查询 - 发布表

.leftJoinAndMapOne('c.user', User, 'user', 'c.userId=user.id') // 没关联的实体查询 - 用户表

.leftJoinAndSelect('user.photo', 'photo') // 继续查询关联下一级 - 用户头像表

.where(queryCondition, {

isDisable: query.isDisable,

content: `%${query.content}%`,

userId: query.userId

})

.orderBy('c.crateTime', 'DESC')

.take(query.pageSize)

.skip((query.page - 1) * query.pageSize)

.getManyAndCount()

return { data: res[0], count:res[1] }

}catch(e) {

console.log(e)

}

}

更新数据

// 更新用户信息

public async updateUser(params: UpdateUserDto) {

try {

let obj = {}

obj['updateTime'] = formatDate()

if (params.email) obj['email'] = params.email

if (params.age) obj['age'] = params.age

if (params.sex) obj['sex'] = params.sex

if (params.address) obj['address'] = params.address

if (params.intro) obj['intro'] = params.intro

if (params.name) obj['name'] = params.name

await this.userRepository

.createQueryBuilder('cu')

.where('id = :id', { id: params.id })

.update(User) // 更新User表

.set(obj)

.execute()

} catch (e) {

console.log('ff', e)

}

}

删除数据

// 删除评论

public async deleteComment(id: string) {

try{

return await this.commentRepository

.createQueryBuilder('c')

.delete()

.from(Comment)

.whereInIds(id)

.execute()

}catch(e){}

}

没有实体关联的两个表查询 - 单独dome写法,对比上面的查询列表综合例子

const posts = await this.commentRepository

.createQueryBuilder('c')

.leftJoinAndSelect(ClientUser, 'clientUser', 'c.userId=clientUser.id')

.getRawMany()

const posts = await this.commentRepository

.createQueryBuilder('c')

.leftJoinAndMapOne('c.user', ClientUser, 'clientUser', 'c.userId=clientUser.id')

.leftJoinAndSelect('clientUser.photo', 'photo')

.getManyAndCount()

console.log(posts)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值