I use mikro-orm in nest js, and i write mikro-orm.config.ts with using configService.get(). But it doesn't get env value. Process.env is too. In other files like main.ts, it can get env properly, but not in mikro-orm.config.ts. How should i get it?
import { Logger } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { Options } from '@mikro-orm/core';
import { SqlHighlighter } from '@mikro-orm/sql-highlighter';
const configService = new ConfigService();
const logger = new Logger('MikroORM');
const MikroORMConfig: Options = {
dbName: process.env.POSTGRES_DATABASE == '' ? 'database' : 'database',
user: process.env.POSTGRES_USER,
password: process.env.POSTGRES_PASSWORD,
host: process.env.POSTGRES_HOST,
port: parseInt(process.env.POSTGRES_PORT),
type: 'postgresql',
entities: ['./dist/**/*.entity.js'],
entitiesTs: ['./src/**/*.entity.ts'],
highlighter: new SqlHighlighter(),
migrations: {
path: './src/feature/database/migrations',
},
logger: logger.log.bind(logger),
};
export default MikroORMConfig;
I use process.env and configService but both of them didn't get env.
You can't use anything from nestjs for the CLI config, the CLI is not aware of such a thing. Where do you define the env vars? I guess some
.env
file? The ORM will try to read that automatically, but only if it's in the root of your project (and named exactly.env
).If the
.env
file is elsewhere or named differently, you could register it in your CLI config: