Make branded column types first class and strict - #20
Merged
Merged
Conversation
Brands were an annotation: rows carried OrgId, but comparisons widened a branded column to plain string, so `$.org_id.eq(userId)` compiled. A brand is the claim that a value is one kind of id and not another; that has to hold where values are compared and written. - Widen keeps branded types: comparisons, IN, insert/update values and INSERT ... SELECT take the brand (a value, a same-brand column, or a param.of(type, name)); plain strings, other brands, param.string and unbranded columns are type errors. Literal unions still widen. - brand(type, schema): narrow any column type with an Effect schema while keeping its SQL type and wire codec (brand(PG.int8, Cents) still reads node-postgres's strings). - SelectRowOf<typeof table>: the whole decoded row, brands kept. 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 detailsLimit details: You’ve used the included review currently available. Review configuration: ⚙️ Run configuration
📒 Files selected for processing (17)
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.
Why
Brands were only an annotation. Rows decoded to
OrgId, but comparisons widened a branded column back to plainstring, so a few obvious mistakes compiled:$.org_id.eq(userId)$.org_id.eq("anything")OrgIdcolumn to an unbranded oneA brand says a value is one kind of id and not another, and that has to hold where values are compared and written. That parity with drizzle's
$type<OrgId>()matters for moving Maple's Postgres code off drizzle.What changed
Strict brands (breaking).
Widenkeeps branded types. Comparisons (eq,in_,between, joins), insert/update values andINSERT ... SELECTnow take only:CH.param.of(orgIdType, "orgId").compilethen requires anOrgIdvalue for it.A plain string, another brand,
param.stringor an unbranded column is a type error. Literal unions still compare against their primitive.brand(type, schema), exported from the root,/typesand/postgres. It narrows any column type with an Effect schema while keeping the base type's SQL type and wire codec.PG.brand(PG.int8, Cents)still reads node-postgres's string form, whichcustomcould not. It nests asnullable(brand(...))andarray(brand(...)), andS.pg.tablewrites the base type in the DDL.SelectRowOf<typeof table>gives the whole decoded row with brands kept (drizzle's$inferSelect).Checks run both ways. A row that fails the brand's checks is a decode error. A literal or param value that fails them is a
QueryBuilderErrorfromcompile.Migration
Replace
$.OrgId.eq(CH.param.string("orgId"))with a param declared once next to the column type:Maple's ClickHouse queries have 847
param.string("orgId" | "traceId" | "spanId")uses across 51 files. The change is mechanical.Testing
src/ch/brand.test-d.ts): every forbidden case is a@ts-expect-error, includingUserIdforOrgId,param.string, plain strings inin_, an unbranded join column and wrong brands in insert/update. Every allowed form compiles.src/ch/brand.test.ts): the base codec is kept, invalid literals and params are refused at compile, and a branded insert/select round-trips withRETURNING. A row that breaks the brand fails to decode.brandis in the live coverage cases for both dialects: a brandedUInt64on ClickHouse and a brandedint8read from text on Postgres.bun run testpasses (610 tests plus the doc citation, export catalog and doc-example checks; the new docs example compiles).tests/suite (283 tests) passes against a freshclickhouse-server:26.2.🤖 Generated with Claude Code
Need help on this PR? Tag
@codesmith-botwith what you need. Autofix is disabled.