Prisma Dmmf TransformerConfiguration

Configuration

Last updated:

Configuration

Configure the Prisma DMMF transformer for your use case.

Basic Configuration

Pass options to the transformDMMF function:

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

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

Environment-Based Configuration

Use environment variables to control transformation:

const options = {
  keepRelationScalarFields: process.env.KEEP_RELATIONS === "true" ? "true" : "false",
  includeRequiredFields: process.env.STRICT_VALIDATION === "true" ? "true" : "false",
  schemaId: process.env.SCHEMA_ID,
};

const schema = transformDMMF(dmmf, options);

Configuration File

Create a reusable configuration:

// config/schema-transformer.ts
import type { TransformOptions } from "@visulima/prisma-dmmf-transformer";

export const productionConfig: TransformOptions = {
  keepRelationScalarFields: "false",
  includeRequiredFields: "true",
  persistOriginalType: "false",
  schemaId: "https://api.example.com/schema/v1",
};

export const developmentConfig: TransformOptions = {
  keepRelationScalarFields: "true",
  includeRequiredFields: "true",
  persistOriginalType: "true",
};

// Usage
import { productionConfig } from "./config/schema-transformer";

const schema = transformDMMF(dmmf, productionConfig);

Generator Configuration

Configure a Prisma generator:

// prisma/generators/json-schema.ts
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 transformOptions = {
      includeRequiredFields: options.generator.config.includeRequired === "true",
      keepRelationScalarFields: options.generator.config.keepRelations === "true",
      persistOriginalType: options.generator.config.persistTypes === "true",
      schemaId: options.generator.config.schemaId,
    };

    const schema = transformDMMF(options.dmmf, transformOptions);

    // Write schema...
  },
});

In schema.prisma:

generator jsonSchema {
  provider        = "./generators/json-schema.ts"
  output          = "./generated"
  includeRequired = "true"
  keepRelations   = "false"
  persistTypes    = "true"
  schemaId        = "https://api.example.com/schema"
}

Per-Model Configuration

Apply different options for different models:

function generateSchemas(dmmf: DMMF.Document) {
  const models = dmmf.datamodel.models;

  const schemas = models.map((model) => {
    // Use strict validation for sensitive models
    const options = model.name === "User" || model.name === "Payment"
      ? {
          includeRequiredFields: "true",
          keepRelationScalarFields: "false",
        }
      : {
          includeRequiredFields: "false",
          keepRelationScalarFields: "true",
        };

    return transformDMMF(dmmf, options);
  });

  return schemas;
}

Conditional Options

Apply options based on runtime conditions:

function getTransformOptions(environment: string): TransformOptions {
  const baseOptions: TransformOptions = {
    persistOriginalType: "true",
  };

  if (environment === "production") {
    return {
      ...baseOptions,
      includeRequiredFields: "true",
      keepRelationScalarFields: "false",
      schemaId: "https://api.example.com/schema/v1",
    };
  }

  if (environment === "staging") {
    return {
      ...baseOptions,
      includeRequiredFields: "true",
      keepRelationScalarFields: "true",
      schemaId: "https://staging-api.example.com/schema",
    };
  }

  // Development
  return {
    ...baseOptions,
    includeRequiredFields: "false",
    keepRelationScalarFields: "true",
  };
}

const options = getTransformOptions(process.env.NODE_ENV || "development");
const schema = transformDMMF(dmmf, options);
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