Prisma Dmmf TransformerQuick Start

Quick Start

Last updated:

Quick Start

Transform Prisma DMMF to JSON Schema in under 5 minutes.

Basic Transformation

Transform your Prisma schema to JSON Schema:

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

const prisma = new PrismaClient();

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

// Transform
const jsonSchema = transformDMMF(dmmf);

console.log(jsonSchema);

Output structure:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "definitions": {
    "User": {
      "type": "object",
      "properties": {
        "id": { "type": "integer" },
        "email": { "type": "string" },
        "name": { "type": ["string", "null"] }
      }
    },
    "Post": {
      "type": "object",
      "properties": {
        "id": { "type": "integer" },
        "title": { "type": "string" },
        "content": { "type": ["string", "null"] }
      }
    }
  },
  "properties": {
    "user": { "$ref": "#/definitions/User" },
    "post": { "$ref": "#/definitions/Post" }
  }
}

With Options

Customize the transformation:

const schema = transformDMMF(dmmf, {
  keepRelationScalarFields: "true",    // Keep foreign keys
  includeRequiredFields: "true",        // Add required fields
  persistOriginalType: "true",          // Keep Prisma types
  schemaId: "https://api.example.com",  // Set schema ID
});

Validate Data

Use the generated schema for validation:

import Ajv from "ajv";

const schema = transformDMMF(dmmf, {
  includeRequiredFields: "true",
});

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

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

const valid = validate(user);

if (!valid) {
  console.error("Validation errors:", validate.errors);
} else {
  console.log("Valid user data!");
}

Generate Documentation

Create API documentation from Prisma models:

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

const schema = transformDMMF(dmmf, {
  schemaId: "https://api.example.com/schema",
});

// Save schema
writeFileSync(
  "./api-schema.json",
  JSON.stringify(schema, null, 2)
);

console.log("Schema saved to api-schema.json");

Access Specific Models

Extract individual model schemas:

const schema = transformDMMF(dmmf);

// Get User model schema
const userSchema = schema.definitions.User;
console.log(userSchema);

// Get all model names
const modelNames = Object.keys(schema.definitions);
console.log("Models:", modelNames);
// Output: Models: ['User', 'Post', ...]

Custom Generator

Build a Prisma generator:

// prisma/generators/json-schema.ts
import { generatorHandler } from "@prisma/generator-helper";
import { transformDMMF } from "@visulima/prisma-dmmf-transformer";
import { writeFileSync } from "fs";

generatorHandler({
  onManifest: () => ({
    defaultOutput: "./generated",
    prettyName: "JSON Schema Generator",
  }),

  onGenerate: async (options) => {
    const schema = transformDMMF(options.dmmf, {
      includeRequiredFields: "true",
      persistOriginalType: "true",
    });

    writeFileSync(
      `${options.generator.output.value}/schema.json`,
      JSON.stringify(schema, null, 2)
    );
  },
});

Add to schema.prisma:

generator jsonSchema {
  provider = "./generators/json-schema.ts"
  output   = "./generated"
}

Run generator:

npx prisma generate

Next Steps

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