Skip to content

Commit

Permalink
fix: astrodb vite plugin throws in vitest and cannot seed database (#…
Browse files Browse the repository at this point in the history
…11435)

* fix: ASTRO_DATABASE_FILE is undefined in virtual module

* changeset

* handle undefined

* Update .changeset/lemon-beans-admire.md

Co-authored-by: Emanuele Stoppa <my.burning@gmail.com>

* use JSON.stringify

---------

Co-authored-by: Emanuele Stoppa <my.burning@gmail.com>
  • Loading branch information
haivuw and ematipico authored Oct 21, 2024
1 parent ff522b9 commit f32a7a8
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/lemon-beans-admire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/db': patch
---

Fixes a bug where `astro:db:seed` couldn't access to the environment variable `ASTRO_DATABASE_FILE`
3 changes: 2 additions & 1 deletion packages/db/src/core/integration/vite-plugin-db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,13 @@ export function getLocalVirtualModContents({
tables: DBTables;
root: URL;
}) {
const { ASTRO_DATABASE_FILE } = getAstroEnv();
const dbInfo = getRemoteDatabaseInfo();
const dbUrl = new URL(DB_PATH, root);
return `
import { asDrizzleTable, createLocalDatabaseClient, normalizeDatabaseUrl } from ${RUNTIME_IMPORT};
const dbUrl = normalizeDatabaseUrl(import.meta.env.ASTRO_DATABASE_FILE, ${JSON.stringify(dbUrl)});
const dbUrl = normalizeDatabaseUrl(${JSON.stringify(ASTRO_DATABASE_FILE)}, ${JSON.stringify(dbUrl)});
export const db = createLocalDatabaseClient({ dbUrl, enableTransactions: ${dbInfo.url === 'libsql'} });
export * from ${RUNTIME_VIRTUAL_IMPORT};
Expand Down

0 comments on commit f32a7a8

Please sign in to comment.