#how to get a list of all available routes
33 messages · Page 1 of 1 (latest)
Hi @weak hazel. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
Short answer: there isn’t a built-in runtime API in Astro pages to list “all routes” for building a navbar.
What you can do instead depends on your goal:
-
For content-driven pages (e.g., blog posts), generate routes from your content and use that same data to render your navigation. Use getStaticPaths() with getCollection() (for content collections) and pass props to the page/template where you render links. [Generating routes; Static (SSG) Mode]
-
If you’re writing an integration or build tooling, Astro exposes route data via integration hooks (not inside page code):
- astro:routes:resolved gives you all resolved routes and metadata during dev/setup. [astro:routes:resolved]
- astro:build:done provides generated pages and an assets map after build (note: the routes array on this hook is deprecated in v5; use astro:routes:resolved plus assets). [astro:build:done; Deprecated routes hook]
Recommendation for a navbar:
- Define your nav structure explicitly (a JSON/TS file or content collection) and render it—this avoids relying on file-system introspection.
- If your nav mirrors dynamic pages you generate, centralize the source of truth (e.g., your content collection) and use it both for getStaticPaths() and for rendering the nav via props. [Generating routes; Use props in dynamic routes]
There’s no documented page-level API like “Astro.getAllRoutes()” in the sources above. If you need the complete route list globally, you’d need to use an integration hook and write out a manifest during build, then import that manifest where needed. [astro:routes:resolved; astro:build:done]
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@weak hazel Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@rotund flame is there any way to get type safety when using route names inside a file? example: an error should be thrown if I use "contct" instead of "contact".
Let me check the knowledge base for you again. One moment! :books:
Short answer: Astro doesn’t provide a built-in, page-level API to type-check route names in your code (e.g., catching "contct" vs "contact") based on your project’s routes.
What you can do:
-
Use an integration to access the resolved routes at build/dev time via the astro:routes:resolved hook, which gives you all routes and metadata. You could generate a typed manifest (e.g., a .ts file with a union of valid paths) and import it where you need to reference routes. [astro:routes:resolved; IntegrationResolvedRoute]
-
If your routes come from dynamic content (SSG), centralize route data (e.g., from getStaticPaths or content collections) and use those values directly rather than hardcoded strings. That way, you reference the same source of truth used to generate pages. [Static (SSG) Mode]
There’s no documented built-in mechanism to enforce type-safe route strings inside components without doing one of the above (e.g., generating types from routes). If you need stronger typing, the integration approach is the recommended path. [astro:routes:resolved]
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@weak hazel Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@rotund flame not able to seed the database. here's my db/seed.ts file
export default async function seed() {
db.insert(Example).values({ foo: "hielo" })
}
but select doesn't return anything. here's my src/pages/index.astro
const res = await db.select().from(Example)
console.log('after',res)
Let me check the knowledge base for you again. One moment! :books:
A few common gotchas can cause seed data not to appear in queries:
- Make sure you’re actually inserting during a seed run
- seed.ts must export a default async function and call db.insert with await (or return the promise). Otherwise the process may exit before the insert completes.
Example:
// db/seed.ts
import { db, Example } from 'astro:db';
export default async function () {
await db.insert(Example).values({ foo: 'hielo' });
}
This is the pattern shown in the docs. Your dev server will re-run seeding when seed.ts changes, generating fresh local data. [Seed your database for development]
- Verify you’re querying the same database (local vs remote)
- By default, astro dev/build uses a local DB file. If you’re running with --remote, you’ll be hitting your remote DB instead. Ensure both seeding and querying target the same environment. [Connecting to remote databases]
- Ensure Example is defined in db/config.ts
- You must define the Example table in db/config.ts. The db client types and operations depend on that schema. [Define your database]
- If you’re trying to seed a remote DB, execute the seed against remote
- Local dev seeding happens automatically on dev start. For remote, run:
astro db execute db/seed.ts --remote
This runs your seed file against the remote database. [Pushing table data; CLI reference]
- Query correctly in your page
- Make sure you import db and Example and await the select:
---
// src/pages/index.astro
import { db, Example } from 'astro:db';
const res = await db.select().from(Example);
console.log('after', res);
---
This is the documented pattern for selecting rows. [Query your database]
If after these checks you still get an empty array:
- Confirm the seed function is being executed (add a console.log in db/seed.ts).
- Restart astro dev to ensure the local DB is regenerated from seed.ts.
- If using references/constraints, catch errors with isDbError to see if the insert failed silently due to constraints. [isDbError()]
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@weak hazel Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@rotund flame in astro db, what is the recommanded way to generate IDs (UUID) to rows?
Let me check the knowledge base for you again. One moment! :books:
I’m sorry, but the provided knowledge sources don’t explicitly recommend a specific “best” way to generate UUIDs in Astro DB.
What the docs do show:
-
You can define your own primary key columns and defaults in table schemas. Columns support a default value, including SQL expressions via the sql helper (from Drizzle) exposed by astro:db. This suggests you could set a default on a text primary key to generate IDs at insert time. [Table config; Drizzle utilities]
-
Column types include text and number with primaryKey, plus default and unique options. You could model a UUID as a text primary key and supply it on insert, or set a default expression if your database supports UUID generation. [Table config]
However, the docs do not provide a built-in UUID generator or a canonical example for UUID generation in Astro DB. I don’t have a documented snippet like column.text({ default: sql...uuid()... }) in the sources above.
Practical approaches (based on what is documented):
- App-level UUIDs: Generate a UUID in your app code and insert it into a text primary key column.
- DB-side defaults: If your libSQL-compatible database supports a function to generate UUID-like IDs, you could set a column default using the sql helper. The docs confirm sql usage is available, but don’t show a UUID example. [Drizzle utilities; Table config]
If you can share which libSQL-compatible backend you’re using and whether it supports UUID functions, I can suggest a concrete schema pattern using the sql default. Otherwise, generating the ID in application code and storing it in a text primary key column is a safe, portable choice per the capabilities shown in the docs. [Table config; Drizzle utilities]
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@weak hazel Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@rotund flame why does id: column.text({ unique: true, primaryKey: true }) in astro db makes the id column optional when inseting new entries?
Let me check the knowledge base for you again. One moment! :books:
Short answer: Because Astro DB treats primary keys as optional on insert by default, so you can omit them and let the database/defaults provide the value.
Details from the docs:
-
In the table config reference, the inferred “insert” type shows that the primary key field is optional when inserting. Example: defining a table with an id primary key results in an insert type where id is optional (id?: …). This allows you to skip providing an id when inserting, e.g., if you rely on a default or generate it elsewhere. [Table configuration reference]
-
Columns support primaryKey and default. If you want the database to always fill the id, give the column a default (static or SQL). Then inserts can omit id, and the DB will supply it. If you want to require callers to provide an id, don’t rely on the default and validate at the app level (Astro DB doesn’t expose a per-column “required on insert” flag beyond not marking it optional in the inferred insert type). [Table configuration reference]
What unique + primaryKey implies here:
- primaryKey marks the column as the identifier and, in generated insert types, makes it optional to support auto-generated keys.
- unique just adds a uniqueness constraint; it doesn’t change optionality of the insert type. [Table configuration reference]
If you need to force providing an id on insert, your options are:
- Don’t set a default and enforce it in your code (e.g., with Zod in an action handler) so missing ids are caught before db.insert. [Query your database]
- Alternatively, accept the optional insert and rely on a default to generate ids automatically (recommended when you want auto IDs). [Table configuration reference]
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@weak hazel Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@rotund flame show example of creating a default and one example for generating it elsewhere
Let me check the knowledge base for you again. One moment! :books:
Here are two concise patterns using the docs’ supported APIs.
- Generate the ID via a database default
- Define a text primary key with a default. Defaults can be static values or SQL expressions. The docs show using default on columns; while they don’t include a UUID example, the pattern is the same: set a default so inserts can omit id. [Table configuration reference]
// db/config.ts
import { defineDb, defineTable, column } from 'astro:db';
const Example = defineTable({
columns: {
// If your DB supports a function for IDs, you can set a default here.
// Example shows static default pattern; replace with an SQL expression your DB supports.
id: column.text({ primaryKey: true, default: 'auto-generated-id' }),
foo: column.text(),
},
});
export default defineDb({ tables: { Example } });
Then you can insert without providing id:
import { db, Example } from 'astro:db';
await db.insert(Example).values({ foo: 'hielo' }); // id is filled by default
This follows the documented “default” option that can be a static value or SQL. The docs do not provide a UUID-specific function example. [Table configuration reference]
- Generate the ID elsewhere (in app code) and insert it
- Model id as a text primary key, and provide it from your app (e.g., in an endpoint/action) before calling insert. [Table configuration reference; Insert]
// db/config.ts
import { defineDb, defineTable, column } from 'astro:db';
const Example = defineTable({
columns: {
id: column.text({ primaryKey: true }),
foo: column.text(),
},
});
export default defineDb({ tables: { Example } });
```ts
// src/pages/api/example.ts (or an action)
import { db, Example } from 'astro:db';
// Generate an ID in your application code, then insert.
// (The docs don’t specify a built-in UUID helper; use your own generator.)
const id = 'my-generated-id';
await db.insert(Example).values({ id, foo: 'hielo' });
Notes and limitations:
- Astro DB’s column.default accepts static values or SQL expressions, but the docs don’t include a built-in UUID helper or a concrete UUID SQL example. If your libSQL-compatible backend supports a UUID function, you can place that in default as SQL; otherwise, generate IDs in application code. [Table configuration reference]
- Primary keys are optional in the inferred insert type to allow defaults/auto-generation; if you want to enforce providing an id, validate at the app boundary (e.g., actions). [Table configuration reference; Insert]
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@weak hazel Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@rotund flame what does the sql function do? can you give me a few examples.
Let me check the knowledge base for you again. One moment! :books:
The sql helper is a Drizzle utility that Astro DB re-exports from the astro:db module. It lets you write raw SQL snippets inside your queries, including using SQL expressions as defaults in column definitions. [Drizzle utilities; Table config]
Examples:
-
Importing sql with other Drizzle helpers
import { eq, gt, count, sql } from 'astro:db';Astro DB exposes Drizzle operators and helpers directly from astro:db. [Drizzle utilities]
-
Using sql to set a column default (SQL expression)
import { defineTable, column } from 'astro:db'; import { sql } from 'astro:db'; const Comment = defineTable({ columns: { // Example pattern: use an SQL-generated timestamp as the default // (Replace with an expression your database supports.) published: column.date({ default: sql`CURRENT_TIMESTAMP` }), }, });Column defaults can be a static value or a string of SQL for generated values like timestamps; sql is used to supply that SQL expression. [Table config]
-
Using sql for raw SQL in queries
import { db, sql } from 'astro:db'; // Select with a raw SQL fragment (e.g., custom function or expression) const result = await db.execute(sql`SELECT 1 AS value`);The docs state the sql helper is available for writing raw SQL queries, alongside other Drizzle helpers. [Drizzle utilities]
Notes:
- The docs don’t provide a long-form API reference for sql beyond stating its purpose (raw SQL and defaults). If you need a specific database function (e.g., UUID), you would place that SQL inside sql
...if your libSQL-compatible backend supports it. [Drizzle utilities; Table config]