Skip to content

Conversation

zpg6
Copy link

@zpg6 zpg6 commented Aug 25, 2025

For cases where you want to use Drizzle with a non-bound D1 instance - like D1 multi-tenancy within Cloudflare or even if you are outside of Cloudflare Workers.

Goal:

function createTenantDb(tenantDatabaseId: string) {
	return drizzle({
		accountId: 'your-cloudflare-account-id',
		databaseId: tenantDatabaseId,
		token: 'your-cloudflare-api-token',
	});
}

@zpg6
Copy link
Author

zpg6 commented Aug 25, 2025

For testing the D1-HTTP driver:

npm install @zpg6-test-pkgs/[email protected]

Usage:

import { drizzle } from '@zpg6-test-pkgs/drizzle-orm/d1-http';
import { sql } from '@zpg6-test-pkgs/drizzle-orm/sql';

const db = drizzle({
  accountId: 'your-account-id',
  databaseId: 'your-database-id', 
  token: 'your-api-token'
});

// Execute raw SQL
const result = await db.run(sql`SELECT * FROM users`);

// Use with Drizzle schema
const users = await db.select().from(usersTable);

@zpg6
Copy link
Author

zpg6 commented Aug 25, 2025

Benchmarking Binding driver vs HTTP driver from within a Cloudflare Worker

The HTTP driver allows you to use Drizzle ORM with D1 outside of Cloudflare Workers (e.g., from CI/CD, other serverless platforms, or local scripts). This benchmark specifically focuses on the latency differences within Cloudflare Workers between using the HTTP driver versus direct bindings. This is particularly relevant for multitenancy scenarios where a single worker may need to access multiple D1 databases that aren't bound to the worker, requiring the HTTP driver even within the Cloudflare environment.

View the Code



image

@zpg6 zpg6 marked this pull request as ready for review August 25, 2025 21:02
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