spring-attic / spring-cloud-gcp

Integration for Google Cloud Platform APIs with Spring
Apache License 2.0
705 stars 693 forks source link

Provide Datastore projection requirements at runtime #2469

Closed BenDol closed 3 years ago

BenDol commented 4 years ago

It would be really helpful for me if there was a way to invoke a projection query at runtime without the need to construct an interface per repository request. Is this currently possible? I haven't seen anything about it.

The ideal solution would be a way to provide all the required projection metadata that you'd generate with the ProjectionFactory, at runtime to generate your query request.

interface ActionProjection {
  @Value("${target.name} + ${target.symbol}")
  String getNameAndSymbol();
}

So this would become some sort of builder request with the DatastoreTemplate like:

template.getProjection(new ProjectionBuilder()
    .targetEntity(Action.class)
    .projecting("name")
    .projecting("symbol", Result.APPEND)
    .returnType(String.class) // maybe? Map could return by default with multiple projection data
    .build();
);

Obviously there might be missing requirements here, but you get the idea. Something a long these lines would be amazing for us.

Appreciate you taking the time to read through this post!

Regards, Ben

meltsufin commented 4 years ago

@mp911de Do you think this kind of feature request fits into the Spring Data concepts?

mp911de commented 4 years ago

Projection interfaces are primarily intended to expose parts of the underlying domain model. Open projections that make use of @Value build on top of the results and add a bit of sugar. While there is a bit of dynamic in how a property is computed, the actual structure (which properties get exposed) gets defined when the interface is defined.

More dynamic is typically handled by the underlying database (e.g. when using a custom SQL statement to select a certain projection or when using MongoDB's aggregation framework) and not Spring Data itself.

That being said, we'd rather expect Google Datastore to provide such functionality as it doesn't fit into what Spring Data is doing.

BenDol commented 4 years ago

Hi @mp911de thanks for the reply! What I'm suggesting here would be the exact same concept as the @Value projection just in a more dynamic way. I guess I'm not fully understanding why compilation projection fits into Spring Data but creating projection metadata dynamically for the Datastore request, doesn't? Cheers!

EDIT: as far as I understand Spring Cloud GCP Datastore is just using Datastore projection? I could be wrong... https://cloud.google.com/appengine/docs/standard/java/datastore/projectionqueries

dmitry-s commented 4 years ago

@BenDol Spring Cloud GCP Datastore uses projection queries. You should be able to use datastoreOperations.queryKeysOrEntities(query, entityType) passing a query of ProjectionEntityQuery type.

Let us know if that doesn't work for you or if you have any questions.