Skip to content
This repository was archived by the owner on Aug 29, 2024. It is now read-only.

Commit

Permalink
Remove partition key and add and query support (#37)
Browse files Browse the repository at this point in the history
* add and query support

* remove partitionkey from repository paramter

* complete test

* split test into partition and non-partition

* enable cross region when partition key value not specified

* add query partitioned collection readme

* add test find by ID field name for partitioned collection

* improve readme
  • Loading branch information
sophiaso authored Mar 1, 2018
1 parent 39af47b commit b72cf3f
Show file tree
Hide file tree
Showing 24 changed files with 591 additions and 320 deletions.
58 changes: 58 additions & 0 deletions QueryPartitionedCollection.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
### How to Query Partitioned DocumentDB Collection

With DocumentDB, you can configure [partition key](https://docs.microsoft.com/en-us/azure/cosmos-db/partition-data) for your collection.

Below is an example about how to query partitioned collection with this spring data module.

#### Example

Given a document entity structure:
```
@Document
@Data
@AllArgsConstructor
public class Address {
@Id
String postalCode;
String street;
@PartitionKey
String city;
}
```

Write the repository interface:
```
@Repository
public interface AddressRepository extends DocumentDbRepository<Address, String> {
// Add query methods here, refer to below
}
```

Query by field name:
```
List<Address> findByCity(String city);
```

Delete by field name:
```
void deleteByStreet(String street);
```

For `Partitioned collection`, if you want to query records by `findById(id)`, exception will be thrown.
```
// Incorrect for partitioned collection, exception will be thrown
Address result = repository.findById(id); // Caution: Works for non-partitioned collection
```

Instead, you can query records by ID field name with custom query.
```
// Correct, postalCode is the ID field in Address domain
@Repository
public interface AddressRepository extends DocumentDbRepository<Address, String> {
List<Address> findByPostalCode(String postalCode);
}
// Query
List<Address> result = repository.findByPostalCode(postalCode);
```

8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* [Sample Code](#sample-codes)
* [Feature List](#feature-list)
* [Quick Start](#quick-start)
* [Query Partitioned Collection](QueryPartitionedCollection.md)
* [Filing Issues](#filing-issues)
* [How to Contribute](#how-to-contribute)
* [Code of Conduct](#code-of-conduct)
Expand All @@ -38,7 +39,7 @@ Please refer to [sample project here](./samplecode).
- Custom collection Name.
By default, collection name will be class name of user domain class. To customize it, add annotation `@Document(collection="myCustomCollectionName")` to domain class, that's all.
- Supports [Azure Cosmos DB partition](https://docs.microsoft.com/en-us/azure/cosmos-db/partition-data). To specify a field of domain class to be partition key field, just annotate it with `@PartitionKey`. When you do CRUD operation, pls specify your partition value. For more sample on partition CRUD, pls refer to [test here](./src/test/java/com/microsoft/azure/spring/data/cosmosdb/documentdb/repository/AddressRepositoryIT.java)
- Supports [Spring Data custom query](https://docs.spring.io/spring-data/commons/docs/current/reference/html/#repositories.query-methods.details) find operation.
- Supports [Spring Data custom query](https://docs.spring.io/spring-data/commons/docs/current/reference/html/#repositories.query-methods.details) find operation, e.g., `findByAFieldAndBField
- Supports [spring-boot-starter-data-rest](https://projects.spring.io/spring-data-rest/).
- Supports List and nested type in domain class.

Expand Down Expand Up @@ -104,7 +105,10 @@ public class User {
private String lastName;
... // setters and getters
public User() {
}
public User(String id, String firstName, String lastName) {
this.id = id;
this.firstName = firstName;
Expand Down
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@
<artifactId>lombok</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
<scope>test</scope>
</dependency>

</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package com.microsoft.azure.spring.data.documentdb.core;

import com.microsoft.azure.documentdb.DocumentCollection;
import com.microsoft.azure.documentdb.PartitionKey;
import com.microsoft.azure.spring.data.documentdb.core.convert.MappingDocumentDbConverter;
import com.microsoft.azure.spring.data.documentdb.core.query.Query;

Expand All @@ -20,47 +21,42 @@ DocumentCollection createCollectionIfNotExists(String collectionName,
String partitionKeyFieldName,
Integer requestUnit);

<T> List<T> findAll(Class<T> entityClass,
String partitionKeyFieldName,
String partitionKeyFieldValue);
<T> List<T> findAll(Class<T> entityClass);

<T> List<T> findAll(String collectionName,
Class<T> entityClass,
String partitionKeyFieldName,
String partitionKeyFieldValue);
<T> List<T> findAll(String collectionName, Class<T> entityClass);

<T> T findById(Object id,
Class<T> entityClass,
String partitionKeyFieldValue);
Class<T> entityClass);

<T> T findById(String collectionName,
Object id,
Class<T> entityClass,
String partitionKeyFieldValue);
Class<T> entityClass);

<T> List<T> find(Query query,
Class<T> entityClass,
String collectionName);

<T> T insert(T objectToSave, String partitionKeyFieldValue);
<T> T insert(T objectToSave, PartitionKey partitionKey);

<T> T insert(String collectionName,
T objectToSave,
String partitionKeyFieldValue);
PartitionKey partitionKey);

<T> void upsert(T object, Object id, String partitionKeyFieldValue);
<T> void upsert(T object, Object id, PartitionKey partitionKey);

<T> void upsert(String collectionName,
T object,
Object id,
String partitionKeyFieldValue);
PartitionKey partitionKey);

<T> void deleteById(String collectionName,
Object id,
Class<T> domainClass,
String partitionKeyFieldValue);
PartitionKey partitionKey);

void deleteAll(String collectionName);

<T> List<T> delete(Query query, Class<T> entityClass, String collectionName);

MappingDocumentDbConverter getConverter();
}
Loading

0 comments on commit b72cf3f

Please sign in to comment.