# Cache, storage, notifications, configuration and factories

## Cache

`cache.get/set/delete/remember/increment/add` is scoped to the current tenant (`fellow:t:<tenant>:`);
`cache.global` is shared. The store is in memory by default and Redis with `FELLOW_CACHE=redis` and `REDIS_URL`
(through `Bun.RedisClient`). Tests always use memory.

## Storage

`storage.put/get/exists/delete/list/temporaryUrl` keeps files under `tenants/<tenant>/` and requires a tenant;
`storage.global` uses `global/`. Disks: local (`FELLOW_STORAGE_PATH`, default `./storage`) and S3-compatible
(`FELLOW_STORAGE=s3`, via `Bun.S3Client` and the usual `S3_*` variables). Paths with `..` are rejected.

Uploads: `validateUpload(file, { maxBytes, types })` checks the size and the type **detected from the bytes**
(PNG, JPEG, GIF, WebP, PDF, ZIP, text), not the declared one. Store with `safeFileName(file.name)` or a generated key.

## Notifications

```ts
export default defineNotification({
  name: 'records.document.assigned',
  payload: z.object({ title: z.string() }),
  channels: {
    mail: (p) => ({ subject: `New document: ${p.title}`, text: p.title }),
    database: (p) => ({ title: p.title }),
  },
})

await notify({ subject: user.id, email: user.email }, assigned, { title })
```

Channels: `logChannel` (default), `databaseChannel` (in-app, `fellow_notifications`, tenant-isolated;
`listNotifications`, `markNotificationRead`), `smtpChannel({ url, from })`, `webhookChannel({ url })` for SMS and
WhatsApp gateways, `memoryChannel` (tests: `app.notifications`). Configure them in `app.ts`'s `boot` with
`configureNotifications`. A failing channel does not stop the others. Delivery is synchronous until queues land.

## Module configuration

```ts
export default defineConfig({ module: 'records', schema: z.object({ RECORDS_PREFIX: z.string().default('PROT') }) })
```

Variables must start with the module id in upper case (FW-CONFIG-002). `config()` returns the parsed values
or throws `config.invalid` listing every problem; `config.check()` returns the issues without throwing.

## Factories

```ts
export default defineFactory(document, ({ sequence }) => ({ title: `Document ${sequence}`, status: 'open' }))
await runAsTenant(tenant, () => documentFactory.createMany(3))
```

Values are deterministic (`sequence`); rows are always created in the current tenant.
