---
title: "Architecture and source code structure"
url: https://phatdev.net/en/docs/erp-tan-doanh/kien-truc
lang: en
updated: 2026-10-20
alternate:
  vi: https://phatdev.net/docs/erp-tan-doanh/kien-truc
markdown: https://phatdev.net/en/docs/erp-tan-doanh/kien-truc.md
---

# Architecture and source code structure

Part of the doc set: [ERP Tân Doanh](https://phatdev.net/en/docs/erp-tan-doanh.md)

> The application, database, business core and naming conventions.

## Architecture
A single **Next.js 16** application (App Router, Server Components and Server Actions) talking directly to **PostgreSQL 16**, packaged with Docker. Sign-in is delegated to a shared authentication service.

**Why PostgreSQL:** the schema needs `JSONB`, `timestamptz`, `EXCLUDE USING gist` constraints, triggers that block ledger edits, generated columns, `pg_trgm` and `unaccent`.

## Code layers
| Layer | Role |
|---|---|
| Screens (routes) | Server Components read data and check permissions; Server Actions handle every write |
| Business core | Inventory ledger posting, order lifecycle, reports, authentication and permissions, with no UI code |
| Reusable UI | Tables, forms, SVG icons, following the Tân Doanh design system, no emoji |
| Migrations | One numbered file per feature; building an empty database runs them all in order |

## Conventions
Folders, routes and business functions use Vietnamese names without diacritics; technical terms stay in English. There is an 8-step recipe for adding a feature: migration, business logic, action, screen, menu and permissions, labels, tests, build.

## Search and speed
The catalogue of more than 5,000 products downloads a slimmed-down copy to the browser so search is instant without asking the server; a trigram index handles fuzzy, accent-insensitive matching; navigation caching makes tab clicks appear almost immediately.
