问题:
装饰@PrimaryGeneratedColumn()
上的实体ID在put
请求中可被覆盖。
预期的行为:
用@PrimaryGeneratedColumn()
装饰的ID不会更新。
复制步骤:
curl --location --request POST 'https://q6zre.sse.codesandbox.io/tests' \
--header 'Content-Type: application/json' \
--data-raw '{"name": "acme"}'
id
,将其添加到放置请求的url中,然后在正文中添加随机的wishedId
:curl --location --request PUT 'https://q6zre.sse.codesandbox.io/tests/{id}' \
--header 'Content-Type: application/json' \
--data-raw '{"id": "{wishedId}"}'
该条目已用wishedId
更新。
工作示例:https://codesandbox.io/s/github/desirelabs/nest-typeorm
有什么办法可以防止这种情况? 谢谢
答案 0 :(得分:0)
我会说这不是TypeORM问题,而是体系结构问题。您应该考虑向架构中添加Anti-corruption Layer或DDD: Anti-corruption Layer等。该模式来自“域驱动设计”,其基本思想是REST层不应直接受到数据库层的影响,并且两者之间应进行映射以帮助满足业务需求。您正在使用TypeORM进行该映射,我建议您应该编写自己的映射以保护外界的数据完整性。