> ## Documentation Index
> Fetch the complete documentation index at: https://synthetiq.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Add a database schema

> Define data models with Prisma and access data through the RLS-scoped database client

Every app gets an isolated database managed through [Prisma](https://www.prisma.io/). The framework wraps the Prisma client with RLS context so that all queries are automatically scoped to the authenticated user's permissions.

## Defining models

The task tracker needs a `Task` model. Define it in `prisma/schema.prisma`:

```prisma theme={null}
model Task {
  id        String   @id @default(cuid())
  title     String
  status    String   @default("active")
  priority  Int      @default(3)
  userId    String
  user      User     @relation(fields: [userId], references: [id])
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt

  @@index([userId])
  @@index([status])
}
```

After modifying the schema, apply changes and generate types:

```bash theme={null}
pnpm install --silent && pnpm run db:push && pnpm run db:generate
```

Type information flows automatically from the Prisma schema through tRPC to the frontend — no manual type definitions needed.

## Database access

All database access in procedure handlers goes through `ctx.db`:

```typescript theme={null}
getMyTasks: scopedProcedure([])
  .meta({ description: "Get current user's tasks" })
  .query(async ({ ctx }) => {
    return ctx.db.task.findMany({
      where: { userId: ctx.userId },
      orderBy: { createdAt: 'desc' },
    });
  }),
```

`ctx.db` is a Prisma client with the authenticated user's RLS context (user ID, role, scopes, org IDs) injected automatically.

## Built-in tables

The scaffolded app includes tables provided by the framework, including common ones like `User` — so you don't need to define them yourself. You can add relations to built-in tables from your own models, as the `Task` model does above with `User`.

For the full list of built-in tables, multi-database support, indexing, transactions, and identity patterns, see the [Database reference](/docs/platform-docs/app-framework/reference/database).
