# Backend: Table Prefix & Controller Folder Mapping

> Referenced from `steps/step-04-backend.md` — Deriving table prefixes and controller organization.

---

## Table Prefix Priority

1. **If a `feature.json` exists** with `metadata.tablePrefix` → use that prefix (e.g., `rh_`, `fi_`)
2. **Otherwise**, derive from navigation context using the fallback table below

---

## Fallback Table (when no feature.json tablePrefix)

| Application | Table Prefix | Controller Folder | Example |
|---------|--------|-------------------|---------|
| `administration.*` | `auth_` or `cfg_` | `Administration` | Table: `auth_Tenants`, Controller: `Controllers/Administration/TenantsController.cs` |
| `support.*` | `support_` | `Support` | Table: `support_Tickets`, Controller: `Controllers/Support/TicketsController.cs` |
| `*` (business apps) | `ref_` or domain-specific | `{ApplicationPascal}` | Table: `ref_Products`, Controller: `Controllers/Sales/ProductsController.cs` |
| `myspace.*` | `usr_` | `MySpace` | Table: `usr_Preferences`, Controller: `Controllers/MySpace/PreferencesController.cs` |
| `api.*` | `auth_` or `ext_` | `Api` | Table: `auth_ExternalApplications`, Controller: `Controllers/Platform/Api/ExternalApplications/` |

---

## Feature.json Table Prefix Detection

```
Search for feature.json in:
  - docs/business/{application}/business-analyse/*/feature.json
  - Read metadata.tablePrefix if exists
```

**Example from feature.json:**

```json
{
  "metadata": {
    "tablePrefix": "rh_",
    "schema": "core"
  },
  "analysis": { ... }
}
```

---

## Implementation

```csharp
// Check for feature.json tablePrefix
Search for feature.json in:
  - docs/business/{application}/business-analyse/*/feature.json
  - Read metadata.tablePrefix if exists

IF metadata.tablePrefix exists:
  {prefix} = metadata.tablePrefix   # e.g., "rh_"
ELSE:
  {prefix} = derived from context table above
```

Store: `{controller_folder}` = the Controller Folder from the table above.

---

## Usage in MCP Call

When calling `mcp__smartstack__scaffold_extension`:

```
Tool: mcp__smartstack__scaffold_extension
Args:
  type: "feature"
  name: "{entityName}"
  options:
    navRoute: "{full_path}"
    tablePrefix: "{prefix}"        ← Derived prefix
    schema: "core"
    ...
```
