Skip to content

feat(postgres): client-side defaults as Effects with defaultFn and onUpdate - #111

Merged
Makisuo merged 3 commits into
MapleTechLabs:mainfrom
artile:feat/client-defaults
Nov 2, 2026
Merged

Makisuo merged 3 commits into
MapleTechLabs:mainfrom
artile:feat/client-defaults

Conversation

@artile

@artile artile commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Drizzle has $defaultFn / $onUpdate and Prisma has @default(cuid()) / @updatedAt, but effect-orm had only database defaults. A database without cuid() or triggers needs the default to run in the app, and as an Effect it can use Clock, Random or a service, and tests can control it.

Closes #82.
Part of #52.

Changes

  • PG.column(type, { defaultFn, onUpdate }): each is an Effect<InsertValue<T>, never, R>. They write no DDL.
  • A column with either one is optional in the insert row type (DefaultedColumnsOf).
  • Database.run runs them before it compiles a write. defaultFn runs once for each row that leaves its column out (or gives undefined). onUpdate runs once for an update that does not set its column. An insert runs onUpdate too when the column has no defaultFn, default, defaultExpr or identity (Drizzle does this too). A value the write gives, null included, is kept, and its Effect does not run.
  • The requirements flow through the types. Table gets two type parameters, InsertR and UpdateR (default never). They are carried by a contravariant phantom, so existing Table<N, C> annotations still accept these tables. insertInto / update keep them on CHInsert / CHUpdate, and Database.run returns Effect<..., ..., RequirementsOf<Q>> (Db.run adds Database). An update requires only what the onUpdate Effects need.
  • New export fillDefaults(write), on both entries: it runs the Effects for a write you compile yourself. compile stays pure and refuses (QueryBuilderDefect) a write that still leaves out a client default, so a missing value never turns silently into DEFAULT.
  • Docs: inserts.md (new "Client-side defaults" section), updates-and-deletes.md, database.md, reference.md, and gap-review.md (item marked built). Changeset: minor.

Left out

  • ClickHouse table and the bare table() options: only PG.column takes the options for now. The runtime path is dialect-neutral (Table.clientDefaults), so adding them later is small.
  • Effects that can fail: the error type is never, so the error union of run does not change. This can come later if someone needs it.
  • INSERT ... SELECT and the SET of onConflictDoUpdate get no client defaults. The docs say to select or set the column yourself.
  • With a SET callback, onUpdate runs before the callback, and a value the callback gives wins. The callback is only read at compile time, so the Effect runs even when the callback sets the column.
  • The insert type requires a column's defaultFn services even when every row gives that column. Proving per call which columns a row leaves out would need much heavier types.
  • SQLite / MySQL / D1: nothing here depends on a dialect. This PR does not depend on feat: add SQLite and Cloudflare D1 query dialects #49.

Validation

With EFFECT_ORM_CLICKHOUSE_URL=http://127.0.0.1:8123 EFFECT_ORM_CLICKHOUSE_USER=maple EFFECT_ORM_CLICKHOUSE_PASSWORD=maple (ClickHouse 26.8.2.7):

  • bun run build: pass.
  • bun run typecheck: pass (includes the new src/database/client-defaults.test-d.ts).
  • bunx vitest run --testTimeout=60000 --hookTimeout=60000: 61 files passed, 1040 tests passed, 0 skipped.
  • node scripts/check-doc-citations.mjs, node scripts/check-exports-documented.mjs, node scripts/check-doc-examples.mjs: pass.
  • bun scripts/check-package.ts: pass.

New tests: src/database/client-defaults.test.ts (PGlite, with TestClock and a counter IdGen layer) and src/database/client-defaults.test-d.ts.

Upstream CI for a fork PR waits for a maintainer to approve the run.


View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need.

artile and others added 2 commits October 25, 2026 17:31
…Update

A PG.column takes defaultFn and onUpdate, each an Effect of a column value
that cannot fail. Database.run runs them through fillDefaults: defaultFn for
each row an insert leaves the column out of, onUpdate once for an update
that does not set it (and for an insert, when the column has no other
default). Their requirements flow into the write's type and into run's.
compile refuses a write that still leaves a client default out.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 39 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 7140e53a-aa54-4acb-abec-1b8e728aaab9
Commits

Reviewing files that changed from the base of the PR and between b57faf1 and 36c6c8b.

Files selected for processing: 20
  • .changeset/client-defaults.md
  • design/gap-review.md
  • docs/database.md
  • docs/inserts.md
  • docs/reference.md
  • docs/updates-and-deletes.md
  • src/ch/client-defaults.ts
  • src/ch/compile.ts
  • src/ch/insert.ts
  • src/ch/table.ts
  • src/ch/update.ts
  • src/core.ts
  • src/database/client-defaults.test-d.ts
  • src/database/client-defaults.test.ts
  • src/database/database.ts
  • src/kit/sqlite-generate.test.ts
  • src/schema/pg-define.ts
  • src/schema/sqlite-define.ts
  • src/sqlite.ts
  • src/sqlite/client-defaults.test.ts
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Resolve the pg-define import and reference.md conflicts. Bring defaultFn
and onUpdate to SQLite.column (the run path is dialect-neutral, so D1 gets
them too): the shared clientDefaultsOf builds Table.clientDefaults for both
dialects, and SqliteSchemaTable carries the requirements. INSERT ... SELECT
now refuses a source that leaves out a client-defaulted column, which would
otherwise have been written as NULL or the database default. Tests: SQLite
on node:sqlite, no DEFAULT in the PG and SQLite DDL or SQLite migrations,
and type tests for the SQLite requirements.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Makisuo

Makisuo commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Merge-readiness pass (merge commit 36c6c8b, no rebase):

  • Merged current main and resolved the conflicts in the src/schema/pg-define.ts imports (main dropped two of them) and in docs/reference.md.
  • Added SQLite support. It was cheap because the run path is dialect-neutral, and D1 goes through the same fromSqlClient run.
    • SQLite.column takes defaultFn / onUpdate with the same types.
    • SqliteSchemaTable carries the insert/update requirements, and external SQLite tables carry clientDefaults.
    • Both dialects now build Table.clientDefaults through one shared clientDefaultsOf in src/ch/client-defaults.ts.
    • ClickHouse columns still do not take the options, as the PR body says.
  • Bug fix: an INSERT ... SELECT whose SELECT left out a column with defaultFn (or an onUpdate with no database default) type-checked and compiled. It silently wrote NULL or the database default. compile now refuses it with a QueryBuilderDefect that names the columns. The docs said "select the column yourself"; this enforces it.
  • New coverage:
    • src/sqlite/client-defaults.test.ts: on node:sqlite, insert and update fill via Database.run (TestClock and a service-backed id). Also the fillDefaults/compile refusal, the INSERT ... SELECT refusal, and a check that the SQLite DDL carries no DEFAULT for a client default.
    • src/kit/sqlite-generate.test.ts: adding defaultFn/onUpdate to existing columns generates no migration, and no DEFAULT is written.
    • src/database/client-defaults.test.ts: PG DDL has no DEFAULT for client defaults; PG INSERT ... SELECT refusal.
    • client-defaults.test-d.ts: SQLite insert/update requirements and value typing.
  • Docs (inserts.md, updates-and-deletes.md, reference.md, gap review) and the changeset now cover SQLite and the INSERT ... SELECT rule.

Design note for the maintainer: the insert type requires an onUpdate's services even when the column also has a database default and the runtime will not run it. That over-approximation was already in the PR, and I left it as is.

Verification (local, ClickHouse 26.8):

  • bun run build, bun run typecheck: pass.
  • bun run test: 1442/1442 tests passed, plus the doc citation, export catalog and doc example checks.
  • FUZZ_SCALE=0.3 bunx vitest run tests/fuzz: 269/269 passed.
  • Not run: a D1 (miniflare) test for client defaults. D1 shares the code path, but I did not exercise it.

CI run 41474420219: the ClickHouse 26.8 job passed. The 26.2 job failed only on generated fixtures: union results agree (clickhouse-join-nulls) with ClickHouse "Division by zero", the known flake that #131 fixes.

@Makisuo
Makisuo merged commit 5cafcb4 into MapleTechLabs:main Nov 2, 2026
2 of 3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Client-side defaults as Effects: defaultFn and onUpdate

2 participants