Class EntityQueryBuilder
java.lang.Object
dev.hogwai.dynamodb.simplified.entity.EntityQueryBuilder
Builds and executes cross-entity queries across multiple entity types
sharing a single DynamoDB table (single-table design).
Supports partition key filtering, sort key conditions, discriminator-based entity filtering, pagination, and projection.
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionEntityQueryBuilder(@NonNull software.amazon.awssdk.services.dynamodb.DynamoDbClient dynamoDbClient, @NonNull String tableName, @NonNull String discriminatorAttribute) Creates a cross-entity query builder for a table and discriminator attribute. -
Method Summary
Modifier and TypeMethodDescription@NonNull EntityQueryBuilderconsistentRead(boolean consistentRead) Enables or disables strongly consistent reads.longcount()Returns the total number of matching items by iterating all query result pages usingSelect.COUNTserver-side to avoid transferring item data.@NonNull CrossEntityResultexecute()Executes the query and returns all matching items grouped by entity type.@NonNull List<CrossEntityResult> Executes the query and returns all results from all pages.@NonNull Optional<CrossEntityResult> Executes the query and returns the first matching item for each entity type, if any.@NonNull CrossEntityResultWithPaginationExecutes the query and returns the first page of results along with pagination metadata.@NonNull EntityQueryBuilderincludeEntity(@NonNull EntitySchema<?> schema) Includes an entity type using a pre-builtEntitySchema.@NonNull EntityQueryBuilderincludeEntity(@NonNull Class<?> entityClass) Includes an entity type in the cross-entity query results.@NonNull EntityQueryBuilderlimit(int limit) Limits the number of items returned per page.@NonNull EntityQueryBuilderpartitionKey(@NonNull Object partitionKey) Sets the partition key value for the query.@NonNull EntityQueryBuilderRestricts the returned attributes to the specified ones.@NonNull EntityQueryBuilderscanIndexForward(boolean forward) Sets the sort order for the query results.@NonNull EntityQueryBuildersortKeyBeginsWith(@NonNull Object value) Adds a sort key condition: sort key begins with the given prefix.@NonNull EntityQueryBuildersortKeyBetween(@NonNull Object from, @NonNull Object to) Adds a sort key condition: sort key is between the given values (inclusive).@NonNull EntityQueryBuildersortKeyEquals(@NonNull Object value) Adds a sort key condition: sort key equals the given value.@NonNull EntityQueryBuildersortKeyGreaterThan(@NonNull Object value) Adds a sort key condition: sort key is strictly greater than the given value.@NonNull EntityQueryBuildersortKeyGreaterThanOrEqual(@NonNull Object value) Adds a sort key condition: sort key is greater than or equal to the given value.@NonNull EntityQueryBuildersortKeyLessThan(@NonNull Object value) Adds a sort key condition: sort key is strictly less than the given value.@NonNull EntityQueryBuildersortKeyLessThanOrEqual(@NonNull Object value) Adds a sort key condition: sort key is less than or equal to the given value.@NonNull EntityQueryBuilderstartFrom(@Nullable Map<String, software.amazon.awssdk.services.dynamodb.model.AttributeValue> lastEvaluatedKey) Sets the exclusive start key for the next paginated query.
-
Constructor Details
-
EntityQueryBuilder
public EntityQueryBuilder(@NonNull software.amazon.awssdk.services.dynamodb.DynamoDbClient dynamoDbClient, @NonNull String tableName, @NonNull String discriminatorAttribute) Creates a cross-entity query builder for a table and discriminator attribute.- Parameters:
dynamoDbClient- the low-level DynamoDB client used to execute queriestableName- the DynamoDB table shared by the entity typesdiscriminatorAttribute- the attribute containing each entity's discriminator
-
-
Method Details
-
partitionKey
Sets the partition key value for the query. Resets any previously configured sort key condition.- Parameters:
partitionKey- the partition key value- Returns:
- this builder for chaining
-
limit
Limits the number of items returned per page.- Parameters:
limit- the maximum number of items per page- Returns:
- this builder for chaining
-
includeEntity
Includes an entity type in the cross-entity query results. Items matching the entity's discriminator value will be deserialized into the given entity class.- Parameters:
entityClass- the entity class to include- Returns:
- this builder for chaining
-
includeEntity
Includes an entity type using a pre-builtEntitySchema.- Parameters:
schema- the entity schema to include- Returns:
- this builder for chaining
-
sortKeyBeginsWith
Adds a sort key condition: sort key begins with the given prefix.- Parameters:
value- the prefix value- Returns:
- this builder for chaining
-
sortKeyEquals
Adds a sort key condition: sort key equals the given value.- Parameters:
value- the value to match- Returns:
- this builder for chaining
-
sortKeyBetween
Adds a sort key condition: sort key is between the given values (inclusive).- Parameters:
from- the lower bound (inclusive)to- the upper bound (inclusive)- Returns:
- this builder for chaining
-
sortKeyGreaterThan
Adds a sort key condition: sort key is strictly greater than the given value.- Parameters:
value- the threshold value- Returns:
- this builder for chaining
-
sortKeyGreaterThanOrEqual
Adds a sort key condition: sort key is greater than or equal to the given value.- Parameters:
value- the threshold value- Returns:
- this builder for chaining
-
sortKeyLessThan
Adds a sort key condition: sort key is strictly less than the given value.- Parameters:
value- the threshold value- Returns:
- this builder for chaining
-
sortKeyLessThanOrEqual
Adds a sort key condition: sort key is less than or equal to the given value.- Parameters:
value- the threshold value- Returns:
- this builder for chaining
-
project
Restricts the returned attributes to the specified ones.- Parameters:
attributes- the attribute names to include in the result- Returns:
- this builder for chaining
-
consistentRead
Enables or disables strongly consistent reads.- Parameters:
consistentRead-truefor a strongly consistent read,false(the default) for an eventually consistent read- Returns:
- this builder for chaining
-
scanIndexForward
Sets the sort order for the query results.- Parameters:
forward-truefor ascending order (the default),falsefor descending order- Returns:
- this builder for chaining
-
startFrom
public @NonNull EntityQueryBuilder startFrom(@Nullable Map<String, software.amazon.awssdk.services.dynamodb.model.AttributeValue> lastEvaluatedKey) Sets the exclusive start key for the next paginated query. The key is typically obtained fromCrossEntityResultWithPagination.getLastEvaluatedKey().- Parameters:
lastEvaluatedKey- the key returned by a previous page, ornullto start from the beginning- Returns:
- this builder for chaining
-
execute
Executes the query and returns all matching items grouped by entity type.- Returns:
- a
CrossEntityResultcontaining items mapped by entity class - Throws:
IllegalStateException- if the partition key has not been set
-
executeWithPagination
Executes the query and returns the first page of results along with pagination metadata.- Returns:
- a
CrossEntityResultWithPaginationcontaining the first page
-
executeAll
Executes the query and returns all results from all pages.- Returns:
- a list of
CrossEntityResult, one per page
-
executeAndGetFirst
Executes the query and returns the first matching item for each entity type, if any.- Returns:
- an
Optionalcontaining the first page, or empty if no items match
-
count
public long count()Returns the total number of matching items by iterating all query result pages usingSelect.COUNTserver-side to avoid transferring item data.- Returns:
- the total count of matching items
-