🎂 Cake SQL Query Builder for Gleam

DownloadsErlang-compatibleJavaScript CompatibleHex DocsPackage VersionCI TestLicenseDiscord



Cake Logo


🎂Cake is a library written in Gleam to compose SQL queries targeting SQL dialects 🐘PostgreSQL, 🪶SQLite, 🦭MariaDB and 🐬MySQL.



Installation

gleam add cake@4

Further documentation can be found on hexdocs.pm/cake.

Adapters

Usage

Demos

See examples/README.md.

Code examples (from the demos)

Unit tests as examples

See Cake's tests, especially the Setup sections in each test module.

You may also compare the tests with the snapshots results.

Intended aliases

Use the following aliases to make the library more ergonomic:

import cake/select as s // SELECT statements
import cake/where as w // WHERE clauses
import cake/join as j // JOIN clauses
import cake/update as u // UPDATE statements
import cake/insert as i // INSERT statements
import cake/delete as d // DELETE statements
import cake/combined as c // For combined queries such as UNION
import cake/fragment as f // For arbitrary SQL code including functions
import cake/param as p // Typed params

Library Design

CakeQuery Type Hierarchy

The following diagram shows the type hierarchy of CakeQuery and its constructors:

graph TD
CakeQuery["CakeQuery(a)"]:::accent0
CakeQuery --> CakeReadQuery["CakeReadQuery(ReadQuery)"]:::accent1
CakeQuery --> CakeWriteQuery["CakeWriteQuery(WriteQuery(a))"]:::accent2
CakeReadQuery --> SelectQuery["SelectQuery(Select)"]:::accent3
CakeReadQuery --> CombinedQuery["CombinedQuery(Combined)"]:::accent3
CakeWriteQuery --> InsertQuery["InsertQuery(Insert(a))"]:::accent4
CakeWriteQuery --> UpdateQuery["UpdateQuery(Update(a))"]:::accent4
CakeWriteQuery --> DeleteQuery["DeleteQuery(Delete(a))"]:::accent4
SelectQuery --> Select["Select<br/>• SelectKind<br/>• Selects<br/>• From<br/>• Joins<br/>• Where<br/>• GroupBy<br/>• Having<br/>• OrderBy<br/>• Limit<br/>• Offset<br/>• Epilog<br/>• Comment"]:::accent5
CombinedQuery --> Combined["Combined<br/>• CombinedQueryKind<br/>• queries: List(Select)<br/>• OrderBy<br/>• Limit<br/>• Offset<br/>• Epilog<br/>• Comment"]:::accent5
InsertQuery --> Insert["Insert(a)<br/>• InsertIntoTable<br/>• InsertColumns<br/>• InsertModifier<br/>• InsertSource(a)<br/>• InsertConflictStrategy(a)<br/>• Returning<br/>• Epilog<br/>• Comment"]:::accent6
UpdateQuery --> Update["Update(a)<br/>• UpdateTable<br/>• UpdateModifier<br/>• UpdateSets<br/>• From<br/>• Joins<br/>• Where<br/>• Returning<br/>• Epilog<br/>• Comment"]:::accent6
DeleteQuery --> Delete["Delete(a)<br/>• DeleteModifier<br/>• DeleteTable<br/>• DeleteUsing<br/>• Joins<br/>• Where<br/>• Returning<br/>• Epilog<br/>• Comment"]:::accent6

Legend:

Scope

This is an SQL query building library, thus it is not concerned about executing queries or decoding return values from queries, but merely about being a flexible and powerful tool to compose and craft SQL read and write queries.

Goals

Non-goals

Tested targets

The tests run on Erlang but are generally target agnostic.

While the primary use case is to run queries on servers, this library runs on any Gleam target and for example in conjunction with following adapters, you may run queries composed with this library in browsers:

Development

Run test suite locally

bin/docker/attached
# wait a few seconds until everything is ready
# if you run gleam test too early, it will crash
gleam test

Helper commands

bin/docker/attached
bin/docker/detached
bin/docker/down
bin/test
bin/birdie/interactive-review
bin/birdie/accept-all
bin/birdie/reject-all