docs: sync from backend 1543b53 — category uniqueness

This commit is contained in:
Siavash Sameni
2026-06-01 17:22:53 +04:00
parent 78707c11a7
commit 02641e1333
5 changed files with 54 additions and 23 deletions

View File

@@ -1,6 +1,6 @@
---
title: Category
tags: [data-model, mongoose]
tags: [data-model, mongoose, postgres]
aliases: [Category Model, Taxonomy, ICategory]
---
@@ -10,14 +10,16 @@ Hierarchical taxonomy node used by [[PurchaseRequest]] and [[RequestTemplate]].
> [!note] Source
> `backend/src/models/Category.ts:15` — schema definition
> `backend/src/models/Category.ts:60` — model export
> `backend/src/models/Category.ts:64` — model export
> `backend/src/services/marketplace/categoryStore.ts:89` — Postgres runtime bootstrap and duplicate cleanup
> `backend/src/db/schema/category.ts:88` — Drizzle active normalized-name unique index
## Schema
| Field | Type | Required | Default | Validation | Index | Description |
| --- | --- | --- | --- | --- | --- | --- |
| `name` | String | yes | — | trim | yes | Local language name. |
| `nameEn` | String | yes | — | trim | yes | English name. |
| `nameEn` | String | yes | — | trim | unique | English name. |
| `description` | String | no | — | trim | — | Description. |
| `icon` | String | no | — | trim | — | Icon identifier / URL. |
| `isActive` | Boolean | no | `true` | — | yes | Active flag. |
@@ -32,13 +34,19 @@ None defined.
## Indexes
Defined at `backend/src/models/Category.ts:55-58`:
Defined at `backend/src/models/Category.ts:55-62`:
- `{ name: 1 }`
- `{ nameEn: 1 }`
- `{ nameEn: 1 }`, unique
- `{ isActive: 1 }`
- `{ parentId: 1 }`
Postgres runtime and Drizzle additionally enforce:
- `categories_legacy_object_id_uq`: unique Mongo bridge id for idempotent backfill/upsert.
- `categories_active_name_norm_uq`: unique active category display label using `lower(btrim(name)) WHERE is_active = true`.
- Existing duplicate active PG rows are deactivated before the unique index is created; purchase-request category references and child category parents are repointed to the kept row.
## Pre/Post Hooks
None declared.
@@ -70,7 +78,7 @@ stateDiagram-v2
## Common Queries
```ts
// Top-level categories
// Top-level categories; runtime store dedupes active rows by normalized display name.
Category.find({ parentId: null, isActive: true }).sort({ order: 1 });
// Children of a category