AWS Amplify:如何使ID与所有者的值相同?



我想创建一个User模型,并且我希望User ID始终等于Owner ID。
如何做到这一点?

这是我的schema。文件内容:

type User @model {
id: ID! @default(value: @owner) # == owner
name: String!
description: String
birthday: AWSDateTime
}

谢谢

对于我的例子,我只是添加了sub from Congnito User:

type User
@model
@auth(rules: [{ allow: private, operations: [read] }, { allow: owner }]) 
{
id: ID!
sub: String! @index(name: "bySub", queryField: "userBySub")
}

sub是通过Auth.currentAuthenticatedUser()从cognito中检索的,当您第一次登录时,一个新的用户行被添加到dynamo表user中。

我遇到了同样的问题,并找到了下面突出显示的解决方案:

type User
@model
@auth(rules: [
{ allow: owner, ownerField: "id" }
{ allow: private, operations: [read] }
])
{
id: String @auth(rules: [
{ allow: owner, operations: [create, read, delete] } 
{ allow: private, operations: [read] }
])
name: String!
description: String
birthday: AWSDateTime
}

我已经在模型级@auth规则上指定使用id字段作为"owner field";并给予其他已认证用户读访问权限。

然后在字段级别上,我已经删除了对所有者的update访问(再次使用@auth规则),因此以后不可能重新分配记录。

最新更新