diff --git a/packages/void/src/plugin-drizzle.ts b/packages/void/src/plugin-drizzle.ts index 63d44925b..2c5a9adf4 100644 --- a/packages/void/src/plugin-drizzle.ts +++ b/packages/void/src/plugin-drizzle.ts @@ -106,7 +106,11 @@ function createVoidDbModule( // but schema.path from Node's path.join uses backslashes on Windows. const normalizedImporter = normalizePath(importer); const normalizedSchemaPath = normalizePath(schema.path); - if (schema.type === 'file' && normalizedImporter === normalizedSchemaPath) { + if ( + schema.type === 'file' && + (normalizedImporter === normalizedSchemaPath || + normalizedImporter.startsWith(normalizePath(paths.schemaDir) + '/')) + ) { return VIRTUAL_SCHEMA_DB_ID; } if (schema.type === 'directory' && normalizedImporter.startsWith(normalizedSchemaPath)) { diff --git a/packages/void/test/unit/plugin-drizzle.test.ts b/packages/void/test/unit/plugin-drizzle.test.ts index fcb1bf4f9..053d12754 100644 --- a/packages/void/test/unit/plugin-drizzle.test.ts +++ b/packages/void/test/unit/plugin-drizzle.test.ts @@ -750,6 +750,24 @@ export const requestPoolOptions = (env) => expect(code).not.toContain('requireRuntimeBinding'); }); + it('treats files in db/schema/ beside db/schema.ts as schema files', () => { + // `void gen model` writes db/schema/.ts and re-exports it from db/schema.ts. + const root = createProjectRoot(); + const plugin = drizzlePlugin(root, false, 'sqlite'); + const resolveId = getHookHandler(plugin.resolveId) as ( + this: { environment: { name: string } }, + source: string, + importer: string | undefined, + options: { isEntry: boolean }, + ) => string | undefined; + const resolve = (importer: string) => + resolveId.call({ environment: { name: 'ssr' } }, 'void/db', importer, { isEntry: false }); + + expect(resolve(join(root, 'db', 'schema', 'todos.ts'))).toBe('\0virtual:void-db-schema'); + expect(resolve(join(root, 'db', 'schema-helpers.ts'))).toBe('\0virtual:void-db'); + expect(resolve(join(root, 'routes', 'api', 'todos', 'index.ts'))).toBe('\0virtual:void-db'); + }); + it('can virtualize void/db without a user schema when forced', () => { const root = createProjectRoot({ schema: false }); const plugin = drizzlePlugin(root, false, 'postgresql', { forceVirtualDb: true });