繁体   English   中英

在 NestJs 中使用 Typeorm 从数据库中删除一个项目

[英]Delete an item from Database using Typeorm in NestJs

我有Users实体:
id|姓名|年龄|图片
1 | 约翰 | 4 | {123.jpg,258.png}
我想从images列中删除图像。 根据typeorm文档,我可以使用:

 import {getConnection} from "typeorm"; await getConnection().createQueryBuilder().delete().from(Users).where("id =:id", { id: 1 }).execute();

如果使用它,我将删除整个实体,但我只需要从images列中删除123.jpg

问题:如何从images列中删除123.jpg图像?
注意:我使用 NestJs 和 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();

您需要更新 object 并将图像设置为新的所需值,在这种情况下,它应该包含所有没有 123.jpg 的先前图像

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM