Skip to content

Commit

Permalink
Fix database work in progress
Browse files Browse the repository at this point in the history
Signed-off-by: tvallin <[email protected]>
  • Loading branch information
tvallin committed Aug 24, 2023
1 parent c58c629 commit cd0dd70
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 46 deletions.
3 changes: 3 additions & 0 deletions archetypes/helidon/src/main/archetype/common/common.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
<transformation id="packaged">
<replace regex="__pkg__" replacement="${package/\./\/}"/>
</transformation>
<transformation id="package-name">
<replace regex="package" replacement="${package}"/>
</transformation>
<transformation id="json-mustache">
<replace regex="\.json.mustache$" replacement=""/>
</transformation>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ curl -s -X GET http://localhost:8080/health
<value>io.helidon.webserver.observe.health.HealthObserveProvider</value>
</list>
<list key="MainTest-static-imports" if="${flavor} == 'se'">
<value>org.hamcrest.Matchers.containsString</value>
<value>org.hamcrest.CoreMatchers.containsString</value>
</list>
<list key="Main-java-imports" if="${flavor} == 'se'">
<value>java.time.Duration</value>
Expand All @@ -465,7 +465,7 @@ curl -s -X GET http://localhost:8080/health
<value><![CDATA[ private static long serverStartTime;]]></value>
</list>
<list key="Main-main" if="${flavor} == 'se'">
<value><![CDATA[ serverStartTime = System.currentTimeMillis();]]></value>
<value><![CDATA[serverStartTime = System.currentTimeMillis();]]></value>
</list>
<list key="Main-content" if="${flavor} == 'se'">
<value><![CDATA[
Expand Down
71 changes: 55 additions & 16 deletions archetypes/helidon/src/main/archetype/se/custom/database.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ Instructions for H2 can be found here: https://www.h2database.com/html/cheatShee
password:
poolName: h2]]></value>
</list>
<value key="start-db"><![CDATA[
H2:
```
docker run --rm --name h2 -p 9092:9082 -p 8082:8082 nemerosa/h2
```
For details, see http://www.h2database.com/html/cheatSheet.html]]></value>
</model>
</output>
</option>
Expand Down Expand Up @@ -91,6 +97,13 @@ docker run --rm --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root -e MYSQL_D
password: password
poolName: mysql]]></value>
</list>
<value key="start-db"><![CDATA[
MySQL:
```
docker run --rm --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root \
-e MYSQL_DATABASE=pokemon -e MYSQL_USER=user -e MYSQL_PASSWORD=password mysql:5.7
```
]]></value>
</model>
</output>
</option>
Expand Down Expand Up @@ -125,6 +138,13 @@ docker run --rm --name xe -p 1521:1521 -p 8888:8080 -e ORACLE_PWD=oracle wnamele
password: oracle
poolName: oracle]]></value>
</list>
<value key="start-db"><![CDATA[
Oracle:
```
docker run --rm --name xe -p 1521:1521 -p 8888:8080 wnameless/oracle-xe-11g-r2
```
For details on an Oracle Docker image, see https://github.com/oracle/docker-images/tree/master/OracleDatabase/SingleInstance
]]></value>
</model>
</output>
</option>
Expand Down Expand Up @@ -237,26 +257,32 @@ db:
}'
]]></value>
</list>
<value key="start-db"><![CDATA[
```
docker run --rm --name mongo -p 27017:27017 mongo
```
]]></value>
</model>
</output>
</option>
</enum>
</inputs>
<output>
<templates engine="mustache" transformations="mustache,packaged">
<templates engine="mustache" transformations="mustache,packaged,package-name">
<directory>../database/files</directory>
<includes>
<include>src/main/java/**</include>
<include>src/main/**</include>
</includes>
</templates>
<model>
<value key="database">true</value>
<value key="server">${server}</value>
<list key="dependencies">
<map if="${server} != 'mongo'">
<map if="${server} != 'mongodb'">
<value key="groupId">io.helidon.dbclient</value>
<value key="artifactId">helidon-dbclient-jdbc</value>
</map>
<map if="${server} != 'mongo'">
<map if="${server} != 'mongodb'">
<value key="groupId">io.helidon.dbclient</value>
<value key="artifactId">helidon-dbclient-metrics-jdbc</value>
</map>
Expand Down Expand Up @@ -303,7 +329,6 @@ db:
<map>
<value key="groupId">org.slf4j</value>
<value key="artifactId">slf4j-jdk14</value>
<value key="version">2.0.0</value>
</map>
<map>
<value key="groupId">io.helidon.webserver.observe</value>
Expand All @@ -326,30 +351,26 @@ db:
<value key="artifactId">h2</value>
<value key="scope">test</value>
</map>
<map if="${server} == 'mongodb'">
<value key="groupId">io.helidon.dbclient</value>
<value key="artifactId">helidon-dbclient-jdbc</value>
<value key="scope">test</value>
</map>
</list>
<list key="Main-helidon-imports" if="${server} != 'mongo'">
<value>io.helidon.dbclient.DbClient</value>
<value>io.helidon.webserver.observe.ObserveFeature</value>
<value>io.helidon.webserver.tracing.TracingFeature</value>
<value>io.helidon.tracing.TracerBuilder</value>
</list>
<list key="Main-routing-builder" if="${server} != 'mongo'">
<list key="Main-routing-builder" if="${server} != 'mongodb'">
<value><![CDATA[.addFeature(ObserveFeature.create(config))
.addFeature(TracingFeature.create(TracerBuilder.create(config.get("tracing")).build()))
.register("/db", new PokemonService(DbClient.create(config.get("db"))))]]></value>
</list>
<list key="application-yaml-entries" if="${server} != 'mongo'">
<value><![CDATA[
tracing:
service: jdbc-db
]]></value>
<list key="application-yaml-entries" if="${server} != 'mongodb'">
<value file="../database/files/application-jdbc.yaml.mustache" template="mustache"/>
</list>
<list key="config-test">
<value><![CDATA[
tracing:
service: jdbc-db
]]></value>
<value file="../database/files/application-test.yaml"/>
</list>
<list key="modules">
Expand All @@ -360,6 +381,24 @@ tracing:
<value>io.helidon.dbclient.tracing</value>
<value if="${media.json-lib} == 'jackson'">jakarta.json</value>
</list>
<list key="readme-sections">
<value template="mustache"><![CDATA[
### Database Setup
In the `pom.xml` and `application.yaml` we provide configuration needed for {{server}} database.
Start your database before running this example.
Example docker commands to start databases in temporary containers:
{{start-db}}
### Exercise The Database Endpoints
```
curl -X GET http://localhost:8080/db
```
]]></value>
</list>
</model>
</output>
</boolean>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@
<output>
<model>
<value key="title">Helidon SE Database</value>
<value key="readme-description">Helidon SE application that uses JPA with an in-memory H2 database.</value>
<list key="readme-exercise-the-application">
<value file="files/README.md"/>
</list>
</model>
</output>
</archetype-script>

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ db:
type: "query"
statementName: "health-check"
services:
tracing:
# would trace all statement names that start with select-
- statement-names: ["select-.*"]
# would trace all delete statements
- statement-types: ["DELETE"]
metrics:
- type: TIMER
errors: false
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package {{package}};

import io.helidon.http.NotFoundException;
import io.helidon.common.media.type.MediaTypes;
import io.helidon.common.parameters.Parameters;
import io.helidon.dbclient.DbClient;
import io.helidon.dbclient.DbTransaction;
Expand Down Expand Up @@ -32,7 +33,7 @@ public class PokemonService implements HttpService {
@Override
public void routing(HttpRules rules) {
rules
.get("/", this::listPokemons)
.get("/", this::listUsage)
// create new
.put("/", Handler.create(Pokemon.class, this::insertPokemon))
// update existing
Expand All @@ -44,9 +45,7 @@ public class PokemonService implements HttpService {
// delete one
.delete("/{name}", this::deletePokemon)
// example of transactional API (local transaction only!)
.put("/transactional", this::transactional)
// update one (TODO this is intentionally wrong - should use JSON request, just to make it simple we use path)
.put("/{name}/type/{type}", this::updatePokemonType);
.put("/transactional", this::transactional);
}

/**
Expand Down Expand Up @@ -115,15 +114,22 @@ public class PokemonService implements HttpService {
}

/**
* Return JsonArray with all stored Pokémon.
* Display endpoint usage.
*
* @param req the server request
* @param res the server response
*/
private void listPokemons(ServerRequest req, ServerResponse res) {
res.send(dbClient.execute()
.namedQuery("select-all")
.map(it -> it.as(JsonObject.class)));
private void listUsage(ServerRequest req, ServerResponse res) {
res.headers().contentType(MediaTypes.TEXT_PLAIN);
res.send("""
Pokemon Database Example:
PUT / - Create pokemon
DELETE / - Delete all pokemon
POST /{name}/type/{type} - Update existing pokemon
GET /{name} - Get pokemon by name
DELETE /{name} - Delete pokemon by name
PUT /transactional - example of transactional API (local transaction only!)
""");
}

/**
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{package}}.PokemonMapperProvider

0 comments on commit cd0dd70

Please sign in to comment.