@jorgebodega/typeorm-seeding 3.2.0 → 4.0.0-next.3

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 CHANGED
@@ -1,3 +1,28 @@
1
+ # [4.0.0-next.3](https://github.com/jorgebodega/typeorm-seeding/compare/v4.0.0-next.2...v4.0.0-next.3) (2022-03-03)
2
+
3
+ # [4.0.0-next.2](https://github.com/jorgebodega/typeorm-seeding/compare/v4.0.0-next.1...v4.0.0-next.2) (2022-02-12)
4
+
5
+ # [4.0.0-next.1](https://github.com/jorgebodega/typeorm-seeding/compare/v3.2.0...v4.0.0-next.1) (2022-02-12)
6
+
7
+
8
+ ### Bug Fixes
9
+
10
+ * add type check on subfactories creation ([2b2883b](https://github.com/jorgebodega/typeorm-seeding/commit/2b2883b48ea08173a032b219266b1fece937e753))
11
+ * attrs failing with lazy instances ([c8ddda8](https://github.com/jorgebodega/typeorm-seeding/commit/c8ddda841f9e65d7081e419d2d2e662c6d6a78df))
12
+
13
+
14
+ ### Features
15
+
16
+ * add factorized attrs ([4a2ce08](https://github.com/jorgebodega/typeorm-seeding/commit/4a2ce08e1be1d79df8f1c0450ba4b315ebca8c46))
17
+ * add lazyattribute and subfactory as options in attribs ([48a3630](https://github.com/jorgebodega/typeorm-seeding/commit/48a3630b8c606ff5189c05dc51908dc09f142184))
18
+ * add subfactory as valid factorized attr ([0f4b37e](https://github.com/jorgebodega/typeorm-seeding/commit/0f4b37e0f8c3e93aedbd109ddb8f33ac0127a2dc))
19
+ * separate different types of lazy attributes ([3efe94e](https://github.com/jorgebodega/typeorm-seeding/commit/3efe94ea140643ccb12dbf8858968a5866fd7577))
20
+
21
+
22
+ ### BREAKING CHANGES
23
+
24
+ * definition function has been substituted with attrs
25
+
1
26
  # [3.2.0](https://github.com/jorgebodega/typeorm-seeding/compare/v3.1.0...v3.2.0) (2022-02-06)
2
27
 
3
28
 
package/README.md CHANGED
@@ -13,7 +13,7 @@
13
13
  </a>
14
14
  <a href="https://github.com/semantic-release/semantic-release">
15
15
  <img src="https://img.shields.io/badge/semantic--release-angular-e10079?logo=semantic-release&style=for-the-badge" alt="Semantic release" />
16
- </a>
16
+ </a>
17
17
  </p>
18
18
 
19
19
  <p align="center">
@@ -43,14 +43,27 @@
43
43
 
44
44
  <br />
45
45
 
46
- ## Additional contents
46
+ # Contents
47
47
 
48
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)
49
58
  - [Seeder](#seeder-1)
59
+ - [run](#run)
60
+ - [call](#call)
50
61
  - [CLI](#cli-configuration)
62
+ - [config](#config)
63
+ - [seed](#seed)
51
64
  - [Testing features](#testing-features)
52
65
 
53
- ## Installation
66
+ # Installation
54
67
 
55
68
  Before using this TypeORM extension please read the [TypeORM Getting Started](https://typeorm.io/#/) documentation. This explains how to setup a TypeORM project.
56
69
 
@@ -61,9 +74,9 @@ npm i [-D] @jorgebodega/typeorm-seeding
61
74
  yarn add [-D] @jorgebodega/typeorm-seeding
62
75
  ```
63
76
 
64
- ### Configuration
77
+ ## Configuration
65
78
 
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.
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.
67
80
 
68
81
  **ormconfig.js**
69
82
 
@@ -83,7 +96,7 @@ TYPEORM_SEEDING_SEEDERS=src/seeds/**/*{.ts,.js}
83
96
  TYPEORM_SEEDING_DEFAULT_SEEDER=RootSeeder
84
97
  ```
85
98
 
86
- ## Introduction
99
+ # Introduction
87
100
 
88
101
  Isn't it exhausting to create some sample data for your database, well this time is over!
89
102
 
@@ -94,11 +107,24 @@ How does it work? Just create a entity factory and/or seed script.
94
107
  ```typescript
95
108
  @Entity()
96
109
  class User {
97
- @PrimaryGeneratedColumn('uuid') id: string
110
+ @PrimaryGeneratedColumn('increment')
111
+ id!: number
112
+
113
+ @Column()
114
+ name!: string
115
+
116
+ @Column()
117
+ lastName!: string
98
118
 
99
- @Column() name: string
119
+ @Column()
120
+ email!: string
100
121
 
101
- @Column() lastname: string
122
+ @OneToMany(() => Pet, (pet) => pet.owner)
123
+ pets?: Pet[]
124
+
125
+ @ManyToOne(() => Country, (country) => country.users, { nullable: false })
126
+ @JoinColumn()
127
+ country!: Country
102
128
  }
103
129
  ```
104
130
 
@@ -106,13 +132,14 @@ class User {
106
132
 
107
133
  ```typescript
108
134
  class UserFactory extends Factory<User> {
109
- protected definition(): User {
110
- const user = new User()
111
-
112
- user.name = 'John'
113
- user.lastname = 'Doe'
114
-
115
- return 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),
116
143
  }
117
144
  }
118
145
  ```
@@ -120,67 +147,164 @@ class UserFactory extends Factory<User> {
120
147
  ### Seeder
121
148
 
122
149
  ```typescript
123
- export class UserExampleSeeder extends Seeder {
124
- async run() {
125
- await new UserFactory().create({
126
- name: 'Jane',
127
- })
150
+ class UserSeeder extends Seeder {
151
+ async run(connection: Connection) {
152
+ await new UserFactory().createMany(10)
153
+
154
+ await this.call(connection, [PetSeeder])
128
155
  }
129
156
  }
130
157
  ```
131
158
 
132
- ## Factory
159
+ # Factory
133
160
 
134
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.
135
162
 
136
163
  ```typescript
137
164
  class UserFactory extends Factory<User> {
138
- protected definition(): User {
165
+ protected entity = User
166
+ protected attrs: FactorizedAttrs<User> = {
139
167
  ...
140
168
  }
141
169
  }
142
170
  ```
143
171
 
144
- ### `definition`
172
+ ## `attrs`
145
173
 
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.
174
+ Attributes objects are superset from the original entity attributes.
147
175
 
148
176
  ```typescript
149
- protected definition(): User {
150
- const user = new User()
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)
151
194
 
152
- user.name = 'John'
153
- user.lastname = 'Doe'
195
+ ### Simple value
154
196
 
155
- return user
197
+ Nothing special, just a value with same type.
198
+
199
+ ```typescript
200
+ protected attrs: FactorizedAttrs<User> = {
201
+ name: faker.name.firstName(),
156
202
  }
157
203
  ```
158
204
 
159
- It is possible to create more than one factory related to a single entity, with different definition functions.
205
+ ### Function
160
206
 
161
- ### `map`
207
+ Function that could be sync or async, and return a value of the same type. This function will be executed once per entity.
162
208
 
163
- Use the `.map()` function to alter the generated value before they get processed.
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`).
164
228
 
165
229
  ```typescript
166
- map(mapFunction: (entity: Entity) => void): Factory
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
+ }
167
237
  ```
168
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
+
169
243
  ```typescript
170
- new UserFactory().map((user) => {
171
- user.name = 'Jane'
172
- })
244
+ class LazyInstanceAttribute<T, V> {
245
+ constructor(private callback: (entity: T) => V) {}
246
+
247
+ ...
248
+ }
173
249
  ```
174
250
 
175
- ### `make` & `makeMany`
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`
176
300
 
177
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.
178
302
 
179
303
  - **overrideParams** - Override some of the attributes of the entity.
180
304
 
181
305
  ```typescript
182
- make(overrideParams: Partial<Entity> = {}): Promise<Entity>
183
- makeMany(amount: number, overrideParams: Partial<Entity> = {}): Promise<Entity>
306
+ make(overrideParams: Partial<FactorizedAttrs<T>> = {}): Promise<T>
307
+ makeMany(amount: number, overrideParams: Partial<FactorizedAttrs<T>> = {}): Promise<T[]>
184
308
  ```
185
309
 
186
310
  ```typescript
@@ -192,7 +316,7 @@ new UserFactory().make({ email: 'other@mail.com' })
192
316
  new UserFactory().makeMany(10, { email: 'other@mail.com' })
193
317
  ```
194
318
 
195
- ### `create` & `createMany`
319
+ ## `create` & `createMany`
196
320
 
197
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.
198
322
 
@@ -200,8 +324,8 @@ the create and createMany method is similar to the make and makeMany method, but
200
324
  - **saveOptions** - [Save options](https://github.com/typeorm/typeorm/blob/master/src/repository/SaveOptions.ts) from TypeORM
201
325
 
202
326
  ```typescript
203
- create(overrideParams: Partial<Entity> = {}, saveOptions?: SaveOptions): Promise<Entity>
204
- createMany(amount: number, overrideParams: Partial<Entity> = {}, saveOptions?: SaveOptions): Promise<Entity>
327
+ create(overrideParams: Partial<FactorizedAttrs<T>> = {}, saveOptions?: SaveOptions): Promise<T>
328
+ createMany(amount: number, overrideParams: Partial<FactorizedAttrs<T>> = {}, saveOptions?: SaveOptions): Promise<T[]>
205
329
  ```
206
330
 
207
331
  ```typescript
@@ -217,50 +341,24 @@ new UserFactory().create({ email: 'other@mail.com' }, { listeners: false })
217
341
  new UserFactory().createMany(10, { email: 'other@mail.com' }, { listeners: false })
218
342
  ```
219
343
 
220
- ### Execution order
221
-
222
- As the order of execution can be complex, you can check it here:
223
-
224
- 2. **Map function**: Map function alters the already existing entity.
225
- 3. **Override params**: Alters the already existing entity.
226
- 4. **Promises**: If some attribute is a promise, the promise will be resolved before the entity is created.
227
- 5. **Factories**: If some attribute is a factory, the factory will be executed with `make`/`create` like the previous one.
228
-
229
- ### Faker
230
-
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.
232
-
233
- Instead of the previous example:
344
+ ## faker
234
345
 
235
- ```typescript
236
- define(User, (faker: typeof Faker) => {
237
- const firstName = faker.name.firstName()
238
- const lastName = faker.name.lastName()
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.
239
347
 
240
- const user = new User()
241
- user.name = `${firstName} ${lastName}`
242
- return user
243
- })
348
+ ```bash
349
+ npm i [-D] @faker-js/faker
350
+ yarn add [-D] @faker-js/faker
244
351
  ```
245
352
 
246
- You can do:
247
-
248
353
  ```typescript
249
- import * as faker from 'faker'
354
+ import { faker } from '@faker-js/faker'
250
355
 
251
356
  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
- }
357
+ ...
260
358
  }
261
359
  ```
262
360
 
263
- ## Seeder
361
+ # Seeder
264
362
 
265
363
  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.
266
364
 
@@ -272,7 +370,7 @@ class UserSeeder extends Seeder {
272
370
  }
273
371
  ```
274
372
 
275
- ### `run`
373
+ ## `run`
276
374
 
277
375
  This function is the one that needs to be defined when extending the class. Could use `call` to run some other seeders.
278
376
 
@@ -288,7 +386,7 @@ async run(connection: Connection) {
288
386
  }
289
387
  ```
290
388
 
291
- ### `call`
389
+ ## `call`
292
390
 
293
391
  This function allow to run some other seeders in a sequential way.
294
392
 
@@ -298,7 +396,7 @@ In order to use seeders from cli command, a default seeder class must be provide
298
396
  <img src="./seeders.png" alt="logo" />
299
397
  </p>
300
398
 
301
- ## CLI Configuration
399
+ # CLI Configuration
302
400
 
303
401
  There are two possible commands to execute, one to see the current configuration and one to run a seeder.
304
402
 
@@ -312,7 +410,7 @@ Add the following scripts to your `package.json` file to configure them.
312
410
  }
313
411
  ```
314
412
 
315
- ### `config`
413
+ ## `config`
316
414
 
317
415
  This command just print the connection configuration.
318
416
 
@@ -342,7 +440,7 @@ Example result
342
440
  | `--configName` or `-n` | TypeORM default value | Name to the TypeORM config file. |
343
441
  | `--root` or `-r` | TypeORM default value | Path to the TypeORM config file. |
344
442
 
345
- ### `seed`
443
+ ## `seed`
346
444
 
347
445
  This command execute a seeder, that could be specified as a parameter.
348
446
 
@@ -359,12 +457,12 @@ typeorm-seeding seed
359
457
  | `--configName` or `-n` | TypeORM default value | Name to the TypeORM config file. |
360
458
  | `--root` or `-r` | TypeORM default value | Path to the TypeORM config file. |
361
459
 
362
- ## Testing features
460
+ # Testing features
363
461
 
364
462
  We provide some testing features that we already use to test this package, like connection configuration.
365
463
  The entity factories can also be used in testing. To do so call the `useFactories` or `useSeeders` function.
366
464
 
367
- ### `useSeeders`
465
+ ## `useSeeders`
368
466
 
369
467
  Execute one or more seeders.
370
468
 
@@ -375,7 +473,3 @@ useSeeders(
375
473
  customOptions: Partial<ConnectionConfiguration>,
376
474
  ): Promise<void>
377
475
  ```
378
-
379
- ### Factories
380
-
381
- If factories are being used to create entities, just remember to clean up fake data after every execution.
@@ -1 +1 @@
1
- {"version":3,"file":"seed.command.js","sourceRoot":"","sources":["../../src/commands/seed.command.ts"],"names":[],"mappings":";;;;AAAA,iCAA4D;AAC5D,2DAA8B;AAC9B,iCAA4B;AAC5B,8CAAyE;AAEzE,8CAA0C;AAC1C,wDAA0D;AAE1D,6EAAyE;AAUzE,MAAa,WAAW;IAAxB;QACE,YAAO,GAAG,MAAM,CAAA;QAChB,aAAQ,GAAG,gBAAgB,CAAA;IAmF7B,CAAC;IAjFC;;OAEG;IACH,OAAO,CAAC,IAAU;QAChB,OAAO,IAAI;aACR,MAAM,CAAC,GAAG,EAAE;YACX,KAAK,EAAE,YAAY;YACnB,IAAI,EAAE,QAAQ;YACd,QAAQ,EAAE,+CAA+C;SAC1D,CAAC;aACD,MAAM,CAAC,GAAG,EAAE;YACX,KAAK,EAAE,YAAY;YACnB,IAAI,EAAE,QAAQ;YACd,OAAO,EAAE,SAAS;YAClB,QAAQ,EAAE,gCAAgC;SAC3C,CAAC;aACD,MAAM,CAAC,GAAG,EAAE;YACX,KAAK,EAAE,MAAM;YACb,IAAI,EAAE,QAAQ;YACd,QAAQ,EAAE,kCAAkC;SAC7C,CAAC;aACD,MAAM,CAAC,GAAG,EAAE;YACX,KAAK,EAAE,MAAM;YACb,IAAI,EAAE,QAAQ;YACd,QAAQ,EAAE,6BAA6B;SACxC,CAAC,CAAA;IACN,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,OAAO,CAAC,IAA0B;QACtC,MAAM,OAAO,GAAG,IAAA,aAAG,EAAC,EAAE,IAAI,EAAE,mBAAmB,EAAE,QAAQ,EAAE,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,MAAM,EAAE,CAAC,CAAC,KAAK,EAAE,CAAA;QAErG,0BAA0B;QAC1B,IAAI,OAA2B,CAAA;QAC/B,IAAI;YACF,IAAA,gCAAmB,EAAC;gBAClB,IAAI,EAAE,IAAI,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d.ts CHANGED
@@ -1,28 +1,25 @@
1
1
  import type { SaveOptions } from 'typeorm';
2
- export declare abstract class Factory<Entity> {
3
- private mapFunction?;
4
- protected abstract definition(): Promise<Entity>;
5
- /**
6
- * This function is used to alter the generated values of entity, before it
7
- * is persist into the database
8
- */
9
- map(mapFunction: (entity: Entity) => Promise<void> | void): this;
2
+ import type { Constructable, FactorizedAttrs } from './types';
3
+ export declare abstract class Factory<T> {
4
+ protected abstract entity: Constructable<T>;
5
+ protected abstract attrs: FactorizedAttrs<T>;
10
6
  /**
11
7
  * Make a new entity without persisting it
12
8
  */
13
- make(overrideParams?: Partial<Entity>): Promise<Entity>;
9
+ make(overrideParams?: Partial<FactorizedAttrs<T>>): Promise<T>;
14
10
  /**
15
11
  * Make many new entities without persisting it
16
12
  */
17
- makeMany(amount: number, overrideParams?: Partial<Entity>): Promise<Entity[]>;
13
+ makeMany(amount: number, overrideParams?: Partial<FactorizedAttrs<T>>): Promise<T[]>;
18
14
  /**
19
15
  * Create a new entity and persist it
20
16
  */
21
- create(overrideParams?: Partial<Entity>, saveOptions?: SaveOptions): Promise<Entity>;
17
+ create(overrideParams?: Partial<FactorizedAttrs<T>>, saveOptions?: SaveOptions): Promise<T>;
22
18
  /**
23
19
  * Create many new entities and persist them
24
20
  */
25
- createMany(amount: number, overrideParams?: Partial<Entity>, saveOptions?: SaveOptions): Promise<Entity[]>;
21
+ createMany(amount: number, overrideParams?: Partial<FactorizedAttrs<T>>, saveOptions?: SaveOptions): Promise<T[]>;
26
22
  private makeEntity;
27
- private resolveEntity;
23
+ private applyLazyAttributes;
24
+ private static resolveValue;
28
25
  }
package/dist/factory.js CHANGED
@@ -2,21 +2,18 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Factory = void 0;
4
4
  const connection_1 = require("./connection");
5
- const isPromiseLike_1 = require("./utils/isPromiseLike");
5
+ const instanceAttribute_1 = require("./instanceAttribute");
6
+ const lazyInstanceAttribute_1 = require("./lazyInstanceAttribute");
7
+ const subfactory_1 = require("./subfactory");
6
8
  class Factory {
7
- /**
8
- * This function is used to alter the generated values of entity, before it
9
- * is persist into the database
10
- */
11
- map(mapFunction) {
12
- this.mapFunction = mapFunction;
13
- return this;
14
- }
15
9
  /**
16
10
  * Make a new entity without persisting it
17
11
  */
18
12
  async make(overrideParams = {}) {
19
- return this.makeEntity(overrideParams, false);
13
+ const attrs = { ...this.attrs, ...overrideParams };
14
+ const entity = await this.makeEntity(attrs, false);
15
+ await this.applyLazyAttributes(entity, attrs, false);
16
+ return entity;
20
17
  }
21
18
  /**
22
19
  * Make many new entities without persisting it
@@ -32,9 +29,13 @@ class Factory {
32
29
  * Create a new entity and persist it
33
30
  */
34
31
  async create(overrideParams = {}, saveOptions) {
35
- const entity = await this.makeEntity(overrideParams, true);
36
- const connection = await (0, connection_1.fetchConnection)();
37
- return connection.createEntityManager().save(entity, saveOptions);
32
+ const attrs = { ...this.attrs, ...overrideParams };
33
+ const preloadedAttrs = Object.entries(attrs).filter(([, value]) => !(value instanceof lazyInstanceAttribute_1.LazyInstanceAttribute));
34
+ const entity = await this.makeEntity(Object.fromEntries(preloadedAttrs), true);
35
+ const em = (await (0, connection_1.fetchConnection)()).createEntityManager();
36
+ const savedEntity = await em.save(entity, saveOptions);
37
+ await this.applyLazyAttributes(savedEntity, attrs, true);
38
+ return em.save(savedEntity, saveOptions);
38
39
  }
39
40
  /**
40
41
  * Create many new entities and persist them
@@ -46,33 +47,37 @@ class Factory {
46
47
  }
47
48
  return list;
48
49
  }
49
- async makeEntity(overrideParams, isSeeding) {
50
- const entity = await this.definition();
51
- if (this.mapFunction) {
52
- await this.mapFunction(entity);
53
- }
54
- for (const key in overrideParams) {
55
- const actualValue = entity[key];
56
- entity[key] = overrideParams[key];
57
- }
58
- return this.resolveEntity(entity, isSeeding);
59
- }
60
- async resolveEntity(entity, isSeeding) {
61
- for (const attribute in entity) {
62
- const attributeValue = entity[attribute];
63
- if ((0, isPromiseLike_1.isPromiseLike)(attributeValue)) {
64
- entity[attribute] = await attributeValue;
50
+ async makeEntity(attrs, shouldPersist) {
51
+ const entity = new this.entity();
52
+ await Promise.all(Object.entries(attrs).map(async ([key, value]) => {
53
+ Object.assign(entity, { [key]: await Factory.resolveValue(value, shouldPersist) });
54
+ }));
55
+ await Promise.all(Object.entries(attrs).map(async ([key, value]) => {
56
+ if (value instanceof instanceAttribute_1.InstanceAttribute) {
57
+ const newAttrib = value.resolve(entity);
58
+ Object.assign(entity, { [key]: await Factory.resolveValue(newAttrib, shouldPersist) });
65
59
  }
66
- if (attributeValue instanceof Factory) {
67
- if (isSeeding) {
68
- entity[attribute] = await attributeValue.create();
69
- }
70
- else {
71
- entity[attribute] = await attributeValue.make();
72
- }
60
+ }));
61
+ return entity;
62
+ }
63
+ async applyLazyAttributes(entity, attrs, shouldPersist) {
64
+ await Promise.all(Object.entries(attrs).map(async ([key, value]) => {
65
+ if (value instanceof lazyInstanceAttribute_1.LazyInstanceAttribute) {
66
+ const newAttrib = value.resolve(entity);
67
+ Object.assign(entity, { [key]: await Factory.resolveValue(newAttrib, shouldPersist) });
73
68
  }
69
+ }));
70
+ }
71
+ static resolveValue(value, shouldPersist) {
72
+ if (value instanceof subfactory_1.Subfactory) {
73
+ return shouldPersist ? value.create() : value.make();
74
+ }
75
+ else if (value instanceof Function) {
76
+ return value();
77
+ }
78
+ else {
79
+ return value;
74
80
  }
75
- return entity;
76
81
  }
77
82
  }
78
83
  exports.Factory = Factory;
@@ -1 +1 @@
1
- {"version":3,"file":"factory.js","sourceRoot":"","sources":["../src/factory.ts"],"names":[],"mappings":";;;AACA,6CAA8C;AAC9C,yDAAqD;AAErD,MAAsB,OAAO;IAI3B;;;OAGG;IACH,GAAG,CAAC,WAAqD;QACvD,IAAI,CAAC,WAAW,GAAG,WAAW,CAAA;QAC9B,OAAO,IAAI,CAAA;IACb,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,IAAI,CAAC,iBAAkC,EAAE;QAC7C,OAAO,IAAI,C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AO,CAAC,GAAG,CACf,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,KAAK,EAAE,CAAC,GAAG,EAAE,KAAK,CAAC,EAAE,EAAE;YAC/C,IAAI,KAAK,YAAY,6CAAqB,EAAE;gBAC1C,MAAM,SAAS,GAAG,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,CAAA;gBACvC,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE,EAAE,CAAC,GAAG,CAAC,EAAE,MAAM,OAAO,CAAC,YAAY,CAAC,SAAS,EAAE,aAAa,CAAC,EAAE,CAAC,CAAA;aACvF;QACH,CAAC,CAAC,CACH,CAAA;IACH,CAAC;IAEO,MAAM,CAAC,YAAY,CAAC,KAAc,EAAE,aAAsB;QAChE,IAAI,KAAK,YAAY,uBAAU,EAAE;YAC/B,OAAO,aAAa,CAAC,CAAC,CAAC,KAAK,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,EAAE,CAAA;SACrD;aAAM,IAAI,KAAK,YAAY,QAAQ,EAAE;YACpC,OAAO,KAAK,EAAE,CAAA;SACf;aAAM;YACL,OAAO,KAAK,CAAA;SACb;IACH,CAAC;CACF;AAnGD,0BAmGC"}
package/dist/index.d.ts CHANGED
@@ -1,5 +1,8 @@
1
1
  export * from './connection';
2
2
  export * from './factory';
3
+ export * from './instanceAttribute';
4
+ export * from './lazyInstanceAttribute';
3
5
  export * from './seeder';
6
+ export * from './subfactory';
4
7
  export * from './types';
5
8
  export * from './useSeeders';
package/dist/index.js CHANGED
@@ -3,7 +3,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const tslib_1 = require("tslib");
4
4
  (0, tslib_1.__exportStar)(require("./connection"), exports);
5
5
  (0, tslib_1.__exportStar)(require("./factory"), exports);
6
+ (0, tslib_1.__exportStar)(require("./instanceAttribute"), exports);
7
+ (0, tslib_1.__exportStar)(require("./lazyInstanceAttribute"), exports);
6
8
  (0, tslib_1.__exportStar)(require("./seeder"), exports);
9
+ (0, tslib_1.__exportStar)(require("./subfactory"), exports);
7
10
  (0, tslib_1.__exportStar)(require("./types"), exports);
8
11
  (0, tslib_1.__exportStar)(require("./useSeeders"), exports);
9
12
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,4DAA4B;AAC5B,yDAAyB;AACzB,wDAAwB;AACxB,uDAAuB;AACvB,4DAA4B"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,4DAA4B;AAC5B,yDAAyB;AACzB,mEAAmC;AACnC,uEAAuC;AACvC,wDAAwB;AACxB,4DAA4B;AAC5B,uDAAuB;AACvB,4DAA4B"}
@@ -0,0 +1,3 @@
1
+ import { LazyAttribute } from './lazyAttribute';
2
+ export declare class InstanceAttribute<T, V> extends LazyAttribute<T, V> {
3
+ }
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.InstanceAttribute = void 0;
4
+ const lazyAttribute_1 = require("./lazyAttribute");
5
+ class InstanceAttribute extends lazyAttribute_1.LazyAttribute {
6
+ }
7
+ exports.InstanceAttribute = InstanceAttribute;
8
+ //# sourceMappingURL=instanceAttribute.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"instanceAttribute.js","sourceRoot":"","sources":["../src/instanceAttribute.ts"],"names":[],"mappings":";;;AAAA,mDAA+C;AAE/C,MAAa,iBAAwB,SAAQ,6BAAmB;CAAG;AAAnE,8CAAmE"}
@@ -0,0 +1,6 @@
1
+ import type { FactorizedAttr, LazyAttributeCallback } from './types';
2
+ export declare abstract class LazyAttribute<T, V> {
3
+ private callback;
4
+ constructor(callback: LazyAttributeCallback<T, V>);
5
+ resolve(entity: T): FactorizedAttr<V>;
6
+ }
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.LazyAttribute = void 0;
4
+ class LazyAttribute {
5
+ constructor(callback) {
6
+ this.callback = callback;
7
+ }
8
+ resolve(entity) {
9
+ return this.callback(entity);
10
+ }
11
+ }
12
+ exports.LazyAttribute = LazyAttribute;
13
+ //# sourceMappingURL=lazyAttribute.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lazyAttribute.js","sourceRoot":"","sources":["../src/lazyAttribute.ts"],"names":[],"mappings":";;;AAEA,MAAsB,aAAa;IACjC,YAAoB,QAAqC;QAArC,aAAQ,GAAR,QAAQ,CAA6B;IAAG,CAAC;IAE7D,OAAO,CAAC,MAAS;QACf,OAAO,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAA;IAC9B,CAAC;CACF;AAND,sCAMC"}
@@ -0,0 +1,3 @@
1
+ import { LazyAttribute } from './lazyAttribute';
2
+ export declare class LazyInstanceAttribute<T, V> extends LazyAttribute<T, V> {
3
+ }
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.LazyInstanceAttribute = void 0;
4
+ const lazyAttribute_1 = require("./lazyAttribute");
5
+ class LazyInstanceAttribute extends lazyAttribute_1.LazyAttribute {
6
+ }
7
+ exports.LazyInstanceAttribute = LazyInstanceAttribute;
8
+ //# sourceMappingURL=lazyInstanceAttribute.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lazyInstanceAttribute.js","sourceRoot":"","sources":["../src/lazyInstanceAttribute.ts"],"names":[],"mappings":";;;AAAA,mDAA+C;AAE/C,MAAa,qBAA4B,SAAQ,6BAAmB;CAAG;AAAvE,sDAAuE"}
package/dist/seeder.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import type { Connection } from 'typeorm';
2
- import type { ClassConstructor } from './types';
2
+ import type { Constructable } from './types';
3
3
  export declare abstract class Seeder {
4
4
  abstract run(connection: Connection): Promise<void>;
5
- protected call(connection: Connection, seeders: ClassConstructor<Seeder>[]): Promise<void>;
5
+ protected call(connection: Connection, seeders: Constructable<Seeder>[]): Promise<void>;
6
6
  }
@@ -1 +1 @@
1
- {"version":3,"file":"seeder.js","sourceRoot":"","sources":["../src/seeder.ts"],"names":[],"mappings":";;;AAGA,MAAsB,MAAM;IAGhB,KAAK,CAAC,IAAI,CAAC,UAAsB,EAAE,OAAmC;QAC9E,KAAK,MAAM,MAAM,IAAI,OAAO,EAAE;YAC5B,MAAM,IAAI,MAAM,EAAE,CAAC,GAAG,CAAC,UAAU,CAAC,CAAA;SACnC;IACH,CAAC;CACF;AARD,wBAQC"}
1
+ {"version":3,"file":"seeder.js","sourceRoot":"","sources":["../src/seeder.ts"],"names":[],"mappings":";;;AAGA,MAAsB,MAAM;IAGhB,KAAK,CAAC,IAAI,CAAC,UAAsB,EAAE,OAAgC;QAC3E,KAAK,MAAM,MAAM,IAAI,OAAO,EAAE;YAC5B,MAAM,IAAI,MAAM,EAAE,CAAC,GAAG,CAAC,UAAU,CAAC,CAAA;SACnC;IACH,CAAC;CACF;AARD,wBAQC"}
@@ -0,0 +1,13 @@
1
+ import type { Factory } from './factory';
2
+ import type { Constructable, FactorizedAttrs } from './types';
3
+ export declare class Subfactory<T> {
4
+ private factoryInstance;
5
+ private values?;
6
+ private count?;
7
+ constructor(factory: Constructable<Factory<T>>);
8
+ constructor(factory: Constructable<Factory<T>>, values?: Partial<FactorizedAttrs<T>>);
9
+ constructor(factory: Constructable<Factory<T>>, count?: number);
10
+ constructor(factory: Constructable<Factory<T>>, values?: Partial<FactorizedAttrs<T>>, count?: number);
11
+ create(): Promise<T[]> | Promise<T>;
12
+ make(): Promise<T[]> | Promise<T>;
13
+ }
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Subfactory = void 0;
4
+ class Subfactory {
5
+ constructor(factory, countOrValues, count) {
6
+ this.factoryInstance = new factory();
7
+ this.values = typeof countOrValues === 'number' ? undefined : countOrValues;
8
+ this.count = typeof countOrValues === 'number' ? countOrValues : count;
9
+ }
10
+ create() {
11
+ if (this.count !== undefined) {
12
+ return this.factoryInstance.createMany(this.count, this.values);
13
+ }
14
+ return this.factoryInstance.create(this.values);
15
+ }
16
+ make() {
17
+ if (this.count !== undefined) {
18
+ return this.factoryInstance.makeMany(this.count, this.values);
19
+ }
20
+ return this.factoryInstance.make(this.values);
21
+ }
22
+ }
23
+ exports.Subfactory = Subfactory;
24
+ //# sourceMappingURL=subfactory.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"subfactory.js","sourceRoot":"","sources":["../src/subfactory.ts"],"names":[],"mappings":";;;AAGA,MAAa,UAAU;IAUrB,YACE,OAAkC,EAClC,aAAoD,EACpD,KAAc;QAEd,IAAI,CAAC,eAAe,GAAG,IAAI,OAAO,EAAE,CAAA;QACpC,IAAI,CAAC,MAAM,GAAG,OAAO,aAAa,KAAK,QAAQ,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,aAAa,CAAA;QAC3E,IAAI,CAAC,KAAK,GAAG,OAAO,aAAa,KAAK,QAAQ,CAAC,CAAC,CAAC,aAAa,CAAC,CAAC,CAAC,KAAK,CAAA;IACxE,CAAC;IAED,MAAM;QACJ,IAAI,IAAI,CAAC,KAAK,KAAK,SAAS,EAAE;YAC5B,OAAO,IAAI,CAAC,eAAe,CAAC,UAAU,CAAC,IAAI,CAAC,KAAK,EAAE,IAAI,CAAC,MAAM,CAAC,CAAA;SAChE;QAED,OAAO,IAAI,CAAC,eAAe,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAA;IACjD,CAAC;IAED,IAAI;QACF,IAAI,IAAI,CAAC,KAAK,KAAK,SAAS,EAAE;YAC5B,OAAO,IAAI,CAAC,eAAe,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,EAAE,IAAI,CAAC,MAAM,CAAC,CAAA;SAC9D;QAED,OAAO,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,CAAA;IAC/C,CAAC;CACF;AAnCD,gCAmCC"}
package/dist/types.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import type { ConnectionOptions as TypeORMConnectionOptions } from 'typeorm';
2
- export declare type ClassConstructor<T> = new () => T;
2
+ import type { LazyAttribute } from './lazyAttribute';
3
+ import type { Subfactory } from './subfactory';
3
4
  export declare type ConnectionOptions = TypeORMConnectionOptions & {
4
5
  seeders: string[];
5
6
  defaultSeeder: string;
@@ -9,3 +10,9 @@ export declare type ConnectionConfiguration = {
9
10
  configName?: string;
10
11
  connection: string;
11
12
  };
13
+ export declare type Constructable<T> = new () => T;
14
+ export declare type FactorizedAttr<V> = V | (() => V | Promise<V>) | Subfactory<V extends Array<infer U> ? U : V>;
15
+ export declare type FactorizedAttrs<T> = {
16
+ [K in keyof Partial<T>]: FactorizedAttr<T[K]> | LazyAttribute<T, FactorizedAttr<T[K]>>;
17
+ };
18
+ export declare type LazyAttributeCallback<T, V> = (entity: T) => V;
@@ -1,4 +1,4 @@
1
1
  import { Seeder } from './seeder';
2
- import type { ClassConstructor, ConnectionConfiguration } from './types';
3
- export declare function useSeeders(entrySeeders: ClassConstructor<Seeder> | ClassConstructor<Seeder>[]): Promise<void>;
4
- export declare function useSeeders(entrySeeders: ClassConstructor<Seeder> | ClassConstructor<Seeder>[], customOptions: Partial<ConnectionConfiguration>): Promise<void>;
2
+ import type { ConnectionConfiguration, Constructable } from './types';
3
+ export declare function useSeeders(entrySeeders: Constructable<Seeder> | Constructable<Seeder>[]): Promise<void>;
4
+ export declare function useSeeders(entrySeeders: Constructable<Seeder> | Constructable<Seeder>[], customOptions: Partial<ConnectionConfiguration>): Promise<void>;
@@ -1 +1 @@
1
- {"version":3,"file":"useSeeders.js","sourceRoot":"","sources":["../src/useSeeders.ts"],"names":[],"mappings":";;;AAAA,6CAAmE;AAU5D,KAAK,UAAU,UAAU,CAC9B,YAAmE,EACnE,aAAgD;IAEhD,IAAI,aAAa;QAAE,IAAA,gCAAmB,EAAC,aAAa,CAAC,CAAA;IAErD,MAAM,UAAU,GAAG,MAAM,IAAA,4BAAe,GAAE,CAAA;IAE1C,MAAM,OAAO,GAAG,KAAK,CAAC,OAAO,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,YAAY,CAAC,CAAA;IAC3E,KAAK,MAAM,MAAM,IAAI,OAAO,EAAE;QAC5B,MAAM,IAAI,MAAM,EAAE,CAAC,GAAG,CAAC,UAAU,CAAC,CAAA;KACnC;AACH,CAAC;AAZD,gCAYC"}
1
+ {"version":3,"file":"useSeeders.js","sourceRoot":"","sources":["../src/useSeeders.ts"],"names":[],"mappings":";;;AAAA,6CAAmE;AAU5D,KAAK,UAAU,UAAU,CAC9B,YAA6D,EAC7D,aAAgD;IAEhD,IAAI,aAAa;QAAE,IAAA,gCAAmB,EAAC,aAAa,CAAC,CAAA;IAErD,MAAM,UAAU,GAAG,MAAM,IAAA,4BAAe,GAAE,CAAA;IAE1C,MAAM,OAAO,GAAG,KAAK,CAAC,OAAO,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,YAAY,CAAC,CAAA;IAC3E,KAAK,MAAM,MAAM,IAAI,OAAO,EAAE;QAC5B,MAAM,IAAI,MAAM,EAAE,CAAC,GAAG,CAAC,UAAU,CAAC,CAAA;KACnC;AACH,CAAC;AAZD,gCAYC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jorgebodega/typeorm-seeding",
3
- "version": "3.2.0",
3
+ "version": "4.0.0-next.3",
4
4
  "description": "🌱 A delightful way to seed test data into your database.",
5
5
  "license": "MIT",
6
6
  "author": "Gery Hirschfeld <gery.hirschfeld@w3tec.ch> (https://github.com/hirsch88)",
@@ -36,39 +36,38 @@
36
36
  "typecheck": "tsc --noEmit"
37
37
  },
38
38
  "devDependencies": {
39
+ "@faker-js/faker": "6.0.0-alpha.7",
39
40
  "@semantic-release/changelog": "6.0.1",
40
41
  "@semantic-release/git": "10.0.1",
41
42
  "@tsconfig/node16": "1.0.2",
42
43
  "@types/bcryptjs": "2.4.2",
43
44
  "@types/chalk": "2.2.0",
44
- "@types/faker": "5.5.9",
45
45
  "@types/glob": "7.2.0",
46
- "@types/jest": "27.4.0",
47
- "@types/node": "16.11.19",
46
+ "@types/jest": "27.4.1",
47
+ "@types/node": "17.0.17",
48
48
  "@types/yargs": "17.0.8",
49
- "@typescript-eslint/eslint-plugin": "5.9.0",
50
- "@typescript-eslint/parser": "5.9.0",
49
+ "@typescript-eslint/eslint-plugin": "5.13.0",
50
+ "@typescript-eslint/parser": "5.13.0",
51
51
  "bcryptjs": "2.4.3",
52
- "eslint": "8.6.0",
53
- "eslint-config-prettier": "8.3.0",
52
+ "eslint": "8.10.0",
53
+ "eslint-config-prettier": "8.4.0",
54
54
  "eslint-plugin-import": "2.25.4",
55
- "faker": "5.5.3",
56
- "jest": "27.4.7",
55
+ "jest": "27.5.1",
57
56
  "prettier": "2.5.1",
58
57
  "rimraf": "3.0.2",
59
- "semantic-release": "18.0.1",
58
+ "semantic-release": "19.0.2",
60
59
  "sqlite3": "5.0.2",
61
- "ts-jest": "27.1.2",
62
- "ts-node": "10.4.0",
60
+ "ts-jest": "27.1.3",
61
+ "ts-node": "10.5.0",
63
62
  "typeorm": "0.2.41",
64
- "typescript": "4.5.4"
63
+ "typescript": "4.5.5"
65
64
  },
66
65
  "dependencies": {
67
- "chalk": "^4",
68
- "glob": "^7.2.0",
69
- "ora": "^5",
70
- "reflect-metadata": "^0.1.13",
71
- "yargs": "^17.3.0"
66
+ "chalk": "4.1.2",
67
+ "glob": "7.2.0",
68
+ "ora": "5.4.1",
69
+ "reflect-metadata": "0.1.13",
70
+ "yargs": "17.3.1"
72
71
  },
73
72
  "peerDependencies": {
74
73
  "typeorm": "^0.2.41"
@@ -1 +0,0 @@
1
- export declare const isPromiseLike: (o: any) => o is Promise<any>;
@@ -1,6 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isPromiseLike = void 0;
4
- const isPromiseLike = (o) => o && Object.prototype.toString.call(o) === '[object Promise]';
5
- exports.isPromiseLike = isPromiseLike;
6
- //# sourceMappingURL=isPromiseLike.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"isPromiseLike.js","sourceRoot":"","sources":["../../src/utils/isPromiseLike.ts"],"names":[],"mappings":";;;AAAO,MAAM,aAAa,GAAG,CAAC,CAAM,EAAqB,EAAE,CACzD,CAAC,IAAI,MAAM,CAAC,SAAS,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,kBAAkB,CAAA;AADlD,QAAA,aAAa,iBACqC"}