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:
parent
f508c4b7d5
commit
2d2ff58bb9
9 changed files with 109 additions and 1 deletions
5
.changeset/spotty-hounds-ring.md
Normal file
5
.changeset/spotty-hounds-ring.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
"@astrojs/db": patch
|
||||
---
|
||||
|
||||
Fix db seeding when srcDir is root
|
|
@ -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;
|
||||
|
|
37
packages/db/test/db-in-src.test.js
Normal file
37
packages/db/test/db-in-src.test.js
Normal 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');
|
||||
});
|
||||
});
|
||||
});
|
10
packages/db/test/fixtures/db-in-src/astro.config.ts
vendored
Normal file
10
packages/db/test/fixtures/db-in-src/astro.config.ts
vendored
Normal 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,
|
||||
},
|
||||
});
|
13
packages/db/test/fixtures/db-in-src/db/config.ts
vendored
Normal file
13
packages/db/test/fixtures/db-in-src/db/config.ts
vendored
Normal 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 },
|
||||
});
|
8
packages/db/test/fixtures/db-in-src/db/seed.ts
vendored
Normal file
8
packages/db/test/fixtures/db-in-src/db/seed.ts
vendored
Normal 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' }]),
|
||||
]);
|
||||
}
|
14
packages/db/test/fixtures/db-in-src/package.json
vendored
Normal file
14
packages/db/test/fixtures/db-in-src/package.json
vendored
Normal 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:*"
|
||||
}
|
||||
}
|
11
packages/db/test/fixtures/db-in-src/pages/index.astro
vendored
Normal file
11
packages/db/test/fixtures/db-in-src/pages/index.astro
vendored
Normal 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>
|
|
@ -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':
|
||||
|
|
Loading…
Reference in a new issue