connect method

Future<void> connect()

Establishes a connection to the PostgreSQL database.

Uses the connection details provided in the config object. Logs the connection attempt and success or failure.

{@tool example}

final config = SupabaseGenConfig(/*...*/);
final reader = SchemaReader(config);
try {
  await reader.connect();
  // Connection successful
} catch (e) {
  print('Failed to connect: $e');
}

{@end-tool}

@return A Future that completes when the connection is established. @throws Exception if the connection fails (e.g., network error, authentication failure).

Implementation

Future<void> connect() async {
  _logger.info(
    'Connecting to database: ${config.username}@${config.host}:${config.port}/${config.database}',
  );

  try {
    _connection = await Connection.open(
      Endpoint(
        host: config.host,
        port: config.port,
        database: config.database,
        username: config.username,
        password: config.password,
      ),
      // Note: SSL mode is set to disable. This might need adjustment (e.g., to `require`)
      // for secure connections, especially to cloud databases.
      settings: ConnectionSettings(sslMode: SslMode.disable),
    );
    _logger.info('Connected successfully');
  } catch (e) {
    _logger.severe('Connection failed: $e');
    // Rethrow the exception so the caller can handle connection failures.
    rethrow;
  }
}