Skip to content

Commit

Permalink
Fix db seeding when srcDir is root (withastro#10720)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewp authored and PeterDraex committed Apr 23, 2024
1 parent 3715835 commit c0c1f74
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/spotty-hounds-ring.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@astrojs/db": patch
---

Fix db seeding when srcDir is root
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 @@ -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',
Expand All @@ -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;
Expand Down
37 changes: 37 additions & 0 deletions packages/db/test/db-in-src.test.js
Original file line number Diff line number Diff line change
@@ -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');
});
});
});
10 changes: 10 additions & 0 deletions packages/db/test/fixtures/db-in-src/astro.config.ts
Original file line number Diff line number Diff line change
@@ -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,
},
});
13 changes: 13 additions & 0 deletions packages/db/test/fixtures/db-in-src/db/config.ts
Original file line number Diff line number Diff line change
@@ -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 },
});
8 changes: 8 additions & 0 deletions packages/db/test/fixtures/db-in-src/db/seed.ts
Original file line number Diff line number Diff line change
@@ -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' }]),
]);
}
14 changes: 14 additions & 0 deletions packages/db/test/fixtures/db-in-src/package.json
Original file line number Diff line number Diff line change
@@ -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:*"
}
}
11 changes: 11 additions & 0 deletions packages/db/test/fixtures/db-in-src/pages/index.astro
Original file line number Diff line number Diff line change
@@ -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>
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c0c1f74

Please sign in to comment.