【发布时间】:2022-08-23 16:04:44
【问题描述】:
async updateOne(
customerId: string,
name: string,
legalStatus: LegalStatus,
legalRegistrationDate: Date,
address: string,
city: City,
businessPhone: string,
businessEmail: string,
businessWebsite: string,
businessType: BusinessType,
activityStartingDate: Date,
fullTimeEmployees: number,
partTimeEmployees: number,
yearlyTurnover: number,
otherInfo: string
) {
const customer = await this.customersRepository.findOne(customerId);
if (!customer) {
throw new HttpException(\'Failed to find the Customer with given id\', HttpStatus.NOT_FOUND);
}
if (name) {
const { id } = customer;
const { name } = customer;
const customers = await this.customersRepository.find({
where: {
id: Not(id),
name,
},
});
if (customers.length > 0) {
throw new HttpException(
\'The Customer with the given name already exists\',
HttpStatus.BAD_REQUEST
);
}
}
const payload = {
name,
legalStatus,
legalRegistrationDate,
address,
city,
businessPhone,
businessEmail,
businessWebsite,
businessType,
activityStartingDate,
fullTimeEmployees,
partTimeEmployees,
yearlyTurnover,
otherInfo,
};
console.log(payload);
// const updatedCustomer = Object.assign(customer, payload);
const updatedCustomer = this.customersRepository.update(customer.id, payload);
if (!updatedCustomer) {
throw new HttpException(\'Failed to update the Customer\', HttpStatus.INTERNAL_SERVER_ERROR);
}
const savedCustomer = this.customersRepository.save(customer);
if (!savedCustomer) {
throw new HttpException(\'Failed to save the Customer\', HttpStatus.INTERNAL_SERVER_ERROR);
}
return savedCustomer;
}
我正在使用nest.js、typescript、typeorm、postgress sql。 我想更新实体的特定字段,并且我希望我没有输入的字段不更新。有什么方法可以用来部分而不是全部更新实体。我知道一个方法 Partial< EntityName > 但它不适用于对象作为字段。如果有人能找到它,我想要解决这个问题。
标签: typescript postgresql nestjs backend crud