Prisma Dmmf TransformerConceptsPrisma DMMF

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 definitions

Model 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 TypePostgreSQLMySQLSQLiteMongoDB
Stringtextvarchartextstring
Intintegerintintegerint
BigIntbigintbigintintegerlong
Floatdouble precisionfloatrealdouble
DecimaldecimaldecimalN/Adecimal
Booleanbooleanbooleanintegerbool
DateTimetimestampdatetimedatetimedate
Jsonjsonbjsontextobject
BytesbyteablobblobbinData

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: []
  }
}
Support

Contribute to our work and keep us going

Community is the heart of open source. The success of our packages wouldn't be possible without the incredible contributions of users, testers, and developers who collaborate with us every day.Want to get involved? Here are some tips on how you can make a meaningful impact on our open source projects.

Ready to help us out?

Be sure to check out the package's contribution guidelines first. They'll walk you through the process on how to properly submit an issue or pull request to our repositories.

Submit a pull request

Found something to improve? Fork the repo, make your changes, and open a PR. We review every contribution and provide feedback to help you get merged.

Good first issues

Simple issues suited for people new to open source development, and often a good place to start working on a package.
View good first issues