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
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
Be able to run nest start
with my custom orm configuration
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
Nest version: 7.2.0
For Tooling issues:
- Node version: v10.15.3
- Platform: Mac
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.
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
app.module.ts
, We reuse this config asimport * as connectionOptions from './ormconfig';
imports: [
TypeOrmModule.forRoot(connectionOptions),
],
controllers: [AppController],
providers: [AppService]
})
export class AppModule {}
"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"
Most helpful comment
Thank @jdnichollsc ,
For other peoples, I summary the solution of you as below.
ormconfig.ts
app.module.ts
, We reuse this config as// please correct the --config src/ormconfig.ts as your Dir