# OKModel

> okmodel is a catalog-first TypeScript ORM, PostgreSQL first.

Fetch `/llms.mdx/docs/⟨slug⟩.md` for one page as markdown, or `/llms-full.txt` for every page.

## Start

- [Documentation](https://okm.omqkhafi.dev/llms.mdx/docs/index.md): okmodel is a catalog-first TypeScript ORM, PostgreSQL first.

## Start

- [Let's start](https://okm.omqkhafi.dev/llms.mdx/docs/start.md): Install a driver, define the table, then push and connect.
- [Basic Usage](https://okm.omqkhafi.dev/llms.mdx/docs/start/basic-usage.md): Each call on the client: insert, find, one, count, exists, update, and delete.
- [From Drizzle](https://okm.omqkhafi.dev/llms.mdx/docs/start/from-drizzle.md): okm import drizzle writes okmodel table files from a Drizzle schema.

## Define

- [Overview](https://okm.omqkhafi.dev/llms.mdx/docs/define/table.md): table() stores the columns. The key comes first, then each data type, then the options.
- [Columns](https://okm.omqkhafi.dev/llms.mdx/docs/define/table/columns.md): Each field is a data type. The builder name is that type.
- [Keys](https://okm.omqkhafi.dev/llms.mdx/docs/define/table/keys.md): One table has one primary key.
- [Defaults](https://okm.omqkhafi.dev/llms.mdx/docs/define/table/defaults.md): Omitted, the column is required and not null.
- [References](https://okm.omqkhafi.dev/llms.mdx/docs/define/table/references.md): references() points a column at another table.
- [Relations](https://okm.omqkhafi.dev/llms.mdx/docs/define/table/relations.md): relations names edges. schema() resolves the foreign key.
- [Queries](https://okm.omqkhafi.dev/llms.mdx/docs/define/table/queries.md): find returns a list and needs a limit, or .all(reason).
- [Traits](https://okm.omqkhafi.dev/llms.mdx/docs/define/table/traits.md): trait() adds columns. The schema list is the default. A table adds its own, or opts out.
- [Indexes](https://okm.omqkhafi.dev/llms.mdx/docs/define/table/indexes.md): indexes receives column handles and returns an array of index calls.
- [Validation](https://okm.omqkhafi.dev/llms.mdx/docs/define/table/validation.md): A field's rules live in one place. They run when validation is enabled.
- [Shape](https://okm.omqkhafi.dev/llms.mdx/docs/define/table/shape.md): array() wraps a scalar. picklist() narrows a string column. enum() creates a Postgres type.
- [Filters](https://okm.omqkhafi.dev/llms.mdx/docs/define/table/filters.md): filters() allowlists what a caller may send.
- [Presets](https://okm.omqkhafi.dev/llms.mdx/docs/define/table/presets.md): A preset is a named refinement you call on the table.
- [Tenancy](https://okm.omqkhafi.dev/llms.mdx/docs/define/table/tenancy.md): Schema tenancy adds the key to every table.
- [Names](https://okm.omqkhafi.dev/llms.mdx/docs/define/table/names.md): The field name is the TypeScript name.
- [Visibility](https://okm.omqkhafi.dev/llms.mdx/docs/define/table/visibility.md): Three flags. They do not change the SQL type.
- [Checks](https://okm.omqkhafi.dev/llms.mdx/docs/define/table/checks.md): A check is named SQL.
- [Required rows](https://okm.omqkhafi.dev/llms.mdx/docs/define/table/reference-rows.md): Rows the application expects to find, inserted when the key is missing.
- [Overview](https://okm.omqkhafi.dev/llms.mdx/docs/define/schema.md): schema() compiles the tables into one catalog. Each option is a page.
- [Tables](https://okm.omqkhafi.dev/llms.mdx/docs/define/schema/tables.md): The list of tables. A reference names one of them.
- [Casing](https://okm.omqkhafi.dev/llms.mdx/docs/define/schema/casing.md): The SQL name of a camelCase field.
- [Codecs](https://okm.omqkhafi.dev/llms.mdx/docs/define/schema/codecs.md): The TypeScript value. The SQL type stays the same.
- [Row types](https://okm.omqkhafi.dev/llms.mdx/docs/define/schema/types.md): How row types are produced.
- [Id default](https://okm.omqkhafi.dev/llms.mdx/docs/define/schema/defaults.md): What a bare t.id() means.
- [Minimum Postgres](https://okm.omqkhafi.dev/llms.mdx/docs/define/schema/min-postgres.md): minPostgres is the oldest major this schema runs on. Omitted means 15.
- [Traits](https://okm.omqkhafi.dev/llms.mdx/docs/define/schema/traits.md): Columns applied to every table that does not opt out.
- [Tenancy](https://okm.omqkhafi.dev/llms.mdx/docs/define/schema/tenancy.md): Tenant keys on every table that does not opt out.
- [Validation](https://okm.omqkhafi.dev/llms.mdx/docs/define/schema/validation.md): Turns rules on for the schema. schema() does not run them.
- [Extensions](https://okm.omqkhafi.dev/llms.mdx/docs/define/schema/extensions.md): Extension definitions. schema() does not install them.
- [Functions](https://okm.omqkhafi.dev/llms.mdx/docs/define/schema/functions.md): Routines the catalog stores.
- [Triggers](https://okm.omqkhafi.dev/llms.mdx/docs/define/schema/triggers.md): Triggers. Identity is the table plus the name.
- [Views](https://okm.omqkhafi.dev/llms.mdx/docs/define/schema/views.md): Views and materialized views.
- [Several catalogs](https://okm.omqkhafi.dev/llms.mdx/docs/define/schema/catalogs.md): Two catalogs on one database. external() keeps a table the other catalog owns.
- [Overview](https://okm.omqkhafi.dev/llms.mdx/docs/define/connect.md): connect() opens the client. The first argument is the target. schema is required.
- [Drivers](https://okm.omqkhafi.dev/llms.mdx/docs/define/connect/drivers.md): Import connect from the driver you installed.
- [Target](https://okm.omqkhafi.dev/llms.mdx/docs/define/connect/target.md): A string opens a pool. A pool from open is reused.
- [Topology](https://okm.omqkhafi.dev/llms.mdx/docs/define/connect/topology.md): { primary, replicas } opens one pool per endpoint.
- [Check](https://okm.omqkhafi.dev/llms.mdx/docs/define/connect/check.md): The first connection checks the server, the catalog hash, and the floor.
- [Session](https://okm.omqkhafi.dev/llms.mdx/docs/define/connect/session.md): Pool size, prepare mode, TLS, search path, and deadlines.
- [Errors](https://okm.omqkhafi.dev/llms.mdx/docs/define/connect/errors.md): errors.http sets the statuses OkmError.toHttp uses.
- [Generators](https://okm.omqkhafi.dev/llms.mdx/docs/define/connect/generators.md): generators replaces a built-in client generator by name.
- [Observers](https://okm.omqkhafi.dev/llms.mdx/docs/define/connect/observers.md): hookm is a list of read-only observers.
- [OpenTelemetry](https://okm.omqkhafi.dev/llms.mdx/docs/define/connect/otel.md): okmodel/otel turns the existing observers into db.* spans.
- [Overview](https://okm.omqkhafi.dev/llms.mdx/docs/define/config.md): defineConfig is the file okm reads. Each option is a page.
- [Schema](https://okm.omqkhafi.dev/llms.mdx/docs/define/config/schema.md): schema is required. okm loads this file.
- [Database](https://okm.omqkhafi.dev/llms.mdx/docs/define/config/database.md): database is one target named default. targets replaces it.
- [Tables](https://okm.omqkhafi.dev/llms.mdx/docs/define/config/tables.md): tables is a directory okm check reads for orphan table files.
- [Migrations](https://okm.omqkhafi.dev/llms.mdx/docs/define/config/migrations.md): migrations is where okm generate writes SQL.
- [Out](https://okm.omqkhafi.dev/llms.mdx/docs/define/config/out.md): out is where okm build writes the catalog.
- [Timeouts](https://okm.omqkhafi.dev/llms.mdx/docs/define/config/timeouts.md): timeouts sets lock_timeout and statement_timeout for apply.
- [Pooler](https://okm.omqkhafi.dev/llms.mdx/docs/define/config/pooler.md): allowPooler skips the pooler-host refusal.
- [Lint from](https://okm.omqkhafi.dev/llms.mdx/docs/define/config/lint-from.md): lintFrom is the first migration id okm migrate check lints.
- [Backfill](https://okm.omqkhafi.dev/llms.mdx/docs/define/config/backfill.md): backfill sets batch size, pause, and batch statement timeout.
- [Roles](https://okm.omqkhafi.dev/llms.mdx/docs/define/config/roles.md): roles names migration, app, and optional reporting grants.
- [Namespace](https://okm.omqkhafi.dev/llms.mdx/docs/define/config/namespace.md): namespace is the Postgres schema this catalog owns.
- [External](https://okm.omqkhafi.dev/llms.mdx/docs/define/config/external.md): external lists catalog snapshots this project verifies.

## Reference

- [CLI](https://okm.omqkhafi.dev/llms.mdx/docs/reference/cli.md): okm reads okmodel.config.ts. okmodel is the same program.
- [Schema API](https://okm.omqkhafi.dev/llms.mdx/docs/reference/schema-api.md): describe() and buildCatalog() on okmodel/migrate turn a schema into JSON and back.
- [GitHub Action](https://okm.omqkhafi.dev/llms.mdx/docs/reference/github-action.md): The official action posts the migration plan on a pull request and can apply a preview database.
- [Errors](https://okm.omqkhafi.dev/llms.mdx/docs/reference/errors.md): OkmError carries a kind, a category, and a code. okm doctor prints each OKM code.
- [Type change](https://okm.omqkhafi.dev/llms.mdx/docs/reference/type-change.md): A type change that rewrites the table is five migrations, not one ALTER TYPE.
- [OKID](https://okm.omqkhafi.dev/llms.mdx/docs/reference/okid.md): okid() is a client generator. The id column is text with collation C.
- [Environment Variables](https://okm.omqkhafi.dev/llms.mdx/docs/reference/environment-variables.md): connect and defineConfig do not read the environment. okm reads OKM_DEBUG. Local Postgres reads POSTGRES_VERSION and REQUIRE_DOCKER.

## AI Resources

- [MCP](https://okm.omqkhafi.dev/llms.mdx/docs/ai/mcp.md): okmodel does not ship an MCP server. The browser can register read-only handbook tools.
- [Skills](https://okm.omqkhafi.dev/llms.mdx/docs/ai/skills.md): The agent skills in this repository, and the skill the site publishes.
- [LLM](https://okm.omqkhafi.dev/llms.mdx/docs/ai/llm.md): llms.txt lists every handbook page. Fetch that index, then one page.

## Also

- [Changelog](https://okm.omqkhafi.dev/changelog): Published releases.
- [llms-full.txt](https://okm.omqkhafi.dev/llms-full.txt): Every page.
- [GitHub](https://github.com/omqkhafi/okmodel)
- [okmodel](https://www.npmjs.com/package/okmodel)
