How to use views?

I’ve scoured the docs and I can’t figure out how to actually call a view, only how to create it. I think the docs could be more clear/explicit on this.

I would have expected it to be a component but it doesn’t show up:

Even though the view seems to be registered correctly - Main.createKalix looks like this:

    KalixFactory.withComponents(
      new Customer(_),
      new Post(_),
      new PostRepliesView(_))

the view pb looks like this:

service PostReplies {
  option (kalix.codegen) = {
    view: {}
  };

  rpc UpdatePost(domain.PostState) returns (api.PostSummary) {
    option (kalix.method).eventing.in = {
      value_entity: "posts"
    };
    option (kalix.method).view.update = {
      table: "post_replies_v1"
      transform_updates: true
    };
  }

  rpc GetPosts(RepliesRequest) returns (stream api.PostSummary) {
    option (kalix.method).view.query = {
      query: "SELECT * FROM post_replies_v1 WHERE parentPostId = :parentPostId"
    };
  }
}

message RepliesRequest {
  string parentPostId = 1;
}

and the view implementation in PostRepliesView.scala compiles fine.

Appreciate the help in advance.

-m

I don’t think it’s good practice to call a view from within your value/event sourced entity service, which is probably why it’s not made available within that context. Try creating an action component and calling it from there.

I don’t think it’s good practice to call a view from within your value/event sourced entity …

That’s correct, entities should be “leaves” of the application and not depend on other components. For composing multiple component calls you should use an action. The effects on the entities does not allow “async” effects so you can anyway not combine calls to produce a final effect storing state there.

The root problem here thought is that we do not generate a representation for the views in components at all right now. So it is not currently possible to use a view as a part of a composed action out of the box (you could still potentially use a a separate gRPC client to call the service).

We initially decided on this because views are eventually consistent representations of state, they are asynchrounously fed the stream of updates or events and are not updated with the entity itself in a single transaction. Therefore they should (almost) never be used for making decisions, decisions should be done within an entity since it is the boundary of consistency for the given id.

I can see that there could be use cases where it makes sense to query a view and do stuff in an action though, so I’ve created an issue to track adding that to the Java/Scala SDK: Provide view query access through components · Issue #1257 · lightbend/kalix-jvm-sdk · GitHub

Thans Johan!

OK got it. I do see now that inside actions, components does make the views available. To be clear though, I didn’t need the view to gather data that would be saved with the entity’s state, rather I was trying to gather data to enrich a GetX type response with some statistics around the entity, on a best-effort basis, so OK if outdated.

Please clarify though: if entities are ‘leaves’ and not depend on other components, why are the other components (except views) available in entities then? A) Do I need to rely on an action to gather state from related entities and package all that together before calling the entity? or B) should I go ahead and consult the other entities via components right from inside the entity (as I’m able to currently), or perhaps C) I can query other entities from inside my entity, but not mutate them?

Turns out I misremembered the outcome of that design discussion.

Views are available via components in the JVM SDKs, the reason you are not seeing your view in the components is likely that it is streaming responses which is not yet accessible in the JVM SDKs. You will currently have work around this and project the result into a single response message with a list inside it to be able to call it from an Action.

… if entities are ‘leaves’ and not depend on other components, why are the other components (except views) available in entities then

We decided to deprecate and remove but it didn’t happen yet, tracked by: Deprecate deferred calls from entities · Issue #673 · lightbend/kalix-jvm-sdk · GitHub