在我的 AWS Amplify 项目中,我使用带有多个
@model
指令的 GraphQL API。因此,Amplify 在我的 AWS 后端生成了多个 DynamoDB 表。现在,当删除这样的 @model
或重命名它时,旧的 DynamoDB 表将被永久删除,其中包含的所有数据!
如何避免这种情况,避免生产数据出错?
要防止删除 DynamoDB 表,您可以将
DeletionPolicy
设置为 Retain
。不幸的是,Amplify 默认情况下不会这样做。
因此,您可以像这样使用 自定义 GraphQL 指令
@retain
:
npm install --save graphql-retain-transformer
。amplify/backend/api/<YOUR_API>/transform.conf.json
并将 "graphql-retain-transformer"
附加到变压器字段:"transformers": [
"graphql-retain-transformer"
]
schema.graphql
文件中,将 @retain
指令附加到您想要为其激活保留删除策略的所有 @model
类型:type Todo @model @retain {
id: ID!
title: String!
description: String
}
自定义指令的GitHub存储库:https://github.com/flogy/graphql-retain-transformer(如果您喜欢,请留下⭐️🙂)
有关它的更详细的博客文章:https://react-freelancer.ch/blog/amplify-retain-dynamodb-tables
这个库仍然可行吗,因为当我按照 github read me 通过 npm 安装它时,它出错了:
npm WARN deprecated [email protected]: The library contains critical security issues and should not be used for production! The maintenance of the project has been discontinued. Consider migrating your code to isolated-vm.