Skip to content
This repository was archived by the owner on Nov 25, 2026. It is now read-only.

TML-2965: author native Postgres enums in TypeScript - #935

Merged
wmadden-electric merged 8 commits into
mainfrom
tml-2965-native-enum-ts-authoring
Jul 9, 2026
Merged

wmadden-electric merged 8 commits into
mainfrom
tml-2965-native-enum-ts-authoring

Conversation

@wmadden-electric

@wmadden-electric wmadden-electric commented Jul 9, 2026 •

Copy link
Copy Markdown
Contributor

At a glance

Native Postgres enums were PSL-only. Now they're authorable in the TypeScript DSL too:

const AalLevel = nativeEnum('AalLevel', 'aal1', 'aal2', 'aal3').map('aal_level');
model('Session', { namespace: 'auth',
  fields: { aal: field.column(pg.enum(AalLevel)).optional() } });

produces the same contract as the PSL equivalent:

native_enum AalLevel { aal1 = "aal1" aal2 = "aal2" aal3 = "aal3" @@map("aal_level") }  // in schema `auth`
model Session { aal pg.enum(AalLevel)? }

— the same entries.valueSet.AalLevel, the same column { codecId: pg/enum@1, nativeType: "auth.aal_level", typeParams, valueSet }, and the same 'aal1' | 'aal2' | 'aal3' value-union typing — in public and named schemas alike.

The decision

Three moves, in layered order:

  1. A generic pack-entity attachment in the TS authoring chain. TS authoring admitted only the closed models / valueObjects / enums set, so a pack-contributed entity kind had no way into a namespace's entries. A new generic packEntities field threads through the defineContract chain — the same wiring enums already uses — and lands any author-declared entity in entries.<kind>, deriving its value-set via the same hook the PSL interpreter uses. The framework and contract-ts name no specific kind, so RLS role/policy ride this same seam later.
  2. A bespoke nativeEnum(…) + pg.enum(handle) on the Postgres TS surface, following the existing enumType() precedent — rather than repairing the generic auto-composed type.pg.enum path, which is a larger change shared with RLS (TML-2983).
  3. Type-name schema-qualification moved off the codec into the build stage. columnFromEntity used to bake auth.aal_level eagerly; that's the wrong home twice over — qualifying a type name is a generic concern, and a field.column(pg.enum(handle)) builder call can't know its model's namespace yet (the fields object is built before model(…, { namespace }) associates one). So the codec returns the bare name and a generic qualifyTypeName applies the prefix once the namespace is known — for both the PSL and TS paths, at the same stage (the PSL resolver moved onto it too, with zero fixture drift). This is what lets non-public schemas work without a fence.

How it works

The author declares an enum as a const and references it via pg.enum(handle) in a column — there is no separate registration step; the entity is collected from that usage at build time (harvest-from-usage), which is also how it picks up the field's namespace.

  • nativeEnum(name, …values).map(typeName) builds a handle: name is the entity name (the entries.native_enum / entries.valueSet key), .map() overrides the Postgres type name (PSL's @@map), defaulting to name.
  • pg.enum(handle) returns a deferred column descriptor carrying a generic, family-blind entityRef ({ entityKind, entityName, entity }) on the framework ColumnTypeDescriptor — it computes nothing namespace-dependent at builder-call time.
  • build-contract visits each field with its model's now-known namespace and, for a deferred column, generically: schema-qualifies the type name, harvests the entity into that namespace's entries.<kind> (+ derives its value-set, reusing move 1), and wires the column's valueSet ref. It names no entity kind — it reads entityRef off the descriptor. Two different enums sharing a name in one namespace are rejected, matching PSL's PSL_DUPLICATE_DECLARATION.

Proof

PSL↔TS parity, both directions: an in-memory Contract toEqual (ts-psl-parity.real-packs) and an emitted contract.json byte-equality fixture (parity/native-enum/), each covering the default schema and a named schema (auth, the @@map-distinct case). Full gate green — typecheck 143/143, fixtures:check zero drift, lint:casts 1031, lint:framework-vocabulary 906 (the framework stays family-blind), all three test suites.

Follow-ups / known asymmetries

  • TML-2983 — repair the generic auto-composed type.pg.enum path so reference-taking type-constructors work for every pack (would fold the bespoke pg.enum(handle) back onto the generic path).
  • Harvest-from-usage model: a handle referenced in N namespaces materializes N types (by design — a Postgres type lives in one schema, and PSL can't share one across schemas either); a nativeEnum declared but never used is absent from the contract, whereas an unused PSL block still lowers a value-set (minor, inert-only — a noted follow-up).

Shaping + dispatch plan: projects/native-postgres-enums/slices/native-enum-ts-authoring/spec.md.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features
    • Added native Postgres enum authoring with nativeEnum(...), NativeEnumHandle, and pg.enum(...) columns.
    • Added namespace-scoped “pack entity” attachments to contract definitions, including derived value-set wiring.
  • Bug Fixes
    • Improved native enum type qualification so schema qualification occurs consistently at the correct stage.
    • Fixed/strengthened merge behavior when scaffold and factory attach the same pack entities, including identity and collision handling.
  • Tests
    • Added unit and integration coverage for native enum columns, pack entity attachment semantics, and TS/PSL parity across namespaces.

wmadden-electric and others added 6 commits July 9, 2026 07:30
Slice spec + plan entry for the native-enum TypeScript authoring mirror:

- a generic pack-entity attachment through the defineContract chain (following
  the enums wiring) so an author-declared pack entity lands in entries.<kind> +
  derives its value-set;
- a bespoke nativeEnum(...) handle + a deferred pg.enum(handle) descriptor
  (follows the enumType() precedent; the generic type.pg.enum path is bypassed →
  follow-up TML-2983);
- type-name qualification relocated out of the codec (columnFromEntity returns
  the bare name) into a generic build-stage step shared by PSL and TS, so a
  named schema like auth is authorable in TS with no eager, wrongly-timed
  qualification — emitted output byte-identical to PSL.

Proven by a PSL+TS byte-identical parity test covering the default and a named
schema.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
…ML-2965)

TS authoring only admitted the closed models/valueObjects/enums set; a
pack-contributed entity kind (native_enum, and later RLS role/policy) had no way
into a namespace`s entries. Add a generic, namespace-scoped `packEntities` field
(namespace → kind → name → entity) threaded through the defineContract chain the
same way `enums` is — reachable from both the plain-definition and scaffold+factory
forms (with a 3-level deep merge so factory-declared siblings do not drop
scaffold-declared ones). At assembly each entity lands in entries.<kind>, and its
value-set is derived via the same `deriveValueSetFromEntity` hook the PSL
interpreter uses and folded into entries.valueSet. The framework/contract-ts names
no specific kind; the managed table/valueSet kinds are rejected if an author
supplies them.

typecheck 143/143; contract-ts 387/387; target-postgres 686/686; fixtures:check
zero drift; lint:casts flat (1031); lint:deps clean.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
…odec (TML-2965)

`PgEnumDescriptor.columnFromEntity` schema-qualified the enum type name eagerly
(auth.aal_level), which is the wrong home twice over: qualifying a type name by
schema is a generic concern, and the namespace is not known at a TS builder call
site (a column is built before its model associates a namespace). So the codec
now returns the bare type name, and a generic `qualifyTypeName(name, namespaceId,
defaultNamespaceId)` helper (in @prisma-next/sql-contract, reachable by both SQL
authoring paths, target-agnostic — the default namespace id is a parameter since
Postgres`s `public` is target-local) applies the prefix. The PSL path qualifies
at resolution, gated on the codec result carrying `typeParams.typeName`, so only
named-type codecs (pg/enum) qualify and the generic entity-ref path is untouched.

Pure relocation — emitted output byte-identical (fixtures:check zero drift). Sets
up the coming TS path to qualify at builder→IR assembly, where the namespace is
known.

typecheck 143/143; sql-contract 259/259; sql-contract-psl 346/346; target-postgres
686/686; fixtures:check zero drift; lint:casts flat (1031); lint:deps clean.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
…num (TML-2965)

Add the TS authoring surface for native Postgres enums, mirroring the PSL
`native_enum` block + `pg.enum(Ref)` column byte-for-byte:

- `nativeEnum(name, ...values).map(typeName)` (Postgres extension TS surface) —
  a bespoke handle constructor (following the enumType() precedent): validates
  non-empty/unique values, builds a PostgresNativeEnum, derives its value-set at
  build time. `name` is the entity name (the entries/valueSet key); `.map()`
  overrides the Postgres type name (PSL`s `@@map`), default type name = name.
- `pg.enum(handle)` — a deferred column descriptor carrying a generic,
  family-blind `entityRef` ({ entityKind, entityName, entity }) on the framework
  ColumnTypeDescriptor. It computes no namespace-dependent value at builder-call
  time (the column is built before its model has a namespace).
- build-contract resolves each deferred column against the field`s now-known
  model namespace: schema-qualifies the type name (via qualifyTypeName), harvests
  the entity into the namespace`s packEntities so it lands in entries.<kind> +
  derives its value-set (reusing the D1 attachment), and wires the column`s
  valueSet ref — all generically, naming no entity kind.

Proven for public and named (`auth`) schemas incl. an emit-then-consume test
typing the column as the member value union.

typecheck 143/143; contract-ts 387/387; target-postgres 686/686; postgres 101/101;
fixtures:check zero drift; lint:casts 1031; lint:framework-vocabulary 906; lint:deps clean.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
…(TML-2965)

Prove a native_enum + pg.enum column authored in TypeScript produces a contract
identical to the PSL equivalent, in the default schema and a named schema (auth,
the @@map-distinct case):

- extend ts-psl-parity.real-packs.test.ts with in-memory `Contract` toEqual cases
  (default + auth), building the TS side via nativeEnum(...).map(...) + pg.enum(handle)
  and the PSL side via the equivalent native_enum block + pg.enum(Ref) (the shared
  helper now threads codecLookup, which pg.enum resolution needs);
- add a parity/native-enum/ fixture (schema.prisma + contract.ts + packs.ts +
  expected.contract.json) asserting the emitted contract.json is byte-identical
  between the TS and PSL authorings.

No PSL<->TS difference surfaced. Also records the additive TML-2965 postgres-extension
exports in the 0.14->0.15 upgrade instructions (net-new; no extension-author action).

Full gate green: build; typecheck --force 143/143; lint incl lint:casts (1031),
lint:framework-vocabulary (906), lint:deps; fixtures:check zero drift; test:packages,
test:integration (1138), test:e2e (109).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
…ML-2965)

