0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-30 22:03:56 -05:00

Fix db seeding when srcDir is root (#10720)

This commit is contained in:
Matthew Phillips 2024-04-09 09:11:32 -04:00 committed by GitHub
parent f508c4b7d5
commit 2d2ff58bb9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 109 additions and 1 deletions

View file

@ -0,0 +1,5 @@
---
"@astrojs/db": patch
---
Fix db seeding when srcDir is root

View file

@ -40,6 +40,7 @@ type VitePluginDBParams =
export function vitePluginDb(params: VitePluginDBParams): VitePlugin {
const srcDirPath = normalizePath(fileURLToPath(params.srcDir));
const dbDirPath = normalizePath(fileURLToPath(getDbDirectoryUrl(params.root)));
let command: 'build' | 'serve' = 'build';
return {
name: 'astro:db',
@ -54,7 +55,7 @@ export function vitePluginDb(params: VitePluginDBParams): VitePlugin {
const importer = rawImporter ? await this.resolve(rawImporter) : null;
if (!importer) return resolved.virtual;
if (importer.id.startsWith(srcDirPath)) {
if (importer.id.startsWith(srcDirPath) && !importer.id.startsWith(dbDirPath)) {
// Seed only if the importer is in the src directory.
// Otherwise, we may get recursive seed calls (ex. import from db/seed.ts).
return resolved.seedVirtual;

View file

@ -0,0 +1,37 @@
import { expect } from 'chai';
import { load as cheerioLoad } from 'cheerio';
import testAdapter from '../../astro/test/test-adapter.js';
import { loadFixture } from '../../astro/test/test-utils.js';
describe('astro:db', () => {
let fixture;
before(async () => {
fixture = await loadFixture({
root: new URL('./fixtures/db-in-src/', import.meta.url),
output: 'server',
srcDir: '.',
adapter: testAdapter(),
});
});
describe('development: db/ folder inside srcDir', () => {
let devServer;
before(async () => {
devServer = await fixture.startDevServer();
});
after(async () => {
await devServer.stop();
});
it('Prints the list of authors', async () => {
const html = await fixture.fetch('/').then((res) => res.text());
const $ = cheerioLoad(html);
const ul = $('.users-list');
expect(ul.children()).to.have.a.lengthOf(1);
expect($('.users-list li').text()).to.equal('Mario');
});
});
});

View file

@ -0,0 +1,10 @@
import db from '@astrojs/db';
import { defineConfig } from 'astro/config';
// https://astro.build/config
export default defineConfig({
integrations: [db()],
devToolbar: {
enabled: false,
},
});

View file

@ -0,0 +1,13 @@
import { column, defineDb, defineTable } from 'astro:db';
const User = defineTable({
columns: {
id: column.text({ primaryKey: true, optional: false }),
username: column.text({ optional: false, unique: true }),
password: column.text({ optional: false }),
},
});
export default defineDb({
tables: { User },
});

View file

@ -0,0 +1,8 @@
import { asDrizzleTable } from '@astrojs/db/utils';
import { User, db } from 'astro:db';
export default async function () {
await db.batch([
db.insert(User).values([{ id: 'mario', username: 'Mario', password: 'itsame' }]),
]);
}

View file

@ -0,0 +1,14 @@
{
"name": "@test/db-db-in-src",
"version": "0.0.0",
"private": true,
"scripts": {
"dev": "astro dev",
"build": "astro build",
"preview": "astro preview"
},
"dependencies": {
"@astrojs/db": "workspace:*",
"astro": "workspace:*"
}
}

View file

@ -0,0 +1,11 @@
---
/// <reference path="../.astro/db-types.d.ts" />
import { db, User } from 'astro:db';
const users = await db.select().from(User);
---
<h2>Users</h2>
<ul class="users-list">
{users.map((user) => <li>{user.username}</li>)}
</ul>

View file

@ -3933,6 +3933,15 @@ importers:
specifier: workspace:*
version: link:../../../../astro
packages/db/test/fixtures/db-in-src:
dependencies:
'@astrojs/db':
specifier: workspace:*
version: link:../../..
astro:
specifier: workspace:*
version: link:../../../../astro
packages/db/test/fixtures/error-handling:
dependencies:
'@astrojs/db':