Transformation Options
Last updated:
Transformation Options
Configure the DMMF to JSON Schema transformation with various options.
TransformOptions
The transformDMMF function accepts an options object:
interface TransformOptions {
keepRelationScalarFields?: "true" | "false";
includeRequiredFields?: "true" | "false";
persistOriginalType?: "true" | "false";
schemaId?: string;
}keepRelationScalarFields
Control whether foreign key fields are included in the schema.
Type: "true" | "false"
Default: "false"
Example
model Post {
id Int @id
author User @relation(fields: [authorId], references: [id])
authorId Int
}With "false" (default):
{
"Post": {
"properties": {
"id": { "type": "integer" },
"author": { "$ref": "#/definitions/User" }
}
}
}With "true":
{
"Post": {
"properties": {
"id": { "type": "integer" },
"author": { "$ref": "#/definitions/User" },
"authorId": { "type": "integer" }
}
}
}Usage
const schema = transformDMMF(dmmf, {
keepRelationScalarFields: "true",
});When to use "true":
- Need foreign keys for validation
- Building API payloads with IDs
- Database migration tools
When to use "false":
- Documentation generation
- GraphQL schema generation
- Object-oriented APIs
includeRequiredFields
Add required array to schemas for validation.
Type: "true" | "false"
Default: "false"
Example
model User {
id Int @id
email String
name String?
}With "false" (default):
{
"User": {
"type": "object",
"properties": {
"id": { "type": "integer" },
"email": { "type": "string" },
"name": { "type": ["string", "null"] }
}
}
}With "true":
{
"User": {
"type": "object",
"properties": {
"id": { "type": "integer" },
"email": { "type": "string" },
"name": { "type": ["string", "null"] }
},
"required": ["id", "email"]
}
}Usage
const schema = transformDMMF(dmmf, {
includeRequiredFields: "true",
});When to use "true":
- Data validation
- Form validation
- API request validation
When to use "false":
- Documentation only
- Partial update schemas
- Flexible validation
persistOriginalType
Preserve Prisma type information in the schema.
Type: "true" | "false"
Default: "false"
Example
model User {
id Int @id
email String
createdAt DateTime
metadata Json
}With "false" (default):
{
"User": {
"properties": {
"id": { "type": "integer" },
"email": { "type": "string" },
"createdAt": { "type": "string", "format": "date-time" },
"metadata": { "type": "object" }
}
}
}With "true":
{
"User": {
"properties": {
"id": {
"type": "integer",
"prismaType": "Int"
},
"email": {
"type": "string",
"prismaType": "String"
},
"createdAt": {
"type": "string",
"format": "date-time",
"prismaType": "DateTime"
},
"metadata": {
"type": "object",
"prismaType": "Json"
}
}
}
}Usage
const schema = transformDMMF(dmmf, {
persistOriginalType: "true",
});When to use "true":
- Custom code generators
- Type mapping tools
- Prisma-specific tooling
When to use "false":
- Standard JSON Schema validation
- OpenAPI documentation
- Generic schema consumers
schemaId
Set the $id property for the schema.
Type: string
Default: undefined
Example
const schema = transformDMMF(dmmf, {
schemaId: "https://api.example.com/schema/v1",
});Output:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "https://api.example.com/schema/v1",
"type": "object",
"definitions": {}
}Usage
const schema = transformDMMF(dmmf, {
schemaId: "https://api.example.com/schema",
});
// References become absolute
{
"$ref": "https://api.example.com/schema#/definitions/User"
}When to use:
- Publishing schemas to a registry
- Cross-schema references
- API versioning
Combining Options
Use multiple options together:
const schema = transformDMMF(dmmf, {
keepRelationScalarFields: "true", // Include foreign keys
includeRequiredFields: "true", // Add validation constraints
persistOriginalType: "true", // Keep Prisma types
schemaId: "https://api.example.com", // Set schema ID
});Common Configurations
For Validation
transformDMMF(dmmf, {
includeRequiredFields: "true",
keepRelationScalarFields: "false",
});For Documentation
transformDMMF(dmmf, {
includeRequiredFields: "false",
keepRelationScalarFields: "false",
schemaId: "https://docs.example.com/schema",
});For Code Generation
transformDMMF(dmmf, {
includeRequiredFields: "true",
keepRelationScalarFields: "true",
persistOriginalType: "true",
});