#how to fix typeorm with nest js throwing database connection error

2 messages · Page 1 of 1 (latest)

autumn oasis
#

So i am unable to use typ[e orm with nest js so i have used this as config file or data source file

import { DataSourceOptions } from 'typeorm';
import dotenv from 'dotenv';
import path, { join } from 'path';

dotenv.config();

export function getConfig(): DataSourceOptions {
console.log("path.resolve(process.cwd()", join(process.cwd(), 'src/migrations'))
return {
type: 'postgres',
host: process.env.DB_HOST,
port: Number(process.env.DB_PORT),
username: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
synchronize: process.env.DB_SYNCHRONIZATION === 'true',

    migrations: [path.join(process.cwd(), '/src/migrations', '*{.ts,.js}')],

    entities: [
        path.join(process.cwd(), '/src/**/*.entity.{.ts,.js}')
    ],


    migrationsTableName: 'migrations',

    ssl: process.env.NODE_ENV === 'production',
};

}
this is my app.module.ts
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { getConfig} from './config/database.config';
import "reflect-metadata"

@Module({
imports: [
TypeOrmModule.forRoot(getConfig()),
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule{};

the following commands wwork on it when i use the nest migrate run and create and genreate a migration what doesnt work is nest start : dev it is uanble to connect to db however when i use .js format it works normally what has went wrong can u tell

autumn oasis
#

import { DataSource } from 'typeorm';
import dotenv from 'dotenv';
import path from 'path';
dotenv.config();
// Determine the base directory for compiled files.
// In a typical NestJS setup, compiled files are in the 'dist' folder at the project root.
const isTsNode = process.argv.some(arg => arg.includes('ts-node'));
const srcPath = path.join(process.cwd(), 'src');
const distPath = path.join(process.cwd(), 'dist');
export const datasource = new DataSource({
type: 'postgres',
host: process.env.DB_HOST,
port: Number(process.env.DB_PORT),
username: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
synchronize: true,
logging: true, // It is recommended to enable logging for better debugging.
// Paths should point to the compiled JavaScript files (.js)
// relative to the 'dist' directory.
// Assuming 'src/migrations' compiles to 'dist/src/migrations'
migrations: [
isTsNode
? path.join(srcPath, 'migrations', '.{ts,js}') // dev: ts
: path.join(distPath, 'src', 'migrations', '
.js') // prod: js
],

entities: [
isTsNode
? path.join(srcPath, 'Modules', '', 'entities', '*.{ts,js}')
: path.join(distPath, 'src', 'Modules', '
', 'entities', '*.js')
],

migrationsTableName: 'migrations',
ssl: process.env.NODE_ENV === 'production',
});

this is the fixed code u can use it in ur project