Review finding: the harvest of deferred entity-ref columns was last-write-wins,
so two different nativeEnum handles sharing a name in one namespace silently
collided — a column kept its own type/cast but the emitted entries.valueSet came
from the last harvested entity, an internally inconsistent column. PSL hard-errors
on the equivalent (PSL_DUPLICATE_DECLARATION), and byte-parity with PSL is the
slice`s bar, so the TS path now rejects it too.

Guard both `harvestPackEntity` (across columns) and `mergeHarvestedPackEntities`
(harvested vs author-declared) by entity identity — the same handle reused by many
columns is normal and records once; two different entities under one name+kind in a
namespace throws a path-aware error. Also documents two by-design asymmetries of the
harvest-from-usage model (a handle used in N namespaces materialises N types; an
unused nativeEnum is absent — the latter a noted follow-up).

typecheck 143/143; contract-ts 387/387; postgres 103/103 (+2 collision cases);
ts-psl-parity 4/4; fixtures:check zero drift; lint:casts 1031; lint:framework-vocabulary 906.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
@wmadden-electric
wmadden-electric requested a review from a team as a code owner July 9, 2026 08:14
@coderabbitai

coderabbitai Bot commented Jul 9, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

Run ID: d6f3660a-5762-49a7-8ecf-f0eaedcd5d42

📥 Commits

Reviewing files that changed from the base of the PR and between 826c88b and 63ebc13.

📒 Files selected for processing (6)
  • packages/2-sql/2-authoring/contract-ts/src/build-contract.ts
  • packages/2-sql/2-authoring/contract-ts/src/contract-builder.ts
  • packages/2-sql/2-authoring/contract-ts/test/pack-entity-attachment.test.ts
  • packages/3-extensions/postgres/src/contract/native-enum.ts
  • packages/3-extensions/postgres/test/contract-builder/native-enum.test.ts
  • skills/extension-author/prisma-next-extension-upgrade/upgrades/0.14-to-0.15/instructions.md
✅ Files skipped from review due to trivial changes (1)
  • skills/extension-author/prisma-next-extension-upgrade/upgrades/0.14-to-0.15/instructions.md
🚧 Files skipped from review as they are similar to previous changes (3)
  • packages/3-extensions/postgres/src/contract/native-enum.ts
  • packages/2-sql/2-authoring/contract-ts/src/contract-builder.ts
  • packages/2-sql/2-authoring/contract-ts/src/build-contract.ts

📝 Walkthrough

Walkthrough

This PR adds late-resolved entity references to column descriptors, threads packEntities through SQL contract authoring, introduces Postgres nativeEnum/pg.enum authoring helpers, defers enum qualification to the target codec layer, and adds parity fixtures and tests.

Changes

Native Enum Authoring

Layer / File(s) Summary
EntityRef contract
packages/1-framework/1-core/framework-components/src/shared/column-spec.ts, packages/2-sql/2-authoring/contract-psl/src/psl-column-resolution.ts, packages/2-sql/2-authoring/contract-psl/test/interpreter.entity-ref-type-constructor.test.ts
Adds EntityRef and entityRef, and updates PSL entity-ref resolution to use bare type names until later qualification.
TS pack entity lowering
packages/2-sql/2-authoring/contract-ts/src/contract-definition.ts, contract-dsl.ts, contract-lowering.ts, contract-builder.ts, build-contract.ts, test/pack-entity-attachment.test.ts
Adds packEntities to contract surfaces, merges scaffold/factory attachments, harvests deferred entity refs during column building, and derives namespace value sets with validation tests.
Postgres native-enum API
packages/3-extensions/postgres/src/contract/native-enum.ts, src/exports/contract-builder.ts, package.json, test/contract-builder/native-enum.test.ts
Adds NativeEnumHandle, nativeEnum(...), and pg.enum(...), re-exports them, updates dev dependencies, and adds runtime/type tests.
Postgres enum codec qualification
packages/3-targets/3-targets/postgres/src/core/codecs.ts, descriptor-meta.ts, postgres-schema.ts, exports/codecs.ts, exports/types.ts, test/psl-pg-enum-column.test.ts
Removes inline enum schema qualification from columnFromEntity, adds qualifyNativeType and postgresQualifyColumnType, wires target authoring metadata, and updates exports/docs/tests.
Integration parity fixtures
test/integration/test/authoring/parity/native-enum/*, ts-psl-parity.real-packs.test.ts
Adds native-enum schema/contract/snapshot fixtures and TS-vs-PSL parity assertions.
Upgrade instructions
skills/extension-author/prisma-next-extension-upgrade/upgrades/0.14-to-0.15/instructions.md
Adds the TML-2965 upgrade note for native-enum TypeScript authoring.

Estimated code review effort: 4 (Complex) | ~60 minutes

Sequence Diagram(s)

sequenceDiagram
  participant ContractAuthor
  participant NativeEnumAPI
  participant BuildContract
  participant PostgresTarget
  participant StorageNamespace

  ContractAuthor->>NativeEnumAPI: nativeEnum(name, ...members)
  ContractAuthor->>NativeEnumAPI: pg.enum(handle)
  NativeEnumAPI->>BuildContract: ColumnTypeDescriptor with entityRef
  BuildContract->>BuildContract: collect pack entities and qualify column
  BuildContract->>PostgresTarget: qualifyColumnType(enum descriptor, namespaceId)
  PostgresTarget-->>BuildContract: qualified nativeType and typeParams.typeName
  BuildContract->>StorageNamespace: emit entries.native_enum and entries.valueSet
Loading

Possibly related PRs

  • prisma/prisma-next#731: Extends the same buildBoundContract to buildContractFromDsl flow with additional contract-input plumbing.
  • prisma/prisma-next#797: Touches the same PgEnumDescriptor codec area used here for enum authoring and qualification.
  • prisma/prisma-next#906: Shares the entity-ref and native-enum authoring path around columnFromEntity and enum typing.

Suggested reviewers: wmadden, jkomyno

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 45.45% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main change: adding TypeScript authoring support for native Postgres enums.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch tml-2965-native-enum-ts-authoring

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.

@github-actions

github-actions Bot commented Jul 9, 2026 •

Copy link
Copy Markdown

size-limit report 📦

Path Size
postgres / no-emit 153.12 KB (+0.79% 🔺)
postgres / emit 128.46 KB (-0.02% 🔽)
mongo / no-emit 98.36 KB (0%)
mongo / emit 89.39 KB (0%)
cf-worker / no-emit 180.2 KB (+0.76% 🔺)
cf-worker / emit 153.16 KB (+0.02% 🔺)

@pkg-pr-new

pkg-pr-new Bot commented Jul 9, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@prisma-next/extension-author-tools

npm i https://pkg.pr.new/@prisma-next/extension-author-tools@935

@prisma-next/mongo-runtime

npm i https://pkg.pr.new/@prisma-next/mongo-runtime@935

@prisma-next/family-mongo

npm i https://pkg.pr.new/@prisma-next/family-mongo@935

@prisma-next/sql-runtime

npm i https://pkg.pr.new/@prisma-next/sql-runtime@935

@prisma-next/family-sql

npm i https://pkg.pr.new/@prisma-next/family-sql@935

@prisma-next/extension-arktype-json

npm i https://pkg.pr.new/@prisma-next/extension-arktype-json@935

@prisma-next/middleware-cache

npm i https://pkg.pr.new/@prisma-next/middleware-cache@935

@prisma-next/mongo

npm i https://pkg.pr.new/@prisma-next/mongo@935

@prisma-next/extension-paradedb

npm i https://pkg.pr.new/@prisma-next/extension-paradedb@935

@prisma-next/extension-pgvector

npm i https://pkg.pr.new/@prisma-next/extension-pgvector@935

@prisma-next/extension-postgis

npm i https://pkg.pr.new/@prisma-next/extension-postgis@935

@prisma-next/postgres

npm i https://pkg.pr.new/@prisma-next/postgres@935

@prisma-next/sql-orm-client

npm i https://pkg.pr.new/@prisma-next/sql-orm-client@935

@prisma-next/sqlite

npm i https://pkg.pr.new/@prisma-next/sqlite@935

@prisma-next/extension-supabase

npm i https://pkg.pr.new/@prisma-next/extension-supabase@935

@prisma-next/target-mongo

npm i https://pkg.pr.new/@prisma-next/target-mongo@935

@prisma-next/adapter-mongo

npm i https://pkg.pr.new/@prisma-next/adapter-mongo@935

@prisma-next/driver-mongo

npm i https://pkg.pr.new/@prisma-next/driver-mongo@935

@prisma-next/contract

npm i https://pkg.pr.new/@prisma-next/contract@935

@prisma-next/utils

npm i https://pkg.pr.new/@prisma-next/utils@935

@prisma-next/config

npm i https://pkg.pr.new/@prisma-next/config@935

@prisma-next/errors

npm i https://pkg.pr.new/@prisma-next/errors@935

@prisma-next/framework-components

npm i https://pkg.pr.new/@prisma-next/framework-components@935

@prisma-next/operations

npm i https://pkg.pr.new/@prisma-next/operations@935

@prisma-next/ts-render

npm i https://pkg.pr.new/@prisma-next/ts-render@935

@prisma-next/contract-authoring

npm i https://pkg.pr.new/@prisma-next/contract-authoring@935

@prisma-next/ids

npm i https://pkg.pr.new/@prisma-next/ids@935

@prisma-next/psl-parser

npm i https://pkg.pr.new/@prisma-next/psl-parser@935

@prisma-next/psl-printer

npm i https://pkg.pr.new/@prisma-next/psl-printer@935

@prisma-next/cli

npm i https://pkg.pr.new/@prisma-next/cli@935

@prisma-next/cli-telemetry

npm i https://pkg.pr.new/@prisma-next/cli-telemetry@935

@prisma-next/config-loader

npm i https://pkg.pr.new/@prisma-next/config-loader@935

@prisma-next/emitter

npm i https://pkg.pr.new/@prisma-next/emitter@935

@prisma-next/language-server

npm i https://pkg.pr.new/@prisma-next/language-server@935

@prisma-next/migration-tools

npm i https://pkg.pr.new/@prisma-next/migration-tools@935

prisma-next

npm i https://pkg.pr.new/prisma-next@935

@prisma-next/vite-plugin-contract-emit

npm i https://pkg.pr.new/@prisma-next/vite-plugin-contract-emit@935

@prisma-next/mongo-codec

npm i https://pkg.pr.new/@prisma-next/mongo-codec@935

@prisma-next/mongo-contract

npm i https://pkg.pr.new/@prisma-next/mongo-contract@935

@prisma-next/mongo-value

npm i https://pkg.pr.new/@prisma-next/mongo-value@935

@prisma-next/mongo-contract-psl

npm i https://pkg.pr.new/@prisma-next/mongo-contract-psl@935

@prisma-next/mongo-contract-ts

npm i https://pkg.pr.new/@prisma-next/mongo-contract-ts@935

@prisma-next/mongo-emitter

npm i https://pkg.pr.new/@prisma-next/mongo-emitter@935

@prisma-next/mongo-schema-ir

npm i https://pkg.pr.new/@prisma-next/mongo-schema-ir@935

@prisma-next/mongo-query-ast

npm i https://pkg.pr.new/@prisma-next/mongo-query-ast@935

@prisma-next/mongo-orm

npm i https://pkg.pr.new/@prisma-next/mongo-orm@935

@prisma-next/mongo-query-builder

npm i https://pkg.pr.new/@prisma-next/mongo-query-builder@935

@prisma-next/mongo-lowering

npm i https://pkg.pr.new/@prisma-next/mongo-lowering@935

@prisma-next/mongo-wire

npm i https://pkg.pr.new/@prisma-next/mongo-wire@935

@prisma-next/sql-contract

npm i https://pkg.pr.new/@prisma-next/sql-contract@935

@prisma-next/sql-errors

npm i https://pkg.pr.new/@prisma-next/sql-errors@935

@prisma-next/sql-operations

npm i https://pkg.pr.new/@prisma-next/sql-operations@935

@prisma-next/sql-schema-ir

npm i https://pkg.pr.new/@prisma-next/sql-schema-ir@935

@prisma-next/sql-contract-psl

npm i https://pkg.pr.new/@prisma-next/sql-contract-psl@935

@prisma-next/sql-contract-ts

npm i https://pkg.pr.new/@prisma-next/sql-contract-ts@935

@prisma-next/sql-contract-emitter

npm i https://pkg.pr.new/@prisma-next/sql-contract-emitter@935

@prisma-next/sql-lane-query-builder

npm i https://pkg.pr.new/@prisma-next/sql-lane-query-builder@935

@prisma-next/sql-relational-core

npm i https://pkg.pr.new/@prisma-next/sql-relational-core@935

@prisma-next/sql-builder

npm i https://pkg.pr.new/@prisma-next/sql-builder@935

@prisma-next/target-postgres

npm i https://pkg.pr.new/@prisma-next/target-postgres@935

@prisma-next/target-sqlite

npm i https://pkg.pr.new/@prisma-next/target-sqlite@935

@prisma-next/adapter-postgres

npm i https://pkg.pr.new/@prisma-next/adapter-postgres@935

@prisma-next/adapter-sqlite

npm i https://pkg.pr.new/@prisma-next/adapter-sqlite@935

@prisma-next/driver-postgres

npm i https://pkg.pr.new/@prisma-next/driver-postgres@935

@prisma-next/driver-sqlite

npm i https://pkg.pr.new/@prisma-next/driver-sqlite@935

commit: 63ebc13

Comment on lines +20 to +30
/**
* Late-resolved pack-entity reference: `entityKind`/`entityName` identify a
* pack entity whose final placement depends on data not yet known when the
* descriptor carrying this reference is built — e.g. an owning namespace
* resolved only once the surrounding structure is assembled.
*/
export type EntityRef = {
readonly entityKind: string;
readonly entityName: string;
readonly entity: unknown;
};

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this in a file called "column spec"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Repositioned so it no longer orphans the ColumnTypeDescriptor doc, and kept the generic EntityRef name — a column-scoped name (ColumnEntityRef) adds forbidden "column" framework vocab and bumps the lint:framework-vocabulary ratchet (906→909), which cuts against the family-blindness that ratchet enforces. It stays in this file because it is a field on ColumnTypeDescriptor; the doc now frames it as "a field on the descriptor it is declared on". (826c88b)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this belong in SQL at all? Only Postgres has a namespace. Can we move this to the target?

We should otherwise be delegating to the namespace schemaIR node to tell us its ID, which for the unbound namespace should be empty

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed — moved out of the SQL family entirely. Qualification is now a Postgres-target concern: the target contributes an authoring.qualifyColumnType hook, with the rule owned by the pg/enum codec (bare for the default/unbound schema, ${ns}.${name} otherwise). build-contract reads it off definition.target duck-typed (like columnFromEntity) and applies it at column construction, so the storage column and the domain field are both built qualified in one pass — no more build-then-patch. qualifyTypeName is deleted from @prisma-next/sql-contract. (826c88b)

Comment on lines +606 to +612
/**
* The target's default namespace id (e.g. Postgres's `'public'`) — consulted
* only when a type constructor's descriptor declares an `entityRefArg`, to
* decide whether the resolved entity's type name needs schema-qualifying
* against `namespaceId` (see `qualifyEntityRefColumnResult`).
*/
readonly defaultNamespaceId?: string;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit suspicious of this being present in the sql family

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right — the defaultNamespaceId threading is gone and the PSL resolver no longer qualifies; it returns the codec bare name and the target hook qualifies at construction. Nothing about schema-qualification remains in the SQL family. (826c88b)

* of them, silently mismatching the other column's type/cast. PSL hard-errors
* on the equivalent (`PSL_DUPLICATE_DECLARATION`); the TS path rejects it too.
*/
function harvestPackEntity(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wtf does "harvest" mean

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed to plain English: collectPackEntityFromColumn / CollectedPackEntities / mergeCollectedPackEntities. (826c88b)

* author-declared pack entity claiming one of these would silently clobber
* or be clobbered by the managed slot, so it is rejected outright.
*/
const MANAGED_ENTRY_KINDS = new Set(['table', 'valueSet']);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this hardcoded

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sourced from the canonical constants — tableEntityKind.kind / valueSetEntityKind.kind from @prisma-next/sql-contract — instead of the string literals. (826c88b)

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
packages/2-sql/2-authoring/contract-ts/src/build-contract.ts (1)

973-1005: 🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win

Add a collision check before merging namespace valueSet entries at packages/2-sql/2-authoring/contract-ts/src/build-contract.ts:983-990

{ ...enumValueSetEntries, ...packValueSetEntries } lets a pack-entity-derived valueSet overwrite a same-named enumType() value set in the same namespace. That can make a domain enum CHECK resolve to the wrong values via entries.valueSet[entityName]. Reject same-name collisions here instead of letting the spread win.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/2-sql/2-authoring/contract-ts/src/build-contract.ts` around lines
973 - 1005, The namespace valueSet merge in build-contract.ts currently lets
pack-entity-derived entries overwrite enumType()-derived entries with the same
name. Add an explicit collision check before constructing valueSetEntries in the
namespace assembly logic so any duplicate key between storageValueSetsByNs and
derivePackEntityValueSets is rejected with a clear error. Use the existing
collect/merge flow around mergeHarvestedPackEntities, derivePackEntityValueSets,
and createNamespace to locate the fix.
🧹 Nitpick comments (2)
packages/3-extensions/postgres/src/contract/native-enum.ts (1)

