Generating the Client
Overview
The Prisma client is auto-generated using the prisma generate
command of the Prisma CLI. One Prisma client connects to exactly one Prisma service.
prisma generate
reads the information that's specified under the generate
root property in your service's prisma.yml
.
Configuring generate
in prisma.yml
The generate
root property accepts a list of objects. Each object has two fields:
generator
: The programming language in which the Prisma client should be generated.- Possible values:
typescript-client
,javascript-client
,flow-client
,go-client
. - Coming soon:
reason
.
- Possible values:
output
: The path and name of the file in which the Prisma client should be stored.
As an example, consider the following prisma.yml
:
datamodel: datamodel.prisma
endpoint: http://localhost:4000
generate:
- generator: typescript-client
output: ./prisma-client/
Running prisma generate
in the directory where that prisma.yml
is located generates a Prisma client in TypeScript and stores it in a file called prisma.ts
.
The
generator
also acceptsgraphql-schema
as a value. This downloads the GraphQL schema for your Prisma API via an introspection query.
Note that you can also reference environment variables in prisma.yml
by using the env:
-prefix and enclosing them with ${}
. For example, if you have an environment variable set that's called MY_SECRET
:
datamodel: datamodel.prisma
endpoint: http://localhost:4000
generate:
- generator: typescript-client
output: ./prisma-client/
secret: ${env:MY_SECRET}
You can find more info here.
More examples
As generate
accepts a list of objects, you might as well generate multiple files at once:
datamodel: datamodel.prisma
endpoint: http://localhost:4466
secret: mysecret42
generate:
- generator: typescript-client
output: ./prisma-client-ts/
- generator: javascript-client
output: ./prisma-client-js/
- generator: graphql-schema
output: ./graphql-schema/
Generated code
Assume the datamodel for a Prisma service is defined as follows:
type Link {
id: ID! @unique
createdAt: DateTime!
description: String!
url: String!
postedBy: User
votes: [Vote!]!
}
type User {
id: ID! @unique
name: String!
email: String! @unique
password: String!
links: [Link!]!
votes: [Vote!]!
}
type Vote {
id: ID! @unique
link: Link!
user: User!
}
You can view what the generated files look like here:
The generated Prisma client is already aware of the endpoint
and secret
that were specified in prisma.yml
at the time when the client was generated.