Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/void/src/plugin-drizzle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
18 changes: 18 additions & 0 deletions packages/void/test/unit/plugin-drizzle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/<name>.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 });
Expand Down