---
title: "API reference"
description: "The full export catalog by module, plus error types."
---
> Documentation Index
> Fetch the complete documentation index at: https://effect-orm.maple.dev/llms.txt
> Use this file to discover all available pages before exploring further.
# API reference
The package has one entry per database, each self-contained:
```ts
import * as CH from "@maple-dev/effect-orm/clickhouse"
import * as PG from "@maple-dev/effect-orm/postgres"
import * as SQLite from "@maple-dev/effect-orm/sqlite"
```
All carry the whole [query builder](#query-builder-every-entry). Each adds its own column
types, functions, table definitions and a `compile` that defaults to its dialect:
[`/clickhouse`](#clickhouse) and [`/postgres`](#postgres). Everything else is on a
[subpath](#other-subpaths) for a narrower job.
## Naming conventions
Some ClickHouse functions collide with JavaScript reserved words or globals. The source defines
those with a trailing underscore, and **`/clickhouse` drops it**: `min_`, `max_`, `any_`,
`toString_`, `length_`, `left_`, `extract_`, `least_`, `greatest_`, `position_`, `lower_`,
`round_`, `path_` and `domain_` are all exported from `/clickhouse` under their bare names.
One exception, because it cannot be anything else:
| `/clickhouse` name | Also on `/expr` as | Note |
| ------------------ | ------------------ | -------------------------------- |
| `if_` | `if_` | `if` is a reserved word |
| `in_` / `notIn` | â | `Expr` methods; `in` is reserved |
Importing the kitchen-sink namespace
(`import * as CH from "@maple-dev/effect-orm/expr"`) gives you the raw underscored names
uniformly, which some codebases prefer for exactly this reason.
---
## Query builder (every entry)
Everything in this section is exported, identically, from `/clickhouse`, `/postgres`, `/sqlite` and `/mysql`.
### Query construction
| Export | Signature |
| ----------- | ----------------------------------------- |
| `from` | `(table, alias?) => CHQuery` |
| `fromQuery` | `(query, alias) => CHQuery` |
| `fromUnion` | `(union, alias) => CHQuery` |
| `unionAll` | `(...queries) => CHUnionQuery` |
| `update` | `(table) => CHUpdateStart`, then `CHUpdate`: `.set(record \| fn)`, `.where(fn)` or `.allRows()`, `.returning(...)`, `.settings(record)`. See [Updating and deleting](https://effect-orm.maple.dev/clickhouse/queries/updates-and-deletes/) |
| `deleteFrom` | `(table) => CHDelete`: `.where(fn)` or `.allRows()`, `.returning(...)`, `.settings(record)` |
| `tableSchemas` | `(table) => TableSchemas`: the `select`, `insert`, `update`, `json`, `jsonCreate` and `jsonUpdate` row Schemas. See [Row Schemas](https://effect-orm.maple.dev/clickhouse/queries/tables-and-types/#row-schemas) |
| `insertInto` | `(table) => CHInsertStart`, then `CHInsert`; `.values(row \| rows)` or `.select(query)` sets its rows, `.settings(record)` ClickHouse `SETTINGS`, `.returning(...)` the RETURNING list, `.onConflictDoNothing(options?)` / `.onConflictDoUpdate(options)` the ON CONFLICT clause (Postgres and SQLite). See [Inserting rows](https://effect-orm.maple.dev/clickhouse/queries/inserts/) |
| `fillDefaults` | `(write) => Effect>`: runs the `defaultFn` / `onUpdate` Effects of an insert's or update's table into its rows or SET. `Database.run` calls it; call it before `compile` when you compile a write yourself. See [client-side defaults](https://effect-orm.maple.dev/clickhouse/queries/inserts/#client-side-defaults) |
| `excluded` | `(columns) => ExcludedColumns`: an `onConflictDoUpdate` `set` that copies the named columns from the proposed row. See [On conflict](https://effect-orm.maple.dev/clickhouse/queries/inserts/#on-conflict) |
| `excludedAll` | `(except?) => ExcludedAll`: an `onConflictDoUpdate` `set` that copies every column the insert writes, except these and the target's |
Tables come from the dialect's `table`: [`CH.table`](#tables-and-ddl) or
[`PG.table`](#tables-and-ddl-1).
### `CHQuery` methods
| Method | Notes |
| ------------------------------------------------------- | --------------------------------------------------------------------- |
| `select(...names)` / `select(fn)` | Required before compiling |
| `where(fn)` | Returns `Array`; AND-joined. Narrows a joined column it proves not NULL for a later `select` ([joins](https://effect-orm.maple.dev/clickhouse/queries/joins-and-subqueries/#narrowing-a-joined-column-with-where)) |
| `groupBy(...outputKeys)` | Takes select aliases, not column names |
| `having(fn)` | Post-aggregation filter; input accessor or `dynamicColumn` aliases |
| `orderBy(...[col, dir])` | **Tuples**, not two strings |
| `limit(n)` / `offset(n)` | Rounded before emission |
| `format(fmt)` | `"JSON"` \| `"JSONEachRow"` |
| `distinct()` / `distinctOn(...aliases)` | `SELECT DISTINCT` / `SELECT DISTINCT ON (â¦)` |
| `forUpdate` / `forNoKeyUpdate` / `forShare` / `forKeyShare` | Row locks; options `LockOptions` (`skipLocked`, `noWait`, `of`). Postgres writes all four, MySQL and MariaDB `forUpdate` and `forShare`; refused in the type when compiled or run for another dialect |
| `innerJoin` / `leftJoin` / `crossJoin` | `(table, alias, on?)` |
| `innerJoinQuery` / `leftJoinQuery` / `crossJoinQuery` | `(query, alias, on?)` |
| `withCTE(name, query)` / `withCTE(name, sql, options?)` | Typed query derives scope; SQL form can declare `options.tenantScope` |
| `route("ingest")` | Metadata only |
| `crossTenant()` | Forces `tenantScope: "cross-tenant"` |
`CHUnionQuery` offers only `orderBy`, `limit`, `offset`, `format`.
A `CHQuery` is `Pipeable`: `query.pipe(f, g)` is `g(f(query))`.
### `Query` (data-last)
`Query` is a namespace of `dual` functions over the `CHQuery` methods above. Each one is
data-last (`query.pipe(Query.where(fn))`) and data-first (`Query.where(query, fn)`), calls the
fluent method, and writes the same SQL. See [Pipeable, data-last queries](https://effect-orm.maple.dev/clickhouse/queries/queries/#pipeable-data-last-queries).
| Export | Same as |
| --------------------------------------------------------- | ------------------------------- |
| `Query.from` / `Query.fromQuery` / `Query.fromUnion` | `from` / `fromQuery` / `fromUnion` |
| `Query.select()` / `Query.select(...names)` / `Query.select(fn)` | `select` |
| `Query.where(fn)` | `where` |
| `Query.orderBy(...[key, dir])` / `Query.orderBy(fn)` | `orderBy` |
| `Query.limit(n)` / `Query.offset(n)` | `limit` / `offset` |
| `Query.groupBy(...keys)` / `Query.having(fn)` | `groupBy` / `having` |
| `Query.distinct()` / `Query.distinctOn(...keys)` | `distinct` / `distinctOn` |
| `Query.format(fmt)` | `format` |
| `Query.forUpdate` / `Query.forNoKeyUpdate` / `Query.forShare` / `Query.forKeyShare` | `(options?)`, the row locks |
| `Query.route(route)` / `Query.crossTenant()` | `route` / `crossTenant` |
| `Query.innerJoin` / `Query.leftJoin` / `Query.crossJoin` | `(table, alias, on?)` |
| `Query.innerJoinQuery` / `Query.leftJoinQuery` / `Query.crossJoinQuery` | `(query, alias, on?)` |
| `Query.withCTE(name, query)` / `Query.withCTE(name, sql, options?)` | `withCTE` |
`CHUnionQuery` and the writes are not `Pipeable`; use their methods.
### Compilation
Each entry exports `compile`, `compileUnsafe`, `compileUnion` and `compileUnionUnsafe`, defaulting
to its own dialect; `options.dialect` overrides it.
| Export | Signature |
| -------------------- | ------------------------------------------------------------------------------------ |
| `compile` | `(query, params?, options?) => Effect, QueryBuilderError>`; also `(insert, params?, options?)`, whose options (`InsertCompileOptions`) are only `dialect` |
| `compileUnsafe` | The same, returning `CompiledQuery