I have been able to get the ConfigService to work with my TypeORM (v0.3.7) configuration, so my environment (.env) file is being written, and works with my NestJS (v8.4.7) application in a Nrwl NX (v14.4.2) mono repository. My database access via NestJS is working fine.
I have created a database configuration class (database.configuration.ts) which gets my information for the database connection from the.env file using the NestJS Configuration module.
database.configuration.ts
import { ConfigModule, ConfigService } from '@nestjs/config';
import { TypeOrmModuleAsyncOptions, TypeOrmModuleOptions } from '@nestjs/typeorm';
export default class DatabaseConfiguration {
static GetORMConfiguration(ConfigService_: ConfigService): TypeOrmModuleOptions {
return {
type: 'mysql',
host: ConfigService_.get('DB_SERVER_HOST') || 'localhost',
port: parseInt(ConfigService_.get('DB_SERVER_PORT'), 10) || 3306,
username: ConfigService_.get('DB_USER') || 'username',
password: ConfigService_.get('DB_PASSWORD') || 'password',
database: ConfigService_.get('DB_DATABASE') || 'dbname',
autoLoadEntities: true,
synchronize: false,
migrationsRun: false,
logging: ['error', 'schema', 'warn'],
maxQueryExecutionTime: 1000,
logger: 'file',
};
}
}
export const DatabaseConfigurationAsync: TypeOrmModuleAsyncOptions = {
imports: [ConfigModule],
useFactory: async (ConfigService_: ConfigService): Promise<TypeOrmModuleOptions> => DatabaseConfiguration.GetORMConfiguration(ConfigService_),
inject: [ConfigService]
};
Then I have updated by app.module.ts to import and use this information, based on this question and answers :
app.module.ts
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { TypeOrmModule } from '@nestjs/typeorm';
import { DatabaseConfigurationAsync } from './database.configuration';
import { AdminCodesModule } from './admin-codes/admin-codes.module';
import { AppController } from './app.controller';
import { AppService } from './app.service';
@Module({
imports: [
ConfigModule.forRoot({ isGlobal: true }),
TypeOrmModule.forRootAsync(DatabaseConfigurationAsync),
AdminCodesModule,
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
I have a simple definition then for the Administration Codes, to simply have a database. Working with the API from the controller, service, etc., I do receive all my data back. However, when I try to generate the migrations, I get an error that I cannot resolve no matter how I keep trying to change the code.
Trying to run the commands (via npm and package.json or the bash shell):
npx typeorm-ts-node-commonjs migration:generate -d ./app/database.configuration.ts ./migrations/admincodes
or
npx typeorm-ts-node-commonjs migration:generate -d ./app/database.configuration.ts admincodes
gives an error:
Error during migration generation:
Error: Unable to open file: "repo\apps\api\src\app\database.configuration.ts". Cannot use import statement outside a module
I have tried changing this many ways, including even moving to type: module
in my package.json, and using typeorm-ts-node-esm
to run the generation, but each version fails with some part of the import error, or with type: module
the export line fails.
I cannot use the type: module
in my package.json as that breaks other functionality in the NestJS application.
The error specify that the path is incorrect, and can't open the file. check your dist folder, and check where the file is generate when you build your app.
I think you have to add /src
in your path: ./src/app/database.configuration.ts./migrations/admincodes.
this is the simple way i configure it
npm i @nestjs/config
@Module({
imports: [
ConfigModule.forRoot(),
TypeOrmModule.forRootAsync({
imports: [ConfigModule],
name: process.env.DATABASE_NAME,
inject: [ConfigService],
useFactory: async (configService: ConfigService) => {
return {
type: 'mysql',
host: configService.get(`DATABASE_HOST`),
port: configService.get(`DATABASE_PORT`),
username: configService.get('DB_USER'),
database: configService.get('DATABASE_NAME'),
password: configService.get('DB_PASSWORD'),
entities: [__dirname + '/../entities/*.entity{.ts,.js}'],
synchronize: false,
logging: true,
};
},
}),
],
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.