prisma-ast
Parse, query, and modify Prisma schema files as an AST in JavaScript while preserving comments and formatting.
Repository Health
Technical Analysis
prisma-ast uses an abstract syntax tree to parse schema.prisma files into plain JavaScript objects, then lets you inspect, edit, and reprint them without losing comments, model attributes, or formatting. Unlike parsers focused on validation, it prioritizes lossless round-tripping so you can programmatically rewrite Prisma schemas.
It ships a fluent PrismaSchemaBuilder for constructing and mutating models, fields, attributes, enums, and block attributes, plus helpers to produce a modified schema from an existing source. This makes it a foundation for codegen, migration tooling, linting, and any automation that needs to read or transform Prisma schemas.
What You Get
- A lossless parser (getSchema) that converts schema.prisma source into an AST object
- A fluent PrismaSchemaBuilder for adding and editing models, fields, attributes, and enums
- produceSchema, a one-call helper to apply modifications to an existing schema source
- A printer (printSchema) with optional sorting and locale-aware ordering
- Query helpers like findByType to locate models, fields, and attributes by name or context
Common Use Cases
- Generating or modifying Prisma models programmatically in codegen scripts
- Writing custom linters that enforce field naming or attribute conventions
- Building migration and refactoring tools that rewrite schemas without losing comments
- Writing tests that assert the structure of a Prisma schema
Under The Hood
Architecture — The pipeline flows through a chevrotain-based lexer (src/lexer.ts) and parser (src/parser.ts) that build a concrete syntax tree, a visitor (src/visitor.ts) that turns it into the AST, and printSchema.ts that serializes back to source. getSchema.ts is the parse entry point, PrismaSchemaBuilder.ts wraps mutation and querying (finder.ts) over the AST, and produceSchema.ts composes build-then-print in one call.
Tech Stack — Written in TypeScript with only two runtime dependencies: chevrotain for lexing/parsing and lilconfig for reading optional configuration (e.g. parser.nodeTrackingLocation). Built with dts-cli and Babel, tested with Jest, and size-checked with size-limit.
Code Quality — The test/ directory covers getSchema, printSchema, produceSchema, PrismaSchemaBuilder, and finder with snapshot fixtures, indicating solid regression coverage. Source is split into small single-responsibility modules with descriptive names and full TypeScript typing.
API Design — The public surface is deliberately small and ergonomic: produceSchema for the common case, createPrismaSchemaBuilder for interactive editing, and getSchema/printSchema for direct AST access. The chainable builder reads naturally and the README documents each entry point with runnable examples.