diff --git a/src/db/setup.ts b/src/db/setup.ts index 87ffd8316..3d410deae 100644 --- a/src/db/setup.ts +++ b/src/db/setup.ts @@ -74,8 +74,8 @@ export async function resolveDatabaseConfig(nuxt: Nuxt, hub: HubConfig): Promise } break } - // Cloudflare D1 (production only - dev uses local libsql) - if (hub.hosting.includes('cloudflare') && !nuxt.options.dev) { + // Cloudflare D1 (production only - dev/prepare uses local libsql) + if (hub.hosting.includes('cloudflare') && !nuxt.options.dev && !nuxt.options._prepare) { config.driver = 'd1' break } diff --git a/test/database.config.test.ts b/test/database.config.test.ts index 23c064557..37aeb9c51 100644 --- a/test/database.config.test.ts +++ b/test/database.config.test.ts @@ -18,10 +18,12 @@ describe('resolveDatabaseConfig', () => { process.env = originalEnv }) - const createMockNuxt = (layers: any[] = []) => ({ + const createMockNuxt = (layers: any[] = [], opts: { dev?: boolean, _prepare?: boolean } = {}) => ({ options: { rootDir: '/', srcDir: '/app/', + dev: opts.dev ?? false, + _prepare: opts._prepare ?? false, _layers: layers } }) as any @@ -104,8 +106,8 @@ describe('resolveDatabaseConfig', () => { }) }) - it('should use D1 driver when hosting is cloudflare', async () => { - const nuxt = createMockNuxt() + it('should use D1 driver when hosting is cloudflare (production mode)', async () => { + const nuxt = createMockNuxt([], { dev: false, _prepare: false }) const hub = createBaseHubConfig('sqlite') hub.hosting = 'cloudflare' @@ -118,6 +120,34 @@ describe('resolveDatabaseConfig', () => { }) }) + it('should use libsql when hosting is cloudflare in dev mode', async () => { + const nuxt = createMockNuxt([], { dev: true }) + const hub = createBaseHubConfig('sqlite') + hub.hosting = 'cloudflare' + + const result = await resolveDatabaseConfig(nuxt, hub) + + expect(result).toMatchObject({ + dialect: 'sqlite', + driver: 'libsql', + connection: { url: 'file:/tmp/test-hub/db/sqlite.db' } + }) + }) + + it('should use libsql when hosting is cloudflare in prepare mode', async () => { + const nuxt = createMockNuxt([], { dev: false, _prepare: true }) + const hub = createBaseHubConfig('sqlite') + hub.hosting = 'cloudflare' + + const result = await resolveDatabaseConfig(nuxt, hub) + + expect(result).toMatchObject({ + dialect: 'sqlite', + driver: 'libsql', + connection: { url: 'file:/tmp/test-hub/db/sqlite.db' } + }) + }) + it('should preserve custom driver when specified', async () => { const nuxt = createMockNuxt() const hub = createBaseHubConfig({