NestJs TypeORM unable to connect to mysql
Solution 1
Try to change your entities dir in ormconfig.json or .env, it worked for me as:
"entities": ["dist/**/**.entity{.ts,.js}"]
Solution 2
There is an issue, how you are loading the **TypeOrmModule**
. The way, you had loaded, it is synchronous so it simply means custom env
will not be available instantly on application boot.
So what you can do, is to load TypeOrmModule` asynchronusly like below -
ConfigModule.forRoot({
isGlobal: true, // [REQUIRED if want to use env gloablly among all modules]
}),
TypeOrmModule.forRootAsync({
useFactory: () => ({
type: 'mysql',
host: process.env.TYPEORM_HOST,
port: parseInt(process.env.TYPEORM_PORT, 10),
username: process.env.TYPEORM_USERNAME,
password: process.env.TYPEORM_PASSWORD,
database: process.env.TYPEORM_DATABASE,
synchronize: false,
migrations: [process.env.TYPEORM_MIGRATIONS],
cli: {
migrationsDir: process.env.TYPEORM_MIGRATIONS_DIR,
},
logging: process.env.TYPEORM_LOGGING === 'true' ? true : false,
entities: [__dirname + '/../**/*.entity{.ts,.js}'],
}),
}),
Related videos on Youtube
MoSwilam
Updated on June 04, 2022Comments
-
MoSwilam almost 2 years
I am trying to connect to MySQL. I have defined the db connection vars in a .env file in my root dir, and I am initializing the connection in the
app.module.ts
file. the only issue I am facing now is when creating or running migrations using the CLI, I followed the typeorm docs here to configure the connection, however when I runtypeorm migrate:create -n myNewTable
, it should create the migration file in the specified directory, what it does instead is it creates it in the app root directory, similarily, I solved the issue by using the-d
flag after thetypeorm migrate:create
to specify the directory, however when I try running my migration files, I get thisNo connection options were found in any of configurations file.
here is my app.module.ts file.
TypeOrmModule.forRoot({ type: 'mysql', host: process.env.TYPEORM_HOST, port: parseInt(process.env.TYPEORM_PORT, 10), username: process.env.TYPEORM_USERNAME, password: process.env.TYPEORM_PASSWORD, database: process.env.TYPEORM_DATABASE, synchronize: false, migrations: [process.env.TYPEORM_MIGRATIONS], cli: { migrationsDir: process.env.TYPEORM_MIGRATIONS_DIR, }, logging: (process.env.TYPEORM_LOGGING === 'true') ? true : false, entities: [__dirname + '/../**/*.entity{.ts,.js}'], }),
and here is my .env file
# use .ts for development, .js for production TYPEORM_CONNECTION = mysql TYPEORM_HOST = 127.0.0.1 TYPEORM_PORT = 3306 TYPEORM_USERNAME = <username> TYPEORM_PASSWORD = <password> TYPEORM_DATABASE = <dbname> TYPEORM_SYNCHRONIZE = true TYPEORM_MIGRATIONSRUN = true TYPEORM_LOGGING = true TYPEORM_ENTITIES = src/**/**.entity.ts #TYPEORM_ENTITIES = src/**/**.entity.js TYPEORM_SUBSCRIBERS = src/subscriber/*.ts #TYPEORM_SUBSCRIBERS = src/subscriber/*.js TYPEORM_MIGRATIONS = src/database/migration/*.ts TYPEORM_MIGRATIONS_DIR = src/database/migration TYPEORM_SUBSCRIBERS_DIR = src/subscriber
any help/hint is highly appreciated, thanks in advance.
-
onegun about 4 yearsHi, @MoSwilam, have you solved this problem? I encountered same
-
Roger Inacio almost 3 yearsTry this solution: stackoverflow.com/questions/63848877/…
-
-
Rich almost 5 yearsThis is a very bad implementation.