@@ -4,7 +4,7 @@ import { glob } from 'tinyglobby'
44import { join , resolve as resolveFs , relative } from 'pathe'
55import { defu } from 'defu'
66import { addServerImports , addTemplate , addServerPlugin , addTypeTemplate , getLayerDirectories , updateTemplates , logger , addServerHandler } from '@nuxt/kit'
7- import { resolve , resolvePath , logWhenReady } from '../utils'
7+ import { resolve , resolvePath , logWhenReady , addWranglerBinding } from '../utils'
88import { copyDatabaseMigrationsToHubDir , copyDatabaseQueriesToHubDir , copyDatabaseAssets , applyBuildTimeMigrations , getDatabaseSchemaPathMetadata , buildDatabaseSchema } from './lib'
99import { cloudflareHooks } from '../hosting/cloudflare'
1010
@@ -61,6 +61,11 @@ export async function resolveDatabaseConfig(nuxt: Nuxt, hub: HubConfig): Promise
6161 break
6262 }
6363 case 'postgresql' : {
64+ // Cloudflare Hyperdrive with explicit hyperdriveId
65+ if ( hub . hosting . includes ( 'cloudflare' ) && config . connection ?. hyperdriveId && ! config . driver ) {
66+ config . driver = 'postgres-js'
67+ break
68+ }
6469 config . connection = defu ( config . connection , { url : process . env . POSTGRES_URL || process . env . POSTGRESQL_URL || process . env . DATABASE_URL || '' } )
6570 if ( config . driver && [ 'neon-http' , 'postgres-js' ] . includes ( config . driver ) && ! config . connection . url ) {
6671 throw new Error ( `\`${ config . driver } \` driver requires \`DATABASE_URL\`, \`POSTGRES_URL\`, or \`POSTGRESQL_URL\` environment variable` )
@@ -76,6 +81,11 @@ export async function resolveDatabaseConfig(nuxt: Nuxt, hub: HubConfig): Promise
7681 break
7782 }
7883 case 'mysql' : {
84+ // Cloudflare Hyperdrive with explicit hyperdriveId
85+ if ( hub . hosting . includes ( 'cloudflare' ) && config . connection ?. hyperdriveId && ! config . driver ) {
86+ config . driver = 'mysql2'
87+ break
88+ }
7989 config . driver ||= 'mysql2'
8090 config . connection = defu ( config . connection , { uri : process . env . MYSQL_URL || process . env . DATABASE_URL || '' } )
8191 if ( ! config . connection . uri ) {
@@ -97,10 +107,18 @@ export async function setupDatabase(nuxt: Nuxt, hub: HubConfig, deps: Record<str
97107 hub . db = await resolveDatabaseConfig ( nuxt , hub )
98108 if ( ! hub . db ) return
99109
100- const { dialect, driver, migrationsDirs, queriesPaths } = hub . db as ResolvedDatabaseConfig
110+ const { dialect, driver, connection , migrationsDirs, queriesPaths } = hub . db as ResolvedDatabaseConfig
101111
102112 logWhenReady ( nuxt , `\`hub:db\` using \`${ dialect } \` database with \`${ driver } \` driver` , 'info' )
103113
114+ if ( driver === 'd1' && connection ?. databaseId ) {
115+ addWranglerBinding ( nuxt , 'd1_databases' , { binding : 'DB' , database_id : connection . databaseId } )
116+ }
117+ if ( [ 'postgres-js' , 'mysql2' ] . includes ( driver ) && connection ?. hyperdriveId ) {
118+ const binding = driver === 'postgres-js' ? 'POSTGRES' : 'MYSQL'
119+ addWranglerBinding ( nuxt , 'hyperdrive' , { binding, id : connection . hyperdriveId } )
120+ }
121+
104122 // Verify development database dependencies are installed
105123 if ( ! deps [ 'drizzle-orm' ] || ! deps [ 'drizzle-kit' ] ) {
106124 logWhenReady ( nuxt , 'Please run `npx nypm i drizzle-orm drizzle-kit` to properly setup Drizzle ORM with NuxtHub.' , 'error' )
0 commit comments