Load data from databases to dataframes, the fastest way.
86
Quality
Pending
Does it follow best practices?
Impact
86%
1.04xAverage score across 10 eval scenarios
Helper utilities for building and managing database connection strings across different database backends. ConnectorX provides the ConnectionUrl class to simplify connection string construction and ensure proper formatting for various database systems.
Generic connection URL builder that supports multiple database backends with type safety.
class ConnectionUrl(Generic[_BackendT], str):
"""
Helper class to build database connection string URLs.
Inherits from str, so can be used anywhere a connection string is expected.
"""
# SQLite constructor
def __new__(
cls,
*,
backend: Literal["sqlite"],
db_path: str | Path,
) -> ConnectionUrl[Literal["sqlite"]]:
"""Build SQLite connection URL."""
# BigQuery constructor
def __new__(
cls,
*,
backend: Literal["bigquery"],
db_path: str | Path,
) -> ConnectionUrl[Literal["bigquery"]]:
"""Build BigQuery connection URL."""
# Server-based database constructor
def __new__(
cls,
*,
backend: _ServerBackendT,
username: str,
password: str = "",
server: str,
port: int,
database: str = "",
database_options: dict[str, str] | None = None,
) -> ConnectionUrl[_ServerBackendT]:
"""Build server-based database connection URL."""
# Raw connection string constructor
def __new__(
cls,
raw_connection: str,
) -> ConnectionUrl:
"""Build connection from raw connection string."""_ServerBackendT = TypeVar(
"_ServerBackendT",
bound=Literal[
"redshift",
"clickhouse",
"postgres",
"postgresql",
"mysql",
"mssql",
"oracle",
"duckdb",
],
)from connectorx import ConnectionUrl
from pathlib import Path
# Using string path
sqlite_url = ConnectionUrl(
backend="sqlite",
db_path="/path/to/database.db"
)
# Using Path object
db_path = Path("./data/sample.sqlite")
sqlite_url = ConnectionUrl(
backend="sqlite",
db_path=db_path
)
# Result: "sqlite:///path/to/database.db"# BigQuery with service account key file
bq_url = ConnectionUrl(
backend="bigquery",
db_path="/path/to/service-account-key.json"
)
# Result: "bigquery:///path/to/service-account-key.json"# Basic PostgreSQL connection
postgres_url = ConnectionUrl(
backend="postgresql",
username="myuser",
password="mypassword",
server="localhost",
port=5432,
database="mydb"
)
# With connection options
postgres_url = ConnectionUrl(
backend="postgresql",
username="myuser",
password="mypassword",
server="postgres.example.com",
port=5432,
database="production_db",
database_options={
"sslmode": "require",
"application_name": "connectorx_app"
}
)
# Result: "postgresql://myuser:mypassword@postgres.example.com:5432/production_db?sslmode=require&application_name=connectorx_app"mysql_url = ConnectionUrl(
backend="mysql",
username="root",
password="secret",
server="mysql.example.com",
port=3306,
database="analytics"
)
# Result: "mysql://root:secret@mysql.example.com:3306/analytics"mssql_url = ConnectionUrl(
backend="mssql",
username="sa",
password="Password123!",
server="sqlserver.example.com",
port=1433,
database="AdventureWorks",
database_options={
"TrustServerCertificate": "true",
"Encrypt": "false"
}
)oracle_url = ConnectionUrl(
backend="oracle",
username="hr",
password="oracle",
server="oracle.example.com",
port=1521,
database="ORCL"
)# Use existing connection string
raw_conn = "postgresql://user:pass@host:port/db?sslmode=require"
conn_url = ConnectionUrl(raw_conn)
# ConnectionUrl inherits from str, so it works everywhere
import connectorx as cx
df = cx.read_sql(conn_url, "SELECT * FROM table")ConnectorX automatically rewrites certain connection strings for compatibility:
# Redshift connections are rewritten to use PostgreSQL driver
redshift_url = ConnectionUrl(
backend="redshift",
username="user",
password="pass",
server="cluster.redshift.amazonaws.com",
port=5439,
database="dev"
)
# Internally rewritten to: postgresql://user:pass@cluster.redshift.amazonaws.com:5439/dev
# Protocol automatically set to "cursor"
# ClickHouse connections are rewritten to use MySQL driver
clickhouse_url = ConnectionUrl(
backend="clickhouse",
username="default",
password="",
server="clickhouse.example.com",
port=9000,
database="default"
)
# Internally rewritten to: mysql://default:@clickhouse.example.com:9000/default
# Protocol automatically set to "text"ConnectionUrl provides type safety for different backends:
from typing import Literal
def connect_to_postgres(url: ConnectionUrl[Literal["postgresql"]]) -> None:
# Function specifically expects PostgreSQL connection
pass
postgres_url = ConnectionUrl(backend="postgresql", ...) # Type: ConnectionUrl[Literal["postgresql"]]
mysql_url = ConnectionUrl(backend="mysql", ...) # Type: ConnectionUrl[Literal["mysql"]]
connect_to_postgres(postgres_url) # ✓ Type check passes
connect_to_postgres(mysql_url) # ✗ Type errorConnectionUrl objects work seamlessly with all ConnectorX functions:
from connectorx import ConnectionUrl, read_sql, get_meta, partition_sql
# Build connection
conn = ConnectionUrl(
backend="postgresql",
username="user",
password="pass",
server="localhost",
port=5432,
database="mydb"
)
# Use with all ConnectorX functions
df = read_sql(conn, "SELECT * FROM table")
meta = get_meta(conn, "SELECT * FROM table")
queries = partition_sql(conn, "SELECT * FROM table", "id", 4)postgresql:// - PostgreSQL databasesmysql:// - MySQL and MariaDB databasessqlite:// - SQLite database filesmssql:// - Microsoft SQL Serveroracle:// - Oracle databasesbigquery:// - Google BigQueryredshift:// - Amazon Redshift (rewritten to PostgreSQL)clickhouse:// - ClickHouse (rewritten to MySQL)ConnectionUrl automatically handles URL encoding for special characters:
# Password with special characters
conn = ConnectionUrl(
backend="postgresql",
username="user",
password="p@ssw0rd!#$", # Special characters automatically encoded
server="localhost",
port=5432,
database="mydb"
)# Local development
local_pg = ConnectionUrl(
backend="postgresql",
username="postgres",
password="postgres",
server="localhost",
port=5432,
database="dev_db"
)
# Production with SSL
prod_pg = ConnectionUrl(
backend="postgresql",
username="app_user",
password="secure_password",
server="prod-db.company.com",
port=5432,
database="production",
database_options={"sslmode": "require"}
)
# Read-only replica
replica_pg = ConnectionUrl(
backend="postgresql",
username="readonly_user",
password="readonly_pass",
server="replica.company.com",
port=5432,
database="production",
database_options={
"sslmode": "require",
"default_transaction_read_only": "on"
}
)Install with Tessl CLI
npx tessl i tessl/pypi-connectorxdocs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10