[Drizzle ORM]: feat: Add getTableMetadata for JSON-serializable table introspection - #5773
Open
nbouvrette wants to merge 2 commits into
Open
[Drizzle ORM]: feat: Add getTableMetadata for JSON-serializable table introspection#5773nbouvrette wants to merge 2 commits into
nbouvrette wants to merge 2 commits into
Conversation
… introspection Adds a dialect-agnostic `getTableMetadata(table)` API to drizzle-orm that extracts plain, JSON-serializable metadata from any Drizzle Table — column name, dataType, columnType, sqlType, notNull, hasDefault, primary, isUnique, generated/generatedIdentity, enumValues, plus dialect-specific extras (length, dimensions, size, textType, baseColumn for arrays) when applicable. The output is a pure-data object: no functions, SQL fragments, or Symbol-keyed properties. This unblocks bundle-friendly schema-introspection patterns for drizzle-zod / valibot / typebox / arktype, which today require importing the runtime Table object (and transitively the full pg-core / mysql-core / sqlite-core / singlestore-core runtimes) into client bundles. See drizzle-team#941. Works across all four dialects (pg, mysql, sqlite, singlestore) via a single generic export. Existing `getTableConfig` is unchanged. Tests: 32 new tests across four dialect-specific files plus a JSON round-trip serialization contract test. Full drizzle-orm suite passes (599/599). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Author
|
@AndriiSherman @Sukairo-02 any chance you could consider this PR? its an highly voted issue and I'm happy to get it to the finish line with whatever feedback you can provide. |
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.
Summary
Adds a new dialect-agnostic public export to
drizzle-orm:getTableMetadatawalks a DrizzleTableand produces a plain, JSON-serializable object containing the column properties that downstream tools care about:name,dataType,columnType,sqlType,notNull,hasDefault,primary,isUnique,generated,generatedIdentity,enumValues, plus dialect-specific extras when present (length,dimensions,size,textType,baseColumnfor arrays — recursive).Key properties:
JSON.parse(JSON.stringify(meta))deep-equalsmetagetTableConfigis untouchedWhy
Today, validation extensions like
drizzle-zod(anddrizzle-valibot,drizzle-typebox,drizzle-arktype) require users to import the runtimeTableobject to derive a Zod schema. That import transitively pulls inpgTable, every column constructor, and the rest ofdrizzle-orm/pg-coreinto shared client bundles where Zod schemas are used (tRPC + React forms, etc.). Measured cost in a real Vite-built app: ~56 KB gzipped on first load.Exposing the metadata as a plain object lets downstream packages (and any third-party codegen tool) operate on data alone — no need to import
Tableat runtime on the client. This is the foundation issue #941 has been waiting on: with this in place, a thindrizzle-zodcompanion entry that consumesTableMetadatadirectly becomes possible, and the runtime cost drops to a few KB. (Follow-up PRs indrizzle-kitanddrizzle-zodcan land independently; this PR stands on its own.)The shape was chosen by reading what
drizzle-zod,drizzle-valibot,drizzle-typebox, anddrizzle-arktypeall currently consume fromColumninstances — a strict superset of what each needs, and a strict subset of whatgetTableConfigreturns. FK references, indexes, composite primary keys, checks, RLS, and policies are intentionally not included: none of the validation extensions consume them, and including dialect-specific extras would force the function to import per-dialect packages.What this PR is NOT
createInsertSchema/createSelectSchemaindrizzle-zoddrizzle-kitgetTableConfigIt's a pure additive primitive that other packages can build on.
Test plan
tests/get-table-metadata-{pg,mysql,sqlite,singlestore,serializable}.test.tsnotNull/hasDefault/primary/isUniqueflags, dialect-specific extras (length,dimensions,size,textType), recursive arraybaseColumn,enumValues,generated/generatedIdentityJSON.parse(JSON.stringify(getTableMetadata(t)))deep-equals the original (guarantees no functions, SQL, or Symbols leak through)pnpm testindrizzle-orm/is green: 599/599 tests pass, includingexports.test.ts(no export-name conflicts)Run locally with: