Astro DB is a hosted SQLite database integrated with Astro’s build pipeline. UUID v7 primary keys use the TEXT column type — SQLite’s native string comparison gives chronological ordering with no extra configuration. See UUID v7 in Cloudflare D1 for a comparable SQLite edge pattern.
Install uuid package
npm install uuid
npm install -D @types/uuid
Astro DB schema — db/config.ts
// db/config.ts
import { defineDb, defineTable, column } from 'astro:db';
const Comments = defineTable({
columns: {
id: column.text({ primaryKey: true }), // UUID v7 as TEXT
postSlug: column.text(),
author: column.text(),
content: column.text(),
},
});
const Posts = defineTable({
columns: {
id: column.text({ primaryKey: true }), // UUID v7 as TEXT
slug: column.text({ unique: true }),
title: column.text(),
content: column.text(),
},
});
export default defineDb({ tables: { Comments, Posts } });
Seed file — db/seed.ts
// db/seed.ts
import { db, Comments, Posts } from 'astro:db';
import { v7 as uuidv7 } from 'uuid';
export default async function seed() {
const postId = uuidv7(); // UUID v7 — encodes seed time
await db.insert(Posts).values({
id: postId,
slug: 'hello-world',
title: 'Hello World',
content: 'First post content',
});
await db.insert(Comments).values([
{ id: uuidv7(), postSlug: 'hello-world', author: 'Alice', content: 'Great post!' },
{ id: uuidv7(), postSlug: 'hello-world', author: 'Bob', content: 'Thanks for sharing.' },
]);
}
Astro server action — create comment
// src/actions/index.ts
import { defineAction } from 'astro:actions';
import { z } from 'astro:schema';
import { db, Comments } from 'astro:db';
import { v7 as uuidv7 } from 'uuid';
export const server = {
addComment: defineAction({
input: z.object({
postSlug: z.string(),
author: z.string(),
content: z.string().min(1).max(500),
}),
handler: async (input) => {
const id = uuidv7(); // UUID v7 — server-generated, encodes submission time
await db.insert(Comments).values({ id, ...input });
return { id };
},
}),
};
Astro page — query with cursor pagination
---
// src/pages/comments/[slug].astro
import { db, Comments } from 'astro:db';
import { gt, asc } from 'drizzle-orm';
const { slug } = Astro.params;
const cursor = Astro.url.searchParams.get('cursor') ?? undefined;
const limit = 20;
let query = db.select().from(Comments)
.where(/* postSlug = slug */ undefined)
.orderBy(asc(Comments.id)) // UUID v7 sort = chronological
.limit(limit + 1);
if (cursor) {
// Drizzle gt() compiles to WHERE id > cursor
query = db.select().from(Comments)
.where(gt(Comments.id, cursor))
.orderBy(asc(Comments.id))
.limit(limit + 1);
}
const rows = await query;
const hasMore = rows.length > limit;
const comments = hasMore ? rows.slice(0, limit) : rows;
const nextCursor = hasMore ? comments.at(-1)!.id : null;
function extractAge(id: string): string {
const ms = Date.now() - parseInt(id.replace(/-/g, '').slice(0, 12), 16);
return ms < 60_000 ? 'just now' : `${Math.floor(ms / 60_000)}m ago`;
}
---
<ul>
{comments.map(c => (
<li>
<strong>{c.author}</strong> — {c.content}
<span class="age">{extractAge(c.id)}</span>
</li>
))}
</ul>
{nextCursor && (
<a href={`?cursor=${nextCursor}`}>Load more</a>
)}
Further reading
- UUID v7 in Cloudflare D1 — SQLite at the Edge
- UUID v7 in Drizzle ORM
- UUID v7 in Astro — SSG and SSR Patterns
- UUID v7 for Cursor Pagination
- UUID in SQLite
External references
Frequently asked questions
How do I define a UUID v7 primary key in Astro DB?
In db/config.ts, define the column as text("id").primaryKey() and provide a default with a UUID v7 generator function. Since Astro DB runs on LibSQL (SQLite), UUID v7 is stored as TEXT. Generate UUID v7 in the default factory: default: () => uuidv7() in seed files, or pass explicitly in server actions. See UUID v7 in Cloudflare D1 for the SQLite storage pattern.
How does UUID v7 cursor pagination work in Astro DB?
Use Drizzle's gt(table.id, cursor) with orderBy(asc(table.id)). Astro DB uses Drizzle under the hood, so the same cursor pagination pattern from Drizzle + PostgreSQL applies. UUID v7 TEXT comparison in SQLite is lexicographic — equals chronological for UUID v7 values. See UUID v7 for Cursor Pagination.
Can I use UUID v7 in Astro server actions?
Yes. Astro server actions run on the server where crypto.getRandomValues() is available. Import the uuid npm package and generate UUID v7 in the action handler before inserting into Astro DB. Return the UUID v7 to the client for use as a stable key in Astro components. See UUID in JavaScript.