11import chalk from 'chalk' ;
2- import { DataSource } from 'typeorm' ;
3- import { runSeeders } from 'typeorm-extension' ;
2+ import { DatabaseType , DataSource , DataSourceOptions } from 'typeorm' ;
3+ import { createDatabase , runSeeders } from 'typeorm-extension' ;
44import { NestFactory } from '@nestjs/core' ;
55import { ConfigService } from '@nestjs/config' ;
66
@@ -16,6 +16,8 @@ console.log(chalk.green('Running seeders...'));
1616console . log ( chalk . green ( `Environment: ${ process . env . APP_ENV } \n` ) ) ;
1717
1818async function runSeeder ( ) {
19+ await createDatabaseIfNotExist ( ) ;
20+
1921 const app = await NestFactory . create ( BootstrapModule ) ;
2022 const dataSource = app . get ( DataSource ) ;
2123 const configService = app . get ( ConfigService ) ;
@@ -28,4 +30,20 @@ async function runSeeder() {
2830
2931 await app . close ( ) ;
3032}
33+
34+ async function createDatabaseIfNotExist ( ) {
35+ await createDatabase ( {
36+ ifNotExist : true ,
37+ synchronize : true ,
38+ options : {
39+ type : ( process . env . DB_TYPE as DatabaseType ) || 'mysql' ,
40+ host : process . env . DB_HOST || 'localhost' ,
41+ port : parseInt ( process . env . DB_PORT , 10 ) || 3306 ,
42+ database : process . env . DB_NAME ,
43+ username : process . env . DB_USER ,
44+ password : process . env . DB_PASS ,
45+ } as DataSourceOptions ,
46+ } ) ;
47+ }
48+
3149runSeeder ( ) ;
0 commit comments