在 NestJs 中使用 Typeorm 从数据库中删除项目 [英] Delete an item from Database using Typeorm in NestJs
问题描述
我有 Users
实体:
id|姓名 |年龄|图像
1 |约翰 |4 |{123.jpg,258.png}
我想从 images
列中删除一个图像.根据 typeorm 文档,我可以使用:
I have the Users
entity:
id|name |age|images
1 | John | 4 | {123.jpg,258.png}
I want to delete an image from images
column. According to typeorm documentation i can use:
import {getConnection} from "typeorm";
await getConnection()
.createQueryBuilder()
.delete()
.from(Users)
.where("id = :id", { id: 1 })
.execute();
如果使用它,我将删除整个实体,但我只需要从 images
列中删除 123.jpg
.
If use this i will delete the whole entity, but i need just to delete 123.jpg
from images
column.
问题:如何从 images
列中删除 123.jpg
图像?
注意:我使用 NestJs 和 ProgresQl
Question: How to delete 123.jpg
image from images
column?
Note: I use NestJs and ProgresQl
用户实体:
@Entity()
export class Users {
@PrimaryGeneratedColumn()
id: number;
...
@Column("text",{nullable: true, array: true})
images: string[];
}
推荐答案
await getConnection()
.createQueryBuilder()
.update()
.set({ images: [...valuesWithoutThatImage] })
.where("id = :id", { id: 1 })
.execute();
您需要更新该对象并将图像设置为新的所需值,在这种情况下,它应该包含所有以前的图像而没有 123.jpg
You need to update that object and set images to new desired value, in this case it should have all previous images without 123.jpg
这篇关于在 NestJs 中使用 Typeorm 从数据库中删除项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!