MySQL-style connector for Cloudflare D1 using WebSocket
Project description
🌐 d1-client
d1-client is a lightweight Python client designed to interface with your Cloudflare D1 database through a WebSocket Worker.
It replicates the familiar mysql.connector pattern, without needing to use async/await.
🔗 Worker code: github.com/geetflow/d1_connector
🚀 Features
- ✅ 100% compatible with
mysql.connector-style usage - 🔌 Simple
connect → cursor → execute → fetchpattern - 🧠 No need to manage
asyncioorawait - 🔄 Automatic cleanup of cursors and connections
- 🧵 Internally managed shared event loop
- 🌍 WebSocket-based D1 access via a custom Worker
📦 Installation
pip install d1-client
Or install directly from source:
git clone https://github.com/geetflow/d1_connector
cd d1_connector
pip install -e .
✨ Usage Example
from d1_client import connect
conn = connect(
host="d1_connector.your-worker.workers.dev",
username="root",
password="yourpass",
database="your_db"
)
cursor = conn.cursor()
cursor.execute("SELECT * FROM tracks LIMIT 1")
rows = cursor.fetchall()
print("Query Results:", rows)
# No need to call cursor.close() or conn.close()
🔧 How It Works
The client communicates with a WebSocket-enabled Cloudflare Worker that acts like a SQL gateway. It sends queries as JSON, and the Worker executes them using Cloudflare D1 and returns results.
Client sends:
{
"sql": "SELECT * FROM users WHERE id = ?",
"params": [123]
}
Worker responds:
{
"result": {
"results": [ { "id": 123, "name": "Alice" } ]
}
}
Worker source code: https://github.com/geetflow/d1_connector
🧪 API Reference
connect(host, username, password, database)
Returns a D1Connection object.
D1Connection.cursor()
Returns a D1Cursor object.
D1Cursor.execute(query, params=None)
Executes an SQL query with optional parameters.
D1Cursor.fetchall()
Returns a list of rows (each row is a dictionary).
D1Cursor.fetchone()
Returns a single row (or None if empty).
📂 Project Structure
d1-client/
├── d1_client/
│ ├── __init__.py
│ └── core.py
├── setup.py
├── LICENSE
└── README.md
📜 License
MIT License © GeetFlow
🔗 Links
- 🌍 Worker Source Code: github.com/geetflow/d1_connector
- 📦 PyPI (coming soon)
💬 Need Help?
If you're facing issues with query formats, D1 errors, or WebSocket connections:
- Double check that your Worker is deployed and working
- Ensure the
host,username, andpasswordmatch your Cloudflare Worker setup
⭐ Example Query (with D1 compatibility)
query = """
SELECT
t.id, t.name, t.src, t.image, t.share_id, ts.stream_seconds
FROM tracks t
LEFT JOIN tracks_stream ts ON t.id = ts.track_id
WHERE date(t.created_at) >= date('now', '-10 days')
AND t.releases = 1
ORDER BY ts.stream_seconds DESC, t.plays DESC
LIMIT 50
"""
cursor.execute(query)
print(cursor.fetchall())
💡 Note: Cloudflare D1 uses SQLite syntax. Replace MySQL’s NOW() - INTERVAL with date('now', '-10 days')
Made with ❤️ by GeetFlow
Would you like me to also generate setup.py, LICENSE, and directory structure in real files so you can publish to PyPI?
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file d1_connector-0.1.0.tar.gz.
File metadata
- Download URL: d1_connector-0.1.0.tar.gz
- Upload date:
- Size: 4.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5105b66d9978e28898902586788704d2b94080edd04862fe1234a1b65b5a722d
|
|
| MD5 |
4bede657e93723fdb52681d3db0acb48
|
|
| BLAKE2b-256 |
5dc7c03efcb4af9dff2229419ce7d3a2ae62f3876b89ed420605b38ccfdf669f
|
File details
Details for the file d1_connector-0.1.0-py3-none-any.whl.
File metadata
- Download URL: d1_connector-0.1.0-py3-none-any.whl
- Upload date:
- Size: 4.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
46b5095915ef5c187c9d203f832896ad74c97fb4e4224f661fe3b75adcc99bb0
|
|
| MD5 |
6f7f52d679153c1a7468f4f3e50a028f
|
|
| BLAKE2b-256 |
416a130e5687886f979c8e1c5a9380a897e6e61e887c7d099f1eb8f962df9074
|