Skip to content

Commit

Permalink
move-to-pog
Browse files Browse the repository at this point in the history
  • Loading branch information
inoas committed Nov 14, 2024
1 parent 1b487ea commit 37b7406
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 46 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),

<!-- ## [Unreleased] -->

## [1.0.3] - 2024-11-14

- Renamed library from `cake_gleam_pgo` to `cake_pog`.
- Use `pog` instead of `gleam_pgo` as a gleam postgres client dependency.
- Adapt to breaking changes this move brought.

## [1.0.2] - 2024-11-03

- Added omitted license file.
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
# Adapter between `cake` and `gleam_pgo`
# Adapter between `cake` and `pog`

[![Package <a href="https://github.com/inoas/gleam-cake-pgo/releases"><img src="https://img.shields.io/github/release/inoas/gleam-cake-gleam_pgo" alt="GitHub release"></a> Version](https://img.shields.io/hexpm/v/cake_gleam_pgo)](https://hex.pm/packages/cake_gleam_pgo)
[![Package <a href="https://github.com/inoas/gleam-cake-pgo/releases"><img src="https://img.shields.io/github/release/inoas/gleam-cake-pog" alt="GitHub release"></a> Version](https://img.shields.io/hexpm/v/cake_pog)](https://hex.pm/packages/cake_pog)
[![Erlang-compatible](https://img.shields.io/badge/target-erlang-b83998)](https://www.erlang.org/)
[![Hex Docs](https://img.shields.io/badge/hex-docs-ffaff3)](https://hexdocs.pm/cake_gleam_pgo/)
[![Hex Docs](https://img.shields.io/badge/hex-docs-ffaff3)](https://hexdocs.pm/cake_pog/)
[![Discord](https://img.shields.io/discord/768594524158427167?label=discord%20chat&amp;color=5865F2)](https://discord.gg/Fm8Pwmy)

<!--
[![CI Test](https://github.com/inoas/gleam-cake-gleam_pgo/actions/workflows/test.yml/badge.svg?branch=main&amp;event=push)](https://github.com/inoas/gleam-cake-gleam_pgo/actions/workflows/test.yml)
[![CI Test](https://github.com/inoas/gleam-cake-pog/actions/workflows/test.yml/badge.svg?branch=main&amp;event=push)](https://github.com/inoas/gleam-cake-pog/actions/workflows/test.yml)
-->

🎂[Cake](http://hex.pm/packages/cake) 🐘PostgreSQL adapter which passes `PreparedStatement`s to the [gleam_pgo](http://hex.pm/packages/gleam_pgo) library for execution written in [Gleam](https://gleam.run/).
🎂[Cake](http://hex.pm/packages/cake) 🐘PostgreSQL adapter which passes `PreparedStatement`s to the [pog](http://hex.pm/packages/pog) library for execution written in [Gleam](https://gleam.run/).

## Installation

```sh
gleam add cake_gleam_pgo@1
gleam add cake_pog@1
```

## Example
Expand Down
10 changes: 5 additions & 5 deletions gleam.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
name = "cake_gleam_pgo"
version = "1.0.2"
description = "🎂Cake 🐘PostgreSQL adapter which passes PreparedStatements to the gleam_pgo library for execution written in Gleam."
name = "cake_pog"
version = "1.0.3"
description = "🎂Cake 🐘PostgreSQL adapter which passes PreparedStatements to the pog library for execution written in Gleam."
licences = ["MPL-2.0"]
repository = { type = "github", user = "inoas", repo = "gleam-cake-gleam_pgo" }
repository = { type = "github", user = "inoas", repo = "gleam-cake-pog" }
# links = [{ title = "Website", href = "https://gleam.run" }]

gleam = ">= 1.4.0 and < 2.0.0"
Expand All @@ -14,5 +14,5 @@ pages = [

[dependencies]
cake = ">= 2.0.0 and < 3.0.0"
gleam_pgo = ">= 0.14.0 and < 2.0.0"
pog = ">= 1.0.0 and < 2.0.0"
gleam_stdlib = ">= 0.40.0 and < 2.0.0"
4 changes: 2 additions & 2 deletions manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
packages = [
{ name = "backoff", version = "1.1.6", build_tools = ["rebar3"], requirements = [], otp_app = "backoff", source = "hex", outer_checksum = "CF0CFFF8995FB20562F822E5CC47D8CCF664C5ECDC26A684CBE85C225F9D7C39" },
{ name = "cake", version = "2.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "cake", source = "hex", outer_checksum = "0F9D47447C0BDABE21522CC9792DA42AEBC1EC43E53919F6173FD843DA88A1B8" },
{ name = "gleam_pgo", version = "0.14.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "pgo"], otp_app = "gleam_pgo", source = "hex", outer_checksum = "12256A7F351E994A6E43AB67276DD8AACE752991C013F48A288C4A848F3A9758" },
{ name = "gleam_stdlib", version = "0.40.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "86606B75A600BBD05E539EB59FABC6E307EEEA7B1E5865AFB6D980A93BCB2181" },
{ name = "opentelemetry_api", version = "1.4.0", build_tools = ["rebar3", "mix"], requirements = [], otp_app = "opentelemetry_api", source = "hex", outer_checksum = "3DFBBFAA2C2ED3121C5C483162836C4F9027DEF469C41578AF5EF32589FCFC58" },
{ name = "pg_types", version = "0.4.0", build_tools = ["rebar3"], requirements = [], otp_app = "pg_types", source = "hex", outer_checksum = "B02EFA785CAECECF9702C681C80A9CA12A39F9161A846CE17B01FB20AEEED7EB" },
{ name = "pgo", version = "0.14.0", build_tools = ["rebar3"], requirements = ["backoff", "opentelemetry_api", "pg_types"], otp_app = "pgo", source = "hex", outer_checksum = "71016C22599936E042DC0012EE4589D24C71427D266292F775EBF201D97DF9C9" },
{ name = "pog", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "pgo"], otp_app = "pog", source = "hex", outer_checksum = "00D57120936AFBF486BE357C472E483C1F0CA507FF9C3668075E87C733CA53F8" },
]

[requirements]
cake = { version = ">= 2.0.0 and < 3.0.0" }
gleam_pgo = { version = ">= 0.14.0 and < 2.0.0" }
gleam_stdlib = { version = ">= 0.40.0 and < 2.0.0" }
pog = { version = ">= 1.0.0 and < 2.0.0" }
59 changes: 38 additions & 21 deletions src/cake/adapter/postgres.gleam
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//// 🎂Cake 🐘PostgreSQL adapter which passes `PreparedStatement`s
//// to the `gleam_pgo` library for execution.
//// to the `pog` library for execution.
////

import cake.{
Expand All @@ -13,11 +13,11 @@ import cake/param.{
import gleam/dynamic.{type DecodeError, type Dynamic}
import gleam/list
import gleam/option.{type Option}
import gleam/pgo.{type Connection, type QueryError, type Returned, type Value}
import pog.{type Connection, type QueryError, type Returned, type Value}

/// Connection to a PostgreSQL database.
///
/// This is a thin wrapper around the `gleam_pgo` library's `Connection` type.
/// This is a thin wrapper around the `pog` library's `Connection` type.
///
pub fn with_connection(
host host: String,
Expand All @@ -28,18 +28,18 @@ pub fn with_connection(
callback callback: fn(Connection) -> a,
) -> a {
let connection =
pgo.Config(
..pgo.default_config(),
pog.Config(
..pog.default_config(),
host: host,
port: port,
user: username,
password: password,
database: database,
)
|> pgo.connect
|> pog.connect

let value = callback(connection)
pgo.disconnect(connection)
pog.disconnect(connection)

value
}
Expand All @@ -63,7 +63,7 @@ pub fn write_query_to_prepared_statement(
pub fn run_read_query(
query query: ReadQuery,
decoder decoder: fn(Dynamic) -> Result(a, List(DecodeError)),
db_connection db_connection: Connection,
db_connection on: Connection,
) {
let prepared_statement = query |> read_query_to_prepared_statement
let sql_string = prepared_statement |> cake.get_sql
Expand All @@ -74,10 +74,13 @@ pub fn run_read_query(

let result =
sql_string
|> pgo.execute(on: db_connection, with: db_params, expecting: decoder)
|> pog.query
|> pog_parameters(db_params:)
|> pog.returning(decoder)
|> pog.execute(on: on)

case result {
Ok(pgo.Returned(_result_count, v)) -> Ok(v)
Ok(pog.Returned(_result_count, v)) -> Ok(v)
Error(e) -> Error(e)
}
}
Expand All @@ -87,7 +90,7 @@ pub fn run_read_query(
pub fn run_write_query(
query query: WriteQuery(a),
decoder decoder: fn(Dynamic) -> Result(a, List(DecodeError)),
db_connection db_connection: Connection,
db_connection on: Connection,
) -> Result(List(a), QueryError) {
let prepared_statement = query |> write_query_to_prepared_statement
let sql_string = prepared_statement |> cake.get_sql
Expand All @@ -98,10 +101,13 @@ pub fn run_write_query(

let result =
sql_string
|> pgo.execute(on: db_connection, with: db_params, expecting: decoder)
|> pog.query
|> pog_parameters(db_params:)
|> pog.returning(decoder)
|> pog.execute(on: on)

case result {
Ok(pgo.Returned(_result_count, v)) -> Ok(v)
Ok(pog.Returned(_result_count, v)) -> Ok(v)
Error(e) -> Error(e)
}
}
Expand All @@ -125,18 +131,29 @@ pub fn run_query(

pub fn execute_raw_sql(
sql_string sql_string: String,
db_connection db_connection: Connection,
) -> Result(Returned(Dynamic), QueryError) {
db_connection on: Connection,
) -> Result(Returned(Nil), QueryError) {
sql_string
|> pgo.execute(on: db_connection, with: [], expecting: dynamic.dynamic)
|> pog.query
|> pog.execute(on:)
}

fn cake_param_to_client_param(param param: Param) -> Value {
case param {
BoolParam(param) -> pgo.bool(param)
FloatParam(param) -> pgo.float(param)
IntParam(param) -> pgo.int(param)
StringParam(param) -> pgo.text(param)
NullParam -> pgo.null()
BoolParam(param) -> pog.bool(param)
FloatParam(param) -> pog.float(param)
IntParam(param) -> pog.int(param)
StringParam(param) -> pog.text(param)
NullParam -> pog.null()
}
}

fn pog_parameters(
pog_query pg_qry: pog.Query(a),
db_params db_params: List(pog.Value),
) -> pog.Query(a) {
db_params
|> list.fold(pg_qry, fn(pg_qry, db_param) {
pg_qry |> pog.parameter(db_param)
})
}
12 changes: 0 additions & 12 deletions src/cake_gleam_pgo.gleam

This file was deleted.

9 changes: 9 additions & 0 deletions src/cake_pog.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import gleam/io

/// As a library *cake_pog* cannot be invoked directly in a meaningful way.
///
@internal
pub fn main() {
{ "\n" <> "cake_pog is an adapter library and cannot be invoked directly." }
|> io.println
}

0 comments on commit 37b7406

Please sign in to comment.