Azure / azure-cosmosdb-java

Java Async SDK for SQL API of Azure Cosmos DB
MIT License
54 stars 61 forks source link

Adds support to Mapper in the SDK #44

Closed otaviojava closed 6 years ago

otaviojava commented 6 years ago

Adds support to a Mapper that's a wrapper that abstracts these operations to an entity.

E.g.: Given an Person entity:

@Entity(databaseName = "AzureSampleFamilyDB")
public class Person implements Serializable {

    private String id;

    private String name;

    private Integer age;

}

You can create a Mapper unit that will handle the operations related to a particular class.

        AsyncDocumentClient client = ...;
        MappingManager mappingManager = MappingManager.of(client);
        Mapper<Person> mapper = mappingManager.mapper(Person.class);

        Person ada = new Person();
        ada.setId("ada_" + System.currentTimeMillis());
        ada.setName("Ada Lovelace");
        ada.setAge(20);

        Observable<Person> observable = mapper.save(ada);

        Observable<Person> findById = mapper.findById(ada.getId());

        FeedOptions queryOptions = new FeedOptions();
        queryOptions.setMaxItemCount(100);

        Observable<List<Person>> queryResult = mapper.query("SELECT * FROM Person WHERE Person.name = 'Ada Lovelace'", queryOptions);

         Observable<Void> observable = mapper.deleteById(ada.getId());

:warning: Both MappingManager and Mapper should have one instance at Application scope, that is easily handled with frameworks like Spring or CDI.

Remain tasks:

Next steps to improve the Mapper

IMHO: This PR is just the first step to move forward an easy integration with Mapper.

1) The Repository interface is given an interface that has basic methods a Java developer just need to extend it, so the interface will already be implemented by the Mapper manager.

public interface PersonRepository extends Repository<Person> {
}
    PersonRepository repository = mappingManager.mapper(PersonRepository.class);//ready to use

Done: check: https://github.com/Azure/azure-cosmosdb-java/pull/45 2) Support by query by annotation


public interface PersonRepository extends Repository<Person> {
 @Query("SELECT * FROM Person WHERE Person.name = @name")
 Observable<List<Person>> usingQuery(@Param("name") String name);

}

Done: check: https://github.com/Azure/azure-cosmosdb-java/pull/45

3) And the old, but gold query by the method where the developer writes the method using conventions and the mapper will down the implementation.


public interface PersonRepository extends Repository<Person> {

 Observable<List<Person>> findByName(String name);

}
moderakh commented 6 years ago

@otaviojava Thanks for the PR and sorry for the delayed update on this.

We decided to have support for object mapping outside of SDK as part of Spring project (https://github.com/Microsoft/spring-data-cosmosdb). So we have to reject the PR.