Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@jorgebodega/typeorm-seeding

jorgebodega70.1kMIT7.1.0TypeScript support: included

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

typeorm, seed, seeding, orm

readme

logo

TypeORM Seeding

License Semantic release Biome Coveralls master 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

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()
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
}

Seeder

class UserSeeder extends Seeder {
  async run(dataSource: DataSource) {
    const users: User[] = [...]
    await dataSource.createEntityManager().save<User>(users)
  }
}

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.

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

run

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

CLI Configuration

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",
  ...
}

seed

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.

Options
Option Default Description
--dataSource or -d | Path 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>

Factory

Factory related code has been removed from this package, now on @jorgebodega/typeorm-factory.

changelog

7.1.0 (2024-10-04)

Bug Fixes

Features

  • install biome as formatter and linter (#335) (a195c27)

7.1.0-next.3 (2024-10-04)

Bug Fixes

7.1.0-next.2 (2024-09-25)

Bug Fixes

7.1.0-next.1 (2024-06-22)

Features

  • install biome as formatter and linter (#335) (a195c27)

7.0.0 (2024-03-18)

Bug Fixes

  • deprecation messages (e523142)
  • eslint (769263d)
  • update dependency commander to v10 (b43eca4)
  • update dependency commander to v11 (4eec67d)
  • update dependency commander to v12 (12a46b1)
  • update dependency glob to v10 (859da14)

chore

  • bump minimum node version (c08c2b2)

Features

BREAKING CHANGES

  • remove support for node 16
  • now Node 14 (lts) and 17 are out-of-life

7.0.0-next.3 (2024-03-18)

7.0.0-next.2 (2024-02-28)

Bug Fixes

  • update dependency commander to v12 (12a46b1)

7.0.0-next.1 (2023-10-30)

Bug Fixes

  • deprecation messages (e523142)
  • eslint (769263d)
  • update dependency commander to v10 (b43eca4)
  • update dependency commander to v11 (4eec67d)
  • update dependency glob to v10 (859da14)

chore

  • bump minimum node version (c08c2b2)

Features

BREAKING CHANGES

  • remove support for node 16
  • now Node 14 (lts) and 17 are out-of-life

6.0.1 (2023-01-03)

6.0.0-next.5 (2023-01-03)

Bug Fixes

  • glob pattern on Windows (1246b1c)

6.0.0 (2022-11-02)

chore

  • remove factory definitions (f2ede7c)

Features

  • add support for multiple paths on cli (6ffb5dd)
  • force major version (73ade47)

BREAKING CHANGES

  • Force version 6
  • remove all factory related code in favor of @jorgebodega/typeorm-factory

6.0.0-next.4 (2022-11-02)

6.0.0-next.3 (2022-10-15)

6.0.0-next.2 (2022-08-22)

Features

  • add support for multiple paths on cli (6ffb5dd)

6.0.0-next.1 (2022-08-18)

chore

  • remove factory definitions (f2ede7c)

Features

BREAKING CHANGES

  • Force version 6
  • remove all factory related code in favor of @jorgebodega/typeorm-factory

5.0.1-next.2 (2022-08-18)

chore

  • remove factory definitions (f2ede7c)

BREAKING CHANGES

  • remove all factory related code in favor of @jorgebodega/typeorm-factory

5.0.2 (2022-09-05)

Bug Fixes

  • glob pattern on Windows (1246b1c)

5.0.1 (2022-08-18)

Bug Fixes

5.0.1-next.1 (2022-08-17)

Bug Fixes

5.0.0 (2022-08-01)

Bug Fixes

  • add factory deprecation notice (8d75e05)
  • update dependency glob to v8 (2d3c996)

Features

  • removed unused command and add some more tests (ea6649e)
  • use typeorm 0.3 (aa0153b)

BREAKING CHANGES

  • Removed config command
  • Connection API is deprecated and removed.

5.0.0-next.2 (2022-08-01)

Bug Fixes

  • add factory deprecation notice (8d75e05)

5.0.0-next.1 (2022-07-13)

Bug Fixes

  • update dependency glob to v8 (2d3c996)

Features

  • removed unused command and add some more tests (ea6649e)
  • use typeorm 0.3 (aa0153b)

BREAKING CHANGES

  • Removed config command
  • Connection API is deprecated and removed.

4.0.0 (2022-03-23)

Bug Fixes

  • add type check on subfactories creation (2b2883b)
  • attrs failing with lazy instances (c8ddda8)
  • update dependency yargs to v17.4.0 (b64c224)

Features

  • add factorized attrs (4a2ce08)
  • add lazyattribute and subfactory as options in attribs (48a3630)
  • add subfactory as valid factorized attr (0f4b37e)
  • separate different types of lazy attributes (3efe94e)

BREAKING CHANGES

  • definition function has been substituted with attrs

4.0.0-next.3 (2022-03-03)

4.0.0-next.2 (2022-02-12)

4.0.0-next.1 (2022-02-12)

Bug Fixes

  • add type check on subfactories creation (2b2883b)
  • attrs failing with lazy instances (c8ddda8)

Features

  • add factorized attrs (4a2ce08)
  • add lazyattribute and subfactory as options in attribs (48a3630)
  • add subfactory as valid factorized attr (0f4b37e)
  • separate different types of lazy attributes (3efe94e)

BREAKING CHANGES

  • definition function has been substituted with attrs

3.2.0 (2022-02-06)

Features

  • Allow async map function (2d2c27b)

3.1.0 (2022-01-13)

Features

  • add support to async definition on factory (9432089)

3.1.0-next.1 (2022-01-09)

Features

  • add support to async definition on factory (5fcca0e)

3.0.1 (2022-01-08)

Bug Fixes

  • update dependencies and add rimraf to dev deps (504cef2)

3.0.0 (2022-01-06)

Bug Fixes

  • adapt seeder to new schema (aecf7b4)
  • remove factories option from config (d66f0f1)

chore

  • remove all faker usages from source folder (1e14718)
  • remove faker from dependencies (40f21c8)

Features

  • adapt use seeders to new structure (5bca471)
  • move factory definition to new abstract class (81ccdf6)
  • remove context from factories (#23) (b7ecba4)
  • remove deprecated elements (d09e154)
  • remove factory helper methods and test associateds (#30) (3393724)
  • remove useFactories helper method (#29) (ddb5c2f)
  • seed command will execute now default seeder or seed param (314d8c3)

BREAKING CHANGES

  • useSeeders change its definition and is now incompatible with previous version
  • Faker is not available anymore as function param
  • Faker is removed from dependencies and now users must install it by themselves
  • Deprecated functions are removed
  • New abstract factory class is incompatible with previous version
  • useFactories will no longer be available as is useless in new architecture
  • Context is being removed from factories. This could break some applications that were using it

2.0.0 (2021-12-14)

BREAKING CHANGES

  • useSeeders has been renamed to useFactories to be more consistent with functionality
  • New useSeeders has been created. Returns every Seeder that complies glob pattern and could execute them.
  • Seeder is now an abstract class instead of an interface
  • Seeder must export default, as that is the one that is chosen on useSeeder
  • tearDownDatabase and useRefreshDatabase are deprecated