This guide explains how to add and manage blog articles in your Astro DB-powered blog.
Your blog now uses Astro DB instead of file-based Content Collections. Articles are stored in a SQLite database, which provides more flexibility for querying and managing content.
There are two main ways to add articles to your database:
This method is best if you prefer writing articles in markdown files.
-
Create a new markdown file in
src/content/blog/:# Example: src/content/blog/my-new-article.md -
Add frontmatter and content:
--- title: My New Article description: A brief description of the article pubDate: 2024-01-25 updatedDate: 2024-01-26 # Optional heroImage: /path/to/image.jpg # Optional tags: ['tutorial', 'astro'] externalLink: https://example.com/original-article # Optional - link to original article --- # My New Article Your markdown content here...
-
Run the seed script to add it to the database:
npx astro db execute scripts/seed.ts
The seed script will:
- Read all
.mdfiles fromsrc/content/blog/ - Parse frontmatter and content
- Insert new articles into the database
- Update existing articles if they already exist (based on slug)
- Automatically set
updatedDateto current date when updating existing articles
- Read all
- ✅ Familiar markdown workflow
- ✅ Easy to version control with Git
- ✅ Can edit with any markdown editor
- ✅ Batch import multiple articles at once
This method is best for programmatically adding articles or when you want to add articles directly without markdown files.
-
Edit the helper script
db/add-article.ts:- Modify the
articleDataobject with your article information - Set the
slug,title,description,pubDate,tags, andbody
- Modify the
-
Run the script:
npx astro db execute db/add-article.ts
const articleData = {
slug: 'my-new-post',
title: 'My New Post',
description: 'Description here',
pubDate: new Date('2024-01-25'),
updatedDate: null, // or new Date('2024-01-26')
heroImage: null, // or '/path/to/image.jpg'
tags: ['tutorial', 'astro'],
body: '# My New Post\n\nContent here...',
};- ✅ Direct database insertion
- ✅ Good for automated content generation
- ✅ No markdown files needed
- ✅ Can be integrated into CMS or admin panel
Each article in the database has the following fields:
| Field | Type | Required | Description |
|---|---|---|---|
slug |
text | ✅ Yes | URL-friendly identifier (primary key) |
title |
text | ✅ Yes | Article title |
description |
text | ✅ Yes | Brief description for SEO |
pubDate |
date | ✅ Yes | Publication date |
updatedDate |
date | ❌ No | Last update date (optional) |
heroImage |
text | ❌ No | Path to hero image (optional) |
tags |
json | ❌ No | Array of tags (optional) |
body |
text | ✅ Yes | Markdown content |
externalLink |
text | ❌ No | URL to original/external article (optional) |
- Edit the markdown file in
src/content/blog/ - (Optional) Update the
updatedDatein frontmatter - if not provided, the seed script will automatically set it to the current date - Run the seed script:
npx astro db execute scripts/seed.ts
The seed script will automatically detect that the article already exists and update it with the new content from your markdown file.
Note: The seed script now automatically updates existing articles instead of skipping them. All fields (title, description, body, tags, etc.) will be updated from the markdown file.
Create db/update-article.ts:
import { db, BlogPosts } from 'astro:db';
import { eq } from 'astro:db';
export default async function updateArticle() {
await db
.update(BlogPosts)
.set({
title: 'Updated Title',
body: 'Updated content...',
updatedDate: new Date(),
})
.where(eq(BlogPosts.slug, 'article-slug'));
}Run with: npx astro db execute db/update-article.ts
Articles are automatically displayed on:
- Blog Index:
/blog- Lists all articles - Individual Posts:
/blog/[slug]- Shows full article
The pages query the database using:
// Get all articles
const posts = await db.select().from(BlogPosts).orderBy(desc(BlogPosts.pubDate));
// Get single article
const post = await db.select().from(BlogPosts).where(eq(BlogPosts.slug, slug));Astro DB uses SQLite by default. The database file is located at:
.astro/db.sqlite(local development)- Or configured remote database (production)
-
Slugs: Use URL-friendly slugs (lowercase, hyphens, no spaces)
- ✅ Good:
my-awesome-post - ❌ Bad:
My Awesome Post!
- ✅ Good:
-
Dates: Always use proper date format in frontmatter:
- ✅ Good:
pubDate: 2024-01-25 - ✅ Good:
pubDate: 2024-01-25T10:00:00Z
- ✅ Good:
-
Tags: Use consistent tag naming (lowercase recommended)
- ✅ Good:
['tutorial', 'astro', 'database'] - ❌ Bad:
['Tutorial', 'Astro', 'Database']
- ✅ Good:
-
Body Content: Write in markdown - it will be converted to HTML automatically
-
Hero Images: Use paths relative to
public/folder- ✅ Good:
/images/hero.jpg - ❌ Bad:
../images/hero.jpg
- ✅ Good:
-
External Links: If your article is a summary or repost of an external article, include the original URL
- ✅ Good:
externalLink: https://medium.com/@author/article - ✅ Good:
externalLink: https://example.com/original-post - The external link will be displayed as a button on both the blog listing and individual post pages
- ✅ Good:
- Check if seed script ran successfully
- Verify slug doesn't already exist
- Check browser console for errors
- Restart dev server:
npm run dev
- Each slug must be unique (it's the primary key)
- Change the slug in your markdown filename or articleData
- Verify markdown syntax is correct
- Check that
markedpackage is installed - Ensure
bodyfield contains valid markdown
Here's a complete example of adding a new article:
- Create markdown file:
src/content/blog/my-tutorial.md - Add frontmatter and content:
--- title: My Tutorial description: Learn something new pubDate: 2024-01-25 tags: ['tutorial'] --- # My Tutorial Content here...
- Run seed script to sync to database:
You should see output like:
npx astro db execute scripts/seed.ts
Found 13 blog post(s) to sync... ✓ Added: my-tutorial Sync complete! - Verify: Visit
http://localhost:4321/blog/my-tutorial
That's it! Your article is now in the database and visible on your site.
To update an article you've already added:
- Edit the markdown file in
src/content/blog/ - Make your changes to the content
- Optionally update
updatedDatein frontmatter (if omitted, it will be set automatically) - Run the seed script again:
You should see:
npx astro db execute scripts/seed.ts
Found 13 blog post(s) to sync... ✓ Updated: my-tutorial Sync complete! - Refresh your browser to see the changes
The seed script automatically detects existing articles and updates them with the latest content from your markdown files.