@ackplus/nest-seeder 1.1.12 → 1.1.15-beta.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/QUICKSTART.md +126 -116
- package/README.md +354 -811
- package/dist/cli.js +68 -13
- package/dist/cli.js.map +1 -1
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +22 -23
- package/examples/post.seeder.example.ts +0 -65
- package/examples/product.factory.example.ts +0 -84
- package/examples/seed.script.example.ts +0 -32
- package/examples/seeder.config.example.ts +0 -50
- package/examples/user.factory.example.ts +0 -59
- package/examples/user.seeder.example.ts +0 -55
package/QUICKSTART.md
CHANGED
|
@@ -1,45 +1,62 @@
|
|
|
1
|
-
# Quick Start
|
|
1
|
+
# ⚡ Quick Start - 5 Minutes to Seeding!
|
|
2
2
|
|
|
3
|
-
Get started with nest-seeder in 5
|
|
3
|
+
Get started with `@ackplus/nest-seeder` in just 5 simple steps.
|
|
4
4
|
|
|
5
|
-
## 📦
|
|
5
|
+
## 📦 Installation
|
|
6
6
|
|
|
7
7
|
```bash
|
|
8
8
|
npm install @ackplus/nest-seeder @faker-js/faker
|
|
9
9
|
npm install -D ts-node typescript
|
|
10
10
|
```
|
|
11
11
|
|
|
12
|
-
##
|
|
12
|
+
## Step 1: Create an Entity
|
|
13
13
|
|
|
14
|
-
|
|
14
|
+
```typescript
|
|
15
|
+
// src/entities/user.entity.ts
|
|
16
|
+
import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm';
|
|
17
|
+
|
|
18
|
+
@Entity('users')
|
|
19
|
+
export class User {
|
|
20
|
+
@PrimaryGeneratedColumn()
|
|
21
|
+
id: number;
|
|
22
|
+
|
|
23
|
+
@Column()
|
|
24
|
+
name: string;
|
|
25
|
+
|
|
26
|
+
@Column({ unique: true })
|
|
27
|
+
email: string;
|
|
28
|
+
|
|
29
|
+
@Column({ default: 'user' })
|
|
30
|
+
role: string;
|
|
31
|
+
}
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Step 2: Create a Factory
|
|
15
35
|
|
|
16
36
|
```typescript
|
|
37
|
+
// src/factories/user.factory.ts
|
|
17
38
|
import { Factory } from '@ackplus/nest-seeder';
|
|
18
39
|
|
|
19
40
|
export class UserFactory {
|
|
41
|
+
@Factory((faker) => faker.person.fullName())
|
|
42
|
+
name: string;
|
|
43
|
+
|
|
20
44
|
@Factory((faker) => faker.internet.email())
|
|
21
45
|
email: string;
|
|
22
46
|
|
|
23
|
-
@Factory((faker) => faker.
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
@Factory((faker) => faker.person.lastName())
|
|
27
|
-
lastName: string;
|
|
28
|
-
|
|
29
|
-
@Factory((faker) => faker.internet.password())
|
|
30
|
-
password: string;
|
|
47
|
+
@Factory((faker) => faker.helpers.arrayElement(['admin', 'user', 'guest']))
|
|
48
|
+
role: string;
|
|
31
49
|
}
|
|
32
50
|
```
|
|
33
51
|
|
|
34
|
-
##
|
|
35
|
-
|
|
36
|
-
Create a file `src/database/seeders/user.seeder.ts`:
|
|
52
|
+
## Step 3: Create a Seeder
|
|
37
53
|
|
|
38
54
|
```typescript
|
|
55
|
+
// src/seeders/user.seeder.ts
|
|
39
56
|
import { Injectable } from '@nestjs/common';
|
|
40
57
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
41
58
|
import { Repository } from 'typeorm';
|
|
42
|
-
import { Seeder,
|
|
59
|
+
import { Seeder, DataFactory } from '@ackplus/nest-seeder';
|
|
43
60
|
import { User } from '../entities/user.entity';
|
|
44
61
|
import { UserFactory } from '../factories/user.factory';
|
|
45
62
|
|
|
@@ -50,71 +67,53 @@ export class UserSeeder implements Seeder {
|
|
|
50
67
|
private readonly userRepository: Repository<User>,
|
|
51
68
|
) {}
|
|
52
69
|
|
|
53
|
-
async seed(
|
|
70
|
+
async seed(): Promise<void> {
|
|
54
71
|
const factory = DataFactory.createForClass(UserFactory);
|
|
55
72
|
const users = factory.generate(10);
|
|
56
73
|
await this.userRepository.save(users);
|
|
57
74
|
console.log('✅ Seeded 10 users');
|
|
58
75
|
}
|
|
59
76
|
|
|
60
|
-
async drop(
|
|
77
|
+
async drop(): Promise<void> {
|
|
61
78
|
await this.userRepository.delete({});
|
|
62
79
|
}
|
|
63
80
|
}
|
|
64
81
|
```
|
|
65
82
|
|
|
66
|
-
##
|
|
67
|
-
|
|
68
|
-
Update your `app.module.ts`:
|
|
69
|
-
|
|
70
|
-
```typescript
|
|
71
|
-
import { Module } from '@nestjs/common';
|
|
72
|
-
import { TypeOrmModule } from '@nestjs/typeorm';
|
|
73
|
-
import { SeederModule } from '@ackplus/nest-seeder';
|
|
74
|
-
import { User } from './database/entities/user.entity';
|
|
75
|
-
import { UserSeeder } from './database/seeders/user.seeder';
|
|
83
|
+
## Step 4: Create Seeder Configuration
|
|
76
84
|
|
|
77
|
-
|
|
78
|
-
imports: [
|
|
79
|
-
TypeOrmModule.forRoot({
|
|
80
|
-
type: 'postgres',
|
|
81
|
-
host: 'localhost',
|
|
82
|
-
port: 5432,
|
|
83
|
-
username: 'postgres',
|
|
84
|
-
password: 'postgres',
|
|
85
|
-
database: 'mydb',
|
|
86
|
-
entities: [User],
|
|
87
|
-
synchronize: true,
|
|
88
|
-
}),
|
|
89
|
-
TypeOrmModule.forFeature([User]),
|
|
90
|
-
SeederModule.register({
|
|
91
|
-
seeders: [UserSeeder],
|
|
92
|
-
}),
|
|
93
|
-
],
|
|
94
|
-
})
|
|
95
|
-
export class AppModule {}
|
|
96
|
-
```
|
|
85
|
+
Create `seeder.config.ts` in your **project root**:
|
|
97
86
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
### Create config file `src/database/seeder.config.ts`:
|
|
87
|
+
> **Note:** The seeder configuration is independent and does not require importing your main `AppModule`.
|
|
101
88
|
|
|
102
89
|
```typescript
|
|
90
|
+
// seeder.config.ts (in project root)
|
|
103
91
|
import { TypeOrmModule } from '@nestjs/typeorm';
|
|
104
|
-
import {
|
|
105
|
-
import {
|
|
92
|
+
import { ConfigModule, ConfigService } from '@nestjs/config';
|
|
93
|
+
import { User } from './src/entities/user.entity';
|
|
94
|
+
import { UserSeeder } from './src/seeders/user.seeder';
|
|
95
|
+
import * as dotenv from 'dotenv';
|
|
96
|
+
|
|
97
|
+
dotenv.config();
|
|
106
98
|
|
|
107
99
|
export default {
|
|
108
100
|
imports: [
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
101
|
+
ConfigModule.forRoot({
|
|
102
|
+
isGlobal: true,
|
|
103
|
+
}),
|
|
104
|
+
TypeOrmModule.forRootAsync({
|
|
105
|
+
imports: [ConfigModule],
|
|
106
|
+
inject: [ConfigService],
|
|
107
|
+
useFactory: (config: ConfigService) => ({
|
|
108
|
+
type: 'postgres',
|
|
109
|
+
host: config.get<string>('DB_HOST'),
|
|
110
|
+
port: config.get<number>('DB_PORT'),
|
|
111
|
+
username: config.get<string>('DB_USERNAME'),
|
|
112
|
+
password: config.get<string>('DB_PASSWORD'),
|
|
113
|
+
database: config.get<string>('DB_DATABASE'),
|
|
114
|
+
entities: [User],
|
|
115
|
+
synchronize: true,
|
|
116
|
+
}),
|
|
118
117
|
}),
|
|
119
118
|
TypeOrmModule.forFeature([User]),
|
|
120
119
|
],
|
|
@@ -122,92 +121,103 @@ export default {
|
|
|
122
121
|
};
|
|
123
122
|
```
|
|
124
123
|
|
|
125
|
-
|
|
124
|
+
## Step 5: Add Script & Run
|
|
125
|
+
|
|
126
|
+
Add to `package.json`:
|
|
126
127
|
|
|
127
128
|
```json
|
|
128
129
|
{
|
|
129
130
|
"scripts": {
|
|
130
|
-
"seed": "nest-
|
|
131
|
-
"seed:refresh": "
|
|
131
|
+
"seed": "node -r ts-node/register -r tsconfig-paths/register ./node_modules/@ackplus/nest-seeder/dist/cli.js -c ./seeder.config.ts",
|
|
132
|
+
"seed:refresh": "npm run seed -- --refresh"
|
|
132
133
|
}
|
|
133
134
|
}
|
|
134
135
|
```
|
|
135
136
|
|
|
136
|
-
|
|
137
|
+
Run it:
|
|
137
138
|
|
|
138
139
|
```bash
|
|
139
140
|
npm run seed
|
|
140
141
|
```
|
|
141
142
|
|
|
142
|
-
|
|
143
|
+
**Output:**
|
|
144
|
+
```
|
|
145
|
+
🌱 Starting NestJS Seeder...
|
|
146
|
+
📁 Loading configuration from: seeder.config.ts
|
|
147
|
+
[Nest] Starting Nest application...
|
|
148
|
+
[Nest] TypeOrmModule dependencies initialized
|
|
149
|
+
✅ Seeded 10 users
|
|
150
|
+
```
|
|
143
151
|
|
|
144
|
-
|
|
152
|
+
## 🎉 Success!
|
|
145
153
|
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
async function bootstrap() {
|
|
152
|
-
const app = await NestFactory.createApplicationContext(AppModule);
|
|
153
|
-
const seeder = app.get(SeederService);
|
|
154
|
-
await seeder.run();
|
|
155
|
-
await app.close();
|
|
156
|
-
}
|
|
154
|
+
You've successfully seeded your database!
|
|
155
|
+
|
|
156
|
+
## 📚 Next Steps
|
|
157
|
+
|
|
158
|
+
### Run with options:
|
|
157
159
|
|
|
158
|
-
|
|
160
|
+
```bash
|
|
161
|
+
# Drop and reseed
|
|
162
|
+
npm run seed:refresh
|
|
163
|
+
|
|
164
|
+
# Run specific seeder
|
|
165
|
+
npm run seed -- --name UserSeeder
|
|
166
|
+
|
|
167
|
+
# With dummy data flag
|
|
168
|
+
npm run seed -- --dummyData
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
### Add More Seeders
|
|
172
|
+
|
|
173
|
+
1. Create more factories and seeders
|
|
174
|
+
2. Add them to `seeder.config.ts`:
|
|
175
|
+
|
|
176
|
+
```typescript
|
|
177
|
+
export default {
|
|
178
|
+
imports: [/* ... */],
|
|
179
|
+
seeders: [
|
|
180
|
+
UserSeeder,
|
|
181
|
+
PostSeeder,
|
|
182
|
+
CommentSeeder,
|
|
183
|
+
],
|
|
184
|
+
};
|
|
159
185
|
```
|
|
160
186
|
|
|
161
|
-
###
|
|
187
|
+
### Watch Mode for Development
|
|
188
|
+
|
|
189
|
+
Add to `package.json`:
|
|
162
190
|
|
|
163
191
|
```json
|
|
164
192
|
{
|
|
165
193
|
"scripts": {
|
|
166
|
-
"seed": "
|
|
167
|
-
"seed:refresh": "ts-node src/seed.ts --refresh"
|
|
194
|
+
"seed:watch": "nodemon --watch src/seeders --watch src/factories --ext ts --exec \"npm run seed\""
|
|
168
195
|
}
|
|
169
196
|
}
|
|
170
197
|
```
|
|
171
198
|
|
|
172
|
-
|
|
199
|
+
Run:
|
|
173
200
|
|
|
174
201
|
```bash
|
|
175
|
-
npm run seed
|
|
202
|
+
npm run seed:watch
|
|
176
203
|
```
|
|
177
204
|
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
You've successfully set up nest-seeder!
|
|
181
|
-
|
|
182
|
-
## 📚 Next Steps
|
|
183
|
-
|
|
184
|
-
### Run specific seeders:
|
|
185
|
-
```bash
|
|
186
|
-
npm run seed -- --name UserSeeder
|
|
187
|
-
```
|
|
188
|
-
|
|
189
|
-
### Refresh data (drop and reseed):
|
|
190
|
-
```bash
|
|
191
|
-
npm run seed:refresh
|
|
192
|
-
```
|
|
205
|
+
Now your database will auto-reseed when you modify seeders or factories!
|
|
193
206
|
|
|
194
|
-
|
|
195
|
-
1. Create more factory classes
|
|
196
|
-
2. Create more seeder classes
|
|
197
|
-
3. Add them to your config/module
|
|
198
|
-
4. Run!
|
|
207
|
+
## 💡 Tips
|
|
199
208
|
|
|
200
|
-
|
|
201
|
-
-
|
|
202
|
-
-
|
|
203
|
-
-
|
|
204
|
-
-
|
|
209
|
+
- **Start small**: Begin with one entity, then add more
|
|
210
|
+
- **Use factories**: They make generating data super easy
|
|
211
|
+
- **Order matters**: List seeders in dependency order
|
|
212
|
+
- **Drop method**: Always implement drop() to clear data
|
|
213
|
+
- **Environment variables**: Use them for database config
|
|
205
214
|
|
|
206
|
-
##
|
|
215
|
+
## 🔗 Resources
|
|
207
216
|
|
|
208
|
-
-
|
|
209
|
-
-
|
|
210
|
-
-
|
|
217
|
+
- [Full Documentation](./README.md)
|
|
218
|
+
- [Examples Directory](./examples/)
|
|
219
|
+
- [GitHub Repository](https://github.com/ackplus/nest-seeder)
|
|
211
220
|
|
|
212
|
-
|
|
221
|
+
---
|
|
213
222
|
|
|
223
|
+
Ready to seed! 🌱
|