Current section
Files
Jump to
Current section
Files
README.md
# sqlode
[](https://github.com/nao1215/sqlode/actions/workflows/ci.yml)
[](https://github.com/nao1215/sqlode/actions/workflows/gitleaks.yml)
[](https://github.com/nao1215/sqlode/actions/workflows/trivy.yml)
[](https://github.com/nao1215/sqlode/actions/workflows/osv-scan.yml)
[](https://github.com/nao1215/atago)
[](https://github.com/nao1215/himorime)
[](https://hex.pm/packages/sqlode)
[](https://hexdocs.pm/sqlode/)
[](https://hex.pm/packages/sqlode)
[](https://github.com/nao1215/sqlode/releases)
[](https://github.com/nao1215/sqlode/blob/main/LICENSE)
[](https://scorecard.dev/viewer/?uri=github.com/nao1215/sqlode)
sqlode reads SQL schema and query files and generates typed Gleam code: write SQL, run the generator, call the generated functions. The workflow follows [sqlc](https://sqlc.dev/), and sqlode reads an existing `sqlc.yaml`, but its macros use the `sqlode.*` prefix.
It supports PostgreSQL ([pog](https://hexdocs.pm/pog/)), MySQL 8.0 ([shork](https://hexdocs.pm/shork/)) and SQLite ([sqlight](https://hexdocs.pm/sqlight/)).
## Quickstart
```console
gleam new myapp
cd myapp
gleam add sqlode sqlight
gleam run -m sqlode -- init --engine=sqlite --runtime=native
```
`init` writes `db/schema.sql`, `db/query.sql` and this `sqlode.yaml`:
```yaml
version: "2"
sql:
- schema: "db/schema.sql"
queries: "db/query.sql"
engine: "sqlite"
gen:
gleam:
out: "src/db"
runtime: "native"
```
Put your schema and queries in the two SQL files:
```sql
-- db/schema.sql
CREATE TABLE authors (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
bio TEXT
);
-- db/query.sql
-- name: GetAuthor :one
SELECT id, name, bio FROM authors WHERE id = ?;
-- name: ListAuthors :many
SELECT id, name FROM authors ORDER BY name;
-- name: CreateAuthor :exec
INSERT INTO authors (name, bio) VALUES (sqlode.arg(name), sqlode.narg(bio));
```
```console
$ gleam run -m sqlode -- generate
Loading config from: sqlode.yaml
Successfully generated 4 files
Generated: src/db/params.gleam
Generated: src/db/queries.gleam
Generated: src/db/models.gleam
Generated: src/db/sqlight_adapter.gleam
Formatted generated code
```
The adapter binds parameters, runs the query and decodes the rows:
```gleam
import db/params
import db/sqlight_adapter
import gleam/io
import gleam/option.{Some}
import sqlight
pub fn main() {
let assert Ok(db) = sqlight.open(":memory:")
let assert Ok(_) =
sqlight.exec(
"CREATE TABLE authors (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, bio TEXT);",
db,
)
let assert Ok(_) =
sqlight_adapter.create_author(
db,
params.CreateAuthorParams(name: "Alice", bio: Some("Poet")),
)
let assert Ok(Some(author)) =
sqlight_adapter.get_author(db, params.GetAuthorParams(id: 1))
io.println(author.name)
// -> Alice
}
```
With `runtime: "raw"` (the default) sqlode skips the adapter: `queries.prepare_get_author(id: 1)` returns the SQL and the parameter values for any driver, and `queries.get_author_decoder()` decodes a row. [`examples/sqlite-basic`](https://github.com/nao1215/sqlode/tree/main/examples/sqlite-basic) is a runnable project.
## Writing queries
- Each query starts with `-- name: Name :command`, where the command is `:one`, `:many`, `:exec`, `:execrows`, `:execlastid` or `:execresult` (raw only).
- `sqlode.arg(name)` and `sqlode.narg(name)` name a parameter and a nullable parameter. `@name` is a shorthand on PostgreSQL and SQLite.
- `IN (sqlode.slice(ids))` takes a `List` and binds one placeholder per element. An empty list matches no rows.
- `SELECT sqlode.embed(authors), books.title` returns the table's columns as one nested `Author` field.
- `-- sqlode:skip` before the annotation leaves a query out of generation.
Parameter and column types come from the schema. Where sqlode cannot infer a parameter type, add a cast such as `$1::int`. The [capabilities](https://hexdocs.pm/sqlode/capabilities.html) page lists every engine, annotation and macro, and [configuration](https://hexdocs.pm/sqlode/configuration.html) covers type overrides, the type mapping and the YAML keys.
## Command line
```console
sqlode init [--output=sqlode.yaml] [--engine=postgresql|sqlite|mysql] [--runtime=raw|native]
sqlode generate [--config=sqlode.yaml]
sqlode verify [--config=sqlode.yaml]
sqlode version
```
`verify` checks the project like `generate` without writing files and reports every problem. Errors start with a stable code such as `SQD4003`, explained on the [errors](https://hexdocs.pm/sqlode/errors.html) page.
The command runs as `gleam run -m sqlode --`, as a standalone escript from [GitHub Releases](https://github.com/nao1215/sqlode/releases) (needs Erlang/OTP), or with Docker:
```console
curl -fsSL https://raw.githubusercontent.com/nao1215/sqlode/main/scripts/install.sh | sh
docker run --rm -v "$PWD:/work" ghcr.io/nao1215/sqlode:latest generate
```
Generated code imports `sqlode/runtime`, so keep sqlode as a regular dependency, or set `vendor_runtime: true` to copy the runtime into the output directory.
## Verifying releases
Each release carries `SHA256SUMS` for the escript, its CycloneDX SBOM and the bundled third-party licenses, with a cosign keyless signature and SLSA provenance. With the release files in the current directory:
```console
sha256sum -c SHA256SUMS
cosign verify-blob --bundle SHA256SUMS.sigstore.json \
--certificate-identity-regexp '^https://github.com/nao1215/sqlode/' \
--certificate-oidc-issuer https://token.actions.githubusercontent.com \
SHA256SUMS
gh attestation verify sqlode --repo nao1215/sqlode
```
The Docker image is signed by digest: `cosign verify ghcr.io/nao1215/sqlode:0.36.0 --certificate-identity-regexp '^https://github.com/nao1215/sqlode/' --certificate-oidc-issuer https://token.actions.githubusercontent.com`.
## License
[MIT](https://github.com/nao1215/sqlode/blob/main/LICENSE)