Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
Trying sqlx postgres with json
  • Loading branch information
fpaschos committed Jul 28, 2023
0 parents commit 07abda8
Show file tree
Hide file tree
Showing 8 changed files with 189 additions and 0 deletions.
20 changes: 20 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
### IntelliJ IDEA ###
out/
!**/src/main/**/out/
!**/src/test/**/out/
*.iml
.idea/

### VS Code ###
.vscode/

### Mac OS ###
.DS_Store

### Rust
target/
Cargo.lock




3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[workspace]

members = ["app-claims-service"]
37 changes: 37 additions & 0 deletions USAGE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#### Instructions/ Usage:
Export debezium version (execute once per open terminal session)
```bash
export DEBEZIUM_VERSION=2.3
```

Start up
```bash
docker compose up -d
```

Tear down
```bash
docker compose down
```

#### Utility actions:
Check everything up and running
```bash
docker ps
```

List all available kafka topics
```bash
docker-compose exec kafka /kafka/bin/kafka-topics.sh --bootstrap-server kafka:9092 --list
```

#### Documentation/Resources
For more information see:
- [debezium/kafka](https://hub.docker.com/r/debezium/kafka)

Reference projects:
- [cschaible/rust-microservices-kafka](https://github.com/cschaible/rust-microservices-kafka/blob/master/README.md)
- [debezium-examples/distributed-caching](https://github.com/debezium/debezium-examples/blob/main/distributed-caching/README.md)

Rust libraries:
- [sqlx](https://github.com/launchbadge/sqlx/blob/main/README.md)
13 changes: 13 additions & 0 deletions app-claims-service/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "app-claims-service"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anyhow = "1.0.72"
serde = { version = "1.0.177", features = ["derive"] }
serde_json = "1.0.104"
sqlx = { version = "0.7.1", features = ["postgres", "json", "runtime-tokio"] }
tokio = { version = "1.29.1", features = ["rt-multi-thread", "macros"] }
Empty file added app-claims-service/README.md
Empty file.
5 changes: 5 additions & 0 deletions app-claims-service/migrations/1_claim.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE TABLE IF NOT EXISTS claim
(
id BIGSERIAL PRIMARY KEY,
involved JSONB NOT NULL
);
68 changes: 68 additions & 0 deletions app-claims-service/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use sqlx::postgres::PgPoolOptions;
use anyhow::Context;
use serde::{Deserialize, Serialize};
use sqlx::PgPool;
use sqlx::types::Json;

#[derive(sqlx::FromRow)]
struct ClaimDb {
id: i64,
involved: Json<Party>,
}

#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
struct Party {
first_name: String,
last_name: String,
}

async fn insert_claim(db: &PgPool, involved: Party) -> anyhow::Result<i64> {
let c: (i64,) = sqlx::query_as(r#"INSERT INTO claim (involved) VALUES ($1) RETURNING id"#)
.bind(Json(involved))
.fetch_one(db)
.await?;

Ok(c.0)
}

async fn list_claims(db: &PgPool) -> anyhow::Result<()> {
Ok(())
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {

// TODO 1) config and parameters
let db = PgPoolOptions::new()
.max_connections(5)
.connect("postgres://postgres:postgres@localhost:5432/claimsdb")
.await
.context("Unable to connect to database")?;

sqlx::migrate!()
.run(&db)
.await
.context("Unable to exec db migrations")?;

// Just check the database connection
// Make a simple query to return the given parameter (use a question mark `?` instead of `$1` for MySQL)
let row: (i64, ) = sqlx::query_as("SELECT $1")
.bind(150_i64)
.fetch_one(&db)
.await?;

assert_eq!(row.0, 150);

let involved = Party {
first_name: "Foo".into(),
last_name: "Bar".into(),
};


let new_id = insert_claim(&db, involved).await?;
println!("Claim with id = {new_id} inserted");
list_claims(&db).await?;

Ok(())
}
43 changes: 43 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# example taken from:
# https://github.com/debezium/debezium-examples/blob/main/auditlog/docker-compose.yaml

version: '3.8'
services:
zookeeper:
container_name: kd-zookeeper
image: quay.io/debezium/zookeeper:${DEBEZIUM_VERSION}
ports:
- "2181:2181"
- "2888:2888"
- "3888:3888"
networks:
- kd-demo
kafka:
container_name: kd-kafka
image: quay.io/debezium/kafka:${DEBEZIUM_VERSION}
ports:
- "9092:9092"
depends_on:
- zookeeper
environment:
- ZOOKEEPER_CONNECT=zookeeper:2181
# What is this for????
# For local development of auditlog-enricher
# - ADVERTISED_HOST_NAME=192.168.1.6
- KAFKA_GROUP_MIN_SESSION_TIMEOUT_MS=250
networks:
- kd-demo
postgres:
image: quay.io/debezium/example-postgres:${DEBEZIUM_VERSION}
container_name: kd-postgres
ports:
- "5432:5432"
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
- POSTGRES_DB=claimsdb
networks:
- kd-demo
networks:
kd-demo:
name: kd-demo

0 comments on commit 07abda8

Please sign in to comment.