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

db: Seed on dev server startup #10599

merged 6 commits into from
Apr 1, 2024

Conversation

bholmesdev
Copy link
Contributor

@bholmesdev bholmesdev commented Mar 28, 2024

Changes

This loads your seed file whenever the dev server starts. To make it unblocking and preserve HMR on file changes, we use vite.ssrLoadModule().

  • Add a ssrLoadModule() call for the astro:db:seed module on server startup
  • Add a file watcher to eagerly load this module again whenever a seed file changes. This does not cause duplicate runs in the Vite server; it eagerly loads and allows us to track the promise to log another "seeded database" message. This gives user feedback when your seed file changes and keeps your db up-to-date even when your browser is closed.

Testing

Manual testing to ensure logs fire and the seed file is only loaded/reloaded once.

Docs

N/A

Copy link

changeset-bot bot commented Mar 28, 2024

🦋 Changeset detected

Latest commit: eb0a7e9

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Member

@delucis delucis left a comment

Choose a reason for hiding this comment

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

Excited to see this! Left a query about integrations.

Will save my best puns for an approving seedquel to this review.

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.

@bholmesdev bholmesdev requested a review from delucis March 28, 2024 22:11
Copy link
Member

@ematipico ematipico left a comment

Choose a reason for hiding this comment

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

Looks good, I left a minor nitpick

Comment on lines +129 to +130
.map((s) => (typeof s === 'string' && s.startsWith('.') ? new URL(s, root) : s))
.filter((s): s is URL => s instanceof URL);
Copy link
Member

Choose a reason for hiding this comment

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

nit: you should flip them :)

Copy link
Contributor Author

@bholmesdev bholmesdev Apr 1, 2024

Choose a reason for hiding this comment

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

Ah, I see where you're coming from but we actually cannot flip these. The map() is used to convert relative path strings to URLs, then the filter() consolidates to all URL objects. If we put the filter() first, those relative path strings would get filtered out before they were converted

@bholmesdev bholmesdev merged commit 5a7733d into main Apr 1, 2024
13 checks passed
@bholmesdev bholmesdev deleted the feat/db-seed-on-startup branch April 1, 2024 15:16
@astrobot-houston astrobot-houston mentioned this pull request Apr 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants