Prisma Dmmf TransformerIntroduction

Introduction

Transform Prisma DMMF (Data Model Meta Format) to JSON Schema v7 for validation and documentation generation

Last updated:

@visulima/prisma-dmmf-transformer

Transform Prisma's internal Data Model Meta Format (DMMF) to JSON Schema v7, enabling schema validation, documentation generation, and integration with JSON Schema tools.

Key Features

Prisma Integration

  • DMMF Access - Extract Prisma's internal data model structure
  • Model Transformation - Convert Prisma models to JSON Schema definitions
  • Enum Support - Transform Prisma enums to JSON Schema enums
  • Type Mapping - Map Prisma types to JSON Schema types

JSON Schema Generation

  • v7 Compliance - Generates valid JSON Schema v7 output
  • Definitions - Creates reusable schema definitions
  • Properties - Generates properties with type references
  • Validation - Includes field validation constraints

Configuration Options

  • Relation Fields - Control scalar field exposure (keepRelationScalarFields)
  • Required Fields - Include required field constraints (includeRequiredFields)
  • Original Types - Preserve Prisma type information (persistOriginalType)
  • Schema ID - Set custom schema identifier (schemaId)

Quick Start

import { PrismaClient } from "@prisma/client";
import { transformDMMF } from "@visulima/prisma-dmmf-transformer";

const prisma = new PrismaClient();

// Get DMMF from Prisma
const dmmf = await (prisma as any)._getDmmf();

// Transform to JSON Schema
const jsonSchema = transformDMMF(dmmf, {
  keepRelationScalarFields: "true",
  includeRequiredFields: "true",
  persistOriginalType: "true",
  schemaId: "https://example.com/schema",
});

console.log(jsonSchema);
// Output: JSON Schema v7 with model definitions

Use Cases

Schema Validation

Validate data against Prisma models using JSON Schema validators:

import { transformDMMF } from "@visulima/prisma-dmmf-transformer";
import Ajv from "ajv";

const dmmf = await (prisma as any)._getDmmf();
const schema = transformDMMF(dmmf, {
  includeRequiredFields: "true",
});

const ajv = new Ajv();
const validate = ajv.compile(schema.definitions.User);

const valid = validate({
  id: 1,
  email: "user@example.com",
  name: "John Doe",
});

if (!valid) {
  console.error("Validation errors:", validate.errors);
}

API Documentation

Generate OpenAPI/Swagger documentation from Prisma models:

import { transformDMMF } from "@visulima/prisma-dmmf-transformer";

const dmmf = await (prisma as any)._getDmmf();
const schema = transformDMMF(dmmf, {
  schemaId: "https://api.example.com/schema",
  persistOriginalType: "true",
});

// Use in OpenAPI spec
const openApiSpec = {
  openapi: "3.0.0",
  components: {
    schemas: schema.definitions,
  },
  paths: {
    "/users": {
      get: {
        responses: {
          200: {
            content: {
              "application/json": {
                schema: { $ref: "#/components/schemas/User" },
              },
            },
          },
        },
      },
    },
  },
};

Custom Generators

Build custom Prisma generators using the transformer:

import { generatorHandler } from "@prisma/generator-helper";
import { transformDMMF } from "@visulima/prisma-dmmf-transformer";

generatorHandler({
  onManifest: () => ({
    defaultOutput: "./generated",
    prettyName: "JSON Schema Generator",
  }),
  onGenerate: async (options) => {
    const schema = transformDMMF(options.dmmf, {
      includeRequiredFields: "true",
    });

    // Write schema to file
    await writeFile(
      "./generated/schema.json",
      JSON.stringify(schema, null, 2)
    );
  },
});

Next Steps

Browser and Server Support

This package is designed for server-side use:

  • Node.js - Version 18.x and above
  • Prisma - Version 3.10.0 and above
  • Environment - Server-side only (requires Prisma Client)
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