@inixiative/prisma-map
v0.0.2
Published
Extracts a complete model map (fields, relations, FK info) from a Prisma generated client — supports v6 (schema.prisma) and v7 (runtimeDataModel)
Maintainers
Readme
@inixiative/prisma-map
Extract a structured, runtime-friendly model map from a Prisma generated client.
@inixiative/prisma-map builds a PrismaMap object that includes:
- Models and fields
- Field kinds (
scalar,enum,object) - Relation FK direction (
fromFields,toFields) - Model DB table name (
@@map) asdbName - Enum values (ordered)
It supports both Prisma client layouts:
- Prisma v7: parse generated
internal/class.ts - Prisma v6: parse generated
schema.prisma
Installation
bun add @inixiative/prisma-mapor
npm i @inixiative/prisma-mapQuick Start
Prisma v7
import { buildPrismaMapV7 } from '@inixiative/prisma-map';
const map = buildPrismaMapV7();
// or: buildPrismaMapV7('/absolute/path/to/generated/client')
console.log(map.User.fields.posts);Prisma v6
import { buildPrismaMapV6 } from '@inixiative/prisma-map';
const map = buildPrismaMapV6();
// or: buildPrismaMapV6('/absolute/path/to/generated/client')
console.log(map.Post.fields.author);Output Shape
The library returns:
type PrismaMap = Record<string, {
dbName: string | null;
fields: Record<string, ScalarField | EnumField | RelationField>;
}>;Scalar field
{
"kind": "scalar",
"type": "String",
"isRequired": true,
"isList": false,
"isId": false
}Enum field
{
"kind": "enum",
"type": "UserRole",
"isRequired": true,
"isList": false,
"values": ["ADMIN", "USER", "GUEST"]
}Relation field
{
"kind": "object",
"type": "User",
"isList": false,
"isRequired": true,
"relationName": "AuthoredPosts",
"fromFields": ["authorId"],
"toFields": ["id"]
}fromFields/toFields are empty arrays on back-relations.
Example End-to-End
Given:
enum Role {
ADMIN
USER
}
model User {
id String @id
role Role
posts Post[]
@@map("users")
}
model Post {
id String @id
authorId String
author User @relation("AuthoredPosts", fields: [authorId], references: [id])
}A representative map:
{
"User": {
"dbName": "users",
"fields": {
"id": {
"kind": "scalar",
"type": "String",
"isRequired": true,
"isList": false,
"isId": true
},
"role": {
"kind": "enum",
"type": "Role",
"isRequired": true,
"isList": false,
"values": ["ADMIN", "USER"]
},
"posts": {
"kind": "object",
"type": "Post",
"isList": true,
"isRequired": true,
"fromFields": [],
"toFields": []
}
}
},
"Post": {
"dbName": null,
"fields": {
"author": {
"kind": "object",
"type": "User",
"isList": false,
"isRequired": true,
"relationName": "AuthoredPosts",
"fromFields": ["authorId"],
"toFields": ["id"]
}
}
}
}API
Root exports:
import {
buildPrismaMapV6,
buildPrismaMapV7,
type PrismaMap,
type ModelEntry,
type ModelField,
type ScalarField,
type EnumField,
type RelationField,
} from '@inixiative/prisma-map';Version-specific exports:
import { buildPrismaMapV7, parseRuntimeDataModel, parseInlineSchema, parseRelationFks, parseEnumValues } from '@inixiative/prisma-map/v7';
import { buildPrismaMapV6, parseSchemaText, buildFromSchemaFile } from '@inixiative/prisma-map/v6';Auto-Detection Behavior
If you omit the path, the library walks upward from process.cwd() and checks common generated client locations:
node_modules/.prisma/clientnode_modules/@prisma/clientprisma/generated/clientsrc/generated/clientsrc/generated/prismagenerated/client
v7 detection requires internal/class.ts.
v6 detection requires schema.prisma and excludes directories that also have internal/class.ts.
Practical Use Cases
- Build rule-engine metadata for JSON rules
- Resolve template/package dependency graphs via relation edges
- Generate guardrails (for example, "entity X must include relation Y")
- Build field-level introspection UIs
Notes and Limits
- The output uses logical Prisma field names.
- Field-level DB aliases (
@mapon fields) are not emitted today. - Model DB alias (
@@map) is emitted asdbName. - Native DB type annotations (for example
@db.Text) are not emitted. relationNameis included when available in Prisma runtime metadata.
Development
bun run test
bun run typecheck
bun run check
bun run buildLicense
MIT
