Skip to content

feat: Add native Cloudflare D1 database driver support #2928

@hongkongkiwi

Description

@hongkongkiwi

Summary

This issue tracks the implementation of 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.

Related PR

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions