Skip to content
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

db: Seed on dev server startup #10599

Merged
merged 6 commits into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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/clever-moles-listen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@astrojs/db": patch
---

Seed database on dev server startup, and log whenever the seed file is reloaded.
43 changes: 33 additions & 10 deletions packages/db/src/core/integration/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)),
Expand All @@ -113,6 +106,36 @@ function astroDBIntegration(): AstroIntegration {
server.restart();
}
});
// Wait for dev server log before showing "connected".
await new Promise((resolve) => setTimeout(resolve, 100));
logger.info(
connectToStudio ? 'Connected to remote database.' : 'New local database created.'
);
const seedFileUrls = SEED_DEV_FILE_NAME.map(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this also need to account for integration seed files? Right now it looks like it’s only our static array of project ones?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, that's a good point! This is only for eagerly loading when a seed file changes, so it shouldn't break existing behavior. Happy to add if it's easy enough

Copy link
Contributor Author

@bholmesdev bholmesdev Mar 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update: this will work for relative paths but not package ids. Since eagerly reloading is a nice-to-have, I think this is a fine compromise.

To be clear, eagerly seeding on startup still works! This would only be the nuance of updating your integration in real time and expecting an eager reload. You'll need to visit a page in your browser to see that reload.

(name) => new URL(name, getDbDirectoryUrl(root))
);
if (!connectToStudio) {
// Load seed file on dev server startup.
let seedInFlight = false;
loadSeedModule();
server.watcher.on('all', (event, relativeEntry) => {
// When a seed file changes, load manually
// to track when seeding finishes and log a message.
const entry = new URL(relativeEntry, root);
if (seedFileUrls.find((f) => entry.href === f.href)) {
if (seedInFlight) return;
loadSeedModule();
}
});

function loadSeedModule() {
seedInFlight = true;
server.ssrLoadModule(resolved.seedVirtual).then(() => {
seedInFlight = false;
logger.info('Seeded database.');
});
}
}
},
'astro:build:start': async ({ logger }) => {
if (
Expand Down
2 changes: 1 addition & 1 deletion packages/db/src/core/integration/vite-plugin-db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down
Loading