A delightful way to seed test data into your database.
Inspired by the awesome framework laravel in PHP, MikroORM seeding and the repositories from pleerock
Made with ❤️ by Gery Hirschfeld, Jorge Bodega and contributors
Before using this TypeORM extension please read the TypeORM Getting Started documentation. This explains how to setup a TypeORM project.
After that install the extension with npm
or yarn
. Add development flag if you are not using seeders nor factories in production code.
npm i [-D] @jorgebodega/typeorm-seeding
yarn add [-D] @jorgebodega/typeorm-seeding
pnpm add [-D] @jorgebodega/typeorm-seeding
Isn't it exhausting to create some sample data for your database, well this time is over!
How does it work? Just create a entity factory and/or seed script.
@Entity()
class User {
@PrimaryGeneratedColumn('increment')
id!: number
@Column()
name!: string
@Column()
lastName!: string
@Column()
email!: string
@OneToMany(() => Pet, (pet) => pet.owner)
pets?: Pet[]
@ManyToOne(() => Country, (country) => country.users, { nullable: false })
@JoinColumn()
country!: Country
}
class UserSeeder extends Seeder {
async run(dataSource: DataSource) {
const users: User[] = [...]
await dataSource.createEntityManager().save<User>(users)
}
}
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.
class UserSeeder extends Seeder {
async run(dataSource: DataSource) {
...
}
}
This seeder class must be exported as default to be handled by the CLI.
export default class UserSeeder extends Seeder {
async run(dataSource: DataSource) {
...
}
}
This function is the one that needs to be defined when extending the class.
run(dataSource: DataSource): Promise<void>
async run(dataSource: DataSource) {
const users: User[] = [...]
await dataSource.createEntityManager().save<User>(users)
}
There is a command that allows you to execute multiple seeders from cli.
typeorm-seeding seed -d path/to/datasource src/seeders/*.ts
Add the following script to your package.json
file to configure them.
"scripts": {
"seed:run": "typeorm-seeding seed -d path/to/datasource",
...
}
This command execute a seeder, that could be specified as a parameter. Glob pattern is supported.
typeorm-seeding -d [...] seed <paths>
CLI command only executes default seeders.
Option | Default | Description |
---|---|---|
--dataSource or -d |
Path of the data source |
We provide some testing features that we already use to test this package, like connection configuration.
The entity factories can also be used in testing. To do so call the useFactories
or useSeeders
function.
Execute one or more seeders.
useSeeders(entrySeeders: ClassConstructor<Seeder> | ClassConstructor<Seeder>[]): Promise<void>
useSeeders(
entrySeeders: ClassConstructor<Seeder> | ClassConstructor<Seeder>[],
customOptions: Partial<ConnectionConfiguration>,
): Promise<void>
Use specific data source on the factories. If the data source is not initialized when provided, can be initialized with the forceInitialization
flag.
useDataSource(dataSource: DataSource): Promise<void>
useDataSource(dataSource: DataSource, overrideOptions: Partial<DataSourceOptions>): Promise<void>
useDataSource(dataSource: DataSource, forceInitialization: boolean): Promise<void>
useDataSource(
dataSource: DataSource,
overrideOptions: Partial<DataSourceOptions>,
forceInitialization: boolean,
): Promise<void>
Factory related code has been removed from this package, now on @jorgebodega/typeorm-factory.