forked from mikro-orm/mikro-orm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
closes mikro-orm#251
- Loading branch information
Showing
23 changed files
with
845 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
--- | ||
title: Seeding | ||
--- | ||
|
||
When initializing your application or testing it can be exhausting to create sample data for your database. The solution is to use seeding. Create factories for your entities and use them in the seed script or combine multiple seed scripts. | ||
|
||
## Seeders | ||
A seeder class contains one method `run`. This method is called when you use the command `mikro-orm db:seed`. In the `run` method you define how and what data you want to insert into the database. You can create entities using the [EntityManager](http://mikro-orm.io/docs/entity-manager) or you can use [Factories](#using-entity-factories). | ||
|
||
As an example we will look at a very basic seeder. | ||
```typescript | ||
import { EntityManager } from '@mikro-orm/core'; | ||
import { Seeder } from '@mikro-orm/seeder'; | ||
import { Author } from './author' | ||
|
||
class DatabaseSeeder extends Seeder { | ||
|
||
async run(em: EntityManager): Promise<void> { | ||
const author = em.create(Author, { | ||
name: 'John Snow', | ||
email: 'snow@wall.st' | ||
}); | ||
await em.persistAndFlush(author); | ||
em.clear(); | ||
} | ||
} | ||
``` | ||
|
||
### Using entity factories | ||
Instead of specifying all the attributes for every entity, you can also use [entity factories](#entity-factories). These can be used to generate large amounts of database records. Please read the [documentation on how to define factories](#entity-factories) to learn how to define your factories. | ||
|
||
As an example we will generate 10 authors. | ||
```typescript | ||
import { EntityManager } from '@mikro-orm/core'; | ||
import { Seeder } from '@mikro-orm/seeder'; | ||
import { AuthorFactory } from '../factories/author.factory' | ||
|
||
class DatabaseSeeder extends Seeder { | ||
|
||
async run(em: EntityManager): Promise<void> { | ||
await (new AuthorFactory(em)).count(10).create() | ||
em.clear(); | ||
} | ||
} | ||
``` | ||
|
||
### Calling additional seeders | ||
Inside the `run` method you can specify other seeder classes. You can use the `call` method to breakup the database seeder into multiple files to prevent a seeder file from becoming too large. The `call` method accepts an array of seeder classes. | ||
```typescript | ||
import { EntityManager } from '@mikro-orm/core'; | ||
import { Seeder } from '@mikro-orm/seeder'; | ||
import { AuthorSeeder, BookSeeder } from '../seeders' | ||
|
||
class DatabaseSeeder extends Seeder { | ||
|
||
run(em: EntityManager): Promise<void> { | ||
return this.call(em, [ | ||
AuthorSeeder, | ||
BookSeeder | ||
]); | ||
} | ||
} | ||
``` | ||
|
||
## Entity factories | ||
When testing you may insert entities in the database before starting a test. Instead of specifying every attribute of every entity by hand, you could also use a `Factory` to define a set of default attributes for an entity using entity factories. | ||
|
||
Lets have a look at an example factory for an [Author entity](http://mikro-orm.io/docs/defining-entities). | ||
```typescript | ||
import { Factory } from '@mikro-orm/seeder'; | ||
import * as Faker from 'faker'; | ||
import { Author } from './entities/author.entity'; | ||
|
||
export class AuthorFactory extends Factory<Author> { | ||
model = Author; | ||
|
||
definition(faker: typeof Faker): Partial<Author> { | ||
return { | ||
name: faker.person.findName(), | ||
email: faker.internet.email(), | ||
age: faker.random.number(18, 99) | ||
}; | ||
} | ||
} | ||
``` | ||
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. | ||
|
||
Via the faker property, factories have access to the [Faker library](https://www.npmjs.com/package/faker), which allows you to conveniently generate various kinds of random data for testing. | ||
|
||
### Creating entities using factories | ||
Once you defined your factories you can use them to generate entities. Simply import the factory, instantiate it and call the `make` or `create` method. | ||
```typescript | ||
const author = await (new AuthorFactory(orm.em)).make() as Author; | ||
``` | ||
The `make` method returns the type `Author | Author[]`, so be sure to cast it to the correct type. | ||
|
||
#### Generate multiple entities | ||
Generate multiple entities by chaining the `count` and `make` method. The parameter of the `count` method is the number of entities you generate. | ||
```typescript | ||
// Generate 5 authors | ||
const authors = await (new AuthorFactory(orm.em)).count(5).make() as Author[]; | ||
``` | ||
The `make` method returns the type `Author | Author[]`, since we chained the `count` method here with `5` we can be sure it returns `Author[]`. | ||
|
||
#### Overriding attributes | ||
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. | ||
```typescript | ||
const author = await (new AuthorFactory(orm.em)).make({ | ||
name: 'John Snow' | ||
}) as Author; | ||
``` | ||
|
||
### Persisting entities | ||
The `create` method instantiates entities and persists them to the database using the `persistAndFlush` method of the EntityManager. | ||
```typescript | ||
// Make and persist a single author | ||
const author = await (new AuthorFactory(orm.em)).make() as Author; | ||
|
||
// Make and persist 5 authors | ||
const authors = await (new AuthorFactory(orm.em)).count(5).create() as Author[]; | ||
``` | ||
You can override the default values of your factories by passing an object to the `create` method. | ||
```typescript | ||
// Make and persist a single author | ||
const author = await (new AuthorFactory(orm.em)).create({ | ||
name: 'John Snow' | ||
}) as Author; | ||
``` | ||
### Factory relationships | ||
It is nice to create large quantities of data for one entity, but most of the time we want to create data for multiple entities and also have relations between these. For this we can use the `map` method which can be chained on a factory. The `map` method can be called with a function that maps the output entity from the factory before returning it. Lets look at some examples for the different relations. | ||
|
||
#### ManyToOne and OneToOne relations | ||
```typescript | ||
const book = await (new BookFactory(orm.em)).map(async (book) => { | ||
book.author = await (new AuthorFactory(orm.em)).make() as Author; | ||
return book; | ||
}) as Book; | ||
``` | ||
|
||
#### OneToMany and ManyToMany | ||
```typescript | ||
const book = await (new BookFactory(orm.em)).map(async (book) => { | ||
book.owners = new Collection(book, await (new OwnerFactory(orm.em)).make() as Owner[]); | ||
return book; | ||
}) as Book; | ||
``` | ||
|
||
## Use with CLI | ||
You may execute the `db:seed` Mikro-ORM CLI command to seed your database. By default, the db:seed command runs the DatabaseSeeder class, which may in turn invoke other seed classes. However, you may use the --class option to specify a specific seeder class to run individually: | ||
```shell script | ||
mikro-orm database:seed | ||
|
||
mikro-orm database:seed --class=BookSeeder | ||
``` | ||
|
||
You may also seed your database using the `migrate:fresh` or `schema:fresh` command in combination with the --seed option, which will drop all tables and re-run all of your migrations or generate the database based on the current entities. This command is useful for completely re-building your database: | ||
```shell script | ||
mikro-orm migration:fresh --seed | ||
|
||
mikro-orm schema:fresh --seed | ||
``` | ||
|
||
## Use in tests | ||
Now we know how to create seeders and factories, but how can we effectively use them in tests. We will show an example how it can be used. | ||
|
||
```typescript | ||
let seeder: MikroOrmSeeder; | ||
|
||
beforeAll(async () => { | ||
// Initialize seeder with config | ||
seeder = await MikroOrmSeeder.init(mikroOrmConfig); | ||
|
||
// Refresh the database to start clean | ||
await seeder.refreshDatabase(); | ||
|
||
// Seed using a seeder defined by you | ||
await seeder.seed(DatabaseSeeder); | ||
}) | ||
|
||
test(() => { | ||
// Do tests | ||
}); | ||
|
||
afterAll(() => { | ||
// Close connection | ||
seeder.closeConnection(); | ||
}) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { MikroOrmSeeder } from '@mikro-orm/seeder'; | ||
import c from 'ansi-colors'; | ||
import { Arguments, Argv, CommandModule } from 'yargs'; | ||
import { CLIHelper } from '../CLIHelper'; | ||
|
||
export class DatabaseSeedCommand<T> implements CommandModule<T, {class: string}> { | ||
|
||
command = 'database:seed'; | ||
describe = 'Seed the database using the seeder class'; | ||
builder = (args: Argv) => { | ||
args.option('c', { | ||
alias: 'class', | ||
type: 'string', | ||
desc: 'Seeder class to run', | ||
default: 'DatabaseSeeder', | ||
}); | ||
return args as Argv<{class: string}>; | ||
}; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
async handler(args: Arguments<{class: string}>) { | ||
const seeder = await MikroOrmSeeder.init(await CLIHelper.getConfiguration()); | ||
await seeder.seedString(args.class); | ||
CLIHelper.dump(c.green(`Seeder ${args.class} successfully seeded`)); | ||
await seeder.closeConnection(true); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.