Skip to content

Using and extending your database

Astroship uses Supabase to store data and Prisma ORM to handle models, migrations and queries. Models and migrations are automatically generated when you add or remove new models, fields, or relationships to your Prisma schema (src/db/prisma/schema.prisma).

Core concepts

1. Automatic migrations

Astroship uses Prisma to handle migrations. When you deploy your project, Astroship uses Prisma to automatically apply any pending migrations to your database.

2. Adding new models

To add a new model, modify src/db/prisma/schema.prisma. For example, to add a posts model, add the following to schema.prisma:

src/db/prisma/schema.prisma
...
model Post {
id Int @id @default(autoincrement())
title String
content String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
user User @relation(fields: [userId], references: [id])
// References the included User model
@@map("posts") // We recommend using snake_cased, plural names
}
...

And then generate migrations and a new prisma client with the model by running:

Terminal window
npm run db:dev:generate

Your new model is now accessible on your pages via:

Astro.locals.db.prisma.post

You can fetch the posts for the authenticated user via:

const user = await Astro.locals.auth.fetchUser();
const posts = await Astro.locals.db.prisma.post.findMany({
where: {
userId: user.id,
},
});

Diving Deeper

To learn more about what Prisma offers, follow the Prisma documentation.