Connection awareness

Viewed 22

Is there a way to be aware of connection status with Prisma?

I understand it does everything automatically so I don't have to worry about anything related to connection.
But what if I want to?

Basically I miss two things:

  • Catchable event. Either client.on('disconnected', ...) or implementing an interface (e.g. onDisconnected() { ... })
  • $connect() throwing error if it can not connect. No exception raised when DB is not started and I start the application.
// Context:
//   - DB not started yet

try {
  await client.$connect();
  console.log('DB connected');
} catch (e) {
  console.log('DB is unavailable');
}

// Output
//
// > DB connected

My use case: I would like to send a system message to maintainers and shut the whole service down if the DB stopped and could not recover connection within a time frame.

1 Answers

Okay, I was able to overcome it. I guess it's rather a bug than a feature.

So $connect() does not throw error it could connect successfully before but the DB has been stopped meanwhile and $disconnect() was not called.

So, calling $disconnect() when lost connection is recognized resulted in $connect() throwing error if still not able to connect.

I still miss the ability to watch for connection events but this is enough for me now.

Related