Skip to content

Drizzle Studio in Devtools points wrong DB in cloudflare-dev emulation #805

@nogic1008

Description

@nogic1008

Describe the bug

When using cloudflare-module preset with @nuxthub/core, the db instance provided by @nuxthub/db correctly references the local D1 database (sqlite file in /.wrangler/state/v3/d1/miniflare-D1DatabaseObject), but Drizzle Studio launched from Nuxt Devtools creates and references an empty database (.data/db/sqlite.db) instead.

Steps to reproduce

  1. Create a project from nuxt-hub/starter template
  2. Upgrade @nuxthub/core to 0.10.5
  3. Update nuxt.config.ts with the following:
    • Set nitro.preset to cloudflare-module
    • Set hub.db to { dialect: 'sqlite', driver: 'd1', connection: { databaseId: 'Your Database ID' } }
  4. Run nuxt dev
  5. Open Nuxt Devtools and go to the Database tab
  6. Click the Launch button
    • The migration table is not created (empty database is shown)

Expected behavior

Drizzle Studio should reference the same D1 database file that the application is using (the sqlite file in /.wrangler/state/v3/d1/miniflare-D1DatabaseObject), not create a new empty database.

Additional context

Root Cause:
Looking at https://github.com/nuxt-hub/core/blob/main/src/devtools.ts, when driver is set to d1, the db instance uses D1, but when launching Drizzle Studio, if dialect is sqlite, it uses libsql. Since connection.url is typically not specified when using driver: 'd1', Drizzle Studio is launched with the default value .data/db/sqlite.db, creating and referencing an empty sqlite file.

Proposed Fix:
In https://github.com/nuxt-hub/core/blob/main/src/devtools.ts, when driver is d1, automatically search for the sqlite file in /.wrangler/state/v3/d1/miniflare-D1DatabaseObject and set that path to the url. However, the possibility of wrangler specification changes or configuration-based storage location changes should be considered.

Workaround:
Setting hub.db.connection.url to the sqlite file auto-generated by wrangler can avoid this issue. However, since the filename is a random string and created when nuxt dev is executed, it cannot be configured in advance, and the setting cannot be committed as it's local-dependent.

Version:

  • @nuxthub/core: 0.10.5

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions