Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update event sourced entity template proto to be counter example #371

Merged
merged 1 commit into from
Jun 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -733,9 +733,9 @@ jobs:
# Run the service in background
npm run start &
# Test the event-sourced-entity
until curl -XPOST localhost:9000/com.example.MyServiceEntity/GetValue -H "Content-Type:application/json" -d '{"entityId":"test"}' -o response.txt; do sleep 5; done
until curl -XPOST localhost:9000/com.example.CounterService/GetCurrentCounter -H "Content-Type:application/json" -d '{"counterId":"test"}' -o response.txt; do sleep 5; done
echo "Validating expected error message"
test "$(cat response.txt)" = 'The command handler for `GetValue` is not implemented, yet'
test "$(cat response.txt)" = 'The command handler for `GetCurrentCounter` is not implemented, yet'

publish_native_linux:
machine:
Expand Down
83 changes: 56 additions & 27 deletions npm-js/create-kalix-entity/template/base-common/README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
# {{name}}

Welcome to the project generated by Kalix code generation tools! The tools have generated a project, an example Kalix service, and tests. You can modify the project with your own logic. When you rebuild, the tools will generate implementation and test stubs to accelerate development.
Welcome to the project generated by Kalix code generation tools! The tools have generated a project,
an example Kalix service, and tests. You can modify the project with your own logic. When you
rebuild, the tools will generate implementation and test stubs to accelerate development.

## Prerequisites

No additional tooling is required to develop locally. To package and deploy to Kalix, you will need the following:
No additional tooling is required to develop locally. To package and deploy to Kalix, you will need
the following:

- Docker; see https://docs.docker.com/engine/install/
- Kalix CLI (`kalix`); see https://docs.kalix.io/kalix/install-kalix.html
Expand All @@ -13,20 +16,28 @@ No additional tooling is required to develop locally. To package and deploy to K

Take a look at what the tools generated for you:

