Skip to content

Commit

Permalink
Merge pull request #3 from AzureCosmosDB/auto-20241016210225
Browse files Browse the repository at this point in the history
[AUTO] 2024-10-16 | Merge latest from source repo
  • Loading branch information
2 parents 6fa8254 + fad0040 commit 407ec53
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,15 @@
import com.azure.spring.data.cosmos.config.CosmosConfig;
import com.azure.spring.data.cosmos.repository.config.EnableCosmosRepositories;

// <repository_configuration>
@Configuration
@EnableCosmosRepositories
public class CosmosConfiguration extends AbstractCosmosConfiguration {
// </repository_configuration>

@Value("#{environment.AZURE_COSMOS_DB_NOSQL_ENDPOINT}")
private String uri;

@Bean
public CosmosClientBuilder getCosmosClientBuilder() {
public CosmosClientBuilder getCosmosClientBuilder() {
return new CosmosClientBuilder()
.endpoint("<azure-cosmos-db-nosql-endpoint>")
.key("<azure-cosmos-db-nosql-key>");
Expand All @@ -34,10 +32,8 @@ public CosmosConfig cosmosConfig() {
.build();
}

// <get_database>
@Override
protected String getDatabaseName() {
return "cosmicworks";
}
// </get_database>
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

// <get_container>
@Container(containerName = "products", autoCreateContainer = false)
public class Item {
private String id;
Expand All @@ -15,7 +14,6 @@ public class Item {

@PartitionKey
private String category;
// </get_container>

public Item() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@
import com.azure.spring.data.cosmos.repository.CosmosRepository;
import com.azure.spring.data.cosmos.repository.Query;

// <repository_implementation>
@Repository
public interface ItemRepository extends CosmosRepository<Item, String> {
@Query("SELECT * FROM products p WHERE p.category = @category")
List<Item> getItemsByCategory(@Param("category") String category);
}
// </repository_implementation>
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ public void start() throws Exception {
this.sendMessage(String.format("Get container:\t%s", "products"));

{
// <create_item>
Item item = new Item(
"70b63682-b93a-4c77-aad2-65501347265f",
"gear-surf-surfboards",
Expand All @@ -46,7 +45,7 @@ public void start() throws Exception {
false
);
Item created_item = repository.save(item);
// </create_item>

this.sendMessage(String.format("Upserted item:\t%s", created_item));
}

Expand All @@ -62,24 +61,22 @@ public void start() throws Exception {
this.sendMessage(String.format("Upserted item:\t%s", created_item));
}

// <read_item>
PartitionKey partitionKey = new PartitionKey("gear-surf-surfboards");
Optional<Item> existing_item = repository.findById("70b63682-b93a-4c77-aad2-65501347265f", partitionKey);
if (existing_item.isPresent()) {
// Do something
}
// </read_item>

existing_item.ifPresent(i -> {
this.sendMessage(String.format("Read item id:\t%s", i.getId()));
this.sendMessage(String.format("Read item:\t%s", i));
});

// <query_items>
List<Item> items = repository.getItemsByCategory("gear-surf-surfboards");
for (Item item : items) {
// Do something
}
// </query_items>

this.sendMessage("Found items:");
try {
ObjectWriter writer = new ObjectMapper().writer().withDefaultPrettyPrinter();
Expand Down

0 comments on commit 407ec53

Please sign in to comment.