Skip to content

Commit 056640b

Browse files
committed
Fix db seeding when srcDir is root
1 parent 70ac719 commit 056640b

File tree

9 files changed

+141
-1
lines changed

9 files changed

+141
-1
lines changed

.changeset/spotty-hounds-ring.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@astrojs/db": patch
3+
---
4+
5+
Fix db seeding when srcDir is root

packages/db/src/core/integration/vite-plugin-db.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ type VitePluginDBParams =
4040

4141
export function vitePluginDb(params: VitePluginDBParams): VitePlugin {
4242
const srcDirPath = normalizePath(fileURLToPath(params.srcDir));
43+
const dbDirPath = normalizePath(fileURLToPath(getDbDirectoryUrl(params.root)));
4344
let command: 'build' | 'serve' = 'build';
4445
return {
4546
name: 'astro:db',
@@ -54,7 +55,7 @@ export function vitePluginDb(params: VitePluginDBParams): VitePlugin {
5455
const importer = rawImporter ? await this.resolve(rawImporter) : null;
5556
if (!importer) return resolved.virtual;
5657

57-
if (importer.id.startsWith(srcDirPath)) {
58+
if (importer.id.startsWith(srcDirPath) && !importer.id.startsWith(dbDirPath)) {
5859
// Seed only if the importer is in the src directory.
5960
// Otherwise, we may get recursive seed calls (ex. import from db/seed.ts).
6061
return resolved.seedVirtual;

packages/db/test/db-in-src.test.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { expect } from 'chai';
2+
import { load as cheerioLoad } from 'cheerio';
3+
import testAdapter from '../../astro/test/test-adapter.js';
4+
import { loadFixture } from '../../astro/test/test-utils.js';
5+
6+
describe('astro:db', () => {
7+
let fixture;
8+
before(async () => {
9+
fixture = await loadFixture({
10+
root: new URL('./fixtures/db-in-src/', import.meta.url),
11+
output: 'server',
12+
srcDir: '.',
13+
adapter: testAdapter(),
14+
});
15+
});
16+
17+
describe('development: db/ folder inside srcDir', () => {
18+
let devServer;
19+
20+
before(async () => {
21+
devServer = await fixture.startDevServer();
22+
});
23+
24+
after(async () => {
25+
await devServer.stop();
26+
});
27+
28+
it('Prints the list of authors', async () => {
29+
const html = await fixture.fetch('/').then((res) => res.text());
30+
const $ = cheerioLoad(html);
31+
32+
const ul = $('.users-list');
33+
expect(ul.children()).to.have.a.lengthOf(1);
34+
expect($('.users-list li').text()).to.equal('Mario');
35+
});
36+
});
37+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import db from '@astrojs/db';
2+
import { defineConfig } from 'astro/config';
3+
4+
// https://astro.build/config
5+
export default defineConfig({
6+
integrations: [db()],
7+
devToolbar: {
8+
enabled: false,
9+
},
10+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { column, defineDb, defineTable } from 'astro:db';
2+
3+
const User = defineTable({
4+
columns: {
5+
id: column.text({ primaryKey: true, optional: false }),
6+
username: column.text({ optional: false, unique: true }),
7+
password: column.text({ optional: false }),
8+
},
9+
});
10+
11+
export default defineDb({
12+
tables: { User },
13+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { asDrizzleTable } from '@astrojs/db/utils';
2+
import { User, db } from 'astro:db';
3+
4+
export default async function () {
5+
await db.batch([
6+
db.insert(User).values([{ id: 'mario', username: 'Mario', password: 'itsame' }]),
7+
]);
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "@test/db-db-in-src",
3+
"version": "0.0.0",
4+
"private": true,
5+
"scripts": {
6+
"dev": "astro dev",
7+
"build": "astro build",
8+
"preview": "astro preview"
9+
},
10+
"dependencies": {
11+
"@astrojs/db": "workspace:*",
12+
"astro": "workspace:*"
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
/// <reference path="../.astro/db-types.d.ts" />
3+
import { db, User } from 'astro:db';
4+
5+
const users = await db.select().from(User);
6+
---
7+
8+
<h2>Users</h2>
9+
<ul class="users-list">
10+
{users.map((user) => <li>{user.username}</li>)}
11+
</ul>

pnpm-lock.yaml

+41
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)