0
# Seeder Commands
1
2
MikroORM CLI seeding system for populating databases with initial or test data. Provides commands for creating seeder classes and executing them to insert structured data into the database.
3
4
## Capabilities
5
6
### Run Seeder
7
8
Executes a seeder class to populate the database with data. Supports both explicit seeder specification and default seeder execution.
9
10
```typescript { .api }
11
/**
12
* Run seeder class command
13
*/
14
command: "seeder:run"
15
16
interface SeederRunOptions extends BaseArgs {
17
class?: string; // Seeder class name to execute (--class / -c)
18
}
19
```
20
21
**Usage Examples:**
22
23
```bash
24
# Run default seeder (from configuration)
25
mikro-orm seeder:run
26
27
# Run specific seeder class
28
mikro-orm seeder:run --class UserSeeder
29
30
# Run seeder with custom config
31
mikro-orm seeder:run --class TestDataSeeder --config ./test.config.js
32
33
# Run for specific context
34
mikro-orm seeder:run --class ProductionSeeder --context production
35
```
36
37
### Create Seeder
38
39
Creates a new seeder class file with proper structure and naming conventions. Handles class name formatting and file generation.
40
41
```typescript { .api }
42
/**
43
* Create new seeder class command
44
*/
45
command: "seeder:create <seeder>"
46
47
interface CreateSeederOptions extends BaseArgs {
48
seeder: string; // Seeder name (required positional argument)
49
}
50
```
51
52
**Usage Examples:**
53
54
```bash
55
# Create seeder with simple name
56
mikro-orm seeder:create User
57
58
# Create seeder with descriptive name
59
mikro-orm seeder:create TestData
60
61
# Create seeder that will be named "UserSeeder"
62
mikro-orm seeder:create UserSeeder
63
64
# Create with kebab-case name (becomes "UserDataSeeder")
65
mikro-orm seeder:create user-data
66
```
67
68
## Seeder Class Structure
69
70
Generated seeder classes follow this structure:
71
72
```typescript
73
import { Seeder } from '@mikro-orm/seeder';
74
import { EntityManager } from '@mikro-orm/core';
75
76
export class UserSeeder extends Seeder {
77
78
async run(em: EntityManager): Promise<void> {
79
// Seeder implementation goes here
80
// Example:
81
// const user = em.create(User, {
82
// name: 'John Doe',
83
// email: 'john@example.com'
84
// });
85
}
86
87
}
88
```
89
90
## Seeder Naming Conventions
91
92
The seeder creation command automatically formats class names:
93
94
### Name Formatting Rules
95
96
- Removes existing "seeder" suffix (case-insensitive)
97
- Converts kebab-case to PascalCase
98
- Adds "Seeder" suffix to final class name
99
100
### Examples
101
102
```typescript { .api }
103
// Input -> Generated Class Name
104
"user" -> "UserSeeder"
105
"test-data" -> "TestDataSeeder"
106
"UserSeeder" -> "UserSeeder"
107
"product-catalog" -> "ProductCatalogSeeder"
108
"admin" -> "AdminSeeder"
109
```
110
111
## Command Options
112
113
### Global Options
114
115
All seeder commands support:
116
- `--config`: Path to ORM configuration file(s)
117
- `--contextName` / `--context`: Configuration context name
118
119
### seeder:run Options
120
121
- `--class` / `-c`: Specify seeder class name to execute
122
- If not provided, uses default seeder from configuration
123
- Must match exact class name (case-sensitive)
124
125
### seeder:create Arguments
126
127
- `<seeder>`: Required positional argument for seeder name
128
- Will be formatted according to naming conventions
129
- Generates class file with proper structure
130
131
## Error Handling
132
133
### seeder:run Errors
134
135
- **Class not found**: Specified seeder class doesn't exist or isn't accessible
136
- **No default seeder**: No seeder specified and no default configured
137
- **Runtime errors**: Seeder execution may fail due to:
138
- Database constraint violations
139
- Duplicate data conflicts
140
- Foreign key constraint errors
141
- Entity validation failures
142
143
### seeder:create Errors
144
145
- **File system errors**: Permission issues creating seeder files
146
- **Path conflicts**: Seeder file already exists
147
- **Invalid names**: Seeder name contains invalid characters
148
- **Configuration errors**: Seeder path configuration issues
149
150
## Seeder Implementation
151
152
### Basic Seeder Example
153
154
```typescript
155
import { Seeder } from '@mikro-orm/seeder';
156
import { EntityManager } from '@mikro-orm/core';
157
import { User } from '../entities/User';
158
159
export class UserSeeder extends Seeder {
160
161
async run(em: EntityManager): Promise<void> {
162
const users = [
163
{ name: 'Alice Johnson', email: 'alice@example.com', role: 'admin' },
164
{ name: 'Bob Smith', email: 'bob@example.com', role: 'user' },
165
{ name: 'Carol Davis', email: 'carol@example.com', role: 'moderator' }
166
];
167
168
for (const userData of users) {
169
const user = em.create(User, userData);
170
}
171
172
await em.flush();
173
}
174
175
}
176
```
177
178
### Complex Seeder with Relationships
179
180
```typescript
181
import { Seeder } from '@mikro-orm/seeder';
182
import { EntityManager } from '@mikro-orm/core';
183
import { User } from '../entities/User';
184
import { Post } from '../entities/Post';
185
186
export class BlogSeeder extends Seeder {
187
188
async run(em: EntityManager): Promise<void> {
189
// Create users first
190
const author = em.create(User, {
191
name: 'Jane Author',
192
email: 'jane@example.com'
193
});
194
195
await em.flush(); // Flush to get user ID
196
197
// Create posts with relationships
198
const posts = [
199
{ title: 'First Post', content: 'Content...', author },
200
{ title: 'Second Post', content: 'More content...', author }
201
];
202
203
for (const postData of posts) {
204
em.create(Post, postData);
205
}
206
207
await em.flush();
208
}
209
210
}
211
```
212
213
## Configuration
214
215
Seeder system uses these configuration options:
216
217
```typescript { .api }
218
interface SeederConfiguration {
219
seeder: {
220
path?: string; // Path to seeder files directory
221
defaultSeeder?: string; // Default seeder class name for seeder:run
222
glob?: string; // Glob pattern for finding seeder files
223
emit?: 'ts' | 'js'; // File type to generate
224
};
225
}
226
```
227
228
### Default Configuration Example
229
230
```typescript
231
// In your MikroORM config
232
export default {
233
// ... other config
234
seeder: {
235
path: './src/seeders',
236
defaultSeeder: 'DatabaseSeeder',
237
glob: '!(*.d).{js,ts}',
238
emit: 'ts'
239
}
240
};
241
```
242
243
## Best Practices
244
245
### Seeder Design
246
247
- **Idempotent**: Seeders should be safe to run multiple times
248
- **Data cleanup**: Clear existing data before seeding if needed
249
- **Relationships**: Handle entity relationships and dependencies properly
250
- **Error handling**: Implement proper error handling for constraint violations
251
252
### Development Workflow
253
254
- **Environment-specific**: Use different seeders for development, testing, and production
255
- **Order dependencies**: Consider seeder execution order for related data
256
- **Data volume**: Keep development seeders lightweight, production seeders comprehensive
257
- **Version control**: Include seeder files in version control for team consistency