mirror of
https://github.com/withastro/astro.git
synced 2025-02-17 22:44:24 -05:00
db: Seed on dev server startup (#10599)
* feat: load seed file on dev server startup * feat: handle logging on dev server restart * chore: changeset * feat: support integration seed files * fix: only run when seed is present, ignore unlink * fix: load on startup for integration pkg paths
This commit is contained in:
parent
2cf116f80c
commit
5a7733dde5
3 changed files with 54 additions and 11 deletions
5
.changeset/clever-moles-listen.md
Normal file
5
.changeset/clever-moles-listen.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
"@astrojs/db": patch
|
||||
---
|
||||
|
||||
Seed database on dev server startup, and log whenever the seed file is reloaded.
|
|
@ -13,8 +13,9 @@ import { type ManagedAppToken, getManagedAppTokenOrExit } from '../tokens.js';
|
|||
import { type VitePlugin, getDbDirectoryUrl } from '../utils.js';
|
||||
import { fileURLIntegration } from './file-url.js';
|
||||
import { typegenInternal } from './typegen.js';
|
||||
import { type LateSeedFiles, type LateTables, vitePluginDb } from './vite-plugin-db.js';
|
||||
import { type LateSeedFiles, type LateTables, vitePluginDb, resolved } from './vite-plugin-db.js';
|
||||
import { vitePluginInjectEnvTs } from './vite-plugin-inject-env-ts.js';
|
||||
import { SEED_DEV_FILE_NAME } from '../../runtime/queries.js';
|
||||
|
||||
function astroDBIntegration(): AstroIntegration {
|
||||
let connectToStudio = false;
|
||||
|
@ -94,15 +95,7 @@ function astroDBIntegration(): AstroIntegration {
|
|||
|
||||
await typegenInternal({ tables: tables.get() ?? {}, root: config.root });
|
||||
},
|
||||
'astro:server:start': async ({ logger }) => {
|
||||
// Wait for the server startup to log, so that this can come afterwards.
|
||||
setTimeout(() => {
|
||||
logger.info(
|
||||
connectToStudio ? 'Connected to remote database.' : 'New local database created.'
|
||||
);
|
||||
}, 100);
|
||||
},
|
||||
'astro:server:setup': async ({ server }) => {
|
||||
'astro:server:setup': async ({ server, logger }) => {
|
||||
const filesToWatch = [
|
||||
...CONFIG_FILE_NAMES.map((c) => new URL(c, getDbDirectoryUrl(root))),
|
||||
...configFileDependencies.map((c) => new URL(c, root)),
|
||||
|
@ -113,6 +106,51 @@ function astroDBIntegration(): AstroIntegration {
|
|||
server.restart();
|
||||
}
|
||||
});
|
||||
// Wait for dev server log before showing "connected".
|
||||
setTimeout(() => {
|
||||
logger.info(
|
||||
connectToStudio ? 'Connected to remote database.' : 'New local database created.'
|
||||
);
|
||||
if (connectToStudio) return;
|
||||
|
||||
const localSeedPaths = SEED_DEV_FILE_NAME.map(
|
||||
(name) => new URL(name, getDbDirectoryUrl(root))
|
||||
);
|
||||
let seedInFlight = false;
|
||||
// Load seed file on dev server startup.
|
||||
if (seedFiles.get().length || localSeedPaths.find((path) => existsSync(path))) {
|
||||
loadSeedModule();
|
||||
}
|
||||
const eagerReloadIntegrationSeedPaths = seedFiles
|
||||
.get()
|
||||
// Map integration seed paths to URLs, if possible.
|
||||
// Module paths like `@example/seed` will be ignored
|
||||
// from eager reloading.
|
||||
.map((s) => (typeof s === 'string' && s.startsWith('.') ? new URL(s, root) : s))
|
||||
.filter((s): s is URL => s instanceof URL);
|
||||
const eagerReloadSeedPaths = [...eagerReloadIntegrationSeedPaths, ...localSeedPaths];
|
||||
server.watcher.on('all', (event, relativeEntry) => {
|
||||
if (event === 'unlink' || event === 'unlinkDir') return;
|
||||
// When a seed file changes, load manually
|
||||
// to track when seeding finishes and log a message.
|
||||
const entry = new URL(relativeEntry, root);
|
||||
if (eagerReloadSeedPaths.find((path) => entry.href === path.href)) {
|
||||
loadSeedModule();
|
||||
}
|
||||
});
|
||||
|
||||
function loadSeedModule() {
|
||||
if (seedInFlight) return;
|
||||
|
||||
seedInFlight = true;
|
||||
const mod = server.moduleGraph.getModuleById(resolved.seedVirtual);
|
||||
if (mod) server.moduleGraph.invalidateModule(mod);
|
||||
server.ssrLoadModule(resolved.seedVirtual).then(() => {
|
||||
seedInFlight = false;
|
||||
logger.info('Seeded database.');
|
||||
});
|
||||
}
|
||||
}, 100);
|
||||
},
|
||||
'astro:build:start': async ({ logger }) => {
|
||||
if (
|
||||
|
|
|
@ -7,7 +7,7 @@ import { type VitePlugin, getDbDirectoryUrl, getRemoteDatabaseUrl } from '../uti
|
|||
|
||||
const WITH_SEED_VIRTUAL_MODULE_ID = 'astro:db:seed';
|
||||
|
||||
const resolved = {
|
||||
export const resolved = {
|
||||
virtual: '\0' + VIRTUAL_MODULE_ID,
|
||||
seedVirtual: '\0' + WITH_SEED_VIRTUAL_MODULE_ID,
|
||||
};
|
||||
|
|
Loading…
Add table
Reference in a new issue