# `prisma/` — Postgres schema source of truth

```
prisma/
├── schema.prisma        Tenancy + Identity/RBAC + Portal (Announcement/Document/Approval) + AuditLog
├── seed.ts              Idempotent — re-runnable
├── migrations/          Generated by `prisma migrate dev` (kept under git)
└── package.json
```

## Conventions

- **IDs:** `String @id @default(cuid())` — sortable, URL-safe, no collision under multi-writer.
- **Soft delete:** `deletedAt DateTime?`; queries filter via Prisma client extensions.
- **Audit fields:** `createdAt`, `updatedAt`, `createdBy`, `updatedBy` on all business entities.
- **Multi-tenant ready:** `tenantId` column on tenant-scoped entities. Single tenant today (portal-platform), enable enforcement when `auth.multiTenant=true` in `project.config.json`.
- **Snake_case in DB**, PascalCase in TS via `@map` / `@@map`.
- **Email uniqueness:** scoped to `(tenantId, email)` — soft-deleted rows can retain their email.

## Commands

Run from the repo root so workspaces resolve:

```bash
pnpm db:generate           # prisma generate (regenerates @prisma/client)
pnpm db:migrate            # prisma migrate dev (interactive — names + applies a migration)
pnpm db:migrate:deploy     # prisma migrate deploy (CI/prod)
pnpm db:reset              # prisma migrate reset --force (DROPS DATA, re-applies migrations + seed)
pnpm db:seed               # tsx prisma/seed.ts (idempotent)
pnpm db:studio             # GUI on http://localhost:5555
pnpm db:validate           # prisma validate (no DB connection)
```

Env vars expected:

```env
DATABASE_URL=postgresql://loma_portal:change-me-in-local-only@localhost:5432/loma_portal?schema=public
ADMIN_EMAIL=admin@loma-portal.local              # optional
ADMIN_PASSWORD=ChangeMeOn-FirstLogin!2026         # optional
TENANT_SLUG=loma-portal                           # optional
```

## Schema → EF Core parity

The .NET API (`dotnet/`) consumes the same DB via EF Core with entity classes in `dotnet/src/Domain/`. We model the schema **twice** on purpose:

- Prisma owns migrations (the canonical schema). Run `pnpm db:migrate` first.
- EF Core's `ApplicationDbContext` mirrors only the tables the .NET API reads/writes, with `IEntityTypeConfiguration<T>` configs in `dotnet/src/Infrastructure/Persistence/Configurations/`.

When the schema changes:

1. Update `prisma/schema.prisma` and run `pnpm db:migrate`.
2. Update matching EF entities + configs in `dotnet/`.
3. Run `dotnet build` to verify.
4. Update `packages/api-contracts/` shared types if the change touches an API DTO.

See [`docs/runbooks/db-migrations.md`](../docs/runbooks/db-migrations.md) for the full workflow including breaking-change handling and rollback.
