Skip to content

Conversation

@hongkongkiwi
Copy link

Summary

Add a native D1 driver for Sea-ORM that enables seamless integration with Cloudflare Workers D1 databases.

Key Features

  • D1Connector & D1Connection: Types wrapping worker::D1 binding
  • Full SQL execution: Support for execute, query_one, query_all
  • D1QueryExecutor trait: Entity query support (works around wasm-bindgen Send bound limitation)
  • D1Row & D1ExecResult: Result types for D1 query handling
  • TryGetable support: D1-specific type conversions

Example Usage

use worker::{Env, event};

#[event(fetch)]
async fn fetch(req: Request, env: Env, _ctx: Context) -> Result<Response> {
    let d1 = env.d1("DB")?;
    let db = sea_orm::Database::connect_d1(d1).await?;
    
    // Entity queries work!
    let cakes = cake::Entity::find().all(&db).await?;
    
    // Direct SQL also works
    db.execute(statement).await?;
    
    Ok(Response::ok("Hello")?)
}

Technical Notes

The implementation uses a custom D1QueryExecutor trait with impl Future return type to work around wasm-bindgen's requirement that futures be Send. This allows Entity queries without requiring the full ConnectionTrait implementation.

Files Changed

  • src/driver/d1.rs: Core D1 driver implementation
  • src/driver/mod.rs: Module exports
  • src/database/db_connection.rs: Connection type integration
  • src/database/mod.rs: connect_d1 API
  • src/executor/query.rs: TryGetable support for D1 types
  • examples/d1_example/: Complete working example

🤖 Generated with Claude Code (https://claude.com/claude-code)

Add a native D1 driver for Sea-ORM that enables seamless integration with
Cloudflare Workers D1 databases. This driver provides:

- D1Connector and D1Connection types wrapping worker::D1 binding
- Full SQL execution support (execute, query_one, query_all)
- D1QueryExecutor trait for Entity queries (works around wasm-bindgen Send bound)
- D1Row and D1ExecResult types for result handling
- TryGetable support for D1-specific types

Example usage:
    let db = sea_orm::Database::connect_d1(d1).await?;
    let cakes = cake::Entity::find().all(&db).await?;

Breaking change: None
Feature: D1 database driver for Cloudflare Workers

Co-Authored-By: Claude <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant