Entities no longer registered after Hot Reloading
Problem
Note from @imnotjames I've edited the title. The most common issue I've seen in this thread relates to the hot-reload feature used by development environments & serverless environments. What happens if that the registered entities are changed by the hot reload with new entities that are no longer registered even if they're similar. If you believe your issue is unrelated to that, please open a new issue with details including a reproducible example. --- Issue type: [ ] question [x ] bug report [ ] feature request [x ] documentation issue Database system/driver: [ ] `cordova` [ ] `mongodb` [ ] `mssql` [ ] `mysql` / `mariadb` [ ] `oracle` [x ] `postgres` [ ] `cockroachdb` [ ] `sqlite` [ ] `sqljs` [ ] `react-native` [ ] `expo` TypeORM version: [ ] `latest` [ ] `@next` [x ] `0.2.24` Been using v0.2.22 and my ormconfig.js is as follows [code block] which works fine, but I updated to v0.2.24 and I'm getting "No repository for \"<entity>\" was found. Looks like this entity is not registered in current \"default\" connection?" when doing a simple query [code block] Is there a documentation I am missing?
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Fix Entity Registration After Hot Reload in TypeORM
The issue arises because TypeORM does not retain the entity registration state after a hot reload. When the application is reloaded, the entities may not be re-registered properly, leading to errors indicating that the repository for the entity is not found.
Awaiting Verification
Be the first to verify this fix
- 1
Update Entity Registration Logic
Ensure that all entities are explicitly registered in the connection options. This can be done by using the `entities` array in your `ormconfig.js` or equivalent configuration file.
javascriptentities: [__dirname + '/entity/*.js'] - 2
Implement Hot Reloading Hook
Add a hook to re-initialize the TypeORM connection upon hot reload. This can be done by listening to the hot reload event and calling the `createConnection` method again.
javascriptif (module.hot) { module.hot.accept('./entity', () => { createConnection(); }); } - 3
Clear Previous Connections
Before creating a new connection during hot reload, ensure to close the previous connection to avoid conflicts. This can be done using `getConnection().close()`.
javascriptimport { getConnection } from 'typeorm'; getConnection().close(); - 4
Test Entity Queries
After implementing the above changes, run your application and perform queries on your entities to confirm that they are now recognized and functioning correctly.
typescriptconst userRepository = getRepository(User); const users = await userRepository.find(); - 5
Review Documentation
Check the TypeORM documentation for any updates regarding hot reloading and entity registration to ensure compliance with best practices.
markdownRefer to the official TypeORM documentation at https://typeorm.io.
Validation
Confirm that the error 'No repository for "<entity>" was found' no longer occurs and that entity queries return expected results. Additionally, verify that the application functions correctly after hot reloads without requiring a full restart.
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep