Skip to content

Commit 3c9f8e9

Browse files
committed
refactor(seeder): use community driven faker fork and reexport it
Closes #2673
1 parent 70c795a commit 3c9f8e9

File tree

6 files changed

+31
-32
lines changed

6 files changed

+31
-32
lines changed

docs/docs/seeding.md

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,13 @@ When testing you may insert entities in the database before starting a test. Ins
7575

7676
Lets have a look at an example factory for an [Author entity](http://mikro-orm.io/docs/defining-entities).
7777
```typescript
78-
import { Factory } from '@mikro-orm/seeder';
79-
import Faker from 'faker';
78+
import { Factory, Faker } from '@mikro-orm/seeder';
8079
import { Author } from './entities/author.entity';
8180

8281
export class AuthorFactory extends Factory<Author> {
8382
model = Author;
8483

85-
definition(faker: typeof Faker): Partial<Author> {
84+
definition(faker: Faker): Partial<Author> {
8685
return {
8786
name: faker.person.findName(),
8887
email: faker.internet.email(),
@@ -93,25 +92,25 @@ export class AuthorFactory extends Factory<Author> {
9392
```
9493
Basically you extend the base `Factory` class, define a `model` property and a `definition` method. The `model` defines for which entity the factory generates entity instances. The `definition` method returns the default set of attribute values that should be applied when creating an entity using the factory.
9594

96-
Via the faker property, factories have access to the [Faker library](https://github.com/marak/Faker.js/), which allows you to conveniently generate various kinds of random data for testing.
95+
Via the faker property, factories have access to the [Faker library](https://github.com/faker-js/faker), which allows you to conveniently generate various kinds of random data for testing.
9796

9897
### Creating entities using factories
9998
Once you defined your factories you can use them to generate entities. Simply import the factory, instantiate it and call the `makeOne` method.
10099
```typescript
101-
const author: Author = new AuthorFactory(orm.em).makeOne();
100+
const author = new AuthorFactory(orm.em).makeOne();
102101
```
103102

104103
#### Generate multiple entities
105104
Generate multiple entities by calling the `make` method. The parameter of the `make` method is the number of entities you generate.
106105
```typescript
107106
// Generate 5 authors
108-
const authors: Author[] = new AuthorFactory(orm.em).make(5);
107+
const authors = new AuthorFactory(orm.em).make(5);
109108
```
110109

111110
#### Overriding attributes
112111
If you would like to override some of the default values of your factories, you may pass an object to the make method. Only the specified attributes will be replaced while the rest of the attributes remain set to their default values as specified by the factory.
113112
```typescript
114-
const author: Author = new AuthorFactory(orm.em).make({
113+
const author = new AuthorFactory(orm.em).make({
115114
name: 'John Snow'
116115
});
117116
```
@@ -120,20 +119,20 @@ const author: Author = new AuthorFactory(orm.em).make({
120119
The `create` method instantiates entities and persists them to the database using the `persistAndFlush` method of the EntityManager.
121120
```typescript
122121
// Make and persist a single author
123-
const author: Author = await new AuthorFactory(orm.em).createOne();
122+
const author = await new AuthorFactory(orm.em).createOne();
124123

125124
// Make and persist 5 authors
126-
const authors: Author[] = await new AuthorFactory(orm.em).create(5);
125+
const authors = await new AuthorFactory(orm.em).create(5);
127126
```
128127
You can override the default values of your factories by passing an object to the `create` method.
129128
```typescript
130129
// Make and persist a single author
131-
const author: Author = await new AuthorFactory(orm.em).createOne({
130+
const author = await new AuthorFactory(orm.em).createOne({
132131
name: 'John Snow'
133132
});
134133

135134
// Make and persist a 5 authors
136-
const authors: Author[] = await new AuthorFactory(orm.em).create(5, {
135+
const authors = await new AuthorFactory(orm.em).create(5, {
137136
name: 'John Snow'
138137
});
139138
```

packages/seeder/package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,12 @@
4848
"access": "public"
4949
},
5050
"dependencies": {
51-
"faker": "5.5.3"
51+
"@faker-js/faker": "6.0.0-alpha.5"
5252
},
5353
"peerDependencies": {
5454
"@mikro-orm/core": "^5.0.0-rc.0"
5555
},
5656
"devDependencies": {
57-
"@mikro-orm/core": "^5.0.0-rc.0",
58-
"@types/faker": "5.5.9"
57+
"@mikro-orm/core": "^5.0.0-rc.0"
5958
}
6059
}

packages/seeder/src/factory.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
1-
import Faker from 'faker';
1+
import type { Faker } from '@faker-js/faker';
2+
import faker from '@faker-js/faker';
23
import type { EntityManager } from '@mikro-orm/core';
34

45
export abstract class Factory<C> {
56

67
abstract readonly model: { new(): C };
78
private eachFunction?: (entity: C) => void;
89

9-
constructor(private readonly em: EntityManager) {
10-
}
10+
constructor(private readonly em: EntityManager) { }
1111

12-
protected abstract definition(faker: typeof Faker): Partial<C>;
12+
protected abstract definition(faker: Faker): Partial<C>;
1313

1414
private makeEntity(overrideParameters?: Partial<C>): C {
15-
const entity = this.em.create(this.model, Object.assign({}, this.definition(Faker), overrideParameters));
15+
const entity = this.em.create(this.model, Object.assign({}, this.definition(faker), overrideParameters));
16+
1617
if (this.eachFunction) {
1718
this.eachFunction(entity);
1819
}
20+
1921
return entity;
2022
}
2123

packages/seeder/src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,7 @@
55
export * from './seeder';
66
export * from './factory';
77
export * from './seed-manager';
8+
9+
// reexport faker instance and Faker type
10+
import faker, { Faker } from '@faker-js/faker';
11+
export { faker, Faker };

tests/features/seeder/factory.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { MikroORM } from '@mikro-orm/core';
22
import { Factory } from '@mikro-orm/seeder';
33
import type { SqliteDriver } from '@mikro-orm/sqlite';
4-
import type * as Faker from 'faker';
4+
import type { Faker } from '@mikro-orm/seeder';
55
import { House } from './entities/house.entity';
66
import { Project } from './entities/project.entity';
77
import SpyInstance = jest.SpyInstance;
@@ -10,7 +10,7 @@ export class ProjectFactory extends Factory<Project> {
1010

1111
model = Project;
1212

13-
definition(faker: typeof Faker): Partial<Project> {
13+
definition(faker: Faker): Partial<Project> {
1414
return {
1515
name: 'Money vault',
1616
owner: faker.name.findName(),
@@ -24,7 +24,7 @@ export class HouseFactory extends Factory<House> {
2424

2525
model = House;
2626

27-
definition(faker: typeof Faker): Partial<House> {
27+
definition(faker: Faker): Partial<House> {
2828
return {
2929
address: faker.address.city(),
3030
};

yarn.lock

Lines changed: 5 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)