- The top level directory, `{{name}}`, contains build and packaging configuration, which are described in more detail below.
- The `proto` folder contains `protobuf` descriptors written in the [Proto3](https://developers.google.com/protocol-buffers/docs/proto3) Protocol Buffers Language. The `.proto` files in this folder specify messages describing the external APIs for your service as well as the internal data structures.
- The top level directory, `{{name}}`, contains build and packaging configuration, which are
described in more detail below.
- The `proto` folder contains `protobuf` descriptors written in the
[Proto3](https://developers.google.com/protocol-buffers/docs/proto3) Protocol Buffers Language.
The `.proto` files in this folder specify messages describing the external APIs for your service
as well as the internal data structures.

To understand Kalix services, `protobuf` descriptors, and Entities, see the documentation on [designing services](https://docs.kalix.io/designing/index.html).
To understand Kalix services, `protobuf` descriptors, and Entities, see the documentation on
[designing services](https://docs.kalix.io/designing/index.html).

## Developing

This project has a bare-bones service ready for you to adapt and
extend. To see the range of functionality available for you to use with the Kalix JavaScript SDK, see the documentation [JavaScript section](https://docs.kalix.io/javascript/index.html). After you have modified any source files, build and the tools will preserve your changes as well as generate stubs for you to implement.
This project has a bare-bones service ready for you to adapt and extend. To see the range of
functionality available for you to use with the Kalix JavaScript SDK, see the documentation
[JavaScript section](https://docs.kalix.io/javascript/index.html). After you have modified any
source files, build and the tools will preserve your changes as well as generate stubs for you to
implement.

## Building

To build, at a minimum you need to generate and process sources, particularly when using an IDE.
A convenience is compile your project:
To build your project, including generating sources from protobuf, first install dependencies and
then run the provided `build` script:

```
npm install
Expand All @@ -35,17 +46,25 @@ npm run build

## Testing

Running build generates basic skeleton unit tests for each method in each service. You will need to flesh them out with assertions as behavior is implemented. The testing framework used by the tools is Mocha. You should use Chai for assertions. To execute the tests:
Running `build` generates basic skeleton unit tests for each method in each service. You will need
to flesh them out with assertions as behavior is implemented. The testing framework used by the
tools is Mocha. You should use Chai for assertions. To execute the tests:

```
npm run test
npm test
```

These tests leverage the mock entity classes provided by `@kalix-io/testkit`. These classes mimic the minimal required machinery to execute commands and handle events against a single entity for simple unit testing. Integration tests, which run a local Kalix development environment, can also be written using `@kalix-io/testkit`.
These tests leverage the mock entity classes provided by `@kalix-io/testkit`. These classes mimic
the minimal required machinery to execute commands and handle events against a single entity for
simple unit testing. Integration tests, which run a local Kalix development environment, can also be
written using the testkit.

## Running Locally

To run your application locally, you must run the Kalix proxy. The included `docker-compose` file contains the configuration required to run the proxy for a locally running application. It also contains the configuration to start a local Google Pub/Sub emulator that the Kalix proxy will connect to.
To run your application locally, you must run the Kalix proxy. The included `docker-compose` file
contains the configuration required to run the proxy for a locally running application. It also
contains the configuration to start a local Google Pub/Sub emulator that the Kalix proxy will
connect to.

To start the proxy, run the following command from this directory:

Expand All @@ -58,35 +77,45 @@ To start the application locally, use the following commands:
> Be sure to have performed `npm install` for the first time!

```
npm run build && npm run start
npm run build && npm start
```

With both the proxy and your application running, any defined endpoints should be available at `http://localhost:9000`. In addition to the defined gRPC interface, each method has a corresponding HTTP endpoint. Unless configured otherwise (see [Transcoding HTTP](https://docs.kalix.io/javascript/proto.html#_transcoding_http)), this endpoint accepts POST requests at the path `/[package].[entity name]/[method]`. For example, using `curl`:
With both the proxy and your application running, any defined endpoints should be available at
`http://localhost:9000`. In addition to the defined gRPC interface, each method has a corresponding
HTTP endpoint. Unless configured otherwise (see [Transcoding
HTTP](https://docs.kalix.io/javascript/proto.html#_transcoding_http)), this endpoint accepts POST
requests at the path `/[package].[service name]/[method]`.

For example, using `curl`:

```
> curl -XPOST -H "Content-Type: application/json" localhost:9000/com.example.MyServiceEntity/GetValue -d '{"entityId": "foo"}'
The command handler for `GetValue` is not implemented, yet
> curl -XPOST -H "Content-Type: application/json" localhost:9000/com.example.CounterService/GetCurrentCounter -d '{"counterId": "foo"}'
The command handler for `GetCurrentCounter` is not implemented, yet
```

For example, given [`grpcurl`](https://github.com/fullstorydev/grpcurl):

```
> grpcurl -plaintext -d '{"entityId": "foo"}' localhost:9000 com.example.MyServiceEntity/GetValue
> grpcurl -plaintext -d '{"counterId": "foo"}' localhost:9000 com.example.CounterService/GetCurrentCounter
ERROR:
Code: Unknown
Message: The command handler for `GetValue` is not implemented, yet
Message: The command handler for `GetCurrentCounter` is not implemented, yet
```

> Note: The failure is to be expected if you have not yet provided an implementation of `GetValue` in
> your entity.
> Note: The failure is to be expected if you have not yet provided an implementation of
> `GetCurrentCounter` in your entity.

## Deploying to Kalix

To deploy your service, install the `kalix` CLI as documented in [Setting up a local development environment](https://docs.kalix.io/getting-started/set-up-development-env.html) and configure a Docker Registry to upload your docker image to.
To deploy your service, install the `kalix` CLI as documented in [Setting up a local development
environment](https://docs.kalix.io/getting-started/set-up-development-env.html) and configure a
Docker Registry to upload your docker image to.

You will need to update the `config.dockerImage` property in the `package.json` and refer to [Configuring registries](https://docs.kalix.io/projects/container-registries.html) for more information on how to make your docker image available to Kalix.
You will need to update the `config.dockerImage` property in the `package.json` and refer to
[Configuring registries](https://docs.kalix.io/projects/container-registries.html) for more
information on how to make your docker image available to Kalix.

Finally, you can use the [Kalix Console](https://console.kalix.io)
to create a project and then deploy your service into the project either by using `npm run deploy`,
through the `kalix` CLI or via the web interface. When using `npm run deploy`, npm will also
conveniently package and publish your docker image prior to deployment.
Finally, you can use the [Kalix Console](https://console.kalix.io) to create a project and then
deploy your service into the project either by using `npm run deploy`, through the `kalix` CLI or
via the web interface. When using `npm run deploy`, npm will also conveniently package and publish
your docker image prior to deployment.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// This is the public API offered by your entity.
syntax = "proto3";

import "google/protobuf/empty.proto";
import "kalix/annotations.proto";
import "google/api/annotations.proto";

package com.example;

message IncreaseValue {
string counter_id = 1 [(kalix.field).entity_key = true];
int32 value = 2;
}

message DecreaseValue {
string counter_id = 1 [(kalix.field).entity_key = true];
int32 value = 2;
}

message ResetValue {
string counter_id = 1 [(kalix.field).entity_key = true];
}

message GetCounter {
string counter_id = 1 [(kalix.field).entity_key = true];
}

message CurrentCounter {
int32 value = 1;
}

service CounterService {
option (kalix.service) = {
type : SERVICE_TYPE_ENTITY
component : ".domain.Counter"
};

rpc Increase(IncreaseValue) returns (google.protobuf.Empty);
rpc Decrease(DecreaseValue) returns (google.protobuf.Empty);
rpc Reset(ResetValue) returns (google.protobuf.Empty);
rpc GetCurrentCounter(GetCounter) returns (CurrentCounter);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
syntax = "proto3";

package com.example.domain;

import "kalix/annotations.proto";

option (kalix.file).event_sourced_entity = {
name: "Counter"
entity_type: "counter"
state: "CounterState"
events: "ValueIncreased"
events: "ValueDecreased"
events: "ValueReset"
};

message CounterState {
int32 value = 1;
}

message ValueIncreased {
int32 value = 1;
}

message ValueDecreased {
int32 value = 1;
}

message ValueReset {}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,35 @@ import "google/api/annotations.proto";
package com.example;

message IncreaseValue {
string counter_id = 1 [(kalix.field).entity_key = true];
int32 value = 2;
string counter_id = 1 [(kalix.field).entity_key = true];
int32 value = 2;
}

message DecreaseValue {
string counter_id = 1 [(kalix.field).entity_key = true];
int32 value = 2;
string counter_id = 1 [(kalix.field).entity_key = true];
int32 value = 2;
}

message ResetValue {
string counter_id = 1 [(kalix.field).entity_key = true];
string counter_id = 1 [(kalix.field).entity_key = true];
}

message GetCounter {
string counter_id = 1 [(kalix.field).entity_key = true];
string counter_id = 1 [(kalix.field).entity_key = true];
}

message CurrentCounter {
int32 value = 1;
int32 value = 1;
}

service CounterService {
option (kalix.service) = {
type : SERVICE_TYPE_ENTITY
component : ".domain.Counter"
};

rpc Increase(IncreaseValue) returns (google.protobuf.Empty);
rpc Decrease(DecreaseValue) returns (google.protobuf.Empty);
rpc Reset(ResetValue) returns (google.protobuf.Empty);
rpc GetCurrentCounter(GetCounter) returns (CurrentCounter);
option (kalix.service) = {
type : SERVICE_TYPE_ENTITY
component : ".domain.Counter"
};

rpc Increase(IncreaseValue) returns (google.protobuf.Empty);
rpc Decrease(DecreaseValue) returns (google.protobuf.Empty);
rpc Reset(ResetValue) returns (google.protobuf.Empty);
rpc GetCurrentCounter(GetCounter) returns (CurrentCounter);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ package com.example.domain;
import "kalix/annotations.proto";

option (kalix.file).value_entity = {
name: "Counter"
entity_type: "counter"
state: "CounterState"
name: "Counter"
entity_type: "counter"
state: "CounterState"
};

message CounterState {
int32 value = 1;
int32 value = 1;
}
16 changes: 9 additions & 7 deletions samples/js/valueentity-counter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,24 +64,26 @@ To start the application locally, use the following commands:
npm run build && npm run start
```

With both the proxy and your application running, any defined endpoints should be available at `http://localhost:9000`. In addition to the defined gRPC interface, each method has a corresponding HTTP endpoint. Unless configured otherwise (see [Transcoding HTTP](https://docs.kalix.io/javascript/proto.html#_transcoding_http)), this endpoint accepts POST requests at the path `/[package].[entity name]/[method]`. For example, using `curl`:
With both the proxy and your application running, any defined endpoints should be available at `http://localhost:9000`. In addition to the defined gRPC interface, each method has a corresponding HTTP endpoint. Unless configured otherwise (see [Transcoding HTTP](https://docs.kalix.io/javascript/proto.html#_transcoding_http)), this endpoint accepts POST requests at the path `/[package].[entity name]/[method]`.

For example, using `curl`:

```
> curl -XPOST -H "Content-Type: application/json" localhost:9000/com.example.MyServiceEntity/GetValue -d '{"entityId": "foo"}'
The command handler for `GetValue` is not implemented, yet
> curl -XPOST -H "Content-Type: application/json" localhost:9000/com.example.CounterService/GetCurrentCounter -d '{"counterId": "foo"}'
The command handler for `GetCurrentCounter` is not implemented, yet
```

For example, given [`grpcurl`](https://github.com/fullstorydev/grpcurl):

```
> grpcurl -plaintext -d '{"entityId": "foo"}' localhost:9000 com.example.MyServiceEntity/GetValue
> grpcurl -plaintext -d '{"counterId": "foo"}' localhost:9000 com.example.CounterService/GetCurrentCounter
ERROR:
Code: Unknown
Message: The command handler for `GetValue` is not implemented, yet
Message: The command handler for `GetCurrentCounter` is not implemented, yet
```

> Note: The failure is to be expected if you have not yet provided an implementation of `GetValue` in
> your entity.
> Note: The failure is to be expected if you have not yet provided an implementation of
> `GetCurrentCounter` in your entity.
## Deploying

Expand Down
Loading