0
# Client Generation
1
2
Generate type-safe Prisma Client from schema definitions with support for multiple generators, watch mode, and specialized output configurations.
3
4
## Capabilities
5
6
### Generate Command
7
8
Creates Prisma Client and other artifacts based on schema configuration with full type safety and customization options.
9
10
```bash { .api }
11
/**
12
* Generate Prisma Client and artifacts from schema
13
* Reads prisma/schema.prisma by default or custom schema path
14
*/
15
prisma generate [options]
16
17
Options:
18
--schema <path> Custom schema file path
19
--sql Generate typed SQL module for direct queries
20
--watch Watch mode for continuous generation on file changes
21
--generator <name> Target specific generator by name
22
--no-engine Generate for Accelerate only (no local engine)
23
--no-hints Hide promotional hint messages
24
--allow-no-models Allow generation from schemas without models
25
--require-models Require at least one model in schema
26
--help/-h Show generate command help
27
```
28
29
**Usage Examples:**
30
31
```bash
32
# Basic generation from default schema
33
prisma generate
34
35
# Generate with custom schema location
36
prisma generate --schema ./custom/schema.prisma
37
38
# Watch mode for development
39
prisma generate --watch
40
41
# Generate only specific generator
42
prisma generate --generator client
43
44
# Generate with SQL support for typed queries
45
prisma generate --sql
46
47
# Generate for Accelerate deployment (no local engine)
48
prisma generate --no-engine
49
```
50
51
### SQL Module Generation
52
53
Generate typed SQL module for direct database queries with full type safety and IntelliSense support.
54
55
```bash { .api }
56
/**
57
* Generate typed SQL module alongside Prisma Client
58
* Creates sql/ directory with typed query functions
59
*/
60
prisma generate --sql [options]
61
```
62
63
**Generated SQL Module Usage:**
64
65
```typescript
66
// Generated SQL module provides typed queries
67
import { sql } from './generated/sql';
68
69
// Type-safe raw queries
70
const users = await sql`
71
SELECT id, email, name
72
FROM "User"
73
WHERE active = ${true}
74
`;
75
76
// Introspected query functions
77
const getUsersByEmail = sql.getUsersByEmail;
78
const result = await getUsersByEmail('test@example.com');
79
```
80
81
### Watch Mode
82
83
Continuous generation with file system watching for development workflows.
84
85
```bash { .api }
86
/**
87
* Enable watch mode for continuous generation
88
* Monitors schema files and automatically regenerates on changes
89
*/
90
prisma generate --watch [options]
91
```
92
93
**Watch Mode Features:**
94
95
- Monitors schema file and related files for changes
96
- Automatic regeneration on file modifications
97
- Debounced updates to prevent excessive regeneration
98
- Console output showing generation status
99
- Error reporting for failed generations
100
101
### Generator Selection
102
103
Target specific generators when multiple are configured in the schema.
104
105
```bash { .api }
106
/**
107
* Generate artifacts for specific generator only
108
* Useful when schema has multiple generator blocks
109
*/
110
prisma generate --generator <name> [options]
111
```
112
113
**Usage with Multiple Generators:**
114
115
```prisma
116
// schema.prisma with multiple generators
117
generator client {
118
provider = "prisma-client-js"
119
output = "./generated/client"
120
}
121
122
generator docs {
123
provider = "prisma-docs-generator"
124
output = "./generated/docs"
125
}
126
```
127
128
```bash
129
# Generate only client
130
prisma generate --generator client
131
132
# Generate only docs
133
prisma generate --generator docs
134
```
135
136
### Accelerate Integration
137
138
Generate Prisma Client optimized for Prisma Accelerate deployment without local database engines.
139
140
```bash { .api }
141
/**
142
* Generate client for Accelerate deployment
143
* Excludes binary engines for serverless environments
144
*/
145
prisma generate --no-engine [options]
146
```
147
148
**Accelerate Configuration:**
149
150
```typescript
151
// Generated client works with Accelerate connection string
152
import { PrismaClient } from '@prisma/client'
153
154
const prisma = new PrismaClient({
155
datasourceUrl: process.env.ACCELERATE_URL, // Accelerate connection string
156
})
157
```
158
159
### Schema Validation Options
160
161
Control schema validation behavior during generation process.
162
163
```bash { .api }
164
/**
165
* Control model requirements during generation
166
*/
167
prisma generate --allow-no-models # Allow schemas without model definitions
168
prisma generate --require-models # Require at least one model (default)
169
```
170
171
**Schema Examples:**
172
173
```prisma
174
// Valid with --allow-no-models
175
generator client {
176
provider = "prisma-client-js"
177
}
178
179
datasource db {
180
provider = "postgresql"
181
url = env("DATABASE_URL")
182
}
183
184
// No models defined - normally fails, allowed with --allow-no-models
185
```
186
187
### Generation Process
188
189
The generation process includes several phases:
190
191
1. **Schema Parsing**: Validate and parse Prisma schema file
192
2. **Generator Resolution**: Identify and configure generators
193
3. **Engine Download**: Download required database engines (unless --no-engine)
194
4. **Artifact Generation**: Generate client code and other artifacts
195
5. **Type Generation**: Create TypeScript definitions
196
6. **File Writing**: Write generated files to output directories
197
198
### Error Handling
199
200
Common generation errors and solutions:
201
202
- **Schema Syntax Errors**: Invalid Prisma schema syntax
203
- **Generator Conflicts**: Multiple generators with same output path
204
- **Database Connection**: Issues connecting during introspection
205
- **Engine Download**: Network issues downloading engines
206
- **Permission Errors**: File system permission issues
207
- **Dependency Issues**: Missing generator dependencies
208
209
### Integration
210
211
Generated Prisma Client integrates with:
212
213
- **TypeScript**: Full type safety and IntelliSense
214
- **Node.js**: Runtime support for server applications
215
- **Serverless**: Optimized bundles for serverless deployment
216
- **Edge Runtime**: Compatible with edge computing platforms
217
- **Build Tools**: Integration with webpack, Vite, etc.
218
- **Testing**: Mock client for unit testing