getDatabaseConnection method

Future<PostgreSQLConnection> getDatabaseConnection()

Retrieves a connection to the database this instance connects to.

If no connection exists, one will be created. A store will have no more than one connection at a time.

When executing queries, prefer to use executionContext instead. Failure to do so might result in issues when executing queries during a transaction.


Future<PostgreSQLConnection> getDatabaseConnection() async {
  if (_databaseConnection == null || _databaseConnection!.isClosed) {
    if (_pendingConnectionCompleter == null) {
      _pendingConnectionCompleter = Completer<PostgreSQLConnection>();

      // ignore: unawaited_futures
      _connect().timeout(connectTimeout).then((conn) {
        _databaseConnection = conn;
        _pendingConnectionCompleter = null;
      }).catchError((e) {
            "unable to connect to database",
            underlyingException: e));
        _pendingConnectionCompleter = null;

    return _pendingConnectionCompleter!.future;

  return _databaseConnection!;