In order to use seeds in your project you must install the following dependencies:
import { DataSource } from 'typeorm';
import { runSeeders, Seeder } from 'typeorm-extension';
export default class InitSeeder implements Seeder {
public async run(dataSource: DataSource): Promise<any> {
await runSeeders(dataSource, {
seeds: ['src/common/database/seeds/**/*.seeder{.ts,.js}'],
factories: ['src/common/database/factories/**/*.factory{.ts,.js}'],
});
}
}
import * as dotenv from 'dotenv';
import 'reflect-metadata';
import InitSeeder from 'src/common/database/init.seeder';
import { DataSource, DataSourceOptions } from 'typeorm';
import { SeederOptions } from 'typeorm-extension';
import { SnakeNamingStrategy } from 'typeorm-naming-strategies';
let environment;
if (process.env.NODE_ENV === undefined) {
environment = dotenv.config({ path: `.env.development` }).parsed;
}
const options = {
type: 'postgres',
schema: 'public',
host: environment ? environment.DB_HOST : process.env.DB_HOST,
port: Number(environment ? environment.DB_PORT : process.env.DB_PORT),
username: environment ? environment.DB_USERNAME : process.env.DB_USERNAME,
password: environment ? environment.DB_PASSWORD : process.env.DB_PASSWORD,
database: environment ? environment.DB_DATABASE : process.env.DB_DATABASE,
synchronize: false,
logging: false,
entities: ['src/**/*.entity{.ts,.js}'],
migrations: ['src/migrations/*{.ts,.js}'],
migrationsRun: true,
namingStrategy: new SnakeNamingStrategy(),
seeds: [InitSeeder],
};
const dataSource = new DataSource(options as DataSourceOptions & SeederOptions);
export default dataSource;