@jorgebodega/typeorm-seeding 5.0.0-next.2 → 5.0.1-next.2
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/.commitlintrc +9 -1
- package/CHANGELOG.md +46 -0
- package/README.md +43 -333
- package/dist/cli.d.ts +1 -1
- package/dist/cli.js +2 -12
- package/dist/cli.js.map +1 -1
- package/dist/commands/seed.command.d.ts +1 -25
- package/dist/commands/seed.command.js +51 -129
- package/dist/commands/seed.command.js.map +1 -1
- package/dist/datasource/DataSourceManager.d.ts +1 -1
- package/dist/datasource/DataSourceManager.js +1 -1
- package/dist/datasource/DataSourceManager.js.map +1 -1
- package/dist/errors/DataSourceImportationError.d.ts +2 -0
- package/dist/errors/DataSourceImportationError.js +7 -0
- package/dist/errors/DataSourceImportationError.js.map +1 -0
- package/dist/errors/SeederExecutionError.d.ts +2 -0
- package/dist/errors/SeederExecutionError.js +7 -0
- package/dist/errors/SeederExecutionError.js.map +1 -0
- package/dist/errors/SeederImportationError.d.ts +0 -1
- package/dist/errors/SeederImportationError.js +0 -3
- package/dist/errors/SeederImportationError.js.map +1 -1
- package/dist/errors/index.d.ts +2 -0
- package/dist/errors/index.js +2 -0
- package/dist/errors/index.js.map +1 -1
- package/dist/helpers/useDataSource.js +1 -1
- package/dist/helpers/useDataSource.js.map +1 -1
- package/dist/index.d.ts +0 -4
- package/dist/index.js +0 -4
- package/dist/index.js.map +1 -1
- package/dist/types.d.ts +3 -7
- package/dist/utils/commandUtils.d.ts +7 -0
- package/dist/utils/commandUtils.js +52 -0
- package/dist/utils/commandUtils.js.map +1 -0
- package/dist/utils/fileHandling.js +2 -2
- package/dist/utils/fileHandling.js.map +1 -1
- package/dist/utils/index.d.ts +2 -0
- package/dist/utils/index.js +6 -0
- package/dist/utils/index.js.map +1 -0
- package/package.json +19 -21
- package/pnpm-lock.yaml +4449 -0
- package/dist/factory.d.ts +0 -28
- package/dist/factory.js +0 -87
- package/dist/factory.js.map +0 -1
- package/dist/instanceAttribute.d.ts +0 -3
- package/dist/instanceAttribute.js +0 -8
- package/dist/instanceAttribute.js.map +0 -1
- package/dist/lazyAttribute.d.ts +0 -6
- package/dist/lazyAttribute.js +0 -13
- package/dist/lazyAttribute.js.map +0 -1
- package/dist/lazyInstanceAttribute.d.ts +0 -3
- package/dist/lazyInstanceAttribute.js +0 -8
- package/dist/lazyInstanceAttribute.js.map +0 -1
- package/dist/subfactory.d.ts +0 -13
- package/dist/subfactory.js +0 -24
- package/dist/subfactory.js.map +0 -1
package/.commitlintrc
CHANGED
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,49 @@
|
|
|
1
|
+
## [5.0.1-next.2](https://github.com/jorgebodega/typeorm-seeding/compare/v5.0.1-next.1...v5.0.1-next.2) (2022-08-18)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### chore
|
|
5
|
+
|
|
6
|
+
* remove factory definitions ([f2ede7c](https://github.com/jorgebodega/typeorm-seeding/commit/f2ede7c952a106db98ef817e6c9fc34a1a79dd96))
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### BREAKING CHANGES
|
|
10
|
+
|
|
11
|
+
* remove all factory related code in favor of @jorgebodega/typeorm-factory
|
|
12
|
+
|
|
13
|
+
## [5.0.1](https://github.com/jorgebodega/typeorm-seeding/compare/v5.0.0...v5.0.1) (2022-08-18)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
### Bug Fixes
|
|
17
|
+
|
|
18
|
+
* no application seeds ([30f82f6](https://github.com/jorgebodega/typeorm-seeding/commit/30f82f6c3f3c5c249ed32c78452f066cc7c1ab06))
|
|
19
|
+
|
|
20
|
+
## [5.0.1-next.1](https://github.com/jorgebodega/typeorm-seeding/compare/v5.0.0...v5.0.1-next.1) (2022-08-17)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
### Bug Fixes
|
|
24
|
+
|
|
25
|
+
* no application seeds ([30f82f6](https://github.com/jorgebodega/typeorm-seeding/commit/30f82f6c3f3c5c249ed32c78452f066cc7c1ab06))
|
|
26
|
+
|
|
27
|
+
# [5.0.0](https://github.com/jorgebodega/typeorm-seeding/compare/v4.0.0...v5.0.0) (2022-08-01)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
### Bug Fixes
|
|
31
|
+
|
|
32
|
+
* add factory deprecation notice ([8d75e05](https://github.com/jorgebodega/typeorm-seeding/commit/8d75e05e43b35e770f7c14437a1e6b04c8fffd01))
|
|
33
|
+
* update dependency glob to v8 ([2d3c996](https://github.com/jorgebodega/typeorm-seeding/commit/2d3c99635fcfdd667b38a4bed60d663145dd619b))
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
### Features
|
|
37
|
+
|
|
38
|
+
* removed unused command and add some more tests ([ea6649e](https://github.com/jorgebodega/typeorm-seeding/commit/ea6649e98261d62907118448b047987cbebcd2c7))
|
|
39
|
+
* use typeorm 0.3 ([aa0153b](https://github.com/jorgebodega/typeorm-seeding/commit/aa0153bd050c60ab94f9979eff4e5ab9bec81645))
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
### BREAKING CHANGES
|
|
43
|
+
|
|
44
|
+
* Removed config command
|
|
45
|
+
* Connection API is deprecated and removed.
|
|
46
|
+
|
|
1
47
|
# [5.0.0-next.2](https://github.com/jorgebodega/typeorm-seeding/compare/v5.0.0-next.1...v5.0.0-next.2) (2022-08-01)
|
|
2
48
|
|
|
3
49
|
|
package/README.md
CHANGED
|
@@ -4,33 +4,16 @@
|
|
|
4
4
|
<h1 align="center" style="text-align: center;">TypeORM Seeding</h1>
|
|
5
5
|
|
|
6
6
|
<p align="center">
|
|
7
|
-
<img alt="
|
|
8
|
-
<a href="https://www.npmjs.com/package/@jorgebodega/typeorm-seeding">
|
|
9
|
-
<img alt="NPM latest version" src="https://img.shields.io/npm/v/@jorgebodega/typeorm-seeding/latest?style=for-the-badge">
|
|
10
|
-
</a>
|
|
11
|
-
<a href="https://www.npmjs.com/package/@jorgebodega/typeorm-seeding/v/next">
|
|
12
|
-
<img alt="NPM next version" src="https://img.shields.io/npm/v/@jorgebodega/typeorm-seeding/next?style=for-the-badge">
|
|
13
|
-
</a>
|
|
7
|
+
<img alt="License" src="https://img.shields.io/npm/l/@jorgebodega/typeorm-seeding?style=for-the-badge">
|
|
14
8
|
<a href="https://github.com/semantic-release/semantic-release">
|
|
15
9
|
<img src="https://img.shields.io/badge/semantic--release-angular-e10079?logo=semantic-release&style=for-the-badge" alt="Semantic release" />
|
|
16
10
|
</a>
|
|
17
|
-
</p>
|
|
18
|
-
|
|
19
|
-
<p align="center">
|
|
20
11
|
<a href='https://coveralls.io/github/jorgebodega/typeorm-seeding'>
|
|
21
12
|
<img alt="Coveralls master branch" src="https://img.shields.io/coveralls/github/jorgebodega/typeorm-seeding/master?style=for-the-badge">
|
|
22
13
|
</a>
|
|
23
|
-
<a href='https://coveralls.io/github/jorgebodega/typeorm-seeding?branch=next'>
|
|
24
|
-
<img alt="Coveralls next branch" src="https://img.shields.io/coveralls/github/jorgebodega/typeorm-seeding/next?style=for-the-badge&label=coverage%40next">
|
|
25
|
-
</a>
|
|
26
14
|
</p>
|
|
27
15
|
|
|
28
|
-
|
|
29
|
-
<img alt="Checks for master branch" src="https://img.shields.io/github/checks-status/jorgebodega/typeorm-seeding/master?style=for-the-badge">
|
|
30
|
-
<a href='https://coveralls.io/github/jorgebodega/typeorm-seeding'>
|
|
31
|
-
<img alt="Checks for next branch" src="https://img.shields.io/github/checks-status/jorgebodega/typeorm-seeding/next?label=checks%40next&style=for-the-badge">
|
|
32
|
-
</a>
|
|
33
|
-
</p>
|
|
16
|
+
|
|
34
17
|
|
|
35
18
|
<p align="center">
|
|
36
19
|
<b>A delightful way to seed test data into your database.</b></br>
|
|
@@ -45,21 +28,9 @@
|
|
|
45
28
|
|
|
46
29
|
# Contents
|
|
47
30
|
|
|
48
|
-
- [Factory](#factory-1)
|
|
49
|
-
- [attrs](#attrs)
|
|
50
|
-
- [Simple value](#simple-value)
|
|
51
|
-
- [Function](#function)
|
|
52
|
-
- [InstanceAttribute](#instanceattribute)
|
|
53
|
-
- [LazyInstanceAttribute](#lazyinstanceattribute)
|
|
54
|
-
- [Subfactory](#subfactory)
|
|
55
|
-
- [make & makeMany](#make--makemany)
|
|
56
|
-
- [create & createMany](#create--createmany)
|
|
57
|
-
- [faker](#faker)
|
|
58
31
|
- [Seeder](#seeder-1)
|
|
59
32
|
- [run](#run)
|
|
60
|
-
- [call](#call)
|
|
61
33
|
- [CLI](#cli-configuration)
|
|
62
|
-
- [config](#config)
|
|
63
34
|
- [seed](#seed)
|
|
64
35
|
- [Testing features](#testing-features)
|
|
65
36
|
|
|
@@ -72,29 +43,9 @@ After that install the extension with `npm` or `yarn`. Add development flag if y
|
|
|
72
43
|
```bash
|
|
73
44
|
npm i [-D] @jorgebodega/typeorm-seeding
|
|
74
45
|
yarn add [-D] @jorgebodega/typeorm-seeding
|
|
46
|
+
pnpm add [-D] @jorgebodega/typeorm-seeding
|
|
75
47
|
```
|
|
76
48
|
|
|
77
|
-
## Configuration
|
|
78
|
-
|
|
79
|
-
To configure the path to your seeders extends the TypeORM config file or use environment variables like TypeORM. If both are used the environment variables will be prioritized.
|
|
80
|
-
|
|
81
|
-
**ormconfig.js**
|
|
82
|
-
|
|
83
|
-
```typescript
|
|
84
|
-
module.exports = {
|
|
85
|
-
...
|
|
86
|
-
seeders: ['src/seeds/**/*{.ts,.js}'],
|
|
87
|
-
defaultSeeder: RootSeeder,
|
|
88
|
-
...
|
|
89
|
-
}
|
|
90
|
-
```
|
|
91
|
-
|
|
92
|
-
**.env**
|
|
93
|
-
|
|
94
|
-
```
|
|
95
|
-
TYPEORM_SEEDING_SEEDERS=src/seeds/**/*{.ts,.js}
|
|
96
|
-
TYPEORM_SEEDING_DEFAULT_SEEDER=RootSeeder
|
|
97
|
-
```
|
|
98
49
|
|
|
99
50
|
# Introduction
|
|
100
51
|
|
|
@@ -128,243 +79,24 @@ class User {
|
|
|
128
79
|
}
|
|
129
80
|
```
|
|
130
81
|
|
|
131
|
-
### Factory
|
|
132
|
-
|
|
133
|
-
```typescript
|
|
134
|
-
class UserFactory extends Factory<User> {
|
|
135
|
-
protected entity = User
|
|
136
|
-
protected attrs: FactorizedAttrs<User> = {
|
|
137
|
-
name: faker.name.firstName(),
|
|
138
|
-
lastName: async () => faker.name.lastName(),
|
|
139
|
-
email: new InstanceAttribute((instance) =>
|
|
140
|
-
[instance.name.toLowerCase(), instance.lastName.toLowerCase(), '@email.com'].join(''),
|
|
141
|
-
),
|
|
142
|
-
country: new Subfactory(CountryFactory),
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
```
|
|
146
|
-
|
|
147
82
|
### Seeder
|
|
148
83
|
|
|
149
84
|
```typescript
|
|
150
85
|
class UserSeeder extends Seeder {
|
|
151
|
-
async run(
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
await this.call(connection, [PetSeeder])
|
|
86
|
+
async run(dataSource: DataSource) {
|
|
87
|
+
const users: User[] = [...]
|
|
88
|
+
await dataSource.createEntityManager().save<User>(users)
|
|
155
89
|
}
|
|
156
90
|
}
|
|
157
91
|
```
|
|
158
92
|
|
|
159
|
-
# Factory
|
|
160
|
-
|
|
161
|
-
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.
|
|
162
|
-
|
|
163
|
-
```typescript
|
|
164
|
-
class UserFactory extends Factory<User> {
|
|
165
|
-
protected entity = User
|
|
166
|
-
protected attrs: FactorizedAttrs<User> = {
|
|
167
|
-
...
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
```
|
|
171
|
-
|
|
172
|
-
## `attrs`
|
|
173
|
-
|
|
174
|
-
Attributes objects are superset from the original entity attributes.
|
|
175
|
-
|
|
176
|
-
```typescript
|
|
177
|
-
protected attrs: FactorizedAttrs<User> = {
|
|
178
|
-
name: faker.name.firstName(),
|
|
179
|
-
lastName: async () => faker.name.lastName(),
|
|
180
|
-
email: new InstanceAttribute((instance) =>
|
|
181
|
-
[instance.name.toLowerCase(), instance.lastName.toLowerCase(), '@email.com'].join(''),
|
|
182
|
-
),
|
|
183
|
-
country: new Subfactory(CountryFactory),
|
|
184
|
-
}
|
|
185
|
-
```
|
|
186
|
-
|
|
187
|
-
Those factorized attributes resolves to the value of the original attribute, and could be one of the following types:
|
|
188
|
-
|
|
189
|
-
- [Simple value](#simple-value)
|
|
190
|
-
- [Function](#function)
|
|
191
|
-
- [InstanceAttribute](#instanceattribute)
|
|
192
|
-
- [LazyInstanceAttribute](#lazyinstanceattribute)
|
|
193
|
-
- [Subfactory](#subfactory)
|
|
194
|
-
|
|
195
|
-
### Simple value
|
|
196
|
-
|
|
197
|
-
Nothing special, just a value with same type.
|
|
198
|
-
|
|
199
|
-
```typescript
|
|
200
|
-
protected attrs: FactorizedAttrs<User> = {
|
|
201
|
-
name: faker.name.firstName(),
|
|
202
|
-
}
|
|
203
|
-
```
|
|
204
|
-
|
|
205
|
-
### Function
|
|
206
|
-
|
|
207
|
-
Function that could be sync or async, and return a value of the same type. This function will be executed once per entity.
|
|
208
|
-
|
|
209
|
-
```typescript
|
|
210
|
-
protected attrs: FactorizedAttrs<User> = {
|
|
211
|
-
lastName: async () => faker.name.lastName(),
|
|
212
|
-
}
|
|
213
|
-
```
|
|
214
|
-
|
|
215
|
-
### `InstanceAttribute`
|
|
216
|
-
|
|
217
|
-
```typescript
|
|
218
|
-
class InstanceAttribute<T, V> {
|
|
219
|
-
constructor(private callback: (entity: T) => V) {}
|
|
220
|
-
|
|
221
|
-
...
|
|
222
|
-
}
|
|
223
|
-
```
|
|
224
|
-
|
|
225
|
-
Class with a function that receive the current instance and returns a value of the same type. It is ideal for attributes that could depend on some others to be computed.
|
|
226
|
-
|
|
227
|
-
Will be executed after the entity has been created and the rest of the attributes have been calculated, but before persistance (in case of `create` or `createMany`).
|
|
228
|
-
|
|
229
|
-
```typescript
|
|
230
|
-
protected attrs: FactorizedAttrs<User> = {
|
|
231
|
-
name: faker.name.firstName(),
|
|
232
|
-
lastName: async () => faker.name.lastName(),
|
|
233
|
-
email: new InstanceAttribute((instance) =>
|
|
234
|
-
[instance.name.toLowerCase(), instance.lastName.toLowerCase(), '@email.com'].join(''),
|
|
235
|
-
),
|
|
236
|
-
}
|
|
237
|
-
```
|
|
238
|
-
|
|
239
|
-
In this simple case, if `name` or `lastName` override the value in any way, the `email` attribute will be affected too.
|
|
240
|
-
|
|
241
|
-
### `LazyInstanceAttribute`
|
|
242
|
-
|
|
243
|
-
```typescript
|
|
244
|
-
class LazyInstanceAttribute<T, V> {
|
|
245
|
-
constructor(private callback: (entity: T) => V) {}
|
|
246
|
-
|
|
247
|
-
...
|
|
248
|
-
}
|
|
249
|
-
```
|
|
250
|
-
|
|
251
|
-
Class with similar functionality than `InstanceAttribute`, but it will be executed only after persistance. This is useful for attributes that depends on the database id, like relations.
|
|
252
|
-
|
|
253
|
-
Just remember that, if you use `make` or `makeMany`, the only difference between `InstanceAttribute` and `LazyInstanceAttribute` is that `LazyInstanceAttribute` will be processed the last.
|
|
254
|
-
|
|
255
|
-
```typescript
|
|
256
|
-
protected attrs: FactorizedAttrs<User> = {
|
|
257
|
-
name: faker.name.firstName(),
|
|
258
|
-
email: new LazyInstanceAttribute((instance) =>
|
|
259
|
-
[instance.name.toLowerCase(), instance.id, '@email.com'].join(''),
|
|
260
|
-
),
|
|
261
|
-
}
|
|
262
|
-
```
|
|
263
|
-
|
|
264
|
-
### `Subfactory`
|
|
265
|
-
|
|
266
|
-
```typescript
|
|
267
|
-
export class Subfactory<T> {
|
|
268
|
-
constructor(factory: Constructable<Factory<T>>)
|
|
269
|
-
constructor(factory: Constructable<Factory<T>>, values?: Partial<FactorizedAttrs<T>>)
|
|
270
|
-
constructor(factory: Constructable<Factory<T>>, count?: number)
|
|
271
|
-
constructor(factory: Constructable<Factory<T>>, values?: Partial<FactorizedAttrs<T>>, count?: number)
|
|
272
|
-
|
|
273
|
-
...
|
|
274
|
-
}
|
|
275
|
-
```
|
|
276
|
-
|
|
277
|
-
Subfactories are just a wrapper of another factory, to avoid explicit operations that could lead to unexpected results over that factory, like
|
|
278
|
-
|
|
279
|
-
```typescript
|
|
280
|
-
protected attrs: FactorizedAttrs<User> = {
|
|
281
|
-
country: async () => new CountryFactory().create({
|
|
282
|
-
name: faker.address.country(),
|
|
283
|
-
}),
|
|
284
|
-
}
|
|
285
|
-
```
|
|
286
|
-
|
|
287
|
-
instead of
|
|
288
|
-
|
|
289
|
-
```typescript
|
|
290
|
-
protected attrs: FactorizedAttrs<User> = {
|
|
291
|
-
country: new Subfactory(CountryFactory, {
|
|
292
|
-
name: faker.address.country(),
|
|
293
|
-
}),
|
|
294
|
-
}
|
|
295
|
-
```
|
|
296
|
-
|
|
297
|
-
Subfactory just execute the same kind of operation (`make` or `create`) over the factory. If `count` param is provided, it will execute `makeMany`/`createMany` instead of `make`/`create`, and returns an array.
|
|
298
|
-
|
|
299
|
-
## `make` & `makeMany`
|
|
300
|
-
|
|
301
|
-
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.
|
|
302
|
-
|
|
303
|
-
- **overrideParams** - Override some of the attributes of the entity.
|
|
304
|
-
|
|
305
|
-
```typescript
|
|
306
|
-
make(overrideParams: Partial<FactorizedAttrs<T>> = {}): Promise<T>
|
|
307
|
-
makeMany(amount: number, overrideParams: Partial<FactorizedAttrs<T>> = {}): Promise<T[]>
|
|
308
|
-
```
|
|
309
|
-
|
|
310
|
-
```typescript
|
|
311
|
-
new UserFactory().make()
|
|
312
|
-
new UserFactory().makeMany(10)
|
|
313
|
-
|
|
314
|
-
// override the email
|
|
315
|
-
new UserFactory().make({ email: 'other@mail.com' })
|
|
316
|
-
new UserFactory().makeMany(10, { email: 'other@mail.com' })
|
|
317
|
-
```
|
|
318
|
-
|
|
319
|
-
## `create` & `createMany`
|
|
320
|
-
|
|
321
|
-
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.
|
|
322
|
-
|
|
323
|
-
- **overrideParams** - Override some of the attributes of the entity.
|
|
324
|
-
- **saveOptions** - [Save options](https://github.com/typeorm/typeorm/blob/master/src/repository/SaveOptions.ts) from TypeORM
|
|
325
|
-
|
|
326
|
-
```typescript
|
|
327
|
-
create(overrideParams: Partial<FactorizedAttrs<T>> = {}, saveOptions?: SaveOptions): Promise<T>
|
|
328
|
-
createMany(amount: number, overrideParams: Partial<FactorizedAttrs<T>> = {}, saveOptions?: SaveOptions): Promise<T[]>
|
|
329
|
-
```
|
|
330
|
-
|
|
331
|
-
```typescript
|
|
332
|
-
new UserFactory().create()
|
|
333
|
-
new UserFactory().createMany(10)
|
|
334
|
-
|
|
335
|
-
// override the email
|
|
336
|
-
new UserFactory().create({ email: 'other@mail.com' })
|
|
337
|
-
new UserFactory().createMany(10, { email: 'other@mail.com' })
|
|
338
|
-
|
|
339
|
-
// using save options
|
|
340
|
-
new UserFactory().create({ email: 'other@mail.com' }, { listeners: false })
|
|
341
|
-
new UserFactory().createMany(10, { email: 'other@mail.com' }, { listeners: false })
|
|
342
|
-
```
|
|
343
|
-
|
|
344
|
-
## faker
|
|
345
|
-
|
|
346
|
-
[Faker](https://github.com/faker-js/faker) package has been removed from `dependencies`. If you want to use it, please install it manually and just import when needed.
|
|
347
|
-
|
|
348
|
-
```bash
|
|
349
|
-
npm i [-D] @faker-js/faker
|
|
350
|
-
yarn add [-D] @faker-js/faker
|
|
351
|
-
```
|
|
352
|
-
|
|
353
|
-
```typescript
|
|
354
|
-
import { faker } from '@faker-js/faker'
|
|
355
|
-
|
|
356
|
-
class UserFactory extends Factory<User> {
|
|
357
|
-
...
|
|
358
|
-
}
|
|
359
|
-
```
|
|
360
|
-
|
|
361
93
|
# Seeder
|
|
362
94
|
|
|
363
95
|
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.
|
|
364
96
|
|
|
365
97
|
```typescript
|
|
366
98
|
class UserSeeder extends Seeder {
|
|
367
|
-
async run(
|
|
99
|
+
async run(dataSource: DataSource) {
|
|
368
100
|
...
|
|
369
101
|
}
|
|
370
102
|
}
|
|
@@ -372,92 +104,51 @@ class UserSeeder extends Seeder {
|
|
|
372
104
|
|
|
373
105
|
## `run`
|
|
374
106
|
|
|
375
|
-
This function is the one that needs to be defined when extending the class.
|
|
107
|
+
This function is the one that needs to be defined when extending the class.
|
|
376
108
|
|
|
377
109
|
```typescript
|
|
378
|
-
run(
|
|
110
|
+
run(dataSource: DataSource): Promise<void>
|
|
379
111
|
```
|
|
380
112
|
|
|
381
113
|
```typescript
|
|
382
|
-
async run(
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
await this.call(connection, [PetSeeder])
|
|
114
|
+
async run(dataSource: DataSource) {
|
|
115
|
+
const users: User[] = [...]
|
|
116
|
+
await dataSource.createEntityManager().save<User>(users)
|
|
386
117
|
}
|
|
387
118
|
```
|
|
388
119
|
|
|
389
|
-
## `call`
|
|
390
|
-
|
|
391
|
-
This function allow to run some other seeders in a sequential way.
|
|
392
|
-
|
|
393
|
-
In order to use seeders from cli command, a default seeder class must be provided as root seeder, working as a tree structure.
|
|
394
|
-
|
|
395
|
-
<p align="center">
|
|
396
|
-
<img src="./seeders.png" alt="logo" />
|
|
397
|
-
</p>
|
|
398
|
-
|
|
399
120
|
# CLI Configuration
|
|
400
121
|
|
|
401
|
-
There
|
|
402
|
-
|
|
403
|
-
Add the following scripts to your `package.json` file to configure them.
|
|
404
|
-
|
|
405
|
-
```json
|
|
406
|
-
"scripts": {
|
|
407
|
-
"seed:config": "typeorm-seeding config",
|
|
408
|
-
"seed:run": "typeorm-seeding seed",
|
|
409
|
-
...
|
|
410
|
-
}
|
|
411
|
-
```
|
|
412
|
-
|
|
413
|
-
## `config`
|
|
414
|
-
|
|
415
|
-
This command just print the connection configuration.
|
|
122
|
+
There is a command that allows you to execute multiple seeders from cli.
|
|
416
123
|
|
|
417
124
|
```bash
|
|
418
|
-
typeorm-seeding
|
|
125
|
+
typeorm-seeding seed -d path/to/datasource src/seeders/*.ts
|
|
419
126
|
```
|
|
420
127
|
|
|
421
|
-
|
|
128
|
+
Add the following script to your `package.json` file to configure them.
|
|
422
129
|
|
|
423
130
|
```json
|
|
424
|
-
{
|
|
425
|
-
"
|
|
426
|
-
|
|
427
|
-
"database": "/home/jorgebodega/projects/typeorm-seeding/test.db",
|
|
428
|
-
"entities": ["sample/entities/**/*{.ts,.js}"],
|
|
429
|
-
"seeders": ["sample/seeders/**/*{.ts,.js}"],
|
|
430
|
-
"defaultSeeder": "RootSeeder"
|
|
131
|
+
"scripts": {
|
|
132
|
+
"seed:run": "typeorm-seeding seed -d path/to/datasource",
|
|
133
|
+
...
|
|
431
134
|
}
|
|
432
135
|
```
|
|
433
136
|
|
|
434
|
-
##### Options
|
|
435
|
-
|
|
436
|
-
| Option | Default | Description |
|
|
437
|
-
| ---------------------- | --------------------- | ---------------------------------------------------------------------------- |
|
|
438
|
-
| `--seed` or `-s` | null | Option to specify a seeder class to run individually. (Only on seed command) |
|
|
439
|
-
| `--connection` or `-c` | TypeORM default value | Name of the TypeORM connection. Required if there are multiple connections. |
|
|
440
|
-
| `--configName` or `-n` | TypeORM default value | Name to the TypeORM config file. |
|
|
441
|
-
| `--root` or `-r` | TypeORM default value | Path to the TypeORM config file. |
|
|
442
|
-
|
|
443
137
|
## `seed`
|
|
444
138
|
|
|
445
|
-
This command execute a seeder, that could be specified as a parameter.
|
|
139
|
+
This command execute a seeder, that could be specified as a parameter. Glob pattern is supported.
|
|
446
140
|
|
|
447
141
|
```bash
|
|
448
|
-
typeorm-seeding seed
|
|
142
|
+
typeorm-seeding seed <path>
|
|
449
143
|
```
|
|
450
144
|
|
|
451
|
-
|
|
145
|
+
CLI command only executes default seeders.
|
|
452
146
|
|
|
453
147
|
##### Options
|
|
454
148
|
|
|
455
|
-
| Option | Default | Description
|
|
456
|
-
| ---------------------- | ------------------------------------ |
|
|
457
|
-
| `--
|
|
458
|
-
| `--connection` or `-c` | TypeORM default value | Name of the TypeORM connection. Required if there are multiple connections. |
|
|
459
|
-
| `--configName` or `-n` | TypeORM default value | Name to the TypeORM config file. |
|
|
460
|
-
| `--root` or `-r` | TypeORM default value | Path to the TypeORM config file. |
|
|
149
|
+
| Option | Default | Description |
|
|
150
|
+
| ---------------------- | ------------------------------------ | ----------------------------------------------------- |
|
|
151
|
+
| `--dataSource` or `-d` | | Path of the data source |
|
|
461
152
|
|
|
462
153
|
# Testing features
|
|
463
154
|
|
|
@@ -475,3 +166,22 @@ useSeeders(
|
|
|
475
166
|
customOptions: Partial<ConnectionConfiguration>,
|
|
476
167
|
): Promise<void>
|
|
477
168
|
```
|
|
169
|
+
|
|
170
|
+
## `useDataSource`
|
|
171
|
+
|
|
172
|
+
Use specific data source on the factories. If the data source is not initialized when provided, can be initialized with the `forceInitialization` flag.
|
|
173
|
+
|
|
174
|
+
```typescript
|
|
175
|
+
useDataSource(dataSource: DataSource): Promise<void>
|
|
176
|
+
useDataSource(dataSource: DataSource, overrideOptions: Partial<DataSourceOptions>): Promise<void>
|
|
177
|
+
useDataSource(dataSource: DataSource, forceInitialization: boolean): Promise<void>
|
|
178
|
+
useDataSource(
|
|
179
|
+
dataSource: DataSource,
|
|
180
|
+
overrideOptions: Partial<DataSourceOptions>,
|
|
181
|
+
forceInitialization: boolean,
|
|
182
|
+
): Promise<void>
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
# Factory
|
|
186
|
+
|
|
187
|
+
Factory related code has been removed from this package, now on [@jorgebodega/typeorm-factory](https://github.com/jorgebodega/typeorm-factory).
|
package/dist/cli.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
|
|
2
|
+
export {};
|
package/dist/cli.js
CHANGED
|
@@ -1,17 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
"use strict";
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
const tslib_1 = require("tslib");
|
|
5
|
-
/* istanbul ignore file */
|
|
6
|
-
require("reflect-metadata");
|
|
7
|
-
const yargs_1 = tslib_1.__importDefault(require("yargs"));
|
|
8
4
|
const seed_command_1 = require("./commands/seed.command");
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
.command(new seed_command_1.SeedCommand())
|
|
12
|
-
.recommendCommands()
|
|
13
|
-
.demandCommand(1)
|
|
14
|
-
.strict()
|
|
15
|
-
.help('h')
|
|
16
|
-
.alias('h', 'help').argv;
|
|
5
|
+
/* istanbul ignore file */
|
|
6
|
+
(0, seed_command_1.bootstrap)(process.argv);
|
|
17
7
|
//# sourceMappingURL=cli.js.map
|
package/dist/cli.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cli.js","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"cli.js","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":";;;AACA,0DAAmD;AACnD,0BAA0B;AAC1B,IAAA,wBAAS,EAAC,OAAO,CAAC,IAAI,CAAC,CAAA"}
|
|
@@ -1,25 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import { Arguments, Argv, CommandModule } from 'yargs';
|
|
3
|
-
import { Seeder } from '../seeder';
|
|
4
|
-
import type { Constructable } from '../types';
|
|
5
|
-
interface SeedCommandArguments extends Arguments {
|
|
6
|
-
dataSource?: string;
|
|
7
|
-
path?: string;
|
|
8
|
-
}
|
|
9
|
-
export declare class SeedCommand implements CommandModule {
|
|
10
|
-
command: string;
|
|
11
|
-
describe: string;
|
|
12
|
-
/**
|
|
13
|
-
* @inheritdoc
|
|
14
|
-
*/
|
|
15
|
-
builder(args: Argv): Argv<{
|
|
16
|
-
d: string;
|
|
17
|
-
}>;
|
|
18
|
-
/**
|
|
19
|
-
* @inheritdoc
|
|
20
|
-
*/
|
|
21
|
-
handler(args: SeedCommandArguments): Promise<void>;
|
|
22
|
-
static loadDataSource(dataSourceFilePath: string): Promise<DataSource>;
|
|
23
|
-
static loadSeeders(seederPaths: string[]): Promise<Constructable<Seeder>[]>;
|
|
24
|
-
}
|
|
25
|
-
export {};
|
|
1
|
+
export declare function bootstrap(argv: string[]): Promise<void>;
|