In the event that my MSSQL server experiences a crash and an app client makes a request to the API, the current behavior is for it to endlessly spin until Express times out the unanswered request.
By enabling logging in TypeORM, I am able to observe the execution of the relevant query.
Rather than allow the request to time out, I would prefer to return an API response notifying the client that the database is currently unreachable.
This snippet below showcases the code present in my user controller:
public static listAll = async (req: Request, res: Response) => {
const userRepository = getRepository(User);
const users = await userRepository
.find()
res.send(users);
};
I attempted to use `.catch` on the request being utilized in the corresponding controller, but unfortunately, this did not yield any changes. An example is provided below:
const users = await userRepository
.find()
.catch(error => {
console.log(error); // this is never triggered
});
No error message appeared in the console despite these efforts.
The following are the connection options specified in TypeORM:
createConnection({
type: 'mssql',
host: process.env.TYPEORM_HOST,
port: parseInt(process.env.TYPEORM_PORT, 0),
username: process.env.TYPEORM_USERNAME,
password: process.env.TYPEORM_PASSWORD,
database: process.env.TYPEORM_DATABASE,
synchronize: true,
logging: true,
entities: models,
cli: {
entitiesDir: 'src/entity',
migrationsDir: 'src/migration',
subscribersDir: 'src/subscriber',
},
})
.then(connection => {
const server = http.createServer(App);
server.listen({ port: `${process.env.APP_PORT}` }, () => {
console.log(`🚀 Server ready at http://${process.env.TYPEORM_HOST}:${process.env.APP_PORT}`);
});
if (connection.isConnected) {
console.log(`Database ${connection.options.database} connected`);
}
})
.catch(error => console.log(error));
I also experimented with setting `requestTimeout` in `createConnection` to 1000 milliseconds, with no change in outcome.