Prisma DMMF
Last updated:
Prisma DMMF
Understanding Prisma's Data Model Meta Format (DMMF) and how it relates to JSON Schema.
What is DMMF?
DMMF (Data Model Meta Format) is Prisma's internal representation of your database schema. It contains:
- Models - Database tables/collections
- Fields - Model properties and their types
- Relations - Relationships between models
- Enums - Enumeration types
- Indexes - Database indexes
- Constraints - Unique constraints, foreign keys
DMMF Structure
The DMMF document contains three main sections:
{
datamodel: {
models: Model[], // Your Prisma models
enums: Enum[], // Enum definitions
types: Type[] // Composite types
},
schema: {
// Input/output types for queries
},
mappings: {
// Model to database table mappings
}
}Accessing DMMF
Access DMMF through Prisma Client's internal API:
import { PrismaClient } from "@prisma/client";
const prisma = new PrismaClient();
// Access internal DMMF method
const dmmf = await (prisma as any)._getDmmf();
console.log(dmmf.datamodel.models);
// Output: Array of model definitionsModel Definition
Each model in DMMF contains:
{
name: "User",
fields: [
{
name: "id",
kind: "scalar",
type: "Int",
isRequired: true,
isList: false,
isId: true
},
{
name: "email",
kind: "scalar",
type: "String",
isRequired: true,
isList: false,
isUnique: true
},
{
name: "posts",
kind: "object",
type: "Post",
isRequired: true,
isList: true,
relationName: "UserPosts"
}
]
}Field Types
DMMF distinguishes between field kinds:
Scalar Fields
Primitive types:
model User {
id Int @id
email String @unique
name String?
age Int
}DMMF representation:
{
name: "email",
kind: "scalar",
type: "String",
isRequired: true
}Object Fields
Relations to other models:
model User {
posts Post[]
}
model Post {
author User @relation(fields: [authorId], references: [id])
authorId Int
}DMMF representation:
{
name: "posts",
kind: "object",
type: "Post",
isList: true,
relationName: "UserPosts"
}Enum Fields
Enumeration types:
enum Role {
USER
ADMIN
}
model User {
role Role @default(USER)
}DMMF representation:
{
name: "role",
kind: "enum",
type: "Role",
isRequired: true
}Relations
DMMF represents relationships between models:
One-to-Many
model User {
id Int @id
posts Post[]
}
model Post {
id Int @id
author User @relation(fields: [authorId], references: [id])
authorId Int
}DMMF includes:
- Relation name (
UserPosts) - Foreign key field (
authorId) - Referenced field (
id) - Relation direction (one-to-many)
Many-to-Many
model Post {
categories Category[]
}
model Category {
posts Post[]
}DMMF includes implicit join table information.
Enums
Enum definitions in DMMF:
enum Status {
DRAFT
PUBLISHED
ARCHIVED
}DMMF representation:
{
name: "Status",
values: [
{ name: "DRAFT" },
{ name: "PUBLISHED" },
{ name: "ARCHIVED" }
]
}Type Mapping
Prisma types map to database types:
| Prisma Type | PostgreSQL | MySQL | SQLite | MongoDB |
|---|---|---|---|---|
| String | text | varchar | text | string |
| Int | integer | int | integer | int |
| BigInt | bigint | bigint | integer | long |
| Float | double precision | float | real | double |
| Decimal | decimal | decimal | N/A | decimal |
| Boolean | boolean | boolean | integer | bool |
| DateTime | timestamp | datetime | datetime | date |
| Json | jsonb | json | text | object |
| Bytes | bytea | blob | blob | binData |
Comments and Documentation
DMMF preserves triple-slash comments:
/// User account model
model User {
id Int @id
/// User's email address
email String @unique
// This comment is ignored
name String?
}Triple-slash comments (///) are included in DMMF, while double-slash comments (//) are not.
Default Values
DMMF includes default value information:
model User {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
role Role @default(USER)
}DMMF representation:
{
name: "id",
hasDefaultValue: true,
default: {
name: "autoincrement",
args: []
}
}