Repository navigation
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 27 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. Comment |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.