---
description: Querying the database, editing the database, needing to import types from the database,  or anything related to the database or Prisma.
globs: 
alwaysApply: false
---
- We use Prisma as ORM.
- Remember to check the prisma schema [schema.prisma](mdc:web/prisma/schema.prisma) when doing things related to the database.
- After making changes to the [schema.prisma](mdc:web/prisma/schema.prisma) database or [seed.ts](mdc:web/prisma/seed.ts), you run `npm run db-reset` (from `/web/` folder) [package.json](mdc:web/package.json). And never do the migrations manually.
- Import the types from prisma instead of hardcoding duplicates. Specially use the Prisma.___GetPayload type and the enums. Like this:
    ```ts
    type Props = {
      user: Prisma.UserGetPayload<{
        select: {
          name: true
          displayName: true
          picture: true
        }
      }>
    }
    ```

- Only `select` the necessary fields, no more.
- In prisma preffer `select` over `include` when making queries.
- Avoid hardcoding enums from the database, import them from prisma.
- To query the database from Astro pages, use Astro.locals.try() or Astro.locals.tryMany([]) [errorBanners.ts](mdc:web/src/lib/errorBanners.ts) [middleware.ts](mdc:web/src/middleware.ts) , like so:
```ts
const [user, services] = await Astro.locals.banners.tryMany([
  [
    'Error fetching user',
    () =>
      prisma.user.findUnique({
        where: { id: userId },
        select: {
          name: true,
          displayName: true,
          picture: true,
        },
      }),
  ],
  [
    'Error fetching services',
    () =>
      prisma.service.findMany({
        where: { categories: { some: { id: categoryId } } },
        select: {
          id: true,
          name: true,
          slug: true,
        },
      }),
    [] as [],
  ],
])
```
- When editing the database, remember to edit the db seeding file [seed.ts](mdc:web/prisma/seed.ts) to generate data for the new schema.
