Skip to content

fix(db): treat files in db/schema/ beside db/schema.ts as schema files - #504

Merged
cpojer merged 1 commit into
voidzero-dev:mainfrom
ashunar0:fix/schema-dir-next-to-schema-file
Oct 20, 2026
Merged

cpojer merged 1 commit into
voidzero-dev:mainfrom
ashunar0:fix/schema-dir-next-to-schema-file

Conversation

@ashunar0

Copy link
Copy Markdown
Contributor

Refs #472

Same cause as #472, hit by the default output of void gen model. The #472 layout is not fixed here.

void gen model writes db/schema/<name>.ts, re-exports it from db/schema.ts, and the generated file imports sql from void/db.

When db/schema.ts exists, the plugin treats only that file as a schema file. db/schema/<name>.ts gets virtual:void-db, which imports the schema, so the import goes around: db/schema.ts → db/schema/<name>.ts → virtual:void-db → db/schema.ts. In dev, sql is not ready yet and every page that imports void/db returns 500 (TypeError: __vite_ssr_import_1__.sql is not a function). vp build → vp preview works.

To reproduce:

pnpm create void my-app   # React / D1
cd my-app
pnpm add zod
pnpm exec void gen model todos title:string done:boolean
pnpm exec void db generate
pnpm dev                  # / and /api/todos return 500

This PR also treats files under db/schema/ as schema files when db/schema.ts exists. The folder comes from paths.schemaDir, the same path gen model writes to.

Out of scope: the #472 layout (a file outside db/schema/ re-exported from db/schema.ts). Moving the drizzle-orm re-export above the schema import in virtual:void-db makes sql ready in dev for that case too (checked locally), but it makes the import order matter, so it is not in this PR.

Checked:

  • plugin-drizzle.test.ts (1 new test, fails without the fix)
  • typecheck for void
  • an app after gen model: dev (500 → 200) and vp build → vp preview (200 before and after)

14 tests in init.test.ts and 1 in nitro.test.ts fail locally on main too. This PR adds no new failures.

@cpojer cpojer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@cpojer
cpojer merged commit f638d12 into voidzero-dev:main Oct 20, 2026
2 checks passed
Brooooooklyn added a commit that referenced this pull request Oct 21, 2026
Fixes #472

## Problem

`db/schema.ts` re-exports a table file from outside `db/`, and that file
uses `sql` from `void/db` at the top level:

```
route ──▶ void/db ──▶ db/schema.ts ──▶ src/.../projects.schema.ts ──▶ void/db (still loading)
                                                                         sql = undefined ✗
```

In dev this throws `__vite_ssr_import_0__.sql is not a function`. #504
fixed the `db/schema/` folder. Files anywhere else still hit the error.

## Fix

Vite SSR runs imports in the order they are written. `void/db` now
re-exports the drizzle-orm helpers (`sql`, `eq`, …) **first**, before it
imports the schema. This applies to all three dialects (SQLite,
PostgreSQL, MySQL).

```
void/db
  1. export { sql, eq, ... } from drizzle-orm   ← ready now
  2. driver, env
  3. import * as schema ──▶ projects.schema.ts ──▶ void/db.sql ✓
```

- The fix works wherever the schema file lives. It needs no import-graph
tracking.
- `resolveId`, `virtual:void-db-schema` and `db/schema/**` are
unchanged.
- Build output already linked `sql` directly. It still works, and it
ships the same modules in a different order.
- Still not supported: reading `db` at the top level of a schema file.
It needs a request anyway.

## Tests

| Test | main | this PR |
|---|---|---|
| `plugin-drizzle.test.ts`: helper export comes first
(sqlite/postgresql/mysql) | ✗ | ✓ |
| `plugin-vite-environment.test.ts`: real dev server, #472 layout, route
imports `db` before `@schema` | ✗ `sql is not a function` | ✓ |

Both files pass (103/103). Lint, format and the `void` typecheck are
clean.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
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.

2 participants