Repository navigation
feat(postgres): client-side defaults as Effects with defaultFn and onUpdate - #111
Conversation
…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>
|
Warning Review limit reachedYou'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. View limit details
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. |
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>
|
Merge-readiness pass (merge commit 36c6c8b, no rebase):
Design note for the maintainer: the insert type requires an Verification (local, ClickHouse 26.8):
CI run 41474420219: the ClickHouse 26.8 job passed. The 26.2 job failed only on |
Drizzle has
$defaultFn/$onUpdateand Prisma has@default(cuid())/@updatedAt, but effect-orm had only database defaults. A database withoutcuid()or triggers needs the default to run in the app, and as an Effect it can useClock,Randomor a service, and tests can control it.Closes #82.
Part of #52.
Changes
PG.column(type, { defaultFn, onUpdate }): each is anEffect<InsertValue<T>, never, R>. They write no DDL.DefaultedColumnsOf).Database.runruns them before it compiles a write.defaultFnruns once for each row that leaves its column out (or givesundefined).onUpdateruns once for an update that does not set its column. An insert runsonUpdatetoo when the column has nodefaultFn,default,defaultExproridentity(Drizzle does this too). A value the write gives,nullincluded, is kept, and its Effect does not run.Tablegets two type parameters,InsertRandUpdateR(defaultnever). They are carried by a contravariant phantom, so existingTable<N, C>annotations still accept these tables.insertInto/updatekeep them onCHInsert/CHUpdate, andDatabase.runreturnsEffect<..., ..., RequirementsOf<Q>>(Db.runaddsDatabase). An update requires only what theonUpdateEffects need.fillDefaults(write), on both entries: it runs the Effects for a write you compile yourself.compilestays pure and refuses (QueryBuilderDefect) a write that still leaves out a client default, so a missing value never turns silently intoDEFAULT.minor.Left out
tableand the baretable()options: onlyPG.columntakes the options for now. The runtime path is dialect-neutral (Table.clientDefaults), so adding them later is small.never, so the error union ofrundoes not change. This can come later if someone needs it.INSERT ... SELECTand the SET ofonConflictDoUpdateget no client defaults. The docs say to select or set the column yourself.onUpdateruns 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.defaultFnservices even when every row gives that column. Proving per call which columns a row leaves out would need much heavier types.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 newsrc/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, withTestClockand a counterIdGenlayer) andsrc/database/client-defaults.test-d.ts.Upstream CI for a fork PR waits for a maintainer to approve the run.
Need help on this PR? Tag
@codesmith-botwith what you need.