question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

54reactions
phattrankycommented, Oct 19, 2020

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"
34reactions
kamilmysliwieccommented, Jun 29, 2020

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

No connection options were found in any orm configuration ...
With these settings , I was already connected to the database with the oracledb. I generate project with this typeorm init --name MyProject...
Read more >
No connection options were found in any orm configuration ...
You need to log in to use this function. Qiita can be used more conveniently after logging in. You can also use this...
Read more >
Create Migration Error - Questions - n8n community
When i try to create migration files i got “Cannot use import statement ... No connection options were found in any orm configuration...
Read more >
No Connection Options Were Found In Any Orm Configuration ...
Getting an error that says [ExceptionHandler] No connection options were found in any orm configuration files. when I try running my NestJS project...
Read more >
Connecting TypeScript backend in EC2 to PostgreSQL RDS
js migration:run Error during migration run: TypeORMError: No connection options were found in any orm configuration files. at new TypeORMError (/home/ec2-user/ ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found