# db

Postgres schema and durable state: responsibilities, supported exports and extension points.

## Purpose [#purpose]

Drizzle tables and relations describe auth and application state. The package owns connection lifetimes, migrations, audit/outbox writes, Organization provisioning and scheduled-job leases. Enum subpaths are client-safe leaves. `schema/access.enums` is the permission vocabulary every role layer reads (resources, actions, built-in role grants); `schema/consent.enums` lists the Consent categories ([Roles and permissions](/docs/features/roles-and-permissions)).

## Public exports [#public-exports]

| Subpath                        | Target                                |
| ------------------------------ | ------------------------------------- |
| `.`                            | `./src/index.ts`                      |
| `./audit`                      | `./src/audit.ts`                      |
| `./committed`                  | `./src/committed.ts`                  |
| `./relations`                  | `./src/relations.ts`                  |
| `./schema`                     | `./src/schema/index.ts`               |
| `./schema/auth`                | `./src/schema/auth.ts`                |
| `./schema/oauth-revocation`    | `./src/schema/oauth-revocation.ts`    |
| `./schema/audit`               | `./src/schema/audit.ts`               |
| `./schema/ngo-panel`           | `./src/schema/ngo-panel.ts`           |
| `./schema/ngo-panel.enums`     | `./src/schema/ngo-panel.enums.ts`     |
| `./schema/shelter`             | `./src/schema/shelter.ts`             |
| `./schema/shelter.enums`       | `./src/schema/shelter.enums.ts`       |
| `./schema/pet`                 | `./src/schema/pet.ts`                 |
| `./schema/pet-health`          | `./src/schema/pet-health.ts`          |
| `./schema/pet-health.enums`    | `./src/schema/pet-health.enums.ts`    |
| `./schema/pet.enums`           | `./src/schema/pet.enums.ts`           |
| `./schema/medication`          | `./src/schema/medication.ts`          |
| `./schema/medication.enums`    | `./src/schema/medication.enums.ts`    |
| `./schema/todo`                | `./src/schema/todo.ts`                |
| `./schema/idempotency`         | `./src/schema/idempotency.ts`         |
| `./schema/flags`               | `./src/schema/flags.ts`               |
| `./schema/todo.enums`          | `./src/schema/todo.enums.ts`          |
| `./schema/flags.enums`         | `./src/schema/flags.enums.ts`         |
| `./schema/proposals`           | `./src/schema/proposals.ts`           |
| `./schema/webhooks`            | `./src/schema/webhooks.ts`            |
| `./schema/webhooks.enums`      | `./src/schema/webhooks.enums.ts`      |
| `./schema/proposals.enums`     | `./src/schema/proposals.enums.ts`     |
| `./provision-organization`     | `./src/provision-organization.ts`     |
| `./schema/jobs`                | `./src/schema/jobs.ts`                |
| `./job-db`                     | `./src/job-db.ts`                     |
| `./schema/uploads`             | `./src/schema/uploads.ts`             |
| `./schema/uploads.enums`       | `./src/schema/uploads.enums.ts`       |
| `./schema/billing`             | `./src/schema/billing.ts`             |
| `./schema/notifications`       | `./src/schema/notifications.ts`       |
| `./schema/notifications.enums` | `./src/schema/notifications.enums.ts` |
| `./schema/imports`             | `./src/schema/imports.ts`             |
| `./schema/imports.enums`       | `./src/schema/imports.enums.ts`       |
| `./schema/assistant`           | `./src/schema/assistant.ts`           |
| `./schema/documents`           | `./src/schema/documents.ts`           |
| `./schema/documents.enums`     | `./src/schema/documents.enums.ts`     |
| `./schema/feedback`            | `./src/schema/feedback.ts`            |
| `./schema/feedback.enums`      | `./src/schema/feedback.enums.ts`      |
| `./schema/access.enums`        | `./src/schema/access.enums.ts`        |
| `./schema/consent`             | `./src/schema/consent.ts`             |
| `./schema/consent.enums`       | `./src/schema/consent.enums.ts`       |
| `./schema/analytics`           | `./src/schema/analytics.ts`           |
| `./schema/analytics.enums`     | `./src/schema/analytics.enums.ts`     |
| `./schema/realtime`            | `./src/schema/realtime.ts`            |
| `./schema/realtime.enums`      | `./src/schema/realtime.enums.ts`      |
| `./schema/api-keys`            | `./src/schema/api-keys.ts`            |
| `./schema/api-keys.enums`      | `./src/schema/api-keys.enums.ts`      |
| `./tenancy`                    | `./src/tenancy.ts`                    |

These paths come from `packages/db/package.json`. A `.` entry is the package root. Conditional targets distinguish platform or type resolution; consumers should import the subpath, not its source file.

## How to extend [#how-to-extend]

Generate and review a journaled migration. Register needed relations in the schema aggregate. Keep enum values in import-free leaves and scope domain rows by Organization. Use transactions for domain changes and their audit/outbox records.

Read the [postgres schema and durable state guide](/docs/features/database) for the full flow.

## Demo data [#demo-data]

`pnpm --filter @fofo/db db:seed` creates two accounts with a personal Organization each (`demo@example.com`, and `admin@example.com` with the platform role `admin`) and rewrites the demo User's todos (`SEED_COUNT`, default 200). It connects to `SEED_DATABASE_URL`, or `DATABASE_URL` from `apps/web/.env`.

- **Passwords.** The source holds none. Each account gets the value of `SEED_DEMO_PASSWORD` or `SEED_ADMIN_PASSWORD` (at least 8 characters), or a random password generated for that run and printed once; only its hash is stored.
- **Existing accounts.** An account that already has one of the two addresses is left as it is: role, verification and password are not changed. A second run therefore prints no password; delete the account, or reset its password, to get a new one. The todos are rewritten only for the account the seed itself created (it has the id `seed-demo-user`); someone who signed up with `demo@example.com` keeps theirs. A new account, its credential and its Organization are written in one transaction.
- **Remote databases.** Any host other than `localhost`, `127.0.0.1` or `[::1]` is refused unless `SEED_ALLOW_REMOTE=true` is set and the host is named on the command line: `pnpm --filter @fofo/db db:seed --confirm-remote=<host>`. Do not seed a production database: the seed creates a platform admin.

## Key files [#key-files]

- [`packages/db/package.json`](https://github.com/CarlosZiegler/fofo/blob/main/packages/db/package.json)
