mirror of
https://github.com/withastro/astro.git
synced 2025-03-24 23:21:57 -05:00
Make ASTRO_DATABASE_FILE work with file paths (#10631)
* Make ASTRO_DATABASE_FILE work with file paths * Use pathToFileURL
This commit is contained in:
parent
da2fb875fc
commit
157392ee44
4 changed files with 43 additions and 4 deletions
5
.changeset/tough-years-fry.md
Normal file
5
.changeset/tough-years-fry.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
"@astrojs/db": patch
|
||||
---
|
||||
|
||||
Make ASTRO_DATABASE_FILE work with file paths
|
|
@ -114,12 +114,11 @@ export function getLocalVirtualModContents({
|
|||
|
||||
const dbUrl = new URL(DB_PATH, root);
|
||||
return `
|
||||
import { asDrizzleTable, createLocalDatabaseClient } from ${RUNTIME_IMPORT};
|
||||
import { asDrizzleTable, createLocalDatabaseClient, normalizeDatabaseUrl } from ${RUNTIME_IMPORT};
|
||||
${shouldSeed ? `import { seedLocal } from ${RUNTIME_IMPORT};` : ''}
|
||||
${shouldSeed ? integrationSeedImportStatements.join('\n') : ''}
|
||||
|
||||
const dbUrl = import.meta.env.ASTRO_DATABASE_FILE ?? ${JSON.stringify(dbUrl)};
|
||||
|
||||
const dbUrl = normalizeDatabaseUrl(import.meta.env.ASTRO_DATABASE_FILE, ${JSON.stringify(dbUrl)});
|
||||
export const db = createLocalDatabaseClient({ dbUrl });
|
||||
|
||||
${
|
||||
|
|
|
@ -10,6 +10,7 @@ import {
|
|||
} from 'drizzle-orm/sqlite-core';
|
||||
import { type DBColumn, type DBTable } from '../core/types.js';
|
||||
import { type SerializedSQL, isSerializedSQL } from './types.js';
|
||||
import { pathToFileURL } from 'url';
|
||||
|
||||
export type { Table } from './types.js';
|
||||
export { createRemoteDatabaseClient, createLocalDatabaseClient } from './db-client.js';
|
||||
|
@ -130,3 +131,17 @@ function handleSerializedSQL<T>(def: T | SerializedSQL) {
|
|||
}
|
||||
return def;
|
||||
}
|
||||
|
||||
export function normalizeDatabaseUrl(envDbUrl: string | undefined, defaultDbUrl: string): string {
|
||||
if(envDbUrl) {
|
||||
// This could be a file URL, or more likely a root-relative file path.
|
||||
// Convert it to a file URL.
|
||||
if(envDbUrl.startsWith('file://')) {
|
||||
return envDbUrl;
|
||||
}
|
||||
return new URL(envDbUrl, pathToFileURL(process.cwd())).toString();
|
||||
} else {
|
||||
// This is going to be a file URL always,
|
||||
return defaultDbUrl;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import { expect } from 'chai';
|
||||
import testAdapter from '../../astro/test/test-adapter.js';
|
||||
import { loadFixture } from '../../astro/test/test-utils.js';
|
||||
import { fileURLToPath } from 'url';
|
||||
|
||||
describe('astro:db local database', () => {
|
||||
let fixture;
|
||||
|
@ -12,7 +13,7 @@ describe('astro:db local database', () => {
|
|||
});
|
||||
});
|
||||
|
||||
describe('build (not remote) with DATABASE_FILE env', () => {
|
||||
describe('build (not remote) with DATABASE_FILE env (file URL)', () => {
|
||||
const prodDbPath = new URL('./fixtures/basics/dist/astro.db', import.meta.url).toString();
|
||||
before(async () => {
|
||||
process.env.ASTRO_DATABASE_FILE = prodDbPath;
|
||||
|
@ -31,6 +32,25 @@ describe('astro:db local database', () => {
|
|||
});
|
||||
});
|
||||
|
||||
describe('build (not remote) with DATABASE_FILE env (file path)', () => {
|
||||
const prodDbPath = fileURLToPath(new URL('./fixtures/basics/dist/astro.db', import.meta.url));
|
||||
before(async () => {
|
||||
process.env.ASTRO_DATABASE_FILE = prodDbPath;
|
||||
await fixture.build();
|
||||
});
|
||||
|
||||
after(async () => {
|
||||
delete process.env.ASTRO_DATABASE_FILE;
|
||||
});
|
||||
|
||||
it('Can render page', async () => {
|
||||
const app = await fixture.loadTestAdapterApp();
|
||||
const request = new Request('http://example.com/');
|
||||
const response = await app.render(request);
|
||||
expect(response.status).to.equal(200);
|
||||
});
|
||||
});
|
||||
|
||||
describe('build (not remote)', () => {
|
||||
it('should throw during the build for server output', async () => {
|
||||
delete process.env.ASTRO_DATABASE_FILE;
|
||||
|
|
Loading…
Add table
Reference in a new issue