title: Getting started description: Build and run a small StitchQL store from a local checkout. #
First, install the StitchQL CLI. This walkthrough uses the same local checkout and Docker with a running local daemon. Run commands from the checkout's root unless a step changes the directory.
Create a small application #
Save the checkout path, then create a sibling project:
STITCHQL_CHECKOUT="$PWD"
mkdir ../stitchql-quickstart
cd ../stitchql-quickstart
go mod init example.com/stitchql-quickstart
go mod edit -require=nemith.io/stitchql@v0.0.0
go mod edit -replace="nemith.io/stitchql=$STITCHQL_CHECKOUT"
mkdir store
The local replacement makes the CLI and runtime use the same checkout. Add this configuration at the application root:
database = "docker://postgres/18"
StitchQL will create a disposable PostgreSQL server for generation and populate a scratch database from your schema. This database is for analysis; the application will use its own database below.
Define the schema and queries #
:::note Schemas are optional
This walkthrough keeps the schema beside its queries. StitchQL's schema blocks
and fields fragments provide an optional way to maintain composable schema
definitions in the same package. You can also maintain your schema outside
StitchQL and generate against an existing database.
:::
package store
schema {
CREATE TABLE books (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
title text NOT NULL
);
}
columns bookColumns[b books] {
b.id,
b.title
}
predicate ByTitle[b books](title string) {
b.title = {title}
}
query CreateBook(title string) {
INSERT INTO books (title)
VALUES ({title})
RETURNING id, title
}
query ListBooks(filter predicate[books]) {
SELECT {bookColumns[b]}
FROM books AS b
WHERE {filter[b]}
ORDER BY b.id
}
[b books] binds a source parameter to the books table. At a fragment call,
the brackets supply a SQL alias. Parentheses supply runtime Go values. Read more
about this distinction in Language basics.
Generate Go #
go mod download nemith.io/stitchql
stitchql lint ./store
stitchql generate ./store
go mod tidy
go build ./store
The first generation may pull the PostgreSQL image. It produces
store/books_gen.go, including the result type:
type ListBooksResult struct {
ID int64
Title string
}
Both fields are non-nullable because the schema guarantees that. You can inspect rendered SQL without a database call:
stmt, err := store.ListBooks(store.ByTitle("Kindred")).SQL()
stmt.SQL contains placeholders; stmt.Args contains the captured values.
Run against an application database #
Create a local demo database. Choose another host port if 55432 is already used. The credentials below are only for this loopback-bound demo.
docker run --detach --rm --name stitchql-quickstart \
-e POSTGRES_PASSWORD=stitchql \
-e POSTGRES_DB=library \
-p 127.0.0.1:55432:5432 postgres:18
until docker exec stitchql-quickstart pg_isready -U postgres -d library; do
sleep 1
done
stitchql schema generate ./store > schema.sql
docker exec -i stitchql-quickstart psql -U postgres -d library \
-v ON_ERROR_STOP=1 < schema.sql
Create the application:
package main
import (
"context"
"fmt"
"log/slog"
"os"
"example.com/stitchql-quickstart/store"
"github.com/jackc/pgx/v5/pgxpool"
)
func main() {
if err := run(context.Background()); err != nil {
slog.Error("run application", "error", err)
os.Exit(1)
}
}
func run(ctx context.Context) error {
pool, err := pgxpool.New(ctx, os.Getenv("APP_DATABASE_URL"))
if err != nil {
return err
}
defer pool.Close()
created, err := store.CreateBook("Kindred").SelectOne(ctx, pool)
if err != nil {
return err
}
books, err := store.ListBooks(store.ByTitle(created.Title)).Select(ctx, pool)
if err != nil {
return err
}
for _, book := range books {
fmt.Printf("%d: %s\n", book.ID, book.Title)
}
return nil
}
go mod tidy
APP_DATABASE_URL='postgres://postgres:stitchql@localhost:55432/library?sslmode=disable' go run .
On a fresh database, this prints 1: Kindred. Running it again inserts another
book. APP_DATABASE_URL belongs to this application; StitchQL's analysis settings
remain in stitchql.toml.
Stop and remove the demo server when finished:
docker stop stitchql-quickstart
Keep generated files current #
After editing your schema, queries, or fragments:
stitchql lint ./store
stitchql generate ./store
go build ./...
Use stitchql generate --check ./store to detect stale output without writing
files. Use stitchql fmt --check ./store if the project adopts the canonical
formatter. For a larger working application, try the library example.