0

我还想通过用户名从我的 DynamoDB 用户表(其中 account_sid 是 hash_key)中检索用户。所以我创建了一个索引,如:

class ByUsernameIndex(GlobalSecondaryIndex):
    """Index to query by username"""
    class Meta:
        index_name = "username-index"
        read_capacity_units = 1
        write_capacity_units = 1
        projection = AllProjection()
    # This attribute is the hash key for the index
    username = UnicodeAttribute(hash_key=True)


class User(OrmBaseModel, UserProtocol):
    class Config:
        orm_mode = True

    class Orm(BaseOrm):
        class Meta:
            table_name = "langoo.users"

        sid = UnicodeAttribute(hash_key=True)

        by_username_index = ByUsernameIndex()
        username = UnicodeAttribute(null=False)

        email = UnicodeAttribute(null=False)
        hashed_password = UnicodeAttribute(null=False)
        disabled = BooleanAttribute(default=False)
        date_created = UTCDateTimeAttribute()
        date_updated = UTCDateTimeAttribute()

并查询它我正在使用:


def get_user_by_username(username: str) -> Optional[User]:
    """Get User by username if exists."""
    all = User.Orm.by_username_index.scan()     <-- These are returning empty ResultIterators
    query = User.Orm.by_username_index.query(
        hash_key=username,
        limit=1,
    )
    if query.total_count == 1:
        user_orm = query.next()
        user = User.from_orm(user_orm)
        return user
    else:
        return None

然而,即使扫描操作检索 0 个元素,而我可以使用 cli 检索它们:

 aws --endpoint-url=http://localhost:4567 dynamodb scan \
    --table-name langoo.users \
    --index-name username-index
{
    "Items": [
        {
            "hashed_password": {
                "S": "$2b$12$D1EzLWSXTRfUQXwh8Fn0..LDE.sf6BPtxz024lt1CtgPXjjTN0Dn6"
            },
            "date_updated": {
                "S": "2021-11-06T17:12:36.929351+0000"
            },
            "date_created": {
                "S": "2021-11-06T17:12:36.929334+0000"
            },
            "disabled": {
                "BOOL": false
            },
            "email": {
                "S": "user@example.com"
            },
            "sid": {
                "S": "AC171fc4e44f8240a4923c266a6e359204"
            },
            "username": {
                "S": "MyUsername"
            }
        },
        {
            "hashed_password": {
                "S": "$2b$12$4hjGBu1/34mwznD9zZsxS.qqX/Lz5EDI0rrKI2ImnYw5azCzOgBcq"
            },
            "date_updated": {
                "S": "2021-11-06T17:58:30.846647+0000"
            },
            "date_created": {
                "S": "2021-11-06T17:58:30.846625+0000"
            },
            "disabled": {
                "BOOL": false
            },
            "email": {
                "S": "user@example.com"
            },
            "sid": {
                "S": "AC8e08d2df4b354cebbd0b66913c7080ef"
            },
            "username": {
                "S": "MyUsername2"
            }
        }
    ],
    "Count": 2,
    "ScannedCount": 2,
    "ConsumedCapacity": null
}

注意:我使用 localstack 在端口 4567 上本地模拟 DynamoDB 服务。

4

0 回答 0