A TypeScript-powered builder to generate Prisma-compatible schema files programmatically. Define models, fields, and relationships using a clean API and generate .prisma schemas effortlessly.
Clone this repository and install dependencies:
npm install prismiq- Programmatic Prisma schema generation
- Define models and fields with types, defaults, primary keys, and unique constraints
- Add One-to-One, One-to-Many, Many-to-One, and Many-to-Many relationships
- Export the generated Prisma schema to a file
- Reset and reuse the builder easily
This document explains the parameters for each function in the SchemaBuilder TypeScript class.
Creates a new model with the specified name.
modelName: Name of the model to create.
addField(modelName: string, fieldName: string, type: Type, defaultValue?: Default, isUnique?: boolean)
Adds a field to an existing model with the specified attributes.
modelName: Name of the model to which the field belongs.fieldName: Name of the field to add.type: Type of the field. Supported types are "Int", "String", "Boolean", "Date", "float", "Json".defaultValue(optional): Default value for the field (e.g., "cuid()", "uid()", "now()").isUnique(optional): Specifies if the field should be unique.
Sets the primary key for a field in a model.
modelName: Name of the model.fieldName: Name of the field to set as the primary key.
addRelation(modelName: string, relationName: string, type: RelationType, relatedModel: string, inverseRelation?: string)
Defines a relationship between two models.
modelName: Name of the model where the relationship originates.relationName: Name of the relationship.type: Type of the relationship. Supported types are "OneToMany", "ManyToOne", "ManyToMany", "OneToOne".relatedModel: Name of the related model.inverseRelation(optional): Name of the inverse relationship.
Generates a Prisma schema based on the configured models and relationships.
Returns: A string containing the generated Prisma schema.
Saves the generated Prisma schema to a file.
FileName: Identifier for the schema file.
Resets the SchemaBuilder instance by clearing all models and configurations.
import {
createModel,
addField,
setPrimaryKey,
addRelation,
generatePrismaSchema,
getSchema,
savePrismaSchema,
reset
} from 'prismiq';createModel("User");addField("User", "id", "Int");
addField("User", "email", "String", undefined, true); // unique
addField("User", "createdAt", "Date", "now()"); // default now()setPrimaryKey("User", "id");addRelation("User", "profile", "OneToOne", "Profile", "user");addRelation("User", "posts", "OneToMany", "Post", "author");addRelation("Post", "author", "ManyToOne", "User", "posts");addRelation("User", "groups", "ManyToMany", "Group", "members");const schemaString = generatePrismaSchema();
console.log(schemaString);savePrismaSchema("schema");
// Output: ./prisma/schema.prismareset(); // clears all models and fieldscreateModel("User");
addField("User", "id", "Int");
addField("User", "email", "String", undefined, true);
setPrimaryKey("User", "id");
createModel("Profile");
addField("Profile", "id", "Int");
setPrimaryKey("Profile", "id");
addRelation("User", "profile", "OneToOne", "Profile", "user");
console.log(generatePrismaSchema());Feel free to open issues or submit pull requests!
MIT License