删除没有主要索引DynamoDB的数据

发布于 2025-02-09 13:05:05 字数 599 浏览 1 评论 0原文

目前,我正在使用DynamoDB来存储AWS WebSockets的连接ID,并且我将它们用作文档的主要索引,总的来说,我的模式看起来像这样的

{
      ID: connectionId,
      userId: userId,
      domainName,
      stage,
}

架构都可以使用,只是一个问题,我有一个SNS主题可以调度用户ID对此API端点,我需要删除与该用户ID的所有连接,我正在研究batchWrite,但是它要求我将用户ID用作主索引而不是ConnectionID,我选择了该模式类型,因为它是灵活的,我可以轻松找到它用户断开连接并使用一个命令删除连接ID的文档,并且还添加它,我是否可以选择无主键批量批量表?第二种选择是改变架构,因为

{  
      ID: userId,
      connections: [
          {
              connectionId: connectionId,
              stage,
              domainName
          }
      ],
}

我不太喜欢的,这是唯一的其他选择吗?

currently i am using dynamodb for storing connection id's of aws websockets, and i am using them as primary index for my documents, overall my schema looks like this

{
      ID: connectionId,
      userId: userId,
      domainName,
      stage,
}

everything is okay with this schema, just one problem, i have an sns topic that dispatches user id to this api endpoint, and i need to delete every connection with that userId, i was looking into batchWrite but it requires me to use userId as primary index rather than connectionId, i chose this schema type because it is flexible, i can easily find documents with connection id when user disconnects and delete with one command, and add it as well, is there option for me to batchwrite without primary key? second option is to transform schema as this

{  
      ID: userId,
      connections: [
          {
              connectionId: connectionId,
              stage,
              domainName
          }
      ],
}

which i am not so keen of, is this the only other option?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

站稳脚跟 2025-02-16 13:05:09

您需要按以下方式更改DB模式:

对于主要索引,

connectionId: partition key

创建全局辅助索引:

userId: partition key

第一个方案:

当您需要删除属于用户ID的所有连接时,您需要使用UserID查询,然后运行batchwrite命令使用GSI删除所有行

查询:

const items = ddb.query({
    TableName: "connections",
    IndexName: "globalSecondaryIndexNameHere",
    KeyConditionExpression: "userId = :userId",
    ExpressionAttributeValues: {
        ":userId": "abc"
    }
})

然后循环投掷项目并提出批处理请求以删除:

ddb.batchWrite({
    RequestItems: {
        "connections": [
            {
                DeleteRequest: {
                    Key: {
                        "connectionId": "connectionId1"
                    }
                }
            },
            {
                DeleteRequest: {
                    Key: {
                        "connectionId": "connectionId2"
                    }
                }
            },
            // ...
        ]
    }
})

第二个方案:

当您需要通过ConnectionID delete

delete delete:

ddb.deleteItem({
    TableName: "connections",
    Key: {
        "connectionId": "connectionId1"
    }
})

Note:我建议使用 “ https://aws.amazon.com/appsync/” rel =“ nofollow noreferrer”> aws appsync 而不是API网关,因为AppSync管理您的ConnectionID,而不是在Dynampyodb中保存它们,加上许多其他原因在这里

You need to change the DB schema by the following:

For the primary index

connectionId: partition key

Create global secondary index:

userId: partition key

First scenario:

When you need to delete all connections belonging to userId you need to query using userId and then run batchWrite command to delete all rows

Query using GSI:

const items = ddb.query({
    TableName: "connections",
    IndexName: "globalSecondaryIndexNameHere",
    KeyConditionExpression: "userId = :userId",
    ExpressionAttributeValues: {
        ":userId": "abc"
    }
})

Then loop throw items and make batchWrite request to delete:

ddb.batchWrite({
    RequestItems: {
        "connections": [
            {
                DeleteRequest: {
                    Key: {
                        "connectionId": "connectionId1"
                    }
                }
            },
            {
                DeleteRequest: {
                    Key: {
                        "connectionId": "connectionId2"
                    }
                }
            },
            // ...
        ]
    }
})

Second scenario:

When you need to delete one row by connectionId

Delete:

ddb.deleteItem({
    TableName: "connections",
    Key: {
        "connectionId": "connectionId1"
    }
})

NOTE: I recommend using AWS AppSync instead of API Gateway, since appsync manages your connectionIds instead of saving them in DynamoDB plus many other reasons stated HERE

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文