diff --git a/.changeset/spotty-hounds-ring.md b/.changeset/spotty-hounds-ring.md new file mode 100644 index 000000000000..b423a2ade3d4 --- /dev/null +++ b/.changeset/spotty-hounds-ring.md @@ -0,0 +1,5 @@ +--- +"@astrojs/db": patch +--- + +Fix db seeding when srcDir is root diff --git a/packages/db/src/core/integration/vite-plugin-db.ts b/packages/db/src/core/integration/vite-plugin-db.ts index f1dc3ea7b015..df31d527673b 100644 --- a/packages/db/src/core/integration/vite-plugin-db.ts +++ b/packages/db/src/core/integration/vite-plugin-db.ts @@ -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; diff --git a/packages/db/test/db-in-src.test.js b/packages/db/test/db-in-src.test.js new file mode 100644 index 000000000000..460438001f32 --- /dev/null +++ b/packages/db/test/db-in-src.test.js @@ -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'); + }); + }); +}); diff --git a/packages/db/test/fixtures/db-in-src/astro.config.ts b/packages/db/test/fixtures/db-in-src/astro.config.ts new file mode 100644 index 000000000000..983a6947d115 --- /dev/null +++ b/packages/db/test/fixtures/db-in-src/astro.config.ts @@ -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, + }, +}); diff --git a/packages/db/test/fixtures/db-in-src/db/config.ts b/packages/db/test/fixtures/db-in-src/db/config.ts new file mode 100644 index 000000000000..44c15abe7567 --- /dev/null +++ b/packages/db/test/fixtures/db-in-src/db/config.ts @@ -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 }, +}); diff --git a/packages/db/test/fixtures/db-in-src/db/seed.ts b/packages/db/test/fixtures/db-in-src/db/seed.ts new file mode 100644 index 000000000000..7ff9f5f30dc8 --- /dev/null +++ b/packages/db/test/fixtures/db-in-src/db/seed.ts @@ -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' }]), + ]); +} diff --git a/packages/db/test/fixtures/db-in-src/package.json b/packages/db/test/fixtures/db-in-src/package.json new file mode 100644 index 000000000000..a1580d1cb1cf --- /dev/null +++ b/packages/db/test/fixtures/db-in-src/package.json @@ -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:*" + } +} diff --git a/packages/db/test/fixtures/db-in-src/pages/index.astro b/packages/db/test/fixtures/db-in-src/pages/index.astro new file mode 100644 index 000000000000..90f6381150e5 --- /dev/null +++ b/packages/db/test/fixtures/db-in-src/pages/index.astro @@ -0,0 +1,11 @@ +--- +/// +import { db, User } from 'astro:db'; + +const users = await db.select().from(User); +--- + +

Users

+ diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 79168e0c0349..1fe401c54a44 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -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':