77-96: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

Validate the entity name argument, not just members.

nativeEnum rejects empty/duplicate members but doesn't validate name (or the .map()-provided type name). PSL's native_enum <Name> is grammar-constrained to a valid identifier; this TS surface accepts any string, including '', which would later produce an invalid CREATE TYPE statement.

🛡️ Proposed guard
 export function nativeEnum<
   const Name extends string,
   const Members extends readonly [string, ...string[]],
 >(name: Name, ...members: Members): NativeEnumHandle<Name, Name, Members> {
+  if (name.length === 0) {
+    throw new Error('nativeEnum(): name must be a non-empty string.');
+  }
   if (members.length === 0) {
     throw new Error(`nativeEnum("${name}"): must have at least one member.`);
   }
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/3-extensions/postgres/src/contract/native-enum.ts` around lines 77 -
96, The nativeEnum validation currently checks only member values; add
validation for the enum name/type name passed as name in nativeEnum and the
derived type name used by buildNativeEnumHandle. Ensure the name is constrained
to a valid PSL identifier before returning the NativeEnumHandle, and throw a
clear error for empty or otherwise invalid names. Keep the existing member
uniqueness checks intact, and update nativeEnum/buildNativeEnumHandle so invalid
.map()-produced names cannot flow through to CREATE TYPE generation.
packages/2-sql/2-authoring/contract-ts/src/contract-builder.ts (1)

353-389: 🗄️ Data Integrity & Integration | 🔵 Trivial | ⚡ Quick win

Scaffold-vs-factory pack-entity merge silently overwrites on name collision, unlike the identity-checked harvest merge in build-contract.ts.

mergePackEntityNames does { ...(a ?? {}), ...(b ?? {}) } with no identity check, so a factory-returned pack entity silently clobbers a scaffold-declared one of the same namespace/kind/name with a different entity, with no error — while the equivalent collision between "declared" and "harvested-via-column" pack entities in build-contract.ts's mergeHarvestedPackEntities throws. This mirrors the existing enums merge precedent at Line 475, so it may be an intentional/consistent choice, but it's worth confirming this is deliberate given the PR's broader emphasis on rejecting duplicate name collisions.

♻️ Optional: add identity check consistent with the build-stage merge
 function mergePackEntityNames(
   a: Readonly<Record<string, unknown>> | undefined,
   b: Readonly<Record<string, unknown>> | undefined,
 ): Readonly<Record<string, unknown>> {
-  return { ...(a ?? {}), ...(b ?? {}) };
+  for (const [name, entity] of Object.entries(b ?? {})) {
+    const existing = a?.[name];
+    if (existing !== undefined && existing !== entity) {
+      throw new Error(`mergePackEntities: conflicting entity declared for "${name}".`);
+    }
+  }
+  return { ...(a ?? {}), ...(b ?? {}) };
 }
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/2-sql/2-authoring/contract-ts/src/contract-builder.ts` around lines
353 - 389, The pack-entity merge at mergePackEntityNames currently
shallow-merges scaffold and factory entries, so a same namespace/kind/name
collision can silently overwrite a different entity. Update the merge logic in
mergePackEntities/mergePackEntityKinds/mergePackEntityNames to mirror the
identity-checked behavior used by mergeHarvestedPackEntities in
build-contract.ts: when both sides define the same pack entity key, verify they
refer to the same entity and throw on conflicting definitions instead of
overwriting. Keep the merge shape the same for non-conflicting
namespace/kind/name entries.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In
`@skills/extension-author/prisma-next-extension-upgrade/upgrades/0.14-to-0.15/instructions.md`:
- Around line 262-271: The note describing the new native enum API uses the
wrong shape for `nativeEnum`; update the instructions to reflect that mapping
happens on the returned handle, not as a constructor argument. In the references
to `nativeEnum`, `NativeEnumHandle`, and `pg.enum`, describe the flow as
creating the handle with the enum name and values, then applying the type
mapping on that handle, so the guidance matches the actual contract-builder API.

---

Outside diff comments:
In `@packages/2-sql/2-authoring/contract-ts/src/build-contract.ts`:
- Around line 973-1005: The namespace valueSet merge in build-contract.ts
currently lets pack-entity-derived entries overwrite enumType()-derived entries
with the same name. Add an explicit collision check before constructing
valueSetEntries in the namespace assembly logic so any duplicate key between
storageValueSetsByNs and derivePackEntityValueSets is rejected with a clear
error. Use the existing collect/merge flow around mergeHarvestedPackEntities,
derivePackEntityValueSets, and createNamespace to locate the fix.

---

Nitpick comments:
In `@packages/2-sql/2-authoring/contract-ts/src/contract-builder.ts`:
- Around line 353-389: The pack-entity merge at mergePackEntityNames currently
shallow-merges scaffold and factory entries, so a same namespace/kind/name
collision can silently overwrite a different entity. Update the merge logic in
mergePackEntities/mergePackEntityKinds/mergePackEntityNames to mirror the
identity-checked behavior used by mergeHarvestedPackEntities in
build-contract.ts: when both sides define the same pack entity key, verify they
refer to the same entity and throw on conflicting definitions instead of
overwriting. Keep the merge shape the same for non-conflicting
namespace/kind/name entries.

In `@packages/3-extensions/postgres/src/contract/native-enum.ts`:
- Around line 77-96: The nativeEnum validation currently checks only member
values; add validation for the enum name/type name passed as name in nativeEnum
and the derived type name used by buildNativeEnumHandle. Ensure the name is
constrained to a valid PSL identifier before returning the NativeEnumHandle, and
throw a clear error for empty or otherwise invalid names. Keep the existing
member uniqueness checks intact, and update nativeEnum/buildNativeEnumHandle so
invalid .map()-produced names cannot flow through to CREATE TYPE generation.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

Run ID: 0198edbf-90a9-4e6f-a1fb-7bf61c5ab52b

📥 Commits

Reviewing files that changed from the base of the PR and between f44cbcb and ca74503.

⛔ Files ignored due to path filters (3)
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
  • projects/native-postgres-enums/plan.md is excluded by !projects/**
  • projects/native-postgres-enums/slices/native-enum-ts-authoring/spec.md is excluded by !projects/**
📒 Files selected for processing (28)
  • packages/1-framework/1-core/framework-components/src/shared/column-spec.ts
  • packages/2-sql/1-core/contract/package.json
  • packages/2-sql/1-core/contract/src/exports/qualify-type-name.ts
  • packages/2-sql/1-core/contract/src/qualify-type-name.ts
  • packages/2-sql/1-core/contract/test/qualify-type-name.test.ts
  • packages/2-sql/1-core/contract/tsdown.config.ts
  • packages/2-sql/2-authoring/contract-psl/src/interpreter.ts
  • packages/2-sql/2-authoring/contract-psl/src/psl-column-resolution.ts
  • packages/2-sql/2-authoring/contract-psl/src/psl-field-resolution.ts
  • packages/2-sql/2-authoring/contract-ts/src/build-contract.ts
  • packages/2-sql/2-authoring/contract-ts/src/contract-builder.ts
  • packages/2-sql/2-authoring/contract-ts/src/contract-definition.ts
  • packages/2-sql/2-authoring/contract-ts/src/contract-dsl.ts
  • packages/2-sql/2-authoring/contract-ts/src/contract-lowering.ts
  • packages/2-sql/2-authoring/contract-ts/test/pack-entity-attachment.test.ts
  • packages/3-extensions/postgres/package.json
  • packages/3-extensions/postgres/src/contract/native-enum.ts
  • packages/3-extensions/postgres/src/exports/contract-builder.ts
  • packages/3-extensions/postgres/test/contract-builder/native-enum.test.ts
  • packages/3-targets/3-targets/postgres/src/core/codecs.ts
  • packages/3-targets/3-targets/postgres/src/exports/codecs.ts
  • packages/3-targets/3-targets/postgres/src/exports/types.ts
  • skills/extension-author/prisma-next-extension-upgrade/upgrades/0.14-to-0.15/instructions.md
  • test/integration/test/authoring/parity/native-enum/contract.ts
  • test/integration/test/authoring/parity/native-enum/expected.contract.json
  • test/integration/test/authoring/parity/native-enum/packs.ts
  • test/integration/test/authoring/parity/native-enum/schema.prisma
  • test/integration/test/authoring/parity/ts-psl-parity.real-packs.test.ts

… at construction (TML-2965)

Address the #935 review. Schema-qualifying a native-enum type name
(auth.aal_level) is a Postgres concern — only Postgres has schemas — so it must
not live in the SQL family. Move it to the target, and apply it where the
storage column and the domain field are both first constructed so neither is
built bare-then-patched:

- The Postgres target contributes `authoring.qualifyColumnType`; the rule lives
  on the `pg/enum` codec (`qualifyNativeType`: bare for the default/unbound
  schema, `${ns}.${name}` otherwise). `buildSqlContractFromDefinition` reads the
  hook off `definition.target` duck-typed (like `columnFromEntity`) and applies
  it to each scalar column at construction — so the storage column is built from
  the qualified descriptor and the domain field derives from it, both correct in
  one pass. Both the PSL `pg.enum(Ref)` and TS `pg.enum(handle)` paths route
  through this loop; the dispatch keys off the codec id, catching both.
- Delete `qualifyTypeName` from `@prisma-next/sql-contract` and its
  `defaultNamespaceId` threading through the PSL resolver — the family no longer
  knows about qualification, it only calls a target hook (like `createNamespace`).
- Delete the two fix-up passes the earlier build-then-patch approach needed: the
  `createNamespace` column-walk (+ probe schema + `PostgresSchema.qualifyType`)
  and `syncDomainFieldTypeParams`.

Also from the review: rename `harvest*` → `collect*` (build-contract); source the
managed entry kinds from `tableEntityKind.kind`/`valueSetEntityKind.kind` instead
of literals; reposition `EntityRef` so it no longer orphans the `ColumnTypeDescriptor`
doc.

Emitted output byte-identical (fixtures:check zero drift). typecheck 143/143;
target-postgres 686/686; postgres 103/103; PSL<->TS parity 4/4 + 22/22;
lint:casts 1031; lint:framework-vocabulary 906.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
- Guard the namespace value-set merge: a pack-entity-derived value-set could
  silently overwrite a same-named `enumType()`-derived one in a namespace
  (`entries.valueSet[name]` drives typing / the domain-enum CHECK), corrupting a
  column. `mergeNamespaceValueSets` now throws on a name present in both, like
  the harvest guard.
- `nativeEnum` validates the entity name and the `.map()` type name (reject
  empty/whitespace), not just members — an empty name flowed into an invalid
  CREATE TYPE.
- Make the scaffold-vs-factory pack-entity merge identity-checked (throws on a
  same-name different-instance collision), consistent with the build-stage
  `mergeCollectedPackEntities`.
- Fix the 0.14→0.15 upgrade note to the shipped `nativeEnum(name, …).map(typeName)`
  shape.

typecheck 143/143; contract-ts 389/389; postgres 106/106; target-postgres 686/686;
PSL<->TS parity 26/26; fixtures:check zero drift; lint:casts 1031;
lint:framework-vocabulary 906.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
@wmadden-electric
wmadden-electric added this pull request to the merge queue Jul 9, 2026
Merged via the queue into main with commit 9175ebb Jul 9, 2026
20 checks passed
@wmadden-electric
wmadden-electric deleted the tml-2965-native-enum-ts-authoring branch July 9, 2026 14:31
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants