examples/

directory
v0.11.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 13, 2026 License: Apache-2.0

README

Momo examples

Small programs that each show one task the way Momo intends it to be done. Read one, copy the parts you need. Every example is compiled, and every example that talks to a database is run end to end in CI, so the code here is code that works.

Example Shows
quickstart The whole loop of the root README: models, generate, insert, select, relations, vector search, a Tx.
crud-zero-null Insert, select, update, delete; writing a zero value versus NULL versus the default; soft delete.
transactions db.Tx, rollback on error, savepoints, read-only and isolation options, retry on serialization.
testing momotest: a database or a rolled-back transaction per test with the migrations applied, seeded fixtures, parallel tests.
relations has-many, belongs-to, many-to-many; Load, Preload, Joined, Has/HasNo, Link/Replace, CreateGraph.
projection-pagination Reading a subset of columns into a struct, keyset pagination with a tie-break, counting.
vector-search A vector column, an HNSW index in a migration, nearest-neighbour queries, filters, search settings.
upgrade-database change a model after production: add a column with a default, add an index, rename a column, apply with data kept, generate --check and drift.
custom-sql-migration hand-written SQL migrations beside generated ones: backfill, DO $$ splitting, -- momo:no-transaction, CONCURRENTLY workaround, momo.sum checksums.
adopt-existing-schema put a plain-SQL database under Momo with migrate baseline (no model generation from a database in v0.11.0).

Layout of an example

Each examples/<name>/ is a self-contained program inside the Momo module:

examples/<name>/
  README.md        what it shows and how to run it
  momo.yaml        generator and migration settings
  main.go          package main: main, start (opens the database), run (the steps)
  models/          the schema; models.go is yours, momo_gen.go and <model>/ are generated
  migrations/      written by `momo migrate diff`, applied by `momo migrate apply`
  example_test.go  runs the example end to end (see below)

The generated code and the migrations are committed, like in a real project, and are produced by the real tools: momo generate and momo migrate diff init run in the example directory. Do not edit them by hand.

main.go keeps main and start trivial and puts the steps in run(ctx, db, out), which writes what it finds to out. A step is a function named for what it shows, with a comment that says why it is done this way. The printed lines are the contract: the test compares them.

How an example is tested, and how CI runs it

example_test.go is built with //go:build integration and calls the harness in internal/exampletest:

func TestExample(t *testing.T) {
	exampletest.CheckGenerated(t)               // momo generate --check: generated code is current
	db := exampletest.Open(t)                         // scratch database + the example's migrations applied
	var out bytes.Buffer
	if err := run(context.Background(), db, &out); err != nil {
		t.Fatal(err)
	}
	// compare out.String() with the expected text
}

Open creates a scratch database (testdb.ScratchDatabase), so an example may create the vector extension and any tables without touching the shared test database, and drops it when the test ends. It skips the test when MOMO_TEST_POSTGRES_URL is unset and fails it when MOMO_REQUIRE_INTEGRATION=1.

CI needs no per-example entry. The integration job runs go test -tags integration ./..., which includes every examples/*/ package, and the unit and lint jobs compile and vet each one. An example whose test needs no database (like quickstart) is an ordinary _test.go that the unit job runs.

TestExamplesAreWired in internal/repocheck enforces the convention: every example has a main.go, a README.md and a test, a test that uses the harness carries the integration tag, this index and docs/README.md link the example, and the integration job still runs every package.

Adding an example

  1. Create examples/<name>/ with momo.yaml and models/models.go.
  2. Run momo generate and momo migrate diff init in it. The repository formatter rewrites the *.snapshot.json file; if it does, run momo migrate hash afterwards so momo.sum matches the formatted file.
  3. Write main.go and example_test.go as above. Run the test once, read the output, and paste it in as the expected text only when it is right.
  4. Write README.md: what it shows, and the run command.
  5. Add a row to the table above and a line to the Examples list in docs/README.md.

To run an example by hand against your own database:

cd examples/<name>
export DATABASE_URL=postgres://user:pass@localhost:5432/scratch
momo migrate apply
go run .

Directories

Path Synopsis
Command adopt-existing-schema reads a database that began as plain SQL (legacy.sql) and was then adopted with `momo migrate baseline`.
Command adopt-existing-schema reads a database that began as plain SQL (legacy.sql) and was then adopted with `momo migrate baseline`.
models
Package models describes a schema that already exists in a database.
Package models describes a schema that already exists in a database.
models/order
Package order holds typed column descriptors and relation descriptors for the Order model.
Package order holds typed column descriptors and relation descriptors for the Order model.
models/user
Package user holds typed column descriptors and relation descriptors for the User model.
Package user holds typed column descriptors and relation descriptors for the User model.
Command crud-zero-null shows insert, select, update and delete, and above all how Momo tells a zero value from NULL from "leave it alone".
Command crud-zero-null shows insert, select, update and delete, and above all how Momo tells a zero value from NULL from "leave it alone".
models
Package models is the schema of the crud-zero-null example: one table of tasks that shows how a write tells zero from NULL from "not mentioned".
Package models is the schema of the crud-zero-null example: one table of tasks that shows how a write tells zero from NULL from "not mentioned".
models/task
Package task holds typed column descriptors and relation descriptors for the Task model.
Package task holds typed column descriptors and relation descriptors for the Task model.
Command custom-sql-migration looks accounts up by email, ignoring case, on a database whose migrations (generated and hand-written) are applied.
Command custom-sql-migration looks accounts up by email, ignoring case, on a database whose migrations (generated and hand-written) are applied.
models
Package models is the schema of the custom-sql-migration example.
Package models is the schema of the custom-sql-migration example.
models/account
Package account holds typed column descriptors and relation descriptors for the Account model.
Package account holds typed column descriptors and relation descriptors for the Account model.
internal
exampletest
Package exampletest is the harness every examples/<name> test uses.
Package exampletest is the harness every examples/<name> test uses.
Command projection-pagination shows how to read only the columns a screen needs, page through a large table with a keyset cursor, and count.
Command projection-pagination shows how to read only the columns a screen needs, page through a large table with a keyset cursor, and count.
models
Package models is the schema of the projection-pagination example: a table of articles, plus two small result types that read only some of its columns.
Package models is the schema of the projection-pagination example: a table of articles, plus two small result types that read only some of its columns.
models/article
Package article holds typed column descriptors and relation descriptors for the Article model.
Package article holds typed column descriptors and relation descriptors for the Article model.
Command quickstart is the compiled source of the snippets in the root README.
Command quickstart is the compiled source of the snippets in the root README.
models
Package models is the schema of the quickstart: authors and their posts, each post carrying an embedding for similarity search.
Package models is the schema of the quickstart: authors and their posts, each post carrying an embedding for similarity search.
models/author
Package author holds typed column descriptors and relation descriptors for the Author model.
Package author holds typed column descriptors and relation descriptors for the Author model.
models/post
Package post holds typed column descriptors and relation descriptors for the Post model.
Package post holds typed column descriptors and relation descriptors for the Post model.
Command relations shows how to declare relations (see models/models.go), load them without N+1 queries, filter by them, change them and write whole graphs.
Command relations shows how to declare relations (see models/models.go), load them without N+1 queries, filter by them, change them and write whole graphs.
models
Package models is the schema of the relations example: authors write documents, documents have comments and are tagged with many tags.
Package models is the schema of the relations example: authors write documents, documents have comments and are tagged with many tags.
models/author
Package author holds typed column descriptors and relation descriptors for the Author model.
Package author holds typed column descriptors and relation descriptors for the Author model.
models/comment
Package comment holds typed column descriptors and relation descriptors for the Comment model.
Package comment holds typed column descriptors and relation descriptors for the Comment model.
models/document
Package document holds typed column descriptors and relation descriptors for the Document model.
Package document holds typed column descriptors and relation descriptors for the Document model.
models/tag
Package tag holds typed column descriptors and relation descriptors for the Tag model.
Package tag holds typed column descriptors and relation descriptors for the Tag model.
Command testing shows how to test application code against a real PostgreSQL database with momotest: a database or a transaction per test, the project's embedded migrations applied, seeded fixtures, and tests that run in parallel.
Command testing shows how to test application code against a real PostgreSQL database with momotest: a database or a transaction per test, the project's embedded migrations applied, seeded fixtures, and tests that run in parallel.
models
Package models is the schema of the testing example: bank accounts whose balance the database refuses to take below zero.
Package models is the schema of the testing example: bank accounts whose balance the database refuses to take below zero.
models/account
Package account holds typed column descriptors and relation descriptors for the Account model.
Package account holds typed column descriptors and relation descriptors for the Account model.
Command transactions shows how to group writes so they all happen or none do: db.Tx, rollback on error, savepoints for a step that may fail, read-only and isolation options, and a retry loop for serialization failures.
Command transactions shows how to group writes so they all happen or none do: db.Tx, rollback on error, savepoints for a step that may fail, read-only and isolation options, and a retry loop for serialization failures.
models
Package models is the schema of the transactions example: bank accounts whose balance the database refuses to take below zero.
Package models is the schema of the transactions example: bank accounts whose balance the database refuses to take below zero.
models/account
Package account holds typed column descriptors and relation descriptors for the Account model.
Package account holds typed column descriptors and relation descriptors for the Account model.
Command upgrade-database reads the customers of a database that was upgraded from schema v1 to the latest version with `momo migrate apply`.
Command upgrade-database reads the customers of a database that was upgraded from schema v1 to the latest version with `momo migrate apply`.
models
Package models is the schema of the upgrade-database example, at its latest version.
Package models is the schema of the upgrade-database example, at its latest version.
models/customer
Package customer holds typed column descriptors and relation descriptors for the Customer model.
Package customer holds typed column descriptors and relation descriptors for the Customer model.
Command vector-search shows nearest-neighbour search with pgvector: the column and HNSW index (see models/models.go and the migration), a query ordered by a distance, filters, a limit, and per-search settings.
Command vector-search shows nearest-neighbour search with pgvector: the column and HNSW index (see models/models.go and the migration), a query ordered by a distance, filters, a limit, and per-search settings.
models
Package models is the schema of the vector-search example: items with an embedding, indexed for nearest-neighbour search.
Package models is the schema of the vector-search example: items with an embedding, indexed for nearest-neighbour search.
models/item
Package item holds typed column descriptors and relation descriptors for the Item model.
Package item holds typed column descriptors and relation descriptors for the Item model.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL