---
description: >-
  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.
---

# API Documentation

## SchemaCreator Class

The `SchemaCreator` class is the entry-point to defining schemas that can be parsed by Prisma Util's [Code Schemas](../../optional-features/schema-improvements/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](#model-class).

### static enum(name: string)

Create a new Enum with the name specified. This function will return an [Enum](#enum-class).

### static build()

Returns a string that can be passed to Prisma's Util [Code Schemas](../../optional-features/schema-improvements/code-schemas) feature.

## Constraints

This object provides all of the attributes that a column or model can hold. Some attributes can have [functions](#functions) specified. It follows the exact naming from the official [Prisma Schema API Reference](https://www.prisma.io/docs/reference/api-reference/prisma-schema-reference#attributes) 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:

```javascript
Constraints.DB("VarChar", "200");
```

### 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](https://www.prisma.io/docs/reference/api-reference/prisma-schema-reference#attribute-functions).

## constantModel(path: [Path](../../api-documentation/types-and-objects/path))

This function will return a function that can be used in all places where a [Path](../../api-documentation/types-and-objects/path), [FileModel](../../api-documentation/types-and-objects/file-model) or [FileModelColumn](../../api-documentation/types-and-objects/file-model-column) 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](../../api-documentation/types-and-objects/path).
* `model` is provided, but `column` isn't: This function will return a [FileModel](../../api-documentation/types-and-objects/file-model).
* Both `model` and `column` are provided: This function will return a [FileModelColumn](../../api-documentation/types-and-objects/file-model-column).

## constantGenerator(path: [Path](../../api-documentation/types-and-objects/path))

This function will return a function that can be used in all places where a [FileGenerator](../../api-documentation/types-and-objects/file-generator) 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](../../api-documentation/types-and-objects/path).
* `generator` is provided: This function will return a [FileGenerator](../../api-documentation/types-and-objects/file-generator).

## env(name: [EnvironmentVariable](../../api-documentation/types-and-objects/environment-variable), 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](../../api-documentation/types-and-objects/path), typeName: string)

#### Returns: [ImportedType](../../api-documentation/types-and-objects/imported-type)

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](../../project-toolchain/api-documentation#types). 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](../../api-documentation/types-and-objects/path), globPattern?: string)

#### Return: [Path](../../api-documentation/types-and-objects/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](../../api-documentation/types-and-objects/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](#schemacreator-class) instance.

### Model Class

This class provides an interface to add columns and constraints to a Model created using the [model](#static-model-name-string) function. To make chaining possible, Model offers the same 3 static functions that [SchemaCreator](#schemacreator-class) 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](#column-object).

#### 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-object).

#### model(name: string)

Create a new Model with the name specified. This function will return a [Model](#model-class).

#### enum(name: string)

Create a new Enum with the name specified. This function will return an [Enum](#enum-class).

#### build()

Returns a string that can be passed to Prisma's Util [Code Schemas](../../optional-features/schema-improvements/code-schemas) feature.

### Enum Class

This class provides an interface to add entries to an Enum created using the [enum](#static-enum-name-string) function. To make chaining possible, Enum offers the same 3 static functions that [SchemaCreator](#schemacreator-class) 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](#model-class).

#### enum(name: string)

Create a new Enum with the name specified. This function will return an [Enum](#enum-class).

#### build()

Returns a string that can be passed to Prisma's Util [Code Schemas](../../optional-features/schema-improvements/code-schemas) feature.
