Skip to content

Fix db seeding when srcDir is root #10720

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>
41 changes: 41 additions & 0 deletions pnpm-lock.yaml

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

Loading