Zero-config Vite plugin for SolidStart with Cloudflare D1 and Drizzle ORM.
- Zero Configuration: Just add the plugin to your
app.config.tsand you're ready to go - Automatic Middleware Generation: Generates Cloudflare environment initialization middleware
- Auto-detect Local D1 Database: Automatically finds and configures local D1 database paths
- Unified Environment: Same code works in both development and production
- Type Safety: Full TypeScript support with @cloudflare/workers-types
pnpm add -D vite-plugin-solid-d1
pnpm add -D @cloudflare/workers-types
pnpm add drizzle-orm @libsql/client
pnpm add -D drizzle-kit wranglerimport { defineConfig } from "@solidjs/start/config";
import solidD1 from "vite-plugin-solid-d1";
export default defineConfig({
server: {
preset: "cloudflare-pages",
},
middleware: "src/middleware.d1.ts",
vite: {
plugins: [solidD1()],
},
});name = "my-app"
compatibility_date = "2025-02-24"
[[d1_databases]]
binding = "DB"
database_name = "my-database"
database_id = "your-database-id"import { defineConfig } from "drizzle-kit";
export default defineConfig(
process.env.LOCAL_DB_PATH
? {
// Local development (automatically configured by the plugin)
out: "./drizzle",
schema: "./src/db/schema.ts",
dialect: "sqlite",
dbCredentials: {
url: process.env.LOCAL_DB_PATH,
},
}
: {
// Remote environment
out: "./drizzle",
schema: "./src/db/schema.ts",
dialect: "sqlite",
driver: "d1-http",
dbCredentials: {
accountId: process.env.CLOUDFLARE_ACCOUNT_ID ?? "",
databaseId: process.env.CLOUDFLARE_D1_DATABASE_ID ?? "",
token: process.env.CLOUDFLARE_TOKEN ?? "",
},
},
);// src/db/schema.ts
import { int, sqliteTable, text } from "drizzle-orm/sqlite-core";
export const users = sqliteTable("users", {
id: int().primaryKey({ autoIncrement: true }),
name: text("name").notNull(),
email: text("email").notNull(),
});# Create D1 database
pnpm wrangler d1 create my-database
# Generate migration
pnpm drizzle-kit generate
# Apply migration locally
pnpm wrangler d1 migrations apply my-database --local
# Start development server
pnpm devOn first run, the plugin will automatically generate src/middleware.d1.ts.
import { action } from "@solidjs/router";
import { drizzle } from "drizzle-orm/d1";
import { getRequestEvent } from "solid-js/web";
import { users } from "~/db/schema";
const getUsers = action(async () => {
"use server";
const db = drizzle(
getRequestEvent()?.nativeEvent.context.cloudflare.env.DB
);
return await db.select().from(users);
});interface SolidD1PluginOptions {
/**
* Middleware file output path
* @default "src/middleware.d1.ts"
*/
middlewarePath?: string;
/**
* D1 binding name (defined in wrangler.toml)
* @default "DB"
*/
binding?: string;
/**
* Local D1 database search path
* @default ".wrangler/state/v3/d1"
*/
dbSearchPath?: string;
/**
* Automatically generate middleware file
* @default true
*/
autoGenerateMiddleware?: boolean;
}import solidD1 from "vite-plugin-solid-d1";
export default defineConfig({
vite: {
plugins: [
solidD1({
binding: "MY_DATABASE",
middlewarePath: "src/my-middleware.ts",
}),
],
},
});This plugin automates three key tasks:
Generates src/middleware.d1.ts with Cloudflare environment initialization:
import { createMiddleware } from "@solidjs/start/middleware";
export default createMiddleware({
onRequest: async (event) => {
if (process.env.NODE_ENV === "development") {
const { context } = event.nativeEvent;
if (!context?.cloudflare) {
const { getPlatformProxy } = await import("wrangler");
const cloudflare = await getPlatformProxy();
context.cloudflare = cloudflare;
}
}
},
});Automatically finds the local D1 SQLite file in .wrangler/state/v3/d1/miniflare-D1DatabaseObject/ and sets LOCAL_DB_PATH environment variable for Drizzle Kit.
Extends Vite's SSR configuration to properly bundle Wrangler in development mode.
Initialize your local database first:
pnpm wrangler d1 migrations create my-database init
pnpm wrangler d1 migrations apply my-database --localCheck your app.config.ts:
preset: "cloudflare-pages"is setmiddleware: "src/middleware.d1.ts"is configuredsrc/middleware.d1.tsfile exists
Ensure your wrangler.toml binding name matches the plugin's binding option (default: "DB").
# 1. Edit src/db/schema.ts
# 2. Generate migration
pnpm drizzle-kit generate
# 3. Apply to local DB
pnpm wrangler d1 migrations apply my-database --local
# 4. Test in dev server
pnpm dev# 1. Apply migration to production
pnpm wrangler d1 migrations apply my-database --remote
# 2. Build and deploy
pnpm build
pnpm wrangler pages deployThis plugin is based on the best practices from cytometa-entry-form, which successfully implements "same code for local and production" approach with SolidStart, Cloudflare D1, and Drizzle ORM.
MIT