Skip to content

Commit e27c2b8

Browse files
committed
Add missing references to ObjectLiteral
1 parent 5c4b99a commit e27c2b8

File tree

3 files changed

+15
-12
lines changed

3 files changed

+15
-12
lines changed

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ For all entities we want to create, we need to define a factory. To do so we giv
233233

234234
| Types | Description |
235235
| --------------- | ------------------------------------------------------------------------------- |
236-
| `Entity` | TypeORM Entity like the user or the pet in the samples. |
236+
| `Entity` | TypeORM Entity like the user or the pet in the samples. |
237237
| `Context` | Argument to pass some static data into the factory function. |
238238
| `EntityFactory` | This object is used to make new filled entities or create it into the database. |
239239

@@ -242,7 +242,7 @@ For all entities we want to create, we need to define a factory. To do so we giv
242242
The define function creates a new entity factory.
243243

244244
```typescript
245-
define: <Entity, Context>(entity: Entity, factoryFn: FactoryFunction<Entity, Context>) => void;
245+
define: <Entity extends ObjectLiteral, Context>(entity: Entity, factoryFn: FactoryFunction<Entity, Context>) => void;
246246
```
247247

248248
```typescript
@@ -258,7 +258,7 @@ define(User, (faker: typeof Faker, context: { roles: string[] }) => { ... })
258258
Factory retrieves the defined factory function and returns the EntityFactory to start creating new enities.
259259

260260
```typescript
261-
factory: (entity: Entity) => (context?: Context) => EntityFactory<Entity, Context>
261+
factory: (entity: Entity extends ObjectLiteral) => (context?: Context) => EntityFactory<Entity, Context>
262262
```
263263
264264
```typescript
@@ -273,7 +273,7 @@ factory(Pet)({ name: 'Balou' })
273273
Use the `.map()` function to alter the generated value before they get persisted.
274274
275275
```typescript
276-
map(mapFunction: (entity: Entity) => Promise<Entity>): EntityFactory<Entity, Context>
276+
map(mapFunction: (entity: Entity extends ObjectLiteral) => Promise<Entity>): EntityFactory<Entity, Context>
277277
```
278278
279279
```typescript

src/typeorm-seeding.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import 'reflect-metadata'
2-
import { ObjectType, DataSource } from 'typeorm'
2+
import { ObjectType, DataSource, ObjectLiteral } from 'typeorm'
33

44
import { EntityFactory } from './entity-factory'
55
import { EntityFactoryDefinition, Factory, FactoryFunction, SeederConstructor, Seeder } from './types'
@@ -33,15 +33,18 @@ export { times } from './helpers'
3333
// Facade functions
3434
// -------------------------------------------------------------------------
3535

36-
export const define = <Entity, Context>(entity: ObjectType<Entity>, factoryFn: FactoryFunction<Entity, Context>) => {
36+
export const define = <Entity extends ObjectLiteral, Context>(
37+
entity: ObjectType<Entity>,
38+
factoryFn: FactoryFunction<Entity, Context>,
39+
) => {
3740
;(global as any).seeder.entityFactories.set(getNameOfEntity(entity), {
3841
entity,
3942
factory: factoryFn,
4043
})
4144
}
4245

4346
export const factory: Factory =
44-
<Entity, Context>(entity: ObjectType<Entity>) =>
47+
<Entity extends ObjectLiteral, Context>(entity: ObjectType<Entity>) =>
4548
(context?: Context) => {
4649
const name = getNameOfEntity(entity)
4750
const entityFactoryObject = (global as any).seeder.entityFactories.get(name)

src/types.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
import * as Faker from 'faker'
2-
import { DataSource, ObjectType } from 'typeorm'
2+
import { DataSource, ObjectType, ObjectLiteral } from 'typeorm'
33

44
import { EntityFactory } from './entity-factory'
55

66
/**
77
* FactoryFunction is the fucntion, which generate a new filled entity
88
*/
9-
export type FactoryFunction<Entity, Context> = (faker: typeof Faker, context?: Context) => Entity
9+
export type FactoryFunction<Entity extends ObjectLiteral, Context> = (faker: typeof Faker, context?: Context) => Entity
1010

1111
/**
1212
* EntityProperty defines an object whose keys and values must be properties of the given Entity.
1313
*/
14-
export type EntityProperty<Entity> = { [Property in keyof Entity]?: Entity[Property] }
14+
export type EntityProperty<Entity extends ObjectLiteral> = { [Property in keyof Entity]?: Entity[Property] }
1515

1616
/**
1717
* Factory gets the EntityFactory to the given Entity and pass the context along
1818
*/
19-
export type Factory = <Entity, Context>(
19+
export type Factory = <Entity extends ObjectLiteral, Context>(
2020
entity: ObjectType<Entity>,
2121
) => (context?: Context) => EntityFactory<Entity, Context>
2222

@@ -35,7 +35,7 @@ export type SeederConstructor = new () => Seeder
3535
/**
3636
* Value of our EntityFactory state
3737
*/
38-
export interface EntityFactoryDefinition<Entity, Context> {
38+
export interface EntityFactoryDefinition<Entity extends ObjectLiteral, Context> {
3939
entity: ObjectType<Entity>
4040
factory: FactoryFunction<Entity, Context>
4141
}

0 commit comments

Comments
 (0)