typeorm icon indicating copy to clipboard operation
typeorm copied to clipboard

Seeding

Open JipSterk opened this issue 5 years ago • 5 comments

Issue type:

[ ] question [ ] bug report [x] feature request [ ] documentation issue

Database system/driver:

[ ] cordova [x] mongodb [x] mssql [x] mysql / mariadb [x] oracle [x] postgres [x] cockroachdb [x] sqlite [ ] sqljs [ ] react-native [ ] expo

Propose

Maybe add a field in ormconfig.json that points to a directory where seeders can be located. Add an interface that i could inherit to seed the database with data.

Example ormconfig.json

[
  {
    "type": "postgres",
    "host": "localhost",
    "port": 5432,
    "username": "postgres",
    "password": "postgres",
    "database": "seeded",
    "synchronize": true,
    "logging": true,
    "entities": ["src/entity/**/*.*"],
    "seeders": ["src/seeder/**/*.*"],
    "migrations": ["src/migration/**/*.*"],
    "subscribers": ["src/subscriber/**/*.*"],
    "cli": {
      "entitiesDir": "src/entity",
      "seedersDir": "src/seeder",
      "migrationsDir": "src/migration",
      "subscribersDir": "src/subscriber"
    }
  }
]

Example Seeder

import { SeederInterface, QueryRunner } from "typeorm"

export class UserSeeder implements SeederInterface {
  public async seed(queryRunner: QueryRunner): Promise<any> {
    //...logic here
  }
}

JipSterk avatar Dec 09 '19 09:12 JipSterk

If you need something now this works fine https://github.com/w3tecch/typeorm-seeding

krazibit avatar Dec 17 '19 14:12 krazibit

Hello, This feature is very needed, not for fake data but I have this problem on my job that we have data to be seed to parameterize, you insert some data that you need to start.

A hint I am not a fan of sequelize but they have a must have feature related to this. Like the migrations inserts a line for each migration done, the seed must do as weel because is so much easier iterate over big projects and/or run the migrations and seed on deploy throughout a ci/cd pipeline (https://sequelize.org/master/manual/migrations.html#seed-storage).

Btw great project thank you.

rcaceiro avatar Aug 16 '21 20:08 rcaceiro

Any updates on this?

iamchathu avatar May 15 '22 08:05 iamchathu

Any updates on this?

zlavoie avatar Mar 22 '23 00:03 zlavoie

Hey folks dunno when TypeORM gets up to speed on developing new features, but ATM I think these are potential solutions:

  1. Create a SeederFactory class and then have it implemented for each entity separately. Frankly speaking it is not the most convenient solution.
  2. Create an empty migration and then there seed your DB :thinking: IDK if this is the brightest idea ever or not.
  3. Or we can use 3rd-party libs as it is being suggested in the docs: https://typeorm.io/#extensions.

kasir-barati avatar Dec 11 '24 09:12 kasir-barati

TypeORM has returned to active development after recently transitioning to new leadership (see related announcement).

As part of the new team's efforts to prioritise work on the project moving forward, it is necessary to clean up the backlog of stale issues.

🧹 This issue is being automatically closed because it has had no activity in the last 6 months. If you believe this issue is still relevant, feel free to comment below and the maintainers may reopen it. Thank you for your contribution.

github-actions[bot] avatar Sep 18 '25 16:09 github-actions[bot]