What is @prisma/generator-helper?
@prisma/generator-helper is a utility package designed to assist in the creation of custom generators for Prisma. It provides a set of tools and types to facilitate the development of generators that can extend or customize the behavior of Prisma's schema and client generation.
What are @prisma/generator-helper's main functionalities?
Generator Definition
This feature allows you to define a custom generator by specifying the manifest and generation logic. The `onManifest` function provides metadata about the generator, while the `onGenerate` function contains the logic to execute when the generator is run.
const { generatorHandler } = require('@prisma/generator-helper');
generatorHandler({
onManifest() {
return {
defaultOutput: 'default-output-path',
prettyName: 'My Custom Generator',
};
},
onGenerate(options) {
console.log('Generating with options:', options);
},
});
Helper Types
The package provides TypeScript types for Prisma's Data Model Meta Format (DMMF), which can be used to type-check and process the Prisma schema programmatically.
const { DMMF } = require('@prisma/generator-helper');
/**
* @param {DMMF.Document} dmmf
*/
function processDMMF(dmmf) {
console.log('Processing DMMF:', dmmf);
}
File Writing Utilities
This feature includes utilities for safely writing files, ensuring that directories are created as needed and that existing files are not overwritten unintentionally.
const { writeFileSafely } = require('@prisma/generator-helper');
const path = require('path');
const outputPath = path.join(__dirname, 'output.txt');
writeFileSafely(outputPath, 'Generated content').then(() => {
console.log('File written successfully');
});
Other packages similar to @prisma/generator-helper
yeoman-generator
Yeoman Generator is a robust scaffolding tool for building out new projects. It provides a higher-level abstraction for creating generators, including prompts, file system utilities, and more. Compared to @prisma/generator-helper, Yeoman is more general-purpose and not specifically tailored to Prisma.
plop
Plop is a micro-generator framework that makes it easy to create code generators with a simple API. It focuses on simplicity and ease of use, making it a good alternative for smaller, less complex generator tasks. Unlike @prisma/generator-helper, Plop is not specifically designed for Prisma and lacks Prisma-specific utilities and types.
hygen
Hygen is a fast and lightweight code generator that uses plain text templates. It is designed for quick and easy setup and usage. While it is versatile and can be used for various generation tasks, it does not offer the Prisma-specific features and types provided by @prisma/generator-helper.
@prisma/generator-helper
⚠️ Warning: This package is intended for Prisma's internal use.
Its release cycle does not follow SemVer, which means we might release breaking changes (change APIs, remove functionality) without any prior warning.
If you are using this package, it would be helpful if you could help us gain an understanding where, how and why you are using it. Your feedback will be valuable to us to define a better API. Please share this information at https://github.com/prisma/prisma/discussions/13877 - Thanks!