API Documentation
This is the API Documentation for the built-in schema-creator utility module. All of these functions, classes, and objects have type definitions to facilitate development.
SchemaCreator Class
The SchemaCreator
class is the entry-point to defining schemas that can be parsed by Prisma Util's Code Schemas feature. This class follows the singleton pattern, provides static functions for generating models and enums and allows chaining.
static model(name: string)
Create a new Model with the name specified. This function will return a Model.
static enum(name: string)
Create a new Enum with the name specified. This function will return an Enum.
static build()
Returns a string that can be passed to Prisma's Util Code Schemas feature.
Constraints
This object provides all of the attributes that a column or model can hold. Some attributes can have functions specified. It follows the exact naming from the official Prisma Schema API Reference and differentiates between column and model constraints by offering two objects:
DB(method: string, ...args: string[])
This function corresponds to Prisma's @db
attribute.
For example, if I wanted the string @db.VarChar(200)
, the usage of this function would be:
Column Object
This object provides all of the attributes that can be added to a column (these attributes use only one @ at the beginning). The names of the keys correspond to the official Prisma Schema API Reference provided above.
Model Object
This object provides all of the attributes that can be added to a model (these attributes use two @ symbols at the beginning). The names of the keys correspond to the official Prisma Schema API Reference provided above.
Functions
This object provides all of the functions that can be used with attributes. It follows the exact naming from the official Prisma Schema API Reference.
constantModel(path: Path)
This function will return a function that can be used in all places where a Path, FileModel or FileModelColumn is required. The returned function has this definition:
ConstantModel(model?: string, column?: string)
This function returns a different result based on the parameters passed to it:
No parameters are provided: This function will return a Path.
model
is provided, butcolumn
isn't: This function will return a FileModel.Both
model
andcolumn
are provided: This function will return a FileModelColumn.
constantGenerator(path: Path)
This function will return a function that can be used in all places where a FileGenerator is required. The returned function has this definition:
ConstantGenerator(generator?: string)
This function returns a different result based on the parameters passed to it:
No parameters are provided: This function will return a Path.
generator
is provided: This function will return a FileGenerator.
env(name: EnvironmentVariable, defaultValue?: string)
Returns: String
This function allows you to access environment variables and setting default values. The name
parameter represents the name of the environment variable that you are trying to access and defaultValue
represents a fallback value that will be used if name
isn't an environment variable.
importType(file: Path, typeName: string)
Returns: ImportedType
This function allows you to import a TypeScript type in your configuration file. The file
parameter represents a path to a .d.ts
file relative to your Types resolve root. The typeName
parameter represents the name of a type in the file
.
useEnv(path: string)
Returns: void
Add all environment variables from this file to the current process. The parameter path
represents a path relative to your project root.
globModels(base: Path, globPattern?: string)
Return: Path[]
This function will use glob to provide you an array of schema files that you can pass to Prisma Util's includeFiles
configuration array. The parameter explanation is given below:
base - The path to your base schema. This file will be excluded from the returned array.
globPattern - This parameter is optional. Allows you to specify a different glob pattern when searching for schemas. The default pattern is
**/*.prisma
.
The return value of this function is an array containing a Path for each file that matched the glob pattern and can be directly passed to includeFiles
.
execCommand(command: string)
Return: Promise<void>
This function will run the command
provided while passing the current stdin and stdout streams to the child. The return value of this function is a Promise that resolves after the command has been executed.
Internal Types
Schema Creator has built-in types that can't be imported. These types make the chaining pattern possible, because they hold a reference to the original SchemaCreator instance.
Model Class
This class provides an interface to add columns and constraints to a Model created using the model function. To make chaining possible, Model offers the same 3 static functions that SchemaCreator offers.
name(name: string)
This function allows you to change the name of this Model. For chaining purposes, it returns the current Model being edited.
column(name: string, type: string, ...constraints: string[])
This function allows you to create a new column for this Model. For chaining purposes, it returns the current Model being edited. Schema Creator provides a helper for all of the constraints (called attributes in Prisma) that you can use for columns, accessible via Constraints.Column.
constraints(...constraints: string[])
This function allows you to add constraints for this Model. For chaining purposes, it returns the current Model being edited. Schema Creator provides a helper for all of the constraints (called attributes in Prisma) that you can use for models, accessible via Constraints.Model.
model(name: string)
Create a new Model with the name specified. This function will return a Model.
enum(name: string)
Create a new Enum with the name specified. This function will return an Enum.
build()
Returns a string that can be passed to Prisma's Util Code Schemas feature.
Enum Class
This class provides an interface to add entries to an Enum created using the enum function. To make chaining possible, Enum offers the same 3 static functions that SchemaCreator offers.
name(name: string)
This function allows you to change the name of this Enum. For chaining purposes, it returns the current Enum being edited.
item(name: string)
This function allows you to add a new item to this Enum. For chaining purposes, it returns the current Enum being edited.
model(name: string)
Create a new Model with the name specified. This function will return a Model.
enum(name: string)
Create a new Enum with the name specified. This function will return an Enum.
build()
Returns a string that can be passed to Prisma's Util Code Schemas feature.
Last updated