docs/1.24/prisma-cli-and-configuration/prisma-yml-5cy7.mdx
import Warning from 'components/Markdown/Warning'
export const meta = { title: 'prisma.yml', position: 30, }
prisma.yml is the root configuration file for a Prisma service. Every Prisma service is defined by exactly one prisma.yml. You can think of prisma.yml as a template for one Prisma service.
prisma.yml specifies a number of properties about the Prisma service, e.g.:
The most minimal version of a valid prisma.yml needs to contains at least two properties:
datamodelendpointTo be able to specify an endpoint, you need to have access to a Prisma server. If you don't have access to a Prisma server, you can omit the endpoint from prisma.yml and run prisma deploy only with the datamodel property. In that case, a CLI wizard guides you through the process of creating a local Prisma server or lets you deploy to a Demo server in Prisma Cloud. It then writes the endpoint into prisma.yml for you before the service gets deployed:
datamodel: datamodel.prisma
In this example, the
databaseTypeis inferred and defaults torelational.
The most commonly used properties in prisma.yml are:
datamodelendpointsecrethooksgenerateHere's what a standard prisma.yml with these properties might look like:
datamodel: datamodel.prisma
endpoint: http://localhost:4466/myservice/dev
secret: mysecret42
generate:
- generator: javascript-client
output: ./generated/prisma
hooks:
post-deploy:
- prisma generate
In this example, the
databaseTypeis inferred and defaults torelational.
Here's an example where all properties of prisma.yml are being used:
# This service is based on the type definitions in the two files
# databasetypes.graphql` and `database/enums.graphql`
datamodel:
- database/types.prisma
- database/enums.prisma
# The endpoint represents the HTTP endpoint for your Prisma API.
# It encodes several pieces of information:
# * Prisma server (`localhost:4466` in this example)
# * Service name (`myservice` in this example)
# * Stage (`dev` in this example)
# NOTE: When service name and stage are set to `default`, they
# can be omitted.
# Meaning http://myserver.com/default/default can be written
# as http://myserver.com.
endpoint: http://localhost:4466/myservice/dev
# The secret is used to create JSON web tokens (JWTs). These
# tokens need to be attached in the `Authorization` header
# of HTTP requests made against the Prisma endpoint.
# WARNING: If the secret is not provided, the Prisma API can
# be accessed without authentication!
secret: mysecret123
# Generate a Prisma client in JavaScript and store in
# a folder called `generated/prisma-client`.
# It also downloads the Prisma GraphQL schema and stores it
# in `generated/prisma.graphql`.
generate:
- generator: javascript-client
output: ./generated/prisma-client
- generator: graphql-schema
output: ./generated/
# A "post-deployment" hook that regenerates
# the Prisma client.
hooks:
post-deploy:
- prisma generate
# This service has one event subscription configured. The
# corresponding subscription query is located in `database/subscriptions/welcomeEmail.graphql`.
# When the subscription fires, the specified `webhook`
# is invoked via HTTP.
subscriptions:
sendWelcomeEmail:
query: database/subscriptions/sendWelcomeEmail.graphql
webhook:
url: https://${self:custom.serverlessEndpoint}/sendWelcomeEmail
headers:
Authorization: ${env:MY_ENDPOINT_SECRET}
# Points to a `.graphql` file containing GraphQL
# operations that will be executed when initially
# deploying a service.
seed:
import: database/seed.graphql
# This service only defines one custom variable that's
# referenced in the `webhook` of the `subscription`
custom:
serverlessEndpoint: https://bcdeaxokbj.execute-api.eu-west-1.amazonaws.com/dev
In this example, the
databaseTypeis inferred and defaults torelational.
This prisma.yml expects the following file structure:
.
│ .graphqlconfig.yml
├── prisma.yml
└── database
├── subscriptions
│ └── welcomeEmail.graphql
├── types.graphql
└── enums.graphql
Variables allow you to dynamically replace configuration values in your prisma.yml. They are especially useful when providing secrets for your service and when you have a multi-staging developer workflows.
To use variables inside prisma.yml, you need to reference the values enclosed in ${} brackets:
yamlKeyXYZ: ${variableSource}
A variable source can be either of the following two options:
custom property)You can only use variables in property values - not in property keys. So you can't use variables to e.g. generate dynamic logical IDs in the custom resources section for example.
You can recursively reference other property values that live inside the same prisma.yml file.
When using a recursive self-reference as a variable, the value that you put into the bracket is composed of:
self:subscriptions:
sendWelcomeEmail:
query: database/subscriptions/sendWelcomeEmail.graphql
webhook:
url: https://${self:custom.serverlessEndpoint}/sendWelcomeEmail
custom:
serverlessEndpoint: example.org
This works for any property inside prisma.yml, not just
custom.
You can reference environment variables inside the service definition file.
When using an environment variable, the value that you put into the bracket is composed of:
env:In the following example, an environment variable is referenced to specify the URL and the authentication token for a webhook:
subscriptions:
sendWelcomeEmail:
query: database/subscriptions/sendWelcomeEmail.graphql
webhook:
url: https://example.org/sendWelcomeEmail
headers:
Authorization: ${env:MY_TOKEN}
The service definition file prisma.yml has the following root properties:
datamodel (required): Type definitions for database models, relations, enums and other types.endpoint: HTTP endpoint for the Prisma API. Can be omitted to prompt CLI deployment wizard.secret: Service secret for securing the API endpoint.generate: The list of files (Prisma client and/or Prisma GraphQL schema) to be generated.databaseType: Specifies whether the Prisma service uses a document ro relational database.hooks: Define CLI commands to be executed before/after specific actions of the Prisma CLI.seed: Points to a file containing mutations for data seeding.custom: Used to provide variables which can be referenced elsewhere in prisma.yml.The exact structure of prisma.yml is defined with JSON schema. You can find the corresponding schema definition here. The JSON schem definition also allows to elevate your tooling and let your code editors and IDEs help you with the right structure of prisma.yml.
The datamodel points to one or more .graphql-files containing model definitions written in GraphQL SDL. If multiple files are provided, the CLI simply concatenates their contents at deployment time.
The datamodel property expects a string or a list of strings.
The datamodel is defined in a file called types.graphql.
datamodel: types.graphql
The datamodel is defined in two files called types.graphql and enums.graphl. When the service gets deployed, the contents of both files will be concatenated by the CLI.
datamodel:
- types.graphql
- enums.graphql
The HTTP endpoint for your Prisma API is composed of the following components:
dev, staging, prod).Note that the endpoint is actually required to deploy your Prisma service. However, if you don't specify it in prisma.yml before running prisma deploy, the CLI prompts you with a wizard to help you figure out a Prisma server as deployment target and subsequently writes the endpoint into prisma.yml for you.
The endpoint property expects a string.
The following example endpoint encodes the following information:
http://localhost:4466 means you're running a Prisma server locally on your machine (e.g. using Docker).defaultdefaultWhen service name and stage are both set to
default, they can be omitted and will be inferred by Prisma. This means this example endpoint is equivalent to writing:http://localhost:4466/
endpoint: http://localhost:4466/default/default
The following example endpoint encodes the following information:
https://eu1.prisma.sh means you're using a Prisma Demo server as a deployment target for your Prisma service.jane-doe is the name of your Prisma Cloud workspace.myservicedevendpoint: https://eu1.prisma.sh/jane-doe/myservice/dev
The following example endpoint encodes the following information:
http://my-pr-Publi-1GXX8QUZU3T89-413349553.us-east-1.elb.amazonaws.com means you're using a Prisma server hosted on AWS to deploy your Prisma service.cat-picturesprodendpoint: http://my-pr-Publi-1GXX8QUZU3T89-413349553.us-east-1.elb.amazonaws.com/cat-pictures/prod
The service secret is used to generate (or sign) authentication tokens (JWT). One of these authentication tokens needs to be attached to the HTTP requests made against the Prisma API exposes by the service (in the Authorization header field).
A secret must follow these requirements:
Note that it's possible to encode multiple secrets in this string, which allows for smooth secret rotations.
<Warning>If the Prisma service is deployed without a secret, its API does not require authentication. This means everyone with access to the endpoint is able to send queries and mutations to the API and can therefore arbitrarily read and write to the database!
The secret property expects a string (not a list of strings). If you want to specify multiple secrets, you need to provide them as a comma-separated list (spaces are ignored), but still as a single string value.
Define one secret with value moo4ahn3ahb4phein1eingaep.
secret: moo4ahn3ahb4phein1eingaep
Define three secrets with values myFirstSecret, SECRET_NUMBER_2 and 3rd-secret. Note that the spaces before the second secret are ignored.
secret: myFirstSecret, SECRET_NUMBER_2,3rd-secret
Use the value of the MY_SECRET environment variable as the secret(s).
secret: ${env:MY_SECRET}
The generate property is used to specify how and where a Prisma client (or other files) should be generated.
The following generators are built-into the Prisma CLI:
javascript-clienttypescript-clientflow-clientgo-clientgraphql-schemaThe generate property expects a list of objects. There are two properties on these objects:
generator: One of the available generators from the list above.output: Specifies where the generated files should be located.generate:
- generator: javascript-client
output: ./generated/prisma-client
- generator: graphql-schema
output: ./generated/prisma
The databaseType property expects a string. The value of the string can either be relational or document. If the databaseType property is omitted, its value defaults to relational.
Use a document database.
databaseType: document
Use a document database.
databaseType: relational
The hooks property is used to define terminal commands which will be executed by the Prisma CLI before or after certain commands.
The following hooks are currently available:
post-deploy: Will be invoked after the prisma deploy commandThe hooks property expects an object. The properties of that object match the names of the currently available hooks.
Here is an example that performs two tasks after prisma deploy was executed:
prisma generatehooks:
post-deploy:
- echo "Deployment finished"
- prisma generate
Database seeding is a standardised way to populate a service with test data.
The seed property expects an object, with either one of two sub-properties:
import: Instructions to import data when seeding a service. You can refer to either of two kinds of files:
.graphql file with GraphQL operations..zip file that contains a data set in Normalized Data Format (NDF)run: Shell command that will be executed when seeding a service. This is meant for more complex seed setups that are not covered by import.Seeds are implicitly executed when deploying a service for the first time (unless explicitly disabled using the --no-seed flag on prisma deploy). Track this feature request for additional seeding workflows.
Refer to a .graphql file containing seeding mutations:
seed:
import: database/seed.graphql
Refer to a .zip file with a data set in NDF:
seed:
import: database/backup.zip
Run a Node script when seeding:
seed:
run: node script.js
The custom property lets you specify any sorts of values you want to reuse elsewhere in your prisma.yml. It thus doesn't have a predefined structure. You can reference the values using variables with the self variable source, e.g.: ${self:custom.myVariable}.
The custom property expects an object. There are no assumptions about the shape of the object.
Define two custom values and reuse them in the definition of the event subscription.
custom:
serverlessEndpoint: https://bcdeaxokbj.execute-api.eu-west-1.amazonaws.com/dev
subscriptionQueries: database/subscriptions/
subscriptions:
sendWelcomeEmail:
query: ${self:custom.subscriptionQueries}/sendWelcomeEmail.graphql
webhook: https://${self:custom.serverlessEndpoint}/sendWelcomeEmail