7.0.0 • Published 2 months ago

@jorgebodega/typeorm-seeding v7.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

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

Factory

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

7.0.0

2 months ago

7.0.0-next.3

2 months ago

7.0.0-next.2

2 months ago

7.0.0-next.1

6 months ago

6.0.1

1 year ago

6.0.0-next.5

1 year ago

6.0.0

2 years ago

6.0.0-next.3

2 years ago

6.0.0-next.4

2 years ago

5.0.2

2 years ago

6.0.0-next.2

2 years ago

5.0.1

2 years ago

5.0.0

2 years ago

5.0.0-next.1

2 years ago

5.0.1-next.1

2 years ago

5.0.0-next.2

2 years ago

5.0.1-next.2

2 years ago

6.0.0-next.1

2 years ago

4.0.0

2 years ago

4.0.0-next.3

2 years ago

3.2.0

2 years ago

3.1.0

2 years ago

3.0.1

2 years ago

3.0.0-next.1

2 years ago

3.0.0

2 years ago

3.1.0-next.1

2 years ago

4.0.0-next.1

2 years ago

4.0.0-next.2

2 years ago

2.0.0

2 years ago

2.0.0-next.3

2 years ago

2.0.0-next.2

2 years ago

2.0.0-next.1

2 years ago