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

# PrePrompts Collection

> MongoDB schema and storage notes for masked pre-prompts.

## Overview

All PrePrompt records live in MongoDB under the `preprompts` collection. The backend
never keeps a separate in-memory store—each API read/write goes through the repository
defined in `src/services/database/repositories/preprompt.repository.ts`, which also
initialises the indexes described below.

## Document shape

```json theme={null}
{
  "_id": ObjectId,
  "key": "wealth_tax",
  "public_value": "Ask Handa Uncle for wealth tax help",
  "masked_value": "You are a chartered accountant specialising in wealth tax...",
  "description": "Personalised CA instructions for complex filings",
  "tags": ["tax", "advisor"],
  "is_deleted": false,
  "created_at": ISODate,
  "updated_at": ISODate
}
```

* `key`: unique identifier referenced by clients through `preprompt_key`.
* `public_value`: friendly string rendered in the UI.
* `masked_value`: injected after the system prompt; never exposed to clients.
* `description`, `tags`: optional metadata for catalog filtering.
* `is_deleted`: soft-delete flag used by admin workflows.

## Indexes

| Name                    | Keys                        | Notes                                                                |
| ----------------------- | --------------------------- | -------------------------------------------------------------------- |
| `preprompts_key_unique` | `{ key: 1 }`                | Enforces unique keys (automatic revival if a deleted key is reused). |
| `preprompts_is_deleted` | `{ is_deleted: 1, key: 1 }` | Speeds up admin listing & catalog queries.                           |

Both indexes are created automatically at service startup, so no manual migration is
required.

## Access patterns

| Operation     | Repository method         | Notes                                                    |
| ------------- | ------------------------- | -------------------------------------------------------- |
| Create        | `create(data)`            | Inserts with timestamps, defaulting `is_deleted=false`.  |
| Update        | `update(id, payload)`     | Automatically bumps `updated_at`.                        |
| Soft delete   | `softDelete(id)`          | Marks `is_deleted=true` while retaining history.         |
| List (admin)  | `findAll(includeDeleted)` | Supports auditing by toggling deleted rows.              |
| List (public) | `listPublicOptions()`     | Returns only `{ key, public_value, description, tags }`. |

Because every lookup hits MongoDB, deploying multiple API instances stays consistent
without additional cache invalidation logic.
