Jay as a memoji Jay Wilson

How to get count of all Users in DynamoDB using Swift and Soto

This is just a short code snippet so that I can reference it later. Subscribe for more snippets and other thoughts.

Published:
Originally posted here

This was made using Soto v7.0.0-rc1.

Assuming that your Partition Key and Sort Key are named PK and SK in your table. Also that your PK for all users are USER#.

    let input = DynamoDB.QueryInput(
      expressionAttributeNames: ["#p": "PK"],
      expressionAttributeValues: [":pk" : .s("USER#")],
      keyConditionExpression: "#p = :pk",
      select: .count,
      tableName: tableName
    )
    let output = try await dynamoDB.query(input)
    nextAvailableID = output.count ?? 1

This is just a short code snippet so that I can reference it later. Subscribe for more snippets and other thoughts.


Written by Jay Wilson on


Thanks for reading! If you found this post useful consider buying me a coffee, so I can keep making posts like this.

No comments yet. Share on mastodon and see your comment or write a post on your blog if you support Webmentions

These people reposted this post on various platforms around the internet.

These people liked this post on various platforms around the internet.

These people bookmarked this post on various platforms around the internet.

Powered by Webmentions