Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ features = [
"postgres-array",
"postgres-vector",
"sea-orm-internal",
"d1",
]
rustdoc-args = ["--cfg", "docsrs"]

Expand Down Expand Up @@ -85,6 +86,7 @@ tracing = { version = "0.1", default-features = false, features = [
] }
url = { version = "2.2", default-features = false }
uuid = { version = "1", default-features = false, optional = true }
worker = { version = "0.7", default-features = false, optional = true }

[dev-dependencies]
dotenv = "0.15"
Expand All @@ -105,6 +107,7 @@ tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }
uuid = { version = "1", features = ["v4"] }

[features]
d1 = ["worker/d1"]
debug-print = []
default = [
"macros",
Expand Down
32 changes: 32 additions & 0 deletions examples/d1_example/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[package]
authors = ["Sea ORM Contributors"]
edition = "2024"
name = "sea-orm-d1-example"
publish = false
rust-version = "1.85.0"
version = "0.1.0"

[workspace]

[package.metadata.release]
release = false

# https://github.com/rustwasm/wasm-pack/issues/1247
[package.metadata.wasm-pack.profile.release]
wasm-opt = false

[lib]
crate-type = ["cdylib"]
path = "src/lib.rs"

[dependencies]
serde = { version = "1", features = ["derive"] }
serde_json = "1"

worker = { version = "0.7", features = ["d1"] }

sea-orm = { path = "../../", default-features = false, features = [
"d1",
"with-json",
"macros",
] }
97 changes: 97 additions & 0 deletions examples/d1_example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# Sea-ORM D1 Example

This example demonstrates how to use Sea-ORM with Cloudflare D1.

## Prerequisites

- [Rust](https://rustup.rs/) installed
- [wasm-pack](https://rustwasm.github.io/wasm-pack/installer/) installed
- [Wrangler](https://developers.cloudflare.com/workers/cli-wrangler/install-update/) CLI installed

## Setup

### 1. Create a D1 Database

Create a D1 database in your Cloudflare Workers project:

```bash
wrangler d1 create sea-orm-d1-example
```

### 2. Configure wrangler.toml

Add the D1 binding to your `wrangler.toml`:

```toml
name = "sea-orm-d1-example"
compatibility_date = "2025-01-01"

[[d1_databases]]
binding = "DB"
database_name = "sea-orm-d1-example"
database_id = "your-database-id"
```

### 3. Create the Schema

Create a `schema.sql` file:

```sql
CREATE TABLE IF NOT EXISTS cake (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL
);
```

### 4. Initialize the Database

Run the migrations:

```bash
wrangler d1 execute sea-orm-d1-example --file=./schema.sql --remote
```

## Development

### Build

```bash
wasm-pack build --target web --out-dir ./dist
```

### Deploy

```bash
wrangler deploy
```

## API Endpoints

- `GET /cakes` - List all cakes
- `POST /cakes` - Create a new cake (`{"name": "Chocolate"}`)
- `GET /cakes/:id` - Get a cake by ID
- `DELETE /cakes/:id` - Delete a cake

## Example Usage

```bash
# List all cakes
curl https://your-worker.dev/cakes

# Create a cake
curl -X POST https://your-worker.dev/cakes \
-H "Content-Type: application/json" \
-d '{"name": "Chocolate Cake"}'

# Get a cake
curl https://your-worker.dev/cakes/1

# Delete a cake
curl -X DELETE https://your-worker.dev/cakes/1
```

## Notes

- D1 uses SQLite-compatible SQL syntax
- D1 connections require direct access via `as_d1_connection()` because `wasm-bindgen` futures are not `Send`
- Streaming is not supported for D1; use `query_all()` instead of `stream_raw()`
21 changes: 21 additions & 0 deletions examples/d1_example/src/cake.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//! Cake entity for D1 example

use sea_orm::entity::prelude::*;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name = "cake")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
#[sea_orm(column_name = "name")]
pub name: String,
#[sea_orm(column_name = "price")]
pub price: Option<f64>,
#[sea_orm(column_name = "category")]
pub category: Option<String>,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}

impl ActiveModelBehavior for ActiveModel {}
Loading