Skip to content

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

Open
artile wants to merge 2 commits into
MapleTechLabs:mainfrom
artile:feat/client-defaults
Open

artile wants to merge 2 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 27 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: d527ac57-8733-4de3-9753-b8effd6fc6fc
Commits

Reviewing files that changed from the base of the PR and between c5fa679 and 8588e88.

Files selected for processing: 16
  • .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/schema/pg-define.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

Comment @coderabbitai help to get the list of available commands.

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

1 participant