Skip to content

Commit

Permalink
fragment demo
Browse files Browse the repository at this point in the history
  • Loading branch information
inoas committed Jul 23, 2024
1 parent 175f003 commit 76dc688
Show file tree
Hide file tree
Showing 56 changed files with 284 additions and 56 deletions.
43 changes: 18 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,41 +20,34 @@ gleam add cake

Further documentation can be found on [hexdocs.pm/cake](https://hexdocs.pm/cake).

## Code examples

See Cake's [tests](https://github.com/inoas/gleam-cake/tree/main/test/cake_test), especially the _Setup_ sections in each test module.

You may also compare the tests with the [snapshots](https://github.com/inoas/gleam-cake/tree/main/birdie_snapshots) results.
## Usage

### Demo apps
### Demos

See [docs/demo-apps/README.md](https://github.com/inoas/gleam-cake/blob/main/docs/demo-apps/README.md#available-demos).

## Usage
### Code examples

- SELECT and decoding -
[cake\_demo\_select\_and\_decode.gleam](https://github.com/inoas/gleam-cake/blob/main/docs/demo-apps/demos/01_demo_select_and_decode/src/cake_demo_select_and_decode.gleam)
- UNION and decoding -
[cake\_demo\_union\_and\_decode.gleam](https://github.com/inoas/gleam-cake/blob/main/docs/demo-apps/demos/02_demo_union_and_decode/src/cake_demo_union_and_decode.gleam)
- INSERT -
[cake\_demo\_insert.gleam](https://github.com/inoas/gleam-cake/blob/main/docs/demo-apps/demos/03_demo_insert/src/cake_demo_insert.gleam)
- DELETE -
[cake\_demo\_delete.gleam](https://github.com/inoas/gleam-cake/blob/main/docs/demo-apps/demos/04_demo_delete/src/cake_demo_delete.gleam)
- UPDATE -
[cake\_demo\_update.gleam](https://github.com/inoas/gleam-cake/blob/main/docs/demo-apps/demos/05_demo_update/src/cake_demo_update.gleam)
- INSERT -
[cake\_demo\_insert\_on\_conflict\_update.gleam](https://github.com/inoas/gleam-cake/blob/main/docs/demo-apps/demos/06_demo_insert_on_conflict_update/src/cake_demo_insert_on_conflict_update.gleam)
- ... more to come soon!
- [cake\_demo\_select\_and\_decode.gleam](https://github.com/inoas/gleam-cake/blob/main/docs/demo-apps/demos/01_select_and_decode/src/cake_demo_select_and_decode.gleam)
- [cake\_demo\_union\_and\_decode.gleam](https://github.com/inoas/gleam-cake/blob/main/docs/demo-apps/demos/02_union_and_decode/src/cake_demo_union_and_decode.gleam)
- [cake\_demo\_insert.gleam](https://github.com/inoas/gleam-cake/blob/main/docs/demo-apps/demos/03_demo_insert/src/cake_insert.gleam)
- [cake\_demo\_delete.gleam](https://github.com/inoas/gleam-cake/blob/main/docs/demo-apps/demos/04_demo_delete/src/cake_delete.gleam)
- [cake\_demo\_update.gleam](https://github.com/inoas/gleam-cake/blob/main/docs/demo-apps/demos/05_demo_update/src/cake_update.gleam)
- [cake\_demo\_insert\_on\_conflict\_update.gleam](https://github.com/inoas/gleam-cake/blob/main/docs/demo-apps/demos/06_insert_on_conflict_update/src/cake_demo_insert_on_conflict_update.gleam)
- [cake\_demo\_select\_join.gleam](https://github.com/inoas/gleam-cake/blob/main/docs/demo-apps/demos/07_select_join/src/cake_demo_select_join.gleam)
- [cake\_demo\_prepared\_fragment.gleam](https://github.com/inoas/gleam-cake/blob/main/docs/demo-apps/demos/08_prepared_fragment/src/cake_demo_prepared_fragment.gleam)
<!--
- INNER JOIN -- TODO v1
- LEFT JOIN -- TODO v1
- literal fragment -- TODO v1
- prepared fragment -- TODO v1
- transactions -- TODO v2
- create view -- TODO v3
-->

### Tips
### Unit tests as examples

See Cake's [tests](https://github.com/inoas/gleam-cake/tree/main/test/cake_test), especially the _Setup_ sections in each test module.

You may also compare the tests with the [snapshots](https://github.com/inoas/gleam-cake/tree/main/birdie_snapshots) results.

### Intended aliases

Use the following aliases to make the library more ergonomic:

Expand Down
3 changes: 3 additions & 0 deletions docs/demo-apps/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
- [`INSERT`](./demos/03_demo_insert/README.md)
- [`DELETE`](./demos/04_demo_delete/README.md)
- [`UPDATE`](./demos/05_demo_update/README.md)
- [`INSERT ON CONFLICT UPDATE`](./demos/06_cake_demo_insert_on_conflict_update/README.md)
- [`JOIN`](./demos/07_cake_demo_join/README.md)
- [`prepared fragment`](./demos/08_cake_demo_prepared_fragment/README.md)

## Running local demo apps

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ gleam run

## Demo source code

See the [src directory](https://github.com/inoas/gleam-cake/blob/main/docs/demo-apps/demos/01_demo_select_and_decode/src/).
See the [src directory](https://github.com/inoas/gleam-cake/blob/main/docs/demo-apps/demos/01_select_and_decode/src/).

---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ gleam run

## Demo source code

See the [src directory](https://github.com/inoas/gleam-cake/blob/main/docs/demo-apps/demos/02_demo_union_and_decode/src/).
See the [src directory](https://github.com/inoas/gleam-cake/blob/main/docs/demo-apps/demos/02_union_and_decode/src/).

---

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ gleam run

## Demo source code

See the [src directory](https://github.com/inoas/gleam-cake/blob/main/docs/demo-apps/demos/03_demo_insert/src/).
See the [src directory](https://github.com/inoas/gleam-cake/blob/main/docs/demo-apps/demos/03_insert/src/).

---

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,29 +1,18 @@
import cake/insert as i
import cake/update as u
import cake/where as w
import helper/demo_data
import helper/postgres
import gleam/dynamic
import gleam/io
import helper/demo_data
import helper/postgres
import pprint

fn update() {
u.new() |> u.sets(["counter" |> u.set_expression("counters.counter + 1")])
}

fn insert_query() {
[
[i.string("Whiskers"), i.int(1)] |> i.row,
[i.string("Karl"), i.int(1)] |> i.row,
[i.string("Clara"), i.int(1)] |> i.row,
[i.string("Karl"), i.int(2)] |> i.row,
[i.string("Clara"), i.int(3)] |> i.row,
]
|> i.from_values(table_name: "counters", columns: ["name", "counter"])
|> i.on_columns_conflict_update(
columns: ["name"],
where: w.col("counters.is_active") |> w.is_true,
update: update(),
)
|> i.returning(["name", "counter"])
|> i.from_values(table_name: "cats", columns: ["name", "age"])
|> i.returning(["name", "age"])
|> i.to_query
}

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ gleam run

## Demo source code

See the [src directory](https://github.com/inoas/gleam-cake/blob/main/docs/demo-apps/demos/04_demo_delete/src/).
See the [src directory](https://github.com/inoas/gleam-cake/blob/main/docs/demo-apps/demos/04_delete/src/).

---

Expand Down
File renamed without changes.
File renamed without changes.
19 changes: 19 additions & 0 deletions docs/demo-apps/demos/05_update/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Cake demo app: UPDATE

This demo of Cake shows how to `UPDATE`.

To start postgres via docker compose, see
[docs/demo-apps/README.md](../../README.md#Installing-prerequisites), then:

```shell
gleam clean
gleam run
```

## Demo source code

See the [src directory](https://github.com/inoas/gleam-cake/blob/main/docs/demo-apps/demos/05_update/src/).

---

For further demos see [docs/demo-apps/README.md](../../README.md#available-demos).
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ gleam run

## Demo source code

See the [src directory](https://github.com/inoas/gleam-cake/blob/main/docs/demo-apps/demos/06_cake_demo_insert_on_conflict_update/src/).
See the [src directory](https://github.com/inoas/gleam-cake/blob/main/docs/demo-apps/demos/06_insert_on_conflict_update/src/).

---

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name = "cake_demo_update"
name = "cake_demo_insert_on_conflict_update"
description = "An demo of using Gleam with Cake and Postgres to run INSERT ON CONFLICT UPDATE queries"

[dependencies]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import cake/delete as d
import cake/insert as i
import cake/update as u
import cake/where as w
Expand Down
4 changes: 4 additions & 0 deletions docs/demo-apps/demos/07_select_join/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.beam
*.ez
/build
erl_crash.dump
5 changes: 5 additions & 0 deletions docs/demo-apps/demos/07_select_join/.tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# erlang 26.2.5.1
erlang 27.0
gleam 1.3.2
# erlang 26.2.5.1
# gleam nightly
20 changes: 20 additions & 0 deletions docs/demo-apps/demos/07_select_join/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Cake demo app: SELECT and decode

This demo of Cake shows how to `SELECT` and decode returning rows into
Gleam records.

To start postgres via docker compose, see
[docs/demo-apps/README.md](../../README.md#Installing-prerequisites), then:

```shell
gleam clean
gleam run
```

## Demo source code

See the [src directory](https://github.com/inoas/gleam-cake/blob/main/docs/demo-apps/demos/07_select_join/src/).

---

For further demos see [docs/demo-apps/README.md](../../README.md#available-demos).
9 changes: 9 additions & 0 deletions docs/demo-apps/demos/07_select_join/gleam.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name = "cake_demo_select_join"
description = "An demo of using Gleam with Cake and Postgres to run SELECT and JOIN queries and decode returning data into Gleam records"

[dependencies]
cake = { path = "../../../../" }
helper = { path = "../../helper" }
gleam_pgo = ">= 0.13.0 and < 2.0.0"
gleam_stdlib = ">= 0.39.0 and < 2.0.0"
pprint = ">= 1.0.3 and < 2.0.0"
23 changes: 23 additions & 0 deletions docs/demo-apps/demos/07_select_join/manifest.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# This file was generated by Gleam
# You typically do not need to edit this file

packages = [
{ name = "backoff", version = "1.1.6", build_tools = ["rebar3"], requirements = [], otp_app = "backoff", source = "hex", outer_checksum = "CF0CFFF8995FB20562F822E5CC47D8CCF664C5ECDC26A684CBE85C225F9D7C39" },
{ name = "cake", version = "0.15.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], source = "local", path = "../../../.." },
{ name = "helper", version = "1.0.0", build_tools = ["gleam"], requirements = ["cake", "gleam_pgo", "gleam_stdlib", "pprint"], source = "local", path = "../../helper" },
{ name = "glam", version = "2.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "glam", source = "hex", outer_checksum = "66EC3BCD632E51EED029678F8DF419659C1E57B1A93D874C5131FE220DFAD2B2" },
{ name = "gleam_pgo", version = "0.13.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "pgo"], otp_app = "gleam_pgo", source = "hex", outer_checksum = "6A1E7F3E717C077788254871E4EF4A8DFF58FEC07D7FA7C7702C2CCF66095AC8" },
{ name = "gleam_stdlib", version = "0.39.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "2D7DE885A6EA7F1D5015D1698920C9BAF7241102836CE0C3837A4F160128A9C4" },
{ name = "opentelemetry_api", version = "1.3.0", build_tools = ["rebar3", "mix"], requirements = ["opentelemetry_semantic_conventions"], otp_app = "opentelemetry_api", source = "hex", outer_checksum = "B9E5FF775FD064FA098DBA3C398490B77649A352B40B0B730A6B7DC0BDD68858" },
{ name = "opentelemetry_semantic_conventions", version = "0.2.0", build_tools = ["rebar3", "mix"], requirements = [], otp_app = "opentelemetry_semantic_conventions", source = "hex", outer_checksum = "D61FA1F5639EE8668D74B527E6806E0503EFC55A42DB7B5F39939D84C07D6895" },
{ 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 = "pprint", version = "1.0.3", build_tools = ["gleam"], requirements = ["glam", "gleam_stdlib"], otp_app = "pprint", source = "hex", outer_checksum = "76BBB92E23D12D954BD452686543F29EDE8EBEBB7FC0ACCBCA66EEF276EC3A06" },
]

[requirements]
cake = { path = "../../../../" }
helper = { path = "../../helper" }
gleam_pgo = { version = ">= 0.13.0 and < 2.0.0" }
gleam_stdlib = { version = ">= 0.39.0 and < 2.0.0" }
pprint = { version = ">= 1.0.3 and < 2.0.0" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import cake/join as j
import cake/select as s
import cake/where as w
import gleam/dynamic
import gleam/io
import helper/demo_data
import helper/postgres
import pprint

fn select_join_query() {
s.new()
|> s.selects([s.col("owners.name"), s.col("dogs.name")])
|> s.from_table("owners")
|> s.join(j.left(
with: j.table("dogs"),
on: w.col("owners.id") |> w.eq(w.col("dogs.owner_id")),
alias: "dogs",
))
|> s.to_query
}

pub fn main() {
use conn <- postgres.with_connection
demo_data.create_tables_and_insert_rows(conn)

// NOTICE: This will crash, if the SQL query fails.
let result =
select_join_query() |> postgres.run_read_query(dynamic.dynamic, conn)

io.println("Result: ")

result
|> pprint.debug
}
4 changes: 4 additions & 0 deletions docs/demo-apps/demos/08_prepared_fragment/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.beam
*.ez
/build
erl_crash.dump
4 changes: 4 additions & 0 deletions docs/demo-apps/demos/08_prepared_fragment/.tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# erlang 26.2.5.1
erlang 27.0
gleam 1.3.2
# gleam nightly
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Cake demo app: INSERT

This demo of Cake shows how to `INSERT`.
This demo of Cake shows how to `INSERT ON CONFLICT UPDATE`.

To start postgres via docker compose, see
[docs/demo-apps/README.md](../../README.md#Installing-prerequisites), then:
Expand All @@ -12,7 +12,7 @@ gleam run

## Demo source code

See the [src directory](https://github.com/inoas/gleam-cake/blob/main/docs/demo-apps/demos/05_demo_update/src/).
See the [src directory](https://github.com/inoas/gleam-cake/blob/main/docs/demo-apps/demos/08_prepared_fragment/src/).

---

Expand Down
9 changes: 9 additions & 0 deletions docs/demo-apps/demos/08_prepared_fragment/gleam.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name = "cake_demo_prepared_fragment"
description = "An demo of using Gleam with Cake and Postgres to run queries with fragments including prepared statements"

[dependencies]
cake = { path = "../../../../" }
helper = { path = "../../helper" }
gleam_pgo = ">= 0.13.0 and < 2.0.0"
gleam_stdlib = ">= 0.39.0 and < 2.0.0"
pprint = ">= 1.0.3 and < 2.0.0"
23 changes: 23 additions & 0 deletions docs/demo-apps/demos/08_prepared_fragment/manifest.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# This file was generated by Gleam
# You typically do not need to edit this file

packages = [
{ name = "backoff", version = "1.1.6", build_tools = ["rebar3"], requirements = [], otp_app = "backoff", source = "hex", outer_checksum = "CF0CFFF8995FB20562F822E5CC47D8CCF664C5ECDC26A684CBE85C225F9D7C39" },
{ name = "cake", version = "0.15.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], source = "local", path = "../../../.." },
{ name = "helper", version = "1.0.0", build_tools = ["gleam"], requirements = ["cake", "gleam_pgo", "gleam_stdlib", "pprint"], source = "local", path = "../../helper" },
{ name = "glam", version = "2.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "glam", source = "hex", outer_checksum = "66EC3BCD632E51EED029678F8DF419659C1E57B1A93D874C5131FE220DFAD2B2" },
{ name = "gleam_pgo", version = "0.13.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "pgo"], otp_app = "gleam_pgo", source = "hex", outer_checksum = "6A1E7F3E717C077788254871E4EF4A8DFF58FEC07D7FA7C7702C2CCF66095AC8" },
{ name = "gleam_stdlib", version = "0.39.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "2D7DE885A6EA7F1D5015D1698920C9BAF7241102836CE0C3837A4F160128A9C4" },
{ name = "opentelemetry_api", version = "1.3.0", build_tools = ["rebar3", "mix"], requirements = ["opentelemetry_semantic_conventions"], otp_app = "opentelemetry_api", source = "hex", outer_checksum = "B9E5FF775FD064FA098DBA3C398490B77649A352B40B0B730A6B7DC0BDD68858" },
{ name = "opentelemetry_semantic_conventions", version = "0.2.0", build_tools = ["rebar3", "mix"], requirements = [], otp_app = "opentelemetry_semantic_conventions", source = "hex", outer_checksum = "D61FA1F5639EE8668D74B527E6806E0503EFC55A42DB7B5F39939D84C07D6895" },
{ 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 = "pprint", version = "1.0.3", build_tools = ["gleam"], requirements = ["glam", "gleam_stdlib"], otp_app = "pprint", source = "hex", outer_checksum = "76BBB92E23D12D954BD452686543F29EDE8EBEBB7FC0ACCBCA66EEF276EC3A06" },
]

[requirements]
cake = { path = "../../../../" }
helper = { path = "../../helper" }
gleam_pgo = { version = ">= 0.13.0 and < 2.0.0" }
gleam_stdlib = { version = ">= 0.39.0 and < 2.0.0" }
pprint = { version = ">= 1.0.3 and < 2.0.0" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import cake/fragment as f
import cake/select as s
import cake/where as w
import gleam/dynamic
import gleam/io
import helper/demo_data
import helper/postgres
import pprint

fn fragment_query() {
s.new()
|> s.from_table("cats")
|> s.where(
w.fragment_value(f.literal("LOWER(cats.name)"))
|> w.eq(
w.fragment_value(
f.prepared("LOWER(" <> f.placeholder <> ")", [f.string("cLaRa")]),
),
),
)
|> s.to_query
}

pub fn main() {
use conn <- postgres.with_connection
demo_data.create_tables_and_insert_rows(conn)

// NOTICE: This will crash, if the SQL query fails.
let result =
fragment_query()
|> postgres.run_read_query(dynamic.dynamic, conn)

io.println("Result: ")

result
|> pprint.debug
}
3 changes: 2 additions & 1 deletion docs/demo-apps/helper/src/helper/demo_data.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ fn insert_owners_rows() {
"INSERT INTO owners (id, name, last_name, age) VALUES
(1, 'Alice', 'Wibble', 5),
(2, 'Bob', 'Wobble', 8),
(3, 'Charlie', 'Wabble', 13)
(3, 'Charlie', 'Wabble', 13),
(4, 'Robot', 'User', 999)
;"
}

Expand Down
Loading

0 comments on commit 76dc688

Please sign in to comment.