@jorgebodega/typeorm-seeding 2.0.0-next.3 → 3.0.1
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/CHANGELOG.md +28 -53
- package/README.md +200 -193
- package/dist/commands/config.command.d.ts +6 -0
- package/dist/commands/config.command.js +7 -1
- package/dist/commands/config.command.js.map +1 -1
- package/dist/commands/seed.command.d.ts +6 -0
- package/dist/commands/seed.command.js +32 -34
- package/dist/commands/seed.command.js.map +1 -1
- package/dist/connection/ConnectionConfigurationManager.d.ts +1 -1
- package/dist/connection/configureConnection.d.ts +1 -1
- package/dist/connection/fetchConnection.d.ts +1 -1
- package/dist/connection/fetchConnection.js.map +1 -1
- package/dist/connection/getConnectionOptions.d.ts +1 -1
- package/dist/connection/getConnectionOptions.js +9 -4
- package/dist/connection/getConnectionOptions.js.map +1 -1
- package/dist/errors/DefaultSeederNotDefinedError.d.ts +3 -0
- package/dist/errors/DefaultSeederNotDefinedError.js +10 -0
- package/dist/errors/DefaultSeederNotDefinedError.js.map +1 -0
- package/dist/factory.d.ts +4 -8
- package/dist/factory.js +4 -16
- package/dist/factory.js.map +1 -1
- package/dist/index.d.ts +1 -4
- package/dist/index.js +1 -4
- package/dist/index.js.map +1 -1
- package/dist/seeder.d.ts +4 -3
- package/dist/seeder.js +5 -0
- package/dist/seeder.js.map +1 -1
- package/dist/types.d.ts +3 -20
- package/dist/useSeeders.d.ts +3 -3
- package/dist/useSeeders.js +7 -46
- package/dist/useSeeders.js.map +1 -1
- package/dist/utils/fileHandling.js +1 -1
- package/dist/utils/fileHandling.js.map +1 -1
- package/package.json +35 -33
- package/dist/errors/EntityNotDefinedError.d.ts +0 -3
- package/dist/errors/EntityNotDefinedError.js +0 -10
- package/dist/errors/EntityNotDefinedError.js.map +0 -1
- package/dist/errors/FactoryImportationError.d.ts +0 -3
- package/dist/errors/FactoryImportationError.js +0 -10
- package/dist/errors/FactoryImportationError.js.map +0 -1
- package/dist/errors/FactoryNotDefinedError.d.ts +0 -3
- package/dist/errors/FactoryNotDefinedError.js +0 -10
- package/dist/errors/FactoryNotDefinedError.js.map +0 -1
- package/dist/facade.d.ts +0 -12
- package/dist/facade.js +0 -30
- package/dist/facade.js.map +0 -1
- package/dist/factoriesMap.d.ts +0 -5
- package/dist/factoriesMap.js +0 -23
- package/dist/factoriesMap.js.map +0 -1
- package/dist/runSeeder.d.ts +0 -2
- package/dist/runSeeder.js +0 -11
- package/dist/runSeeder.js.map +0 -1
- package/dist/useFactories.d.ts +0 -3
- package/dist/useFactories.js +0 -44
- package/dist/useFactories.js.map +0 -1
- package/dist/utils/getNameOfEntity.d.ts +0 -2
- package/dist/utils/getNameOfEntity.js +0 -12
- package/dist/utils/getNameOfEntity.js.map +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,73 +1,48 @@
|
|
|
1
|
-
|
|
1
|
+
## [3.0.1](https://github.com/jorgebodega/typeorm-seeding/compare/v3.0.0...v3.0.1) (2022-01-08)
|
|
2
2
|
|
|
3
3
|
|
|
4
4
|
### Bug Fixes
|
|
5
5
|
|
|
6
|
-
*
|
|
7
|
-
|
|
8
|
-
# [2.0.0-next.2](https://github.com/jorgebodega/typeorm-seeding/compare/v2.0.0-next.1...v2.0.0-next.2) (2021-12-13)
|
|
6
|
+
* update dependencies and add rimraf to dev deps ([504cef2](https://github.com/jorgebodega/typeorm-seeding/commit/504cef2f94b1b8e2e47d6bfa6e0bb1e8325490e2))
|
|
9
7
|
|
|
8
|
+
# [3.0.0](https://github.com/jorgebodega/typeorm-seeding/compare/v2.0.0...v3.0.0) (2022-01-06)
|
|
10
9
|
|
|
11
10
|
### Bug Fixes
|
|
12
11
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
# [2.0.0-next.1](https://github.com/jorgebodega/typeorm-seeding/compare/v1.6.2...v2.0.0-next.1) (2021-12-12)
|
|
16
|
-
|
|
12
|
+
- adapt seeder to new schema ([aecf7b4](https://github.com/jorgebodega/typeorm-seeding/commit/aecf7b46d40221d00d11ae15d40e36bc8c3293c0))
|
|
13
|
+
- remove factories option from config ([d66f0f1](https://github.com/jorgebodega/typeorm-seeding/commit/d66f0f184479eeca3fde1b3c5438ee17223ffa2f))
|
|
17
14
|
|
|
18
|
-
|
|
15
|
+
### chore
|
|
19
16
|
|
|
17
|
+
- remove all faker usages from source folder ([1e14718](https://github.com/jorgebodega/typeorm-seeding/commit/1e1471829c4b707fa8f0c9cb44289438ea4a1f85))
|
|
18
|
+
- remove faker from dependencies ([40f21c8](https://github.com/jorgebodega/typeorm-seeding/commit/40f21c86d5bab714581821f80480f091a91b6cf6))
|
|
20
19
|
|
|
21
20
|
### Features
|
|
22
21
|
|
|
23
|
-
|
|
24
|
-
|
|
22
|
+
- adapt use seeders to new structure ([5bca471](https://github.com/jorgebodega/typeorm-seeding/commit/5bca471339a3f8ccdc461160eb3f6c7d551daeab))
|
|
23
|
+
- move factory definition to new abstract class ([81ccdf6](https://github.com/jorgebodega/typeorm-seeding/commit/81ccdf6d295c9d36b68c803dffc197c84605a53b))
|
|
24
|
+
- remove context from factories ([#23](https://github.com/jorgebodega/typeorm-seeding/issues/23)) ([b7ecba4](https://github.com/jorgebodega/typeorm-seeding/commit/b7ecba4d4064525cd4f02da783ef885a12af08a2))
|
|
25
|
+
- remove deprecated elements ([d09e154](https://github.com/jorgebodega/typeorm-seeding/commit/d09e15479cbb49214a81ef5ef49bef6f6adce3a4))
|
|
26
|
+
- remove factory helper methods and test associateds ([#30](https://github.com/jorgebodega/typeorm-seeding/issues/30)) ([3393724](https://github.com/jorgebodega/typeorm-seeding/commit/3393724fe85bb59437ea9b307768ac157d70ec19))
|
|
27
|
+
- remove useFactories helper method ([#29](https://github.com/jorgebodega/typeorm-seeding/issues/29)) ([ddb5c2f](https://github.com/jorgebodega/typeorm-seeding/commit/ddb5c2f6576bdcde72cabb876a43013aff303e01))
|
|
28
|
+
- seed command will execute now default seeder or seed param ([314d8c3](https://github.com/jorgebodega/typeorm-seeding/commit/314d8c30e3ec59cc02074a3846e5df773f337bc6))
|
|
25
29
|
|
|
26
30
|
### BREAKING CHANGES
|
|
27
31
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
### Features
|
|
37
|
-
|
|
38
|
-
* add save options to create and createMany ([ca88005](https://github.com/jorgebodega/typeorm-seeding/commit/ca88005775d0aa37d1668d458ad17d260b192499))
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
### BREAKING CHANGES
|
|
42
|
-
|
|
43
|
-
* just to enforce new major version
|
|
44
|
-
|
|
45
|
-
# [2.0.0-next.1](https://github.com/jorgebodega/typeorm-seeding/compare/v1.6.2...v2.0.0-next.1) (2021-12-12)
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
* :construction_worker: ci: Add changes to how semantic-release work ([c4c34dd](https://github.com/jorgebodega/typeorm-seeding/commit/c4c34dd55882a445992882398c5da74459322a77))
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
### Features
|
|
52
|
-
|
|
53
|
-
* add save options to create and createMany ([ca88005](https://github.com/jorgebodega/typeorm-seeding/commit/ca88005775d0aa37d1668d458ad17d260b192499))
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
### BREAKING CHANGES
|
|
57
|
-
|
|
58
|
-
* just to enforce new major version
|
|
59
|
-
|
|
60
|
-
# [2.0.0-next.1](https://github.com/jorgebodega/typeorm-seeding/compare/v1.6.2...v2.0.0-next.1) (2021-12-12)
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
* :construction_worker: ci: Add changes to how semantic-release work ([c4c34dd](https://github.com/jorgebodega/typeorm-seeding/commit/c4c34dd55882a445992882398c5da74459322a77))
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
### Features
|
|
67
|
-
|
|
68
|
-
* add save options to create and createMany ([ca88005](https://github.com/jorgebodega/typeorm-seeding/commit/ca88005775d0aa37d1668d458ad17d260b192499))
|
|
32
|
+
- `useSeeders` change its definition and is now incompatible with previous version
|
|
33
|
+
- Faker is not available anymore as function param
|
|
34
|
+
- Faker is removed from dependencies and now users must install it by themselves
|
|
35
|
+
- Deprecated functions are removed
|
|
36
|
+
- New abstract factory class is incompatible with previous version
|
|
37
|
+
- `useFactories` will no longer be available as is useless in new architecture
|
|
38
|
+
- Context is being removed from factories. This could break some applications that were using it
|
|
69
39
|
|
|
40
|
+
# [2.0.0](https://github.com/jorgebodega/typeorm-seeding/compare/v1.6.2...v2.0.0) (2021-12-14)
|
|
70
41
|
|
|
71
42
|
### BREAKING CHANGES
|
|
72
43
|
|
|
73
|
-
|
|
44
|
+
- `useSeeders` has been renamed to `useFactories` to be more consistent with functionality
|
|
45
|
+
- New `useSeeders` has been created. Returns every Seeder that complies glob pattern and could execute them.
|
|
46
|
+
- `Seeder` is now an abstract class instead of an interface
|
|
47
|
+
- `Seeder` must export default, as that is the one that is chosen on `useSeeder`
|
|
48
|
+
- `tearDownDatabase` and `useRefreshDatabase` are deprecated
|
package/README.md
CHANGED
|
@@ -34,21 +34,23 @@
|
|
|
34
34
|
|
|
35
35
|
<p align="center">
|
|
36
36
|
<b>A delightful way to seed test data into your database.</b></br>
|
|
37
|
-
<span>Inspired by the awesome framework <a href="https://laravel.com/">laravel</a> in PHP and
|
|
37
|
+
<span>Inspired by the awesome framework <a href="https://laravel.com/">laravel</a> in PHP, <a href="https://mikro-orm.io/docs/next/seeding/">MikroORM seeding</a> and the repositories from <a href="https://github.com/pleerock">pleerock</a></span></br>
|
|
38
|
+
</p>
|
|
39
|
+
|
|
40
|
+
<p align="center">
|
|
38
41
|
<sub>Made with ❤️ by <a href="https://github.com/hirsch88">Gery Hirschfeld</a>, <a href="https://github.com/jorgebodega">Jorge Bodega</a> and <a href="https://github.com/w3tecch/typeorm-seeding/graphs/contributors">contributors</a></sub>
|
|
39
42
|
</p>
|
|
40
43
|
|
|
41
44
|
<br />
|
|
42
45
|
|
|
43
|
-
##
|
|
46
|
+
## Additional contents
|
|
44
47
|
|
|
45
|
-
- [
|
|
46
|
-
- [
|
|
47
|
-
- [
|
|
48
|
-
- [
|
|
49
|
-
- [Seeding Data in Testing](#-seeding-data-in-testing)
|
|
48
|
+
- [Factory](#factory-1)
|
|
49
|
+
- [Seeder](#seeder-1)
|
|
50
|
+
- [CLI](#cli-configuration)
|
|
51
|
+
- [Testing features](#testing-features)
|
|
50
52
|
|
|
51
|
-
##
|
|
53
|
+
## Installation
|
|
52
54
|
|
|
53
55
|
Before using this TypeORM extension please read the [TypeORM Getting Started](https://typeorm.io/#/) documentation. This explains how to setup a TypeORM project.
|
|
54
56
|
|
|
@@ -59,226 +61,118 @@ npm i [-D] @jorgebodega/typeorm-seeding
|
|
|
59
61
|
yarn add [-D] @jorgebodega/typeorm-seeding
|
|
60
62
|
```
|
|
61
63
|
|
|
62
|
-
Optional, install the type definitions of the `Faker` library.
|
|
63
|
-
|
|
64
|
-
```bash
|
|
65
|
-
npm install -D @types/faker
|
|
66
|
-
```
|
|
67
|
-
|
|
68
64
|
### Configuration
|
|
69
65
|
|
|
70
|
-
To configure the path to your
|
|
66
|
+
To configure the path to your seeders change the TypeORM config file or use environment variables like TypeORM. If both are used the environment variables will be prioritized.
|
|
71
67
|
|
|
72
68
|
**ormconfig.js**
|
|
73
69
|
|
|
74
|
-
```
|
|
70
|
+
```typescript
|
|
75
71
|
module.exports = {
|
|
76
72
|
...
|
|
77
|
-
|
|
78
|
-
|
|
73
|
+
seeders: ['src/seeds/**/*{.ts,.js}'],
|
|
74
|
+
defaultSeeder: RootSeeder,
|
|
75
|
+
...
|
|
79
76
|
}
|
|
80
77
|
```
|
|
81
78
|
|
|
82
79
|
**.env**
|
|
83
80
|
|
|
84
81
|
```
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
```
|
|
88
|
-
|
|
89
|
-
### CLI Configuration
|
|
90
|
-
|
|
91
|
-
Add the following scripts to your `package.json` file to configure the seed cli commands.
|
|
92
|
-
|
|
93
|
-
```
|
|
94
|
-
"scripts": {
|
|
95
|
-
"seed:config": "typeorm-seeding config",
|
|
96
|
-
"seed:run": "typeorm-seeding seed",
|
|
97
|
-
...
|
|
98
|
-
}
|
|
82
|
+
TYPEORM_SEEDING_SEEDERS=src/seeds/**/*{.ts,.js}
|
|
83
|
+
TYPEORM_SEEDING_DEFAULT_SEEDER=RootSeeder
|
|
99
84
|
```
|
|
100
85
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
| Option | Default | Description |
|
|
104
|
-
| ---------------------- | --------------------- | ---------------------------------------------------------------------------- |
|
|
105
|
-
| `--seed` or `-s` | null | Option to specify a seeder class to run individually. (Only on seed command) |
|
|
106
|
-
| `--connection` or `-c` | TypeORM default value | Name of the TypeORM connection. Required if there are multiple connections. |
|
|
107
|
-
| `--configName` or `-n` | TypeORM default value | Name to the TypeORM config file. |
|
|
108
|
-
| `--root` or `-r` | TypeORM default value | Path to the TypeORM config file. |
|
|
109
|
-
|
|
110
|
-
## ❯ Introduction
|
|
86
|
+
## Introduction
|
|
111
87
|
|
|
112
88
|
Isn't it exhausting to create some sample data for your database, well this time is over!
|
|
113
89
|
|
|
114
|
-
How does it work? Just create a entity factory
|
|
90
|
+
How does it work? Just create a entity factory and/or seed script.
|
|
115
91
|
|
|
116
92
|
### Entity
|
|
117
93
|
|
|
118
|
-
First create your TypeORM entities.
|
|
119
|
-
|
|
120
94
|
```typescript
|
|
121
|
-
// user.entity.ts
|
|
122
95
|
@Entity()
|
|
123
|
-
|
|
96
|
+
class User {
|
|
124
97
|
@PrimaryGeneratedColumn('uuid') id: string
|
|
125
|
-
@Column({ nullable: true }) name: string
|
|
126
|
-
@Column({ type: 'varchar', length: 100, nullable: false }) password: string
|
|
127
|
-
@OneToMany((type) => Pet, (pet) => pet.user) pets: Pet[]
|
|
128
|
-
|
|
129
|
-
@BeforeInsert()
|
|
130
|
-
async setPassword(password: string) {
|
|
131
|
-
const salt = await bcrypt.genSalt()
|
|
132
|
-
this.password = await bcrypt.hash(password || this.password, salt)
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
98
|
|
|
136
|
-
// pet.entity.ts
|
|
137
|
-
@Entity()
|
|
138
|
-
export class Pet {
|
|
139
|
-
@PrimaryGeneratedColumn('uuid') id: string
|
|
140
99
|
@Column() name: string
|
|
141
|
-
|
|
142
|
-
@
|
|
143
|
-
@JoinColumn({ name: 'user_id' })
|
|
144
|
-
user: User
|
|
100
|
+
|
|
101
|
+
@Column() lastname: string
|
|
145
102
|
}
|
|
146
103
|
```
|
|
147
104
|
|
|
148
105
|
### Factory
|
|
149
106
|
|
|
150
|
-
Then for each entity define a factory. The purpose of a factory is to create new entites with generate data.
|
|
151
|
-
|
|
152
|
-
> Note: Factories can also be used to generate data for testing.
|
|
153
|
-
|
|
154
107
|
```typescript
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
const firstName = faker.name.firstName(gender)
|
|
159
|
-
const lastName = faker.name.lastName(gender)
|
|
108
|
+
class UserFactory extends Factory<User> {
|
|
109
|
+
protected definition(): User {
|
|
110
|
+
const user = new User()
|
|
160
111
|
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
user.password = faker.random.word()
|
|
164
|
-
return user
|
|
165
|
-
})
|
|
166
|
-
|
|
167
|
-
// pet.factory.ts
|
|
168
|
-
define(Pet, (faker: typeof Faker) => {
|
|
169
|
-
const gender = faker.datatype.number(1)
|
|
170
|
-
const name = faker.name.firstName(gender)
|
|
112
|
+
user.name = 'John'
|
|
113
|
+
user.lastname = 'Doe'
|
|
171
114
|
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
pet.user = factory(User)() as any
|
|
176
|
-
return pet
|
|
177
|
-
})
|
|
115
|
+
return user
|
|
116
|
+
}
|
|
117
|
+
}
|
|
178
118
|
```
|
|
179
119
|
|
|
180
120
|
### Seeder
|
|
181
121
|
|
|
182
|
-
And last but not least, create a seeder. The seeder can be called by the configured cli command `seed:run`. In this case it generates 10 pets with a owner (User).
|
|
183
|
-
|
|
184
|
-
> Note: `seed:run` must be configured first. Go to [CLI Configuration](#cli-configuration).
|
|
185
|
-
|
|
186
122
|
```typescript
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
123
|
+
export class UserExampleSeeder extends Seeder {
|
|
124
|
+
async run() {
|
|
125
|
+
await new UserFactory().create({
|
|
126
|
+
name: 'Jane',
|
|
127
|
+
})
|
|
191
128
|
}
|
|
192
129
|
}
|
|
193
130
|
```
|
|
194
131
|
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
## ❯ Basic Seeder
|
|
132
|
+
## Factory
|
|
198
133
|
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
> Note. The seeder files will be executed alphabetically.
|
|
134
|
+
Factory is how we provide a way to simplify entities creation, implementing a [factory creational pattern](https://refactoring.guru/design-patterns/factory-method). It is defined as an abstract class with generic typing, so you have to extend over it.
|
|
202
135
|
|
|
203
136
|
```typescript
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
.createQueryBuilder()
|
|
208
|
-
.insert()
|
|
209
|
-
.into(User)
|
|
210
|
-
.values([
|
|
211
|
-
{ firstName: 'Timber', lastName: 'Saw' },
|
|
212
|
-
{ firstName: 'Phantom', lastName: 'Lancer' },
|
|
213
|
-
])
|
|
214
|
-
.execute()
|
|
137
|
+
class UserFactory extends Factory<User> {
|
|
138
|
+
protected definition(): User {
|
|
139
|
+
...
|
|
215
140
|
}
|
|
216
141
|
}
|
|
217
142
|
```
|
|
218
143
|
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
Of course, manually specifying the attributes for each entity seed is cumbersome. Instead, you can use entity factories to conveniently generate large amounts of database records.
|
|
222
|
-
|
|
223
|
-
For all entities we want to create, we need to define a factory. To do so we give you the awesome [faker](https://github.com/marak/Faker.js/) library as a parameter into your factory. Then create your "fake" entity and return it. Those factory files should be in the `src/database/factories` folder and suffixed with `.factory` like `src/database/factories/user.factory.ts`.
|
|
224
|
-
|
|
225
|
-
| Types | Description |
|
|
226
|
-
| --------------- | ------------------------------------------------------------------------------- |
|
|
227
|
-
| `Entity` | TypeORM Entity like the user or the pet in the samples. |
|
|
228
|
-
| `Context` | Argument to pass some static data into the factory function. |
|
|
229
|
-
| `EntityFactory` | This object is used to make new filled entities or create it into the database. |
|
|
230
|
-
|
|
231
|
-
### `define`
|
|
232
|
-
|
|
233
|
-
The define function creates a new entity factory.
|
|
144
|
+
### `definition`
|
|
234
145
|
|
|
235
|
-
|
|
236
|
-
define: <Entity, Context>(entity: Entity, factoryFn: FactoryFunction<Entity, Context>) => void;
|
|
237
|
-
```
|
|
146
|
+
This function is the one that needs to be defined when extending the class. It is called to instantiate the entity and the result will be used on the rest of factory lifecycle.
|
|
238
147
|
|
|
239
148
|
```typescript
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
...
|
|
243
|
-
return user
|
|
244
|
-
})
|
|
245
|
-
```
|
|
246
|
-
|
|
247
|
-
### `factory`
|
|
248
|
-
|
|
249
|
-
Factory retrieves the defined factory function and returns the EntityFactory to start creating new enities.
|
|
149
|
+
protected definition(): User {
|
|
150
|
+
const user = new User()
|
|
250
151
|
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
```
|
|
152
|
+
user.name = 'John'
|
|
153
|
+
user.lastname = 'Doe'
|
|
254
154
|
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
factory(Pet)({ name: 'Balou' })
|
|
155
|
+
return user
|
|
156
|
+
}
|
|
258
157
|
```
|
|
259
158
|
|
|
260
|
-
|
|
159
|
+
It is possible to create more than one factory related to a single entity, with different definition functions.
|
|
261
160
|
|
|
262
|
-
|
|
161
|
+
### `map`
|
|
263
162
|
|
|
264
163
|
Use the `.map()` function to alter the generated value before they get processed.
|
|
265
164
|
|
|
266
165
|
```typescript
|
|
267
|
-
map(mapFunction: (entity: Entity) =>
|
|
166
|
+
map(mapFunction: (entity: Entity) => void): Factory
|
|
268
167
|
```
|
|
269
168
|
|
|
270
169
|
```typescript
|
|
271
|
-
|
|
272
|
-
.
|
|
273
|
-
|
|
274
|
-
const petIds = pets.map((pet: Pet) => pet.Id)
|
|
275
|
-
await user.pets().attach(petIds)
|
|
276
|
-
return user
|
|
277
|
-
})
|
|
278
|
-
.createMany(5)
|
|
170
|
+
new UserFactory().map((user) => {
|
|
171
|
+
user.name = 'Jane'
|
|
172
|
+
})
|
|
279
173
|
```
|
|
280
174
|
|
|
281
|
-
|
|
175
|
+
### `make` & `makeMany`
|
|
282
176
|
|
|
283
177
|
Make and makeMany executes the factory functions and return a new instance of the given entity. The instance is filled with the generated values from the factory function, but not saved in the database.
|
|
284
178
|
|
|
@@ -290,15 +184,15 @@ makeMany(amount: number, overrideParams: Partial<Entity> = {}): Promise<Entity>
|
|
|
290
184
|
```
|
|
291
185
|
|
|
292
186
|
```typescript
|
|
293
|
-
|
|
294
|
-
|
|
187
|
+
new UserFactory().make()
|
|
188
|
+
new UserFactory().makeMany(10)
|
|
295
189
|
|
|
296
190
|
// override the email
|
|
297
|
-
|
|
298
|
-
|
|
191
|
+
new UserFactory().make({ email: 'other@mail.com' })
|
|
192
|
+
new UserFactory().makeMany(10, { email: 'other@mail.com' })
|
|
299
193
|
```
|
|
300
194
|
|
|
301
|
-
|
|
195
|
+
### `create` & `createMany`
|
|
302
196
|
|
|
303
197
|
the create and createMany method is similar to the make and makeMany method, but at the end the created entity instance gets persisted in the database using TypeORM entity manager.
|
|
304
198
|
|
|
@@ -311,64 +205,177 @@ createMany(amount: number, overrideParams: Partial<Entity> = {}, saveOptions?: S
|
|
|
311
205
|
```
|
|
312
206
|
|
|
313
207
|
```typescript
|
|
314
|
-
|
|
315
|
-
|
|
208
|
+
new UserFactory().create()
|
|
209
|
+
new UserFactory().createMany(10)
|
|
316
210
|
|
|
317
211
|
// override the email
|
|
318
|
-
|
|
319
|
-
|
|
212
|
+
new UserFactory().create({ email: 'other@mail.com' })
|
|
213
|
+
new UserFactory().createMany(10, { email: 'other@mail.com' })
|
|
320
214
|
|
|
321
215
|
// using save options
|
|
322
|
-
|
|
323
|
-
|
|
216
|
+
new UserFactory().create({ email: 'other@mail.com' }, { listeners: false })
|
|
217
|
+
new UserFactory().createMany(10, { email: 'other@mail.com' }, { listeners: false })
|
|
324
218
|
```
|
|
325
219
|
|
|
326
|
-
|
|
220
|
+
### Execution order
|
|
327
221
|
|
|
328
222
|
As the order of execution can be complex, you can check it here:
|
|
329
223
|
|
|
330
|
-
1. **Context**: The context is used when the entity is being created
|
|
331
224
|
2. **Map function**: Map function alters the already existing entity.
|
|
332
225
|
3. **Override params**: Alters the already existing entity.
|
|
333
226
|
4. **Promises**: If some attribute is a promise, the promise will be resolved before the entity is created.
|
|
334
227
|
5. **Factories**: If some attribute is a factory, the factory will be executed with `make`/`create` like the previous one.
|
|
335
228
|
|
|
336
|
-
|
|
229
|
+
### Faker
|
|
337
230
|
|
|
338
|
-
|
|
231
|
+
[Faker](https://github.com/marak/Faker.js/) package was previously a dependency of the project, but now it is optional due to its size. If you want to use faker, you may need to install it and import it.
|
|
339
232
|
|
|
340
|
-
|
|
233
|
+
Instead of the previous example:
|
|
341
234
|
|
|
342
|
-
|
|
235
|
+
```typescript
|
|
236
|
+
define(User, (faker: typeof Faker) => {
|
|
237
|
+
const firstName = faker.name.firstName()
|
|
238
|
+
const lastName = faker.name.lastName()
|
|
239
|
+
|
|
240
|
+
const user = new User()
|
|
241
|
+
user.name = `${firstName} ${lastName}`
|
|
242
|
+
return user
|
|
243
|
+
})
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
You can do:
|
|
343
247
|
|
|
344
248
|
```typescript
|
|
345
|
-
|
|
346
|
-
|
|
249
|
+
import * as faker from 'faker'
|
|
250
|
+
|
|
251
|
+
class UserFactory extends Factory<User> {
|
|
252
|
+
protected definition(): User {
|
|
253
|
+
const user = new User()
|
|
254
|
+
|
|
255
|
+
user.name = faker.name.firstName()
|
|
256
|
+
user.lastname = faker.name.lastName()
|
|
257
|
+
|
|
258
|
+
return user
|
|
259
|
+
}
|
|
260
|
+
}
|
|
347
261
|
```
|
|
348
262
|
|
|
349
|
-
|
|
263
|
+
## Seeder
|
|
350
264
|
|
|
351
|
-
|
|
265
|
+
Seeder class is how we provide a way to insert data into databases, and could be executed by the command line or by helper method. Is an abstract class with one method to be implemented, and a helper function to run some more seeder sequentially.
|
|
352
266
|
|
|
353
267
|
```typescript
|
|
354
|
-
|
|
355
|
-
|
|
268
|
+
class UserSeeder extends Seeder {
|
|
269
|
+
async run(connection: Connection) {
|
|
270
|
+
...
|
|
271
|
+
}
|
|
272
|
+
}
|
|
356
273
|
```
|
|
357
274
|
|
|
358
|
-
### `
|
|
275
|
+
### `run`
|
|
359
276
|
|
|
360
|
-
|
|
277
|
+
This function is the one that needs to be defined when extending the class. Could use `call` to run some other seeders.
|
|
361
278
|
|
|
362
279
|
```typescript
|
|
363
|
-
|
|
280
|
+
run(connection: Connection): Promise<void>
|
|
364
281
|
```
|
|
365
282
|
|
|
366
|
-
### `ConnectionConfiguration`
|
|
367
|
-
|
|
368
283
|
```typescript
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
284
|
+
async run(connection: Connection) {
|
|
285
|
+
await new UserFactory().createMany(10)
|
|
286
|
+
|
|
287
|
+
await this.call(connection, [PetSeeder])
|
|
288
|
+
}
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
### `call`
|
|
292
|
+
|
|
293
|
+
This function allow to run some other seeders in a sequential way.
|
|
294
|
+
|
|
295
|
+
In order to use seeders from cli command, a default seeder class must be provided as root seeder, working as a tree structure.
|
|
296
|
+
|
|
297
|
+
<p align="center">
|
|
298
|
+
<img src="./seeders.png" alt="logo" />
|
|
299
|
+
</p>
|
|
300
|
+
|
|
301
|
+
## CLI Configuration
|
|
302
|
+
|
|
303
|
+
There are two possible commands to execute, one to see the current configuration and one to run a seeder.
|
|
304
|
+
|
|
305
|
+
Add the following scripts to your `package.json` file to configure them.
|
|
306
|
+
|
|
307
|
+
```json
|
|
308
|
+
"scripts": {
|
|
309
|
+
"seed:config": "typeorm-seeding config",
|
|
310
|
+
"seed:run": "typeorm-seeding seed",
|
|
311
|
+
...
|
|
373
312
|
}
|
|
374
313
|
```
|
|
314
|
+
|
|
315
|
+
### `config`
|
|
316
|
+
|
|
317
|
+
This command just print the connection configuration.
|
|
318
|
+
|
|
319
|
+
```bash
|
|
320
|
+
typeorm-seeding config
|
|
321
|
+
```
|
|
322
|
+
|
|
323
|
+
Example result
|
|
324
|
+
|
|
325
|
+
```json
|
|
326
|
+
{
|
|
327
|
+
"name": "default",
|
|
328
|
+
"type": "sqlite",
|
|
329
|
+
"database": "/home/jorgebodega/projects/typeorm-seeding/test.db",
|
|
330
|
+
"entities": ["sample/entities/**/*{.ts,.js}"],
|
|
331
|
+
"seeders": ["sample/seeders/**/*{.ts,.js}"],
|
|
332
|
+
"defaultSeeder": "RootSeeder"
|
|
333
|
+
}
|
|
334
|
+
```
|
|
335
|
+
|
|
336
|
+
##### Options
|
|
337
|
+
|
|
338
|
+
| Option | Default | Description |
|
|
339
|
+
| ---------------------- | --------------------- | ---------------------------------------------------------------------------- |
|
|
340
|
+
| `--seed` or `-s` | null | Option to specify a seeder class to run individually. (Only on seed command) |
|
|
341
|
+
| `--connection` or `-c` | TypeORM default value | Name of the TypeORM connection. Required if there are multiple connections. |
|
|
342
|
+
| `--configName` or `-n` | TypeORM default value | Name to the TypeORM config file. |
|
|
343
|
+
| `--root` or `-r` | TypeORM default value | Path to the TypeORM config file. |
|
|
344
|
+
|
|
345
|
+
### `seed`
|
|
346
|
+
|
|
347
|
+
This command execute a seeder, that could be specified as a parameter.
|
|
348
|
+
|
|
349
|
+
```bash
|
|
350
|
+
typeorm-seeding seed
|
|
351
|
+
```
|
|
352
|
+
|
|
353
|
+
##### Options
|
|
354
|
+
|
|
355
|
+
| Option | Default | Description |
|
|
356
|
+
| ---------------------- | ------------------------------------ | --------------------------------------------------------------------------- |
|
|
357
|
+
| `--seed` or `-s` | Default seeder specified config file | Option to specify a seeder class to run individually. |
|
|
358
|
+
| `--connection` or `-c` | TypeORM default value | Name of the TypeORM connection. Required if there are multiple connections. |
|
|
359
|
+
| `--configName` or `-n` | TypeORM default value | Name to the TypeORM config file. |
|
|
360
|
+
| `--root` or `-r` | TypeORM default value | Path to the TypeORM config file. |
|
|
361
|
+
|
|
362
|
+
## Testing features
|
|
363
|
+
|
|
364
|
+
We provide some testing features that we already use to test this package, like connection configuration.
|
|
365
|
+
The entity factories can also be used in testing. To do so call the `useFactories` or `useSeeders` function.
|
|
366
|
+
|
|
367
|
+
### `useSeeders`
|
|
368
|
+
|
|
369
|
+
Execute one or more seeders.
|
|
370
|
+
|
|
371
|
+
```typescript
|
|
372
|
+
useSeeders(entrySeeders: ClassConstructor<Seeder> | ClassConstructor<Seeder>[]): Promise<void>
|
|
373
|
+
useSeeders(
|
|
374
|
+
entrySeeders: ClassConstructor<Seeder> | ClassConstructor<Seeder>[],
|
|
375
|
+
customOptions: Partial<ConnectionConfiguration>,
|
|
376
|
+
): Promise<void>
|
|
377
|
+
```
|
|
378
|
+
|
|
379
|
+
### Factories
|
|
380
|
+
|
|
381
|
+
If factories are being used to create entities, just remember to clean up fake data after every execution.
|
|
@@ -7,6 +7,9 @@ interface ConfigCommandArguments extends Arguments {
|
|
|
7
7
|
export declare class ConfigCommand implements CommandModule {
|
|
8
8
|
command: string;
|
|
9
9
|
describe: string;
|
|
10
|
+
/**
|
|
11
|
+
* @inheritdoc
|
|
12
|
+
*/
|
|
10
13
|
builder(args: Argv): Argv<{
|
|
11
14
|
n: string | undefined;
|
|
12
15
|
} & {
|
|
@@ -14,6 +17,9 @@ export declare class ConfigCommand implements CommandModule {
|
|
|
14
17
|
} & {
|
|
15
18
|
r: string | undefined;
|
|
16
19
|
}>;
|
|
20
|
+
/**
|
|
21
|
+
* @inheritdoc
|
|
22
|
+
*/
|
|
17
23
|
handler(args: ConfigCommandArguments): Promise<void>;
|
|
18
24
|
}
|
|
19
25
|
export {};
|