Skip to content

Repository files navigation

logo

TypeORM Seeding

NPMNPM latest versionNPM next versionSemantic release

Coveralls master branchCoveralls next branch

Checks for master branchChecks for next branch

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


Contents

Installation

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

Configuration

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.

ormconfig.js

module.exports={
...
seeders: ['src/seeds/**/*{.ts,.js}'],defaultSeeder: RootSeeder,
...
}

.env

TYPEORM_SEEDING_SEEDERS=src/seeds/**/*{.ts,.js}
TYPEORM_SEEDING_DEFAULT_SEEDER=RootSeeder

Introduction

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

@Entity()classUser{
@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}

Factory

classUserFactoryextendsFactory<User>{protectedentity=Userprotectedattrs: FactorizedAttrs<User>={name: faker.name.firstName(),lastName: async()=>faker.name.lastName(),email: newInstanceAttribute((instance)=>[instance.name.toLowerCase(),instance.lastName.toLowerCase(),'@email.com'].join(''),),country: newSubfactory(CountryFactory),}}

Seeder

classUserSeederextendsSeeder{asyncrun(connection: Connection){awaitnewUserFactory().createMany(10)awaitthis.call(connection,[PetSeeder])}}

Factory

Factory is how we provide a way to simplify entities creation, implementing a factory creational pattern. It is defined as an abstract class with generic typing, so you have to extend over it.

classUserFactoryextendsFactory<User>{protectedentity=Userprotectedattrs: FactorizedAttrs<User>={
...
}}

attrs

Attributes objects are superset from the original entity attributes.

protectedattrs: FactorizedAttrs<User>={name: faker.name.firstName(),lastName: async()=>faker.name.lastName(),email: newInstanceAttribute((instance)=>[instance.name.toLowerCase(),instance.lastName.toLowerCase(),'@email.com'].join(''),),country: newSubfactory(CountryFactory),}

Those factorized attributes resolves to the value of the original attribute, and could be one of the following types:

Simple value

Nothing special, just a value with same type.

protectedattrs: FactorizedAttrs<User>={name: faker.name.firstName(),}

Function

Function that could be sync or async, and return a value of the same type. This function will be executed once per entity.

protectedattrs: FactorizedAttrs<User>={lastName: async()=>faker.name.lastName(),}

InstanceAttribute

classInstanceAttribute<T,V>{constructor(privatecallback: (entity: T)=>V){}
...
}

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.

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).

protectedattrs: FactorizedAttrs<User>={name: faker.name.firstName(),lastName: async()=>faker.name.lastName(),email: newInstanceAttribute((instance)=>[instance.name.toLowerCase(),instance.lastName.toLowerCase(),'@email.com'].join(''),),}

In this simple case, if name or lastName override the value in any way, the email attribute will be affected too.

LazyInstanceAttribute

classLazyInstanceAttribute<T,V>{constructor(privatecallback: (entity: T)=>V){}
...
}

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.

Just remember that, if you use make or makeMany, the only difference between InstanceAttribute and LazyInstanceAttribute is that LazyInstanceAttribute will be processed the last.

protectedattrs: FactorizedAttrs<User>={name: faker.name.firstName(),email: newLazyInstanceAttribute((instance)=>[instance.name.toLowerCase(),instance.id,'@email.com'].join(''),),}

Subfactory

exportclassSubfactory<T>{constructor(factory: Constructable<Factory<T>>)constructor(factory: Constructable<Factory<T>>,values?: Partial<FactorizedAttrs<T>>)constructor(factory: Constructable<Factory<T>>,count?: number)constructor(factory: Constructable<Factory<T>>,values?: Partial<FactorizedAttrs<T>>,count?: number)
...
}

Subfactories are just a wrapper of another factory, to avoid explicit operations that could lead to unexpected results over that factory, like

protectedattrs: FactorizedAttrs<User>={country: async()=>newCountryFactory().create({name: faker.address.country(),}),}

instead of

protectedattrs: FactorizedAttrs<User>={country: newSubfactory(CountryFactory,{name: faker.address.country(),}),}

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.

make & makeMany

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.

  • overrideParams - Override some of the attributes of the entity.
make(overrideParams: Partial<FactorizedAttrs<T>>={}): Promise<T>makeMany(amount: number,overrideParams: Partial<FactorizedAttrs<T>>={}): Promise<T[]>
newUserFactory().make()newUserFactory().makeMany(10)// override the emailnewUserFactory().make({email: 'other@mail.com'})newUserFactory().makeMany(10,{email: 'other@mail.com'})

create & createMany

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.

  • overrideParams - Override some of the attributes of the entity.
  • saveOptions - Save options from TypeORM
create(overrideParams: Partial<FactorizedAttrs<T>>={},saveOptions?: SaveOptions): Promise<T>createMany(amount: number,overrideParams: Partial<FactorizedAttrs<T>>={},saveOptions?: SaveOptions): Promise<T[]>
newUserFactory().create()newUserFactory().createMany(10)// override the emailnewUserFactory().create({email: 'other@mail.com'})newUserFactory().createMany(10,{email: 'other@mail.com'})// using save optionsnewUserFactory().create({email: 'other@mail.com'},{listeners: false})newUserFactory().createMany(10,{email: 'other@mail.com'},{listeners: false})

Seeder

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.

classUserSeederextendsSeeder{asyncrun(connection: Connection){
...
}}

run

This function is the one that needs to be defined when extending the class. Could use call to run some other seeders.

run(connection: Connection): Promise<void>
asyncrun(connection: Connection){awaitnewUserFactory().createMany(10)awaitthis.call(connection,[PetSeeder])}

CLI Configuration

There are two possible commands to execute, one to see the current configuration and one to run a seeder.

Add the following scripts to your package.json file to configure them.

"scripts": {
"seed:run": "typeorm-seeding seed",
...
}

seed

This command execute a seeder, that could be specified as a parameter.

typeorm-seeding seed <path>

The name of the seeder to execute (either set with the --seed option or with default in configs) must be the seeder's class name, and thus, the seeder must be exported with a named export. Please avoid default export for seeders: it may imply unwanted behavior. (See #75).

Options
OptionDefaultDescription
--dataSource or -dPath of the data source

Testing features

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.

useSeeders

Execute one or more seeders.

useSeeders(entrySeeders: ClassConstructor<Seeder>|ClassConstructor<Seeder>[]): Promise<void>useSeeders(entrySeeders: ClassConstructor<Seeder>|ClassConstructor<Seeder>[],customOptions: Partial<ConnectionConfiguration>,): Promise<void>

useDataSource

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>

About

🌱 A delightful way to seed test data into your database.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages