Nest: Error: No connection options were found in any orm configuration files.

Created on 29 Jun 2020  路  5Comments  路  Source: nestjs/nest

Bug Report

Current behavior

Getting an error that says [ExceptionHandler] No connection options were found in any orm configuration files. when I try running my NestJS project using the Repository pattern with typeorm and a custom config

Input Code

import { createConnection, Connection } from 'typeorm'

import { DATABASE_CONNECTION } from '../constants'
import * as config from './ormconfig'

export const databaseProviders = [
  {
    provide: DATABASE_CONNECTION,
    useFactory(): Promise<Connection> {
      return createConnection(config)
    }
  }
]

Custom Configuration:

import { join } from 'path'
import { ConnectionOptions } from 'typeorm'

const config = {
  host: 'localhost',
  user: process.env.SQL_USER,
  password: process.env.SQL_PASSWORD,
  database: process.env.SQL_DATABASE,
}

const connectionOptions: ConnectionOptions = {
  type: 'postgres',
  host: config.host,
  port: 5432,
  username: config.user || 'postgres',
  password: config.password || 'postgres',
  database: config.database || 'my_database',
  entities: [
    join(__dirname, '../models/*{.ts,.js}'),
  ],
  // We are using migrations, synchronize should be set to false.
  synchronize: false,
  dropSchema: false,
  // Run migrations automatically,
  // you can disable this if you prefer running migration manually.
  migrationsRun: true,
  logging: true,
  migrations: [
    join(__dirname, 'migrations/*{.ts,.js}')
  ],
  cli: {
    migrationsDir: './migrations'
  }
}

export = connectionOptions

Expected behavior

Be able to run nest start with my custom orm configuration

Possible Solution

It works executing ts-node -r tsconfig-paths/register src/main.ts instead, but I want to execute other scripts like nest start --watch and nest start --debug --watch

Environment


Nest version: 7.2.0

For Tooling issues:

  • Node version: v10.15.3
  • Platform: Mac
needs triage

Most helpful comment

Thank @jdnichollsc ,

For other peoples, I summary the solution of you as below.

  1. Create the separate Ormconfig file to use the same config for both the migration process and the application

ormconfig.ts

import { join } from 'path'
import { ConnectionOptions } from 'typeorm'

import { Country } from './modules/countries/country.entity';
import { Province } from './modules/provinces/entities';

const PROD_ENV = 'production'

const config = {
  host: 'localhost',
  user: process.env.SQL_USER,
  password: process.env.SQL_PASSWORD,
  database: process.env.SQL_DATABASE,
}

// FOR GOOGLE CLOUD SQL
if (
  process.env.INSTANCE_CONNECTION_NAME &&
  process.env.NODE_ENV === PROD_ENV
) {
  config.host = `/cloudsql/${process.env.INSTANCE_CONNECTION_NAME}`
}

const connectionOptions: ConnectionOptions = {
  type: 'postgres',
  host: config.host,
  port: 5432,
  username: config.user || 'postgres',
  password: config.password || '123456789',
  database: config.database || 'vivubook-dev',
  entities: [
    Country,
    Province
  ],
  // We are using migrations, synchronize should be set to false.
  synchronize: false,
  dropSchema: false,
  // Run migrations automatically,
  // you can disable this if you prefer running migration manually.
  migrationsRun: true,
  logging: ['warn', 'error'],
  logger: process.env.NODE_ENV === PROD_ENV ? 'file' : 'debug',
  migrations: [
    join(__dirname, 'migrations/*{.ts,.js}')
  ],
  cli: {
    migrationsDir: 'src/migrations'
  }
}

export = connectionOptions
  1. At the app.module.ts, We reuse this config as
import * as connectionOptions from './ormconfig';

  imports: [
    TypeOrmModule.forRoot(connectionOptions),
  ],
  controllers: [AppController],
  providers: [AppService]
})
export class AppModule {}
  1. Add the script for easy running migration at package.json
    // please correct the --config src/ormconfig.ts as your Dir
"typeorm": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js --config src/ormconfig.ts",
 "migration": "yarn typeorm migration:run",
 "migration:create": "yarn typeorm migration:create -n",
  "migration:revert": "yarn typeorm migration:revert"

All 5 comments

Please, use our Discord channel (support) for such questions. We are using GitHub to track bugs, feature requests, and potential improvements.

Nevermind, It's working well in my template now, thanks! https://github.com/proyecto26/MyAPI

Hey @jdnichollsc how did you manage to fix up this issue, I'm having the same.

Check the configuration of that template 馃檪

Thank @jdnichollsc ,

For other peoples, I summary the solution of you as below.

  1. Create the separate Ormconfig file to use the same config for both the migration process and the application

ormconfig.ts

import { join } from 'path'
import { ConnectionOptions } from 'typeorm'

import { Country } from './modules/countries/country.entity';
import { Province } from './modules/provinces/entities';

const PROD_ENV = 'production'

const config = {
  host: 'localhost',
  user: process.env.SQL_USER,
  password: process.env.SQL_PASSWORD,
  database: process.env.SQL_DATABASE,
}

// FOR GOOGLE CLOUD SQL
if (
  process.env.INSTANCE_CONNECTION_NAME &&
  process.env.NODE_ENV === PROD_ENV
) {
  config.host = `/cloudsql/${process.env.INSTANCE_CONNECTION_NAME}`
}

const connectionOptions: ConnectionOptions = {
  type: 'postgres',
  host: config.host,
  port: 5432,
  username: config.user || 'postgres',
  password: config.password || '123456789',
  database: config.database || 'vivubook-dev',
  entities: [
    Country,
    Province
  ],
  // We are using migrations, synchronize should be set to false.
  synchronize: false,
  dropSchema: false,
  // Run migrations automatically,
  // you can disable this if you prefer running migration manually.
  migrationsRun: true,
  logging: ['warn', 'error'],
  logger: process.env.NODE_ENV === PROD_ENV ? 'file' : 'debug',
  migrations: [
    join(__dirname, 'migrations/*{.ts,.js}')
  ],
  cli: {
    migrationsDir: 'src/migrations'
  }
}

export = connectionOptions
  1. At the app.module.ts, We reuse this config as
import * as connectionOptions from './ormconfig';

  imports: [
    TypeOrmModule.forRoot(connectionOptions),
  ],
  controllers: [AppController],
  providers: [AppService]
})
export class AppModule {}
  1. Add the script for easy running migration at package.json
    // please correct the --config src/ormconfig.ts as your Dir
"typeorm": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js --config src/ormconfig.ts",
 "migration": "yarn typeorm migration:run",
 "migration:create": "yarn typeorm migration:create -n",
  "migration:revert": "yarn typeorm migration:revert"
Was this page helpful?
0 / 5 - 0 ratings

Related issues

FranciZ picture FranciZ  路  3Comments

rlesniak picture rlesniak  路  3Comments

2233322 picture 2233322  路  3Comments

KamGor picture KamGor  路  3Comments

marshall007 picture marshall007  路  3Comments