This file provides guidance to WARP (warp.dev) when working with code in this repository.
npm install
docker-compose --env-file .env.development build api
docker-compose --env-file .env.development up -dcurl --location --request POST 'localhost:3000/api/storage/initializeDatabase'
curl --location --request POST 'localhost:3000/api/storage/loadConfigs'# Start development server with hot reload
npm run dev
# Build TypeScript to JavaScript
npm run build
# Start production server
npm start
# Run individual containers
docker-compose --env-file .env.development up postgres -d
docker-compose --env-file .env.development up redis -d
docker-compose --env-file .env.development up api -d
# Check application health
curl localhost:3000/health
# Environment setup
cp .env.example .env.development
# Edit .env.development with your configuration# Rebuild and restart all services
docker-compose --env-file .env.development down
docker-compose --env-file .env.development build
docker-compose --env-file .env.development up -d
# View logs
docker-compose logs api
docker-compose logs postgres
docker-compose logs redis
# Connect to PostgreSQL container for debugging
docker exec -it ais_postgres psql -U ${POSTGRES_USER} -d ${POSTGRES_DB}
# Complete environment reset (removes all data)
docker-compose --env-file .env.development down -v- Runtime: Node.js 20 with TypeScript (ES Modules)
- Web Framework: Hono.js with middleware for CORS, logging, timing, and pretty JSON
- Database: PostgreSQL 15 with connection pooling
- Cache: Redis 7 with memory optimization
- Authentication: JWT with refresh tokens
- Container: Docker with multi-stage builds
The application follows a layered architecture pattern:
Entry Point (src/index.ts)
- Configures Hono app with middleware stack
- Sets up error handling and CORS
- Initializes database and cache connections before server start
- Implements graceful shutdown handling
Service Layer Architecture
BaseService: Abstract base class providing database query execution patterns- All services inherit from BaseService for consistent data access
- Query execution abstracted through different return type methods (scalar, single row, multiple rows)
Authentication & Authorization System
- Role-based access control with hierarchical resources
- JWT authentication with refresh token support
- Resource tree caching in Redis for performance
- Complex permission model supporting access types and levels
Database Layer
- Singleton pattern for database connection management
- Connection pooling with health checks
- Sequential SQL file execution for database schema setup
- Support for multiple query return types (scalar, single row, multiple rows)
Route Organization
/api/storage: Database management and configuration loading/api/auth: Authentication, user management, and role assignment/api/generic: General-purpose API endpoints/health: Application health monitoring
Database Setup Flow
- Execute schema.sql for base structure
- Create authentication tables (user, role, resource, etc.)
- Set up geographical data tables (country, state, city, district)
- Initialize business domain tables (transport, material, warehouse, broker, party)
- Create order management tables (purchase_order, sales_order, picking_list)
- Load initial data and query functions
Caching Strategy
- User permissions cached with pattern:
user-{userId}-role-{roleId}-res-{resourceId}-at-{accessType}-al-{accessLevel} - Resource tree stored in Redis for hierarchical access control
- Cache invalidation on user-role mapping changes
Environment Configuration
- Uses
.env.developmentfor Docker Compose configuration - Supports PostgreSQL, Redis, and JWT configuration
- Admin user setup through environment variables
This API is heavily config-driven, with YAML/JSON configuration files defining UI forms, data tables, queries, and business logic.
Config Location: All configuration files are stored in data/fs/configs/
Config Types:
- List Configs: Define data tables with columns, actions, queries, and filters (e.g.,
list-users.yaml,list-categories.yaml) - Form Configs: Define input forms with fields, validation, and save actions (e.g.,
add-user.yaml,add-role.yaml) - Mixed Configs: Support both sections and multiple interaction patterns
List Configuration Pattern:
actionType: EXECUTE_QUERY
queryInfo:
returnType: MULTIPLE_ROWS
query: |
SELECT id, name, email FROM users WHERE active = true
options:
applyFiltering: true
applySorting: true
applyPagination: true
applicableActions:
- add
- edit
- delete
actionConfig:
add:
label: Add User
actionType: DISPLAY_FORM
formConfig: add-user
accessTypeRequired: 6-ADD
columns:
- name: username
label: Username
sortable: true
filterType: stringForm Configuration Pattern:
sections:
- sectionType: FIELDS
sectionName: User Details
queryInfo:
returnType: SINGLE_ROW
query: SELECT * FROM users WHERE id = $1
payload: [user_id]
applicableActions: [SAVE, CANCEL]
actionConfig:
SAVE:
actionType: EXECUTE_QUERY
queryInfo:
query: SELECT insert_user($1, $2, $3) as id
payload: [username, email, password]
fields:
- name: username
label: Username
type: TEXT
required: true
- name: password
label: Password
type: PASSWORD
required: trueQuery Return Types:
SCALAR: Single valueSINGLE_ROW: One database rowMULTIPLE_ROWS: Array of rowsSCALAR_ARRAY: Array of scalar values
Action Types:
EXECUTE_QUERY: Run database queryDISPLAY_FORM: Show form interfaceFUNCTION_CALL: Execute JavaScript function
Field Types:
TEXT,TEXTAREA,PASSWORDSELECT(withmulti: true/false)- Complex select configurations with
selectConfig
Access Control:
accessTypeRequired: Format like{resourceId}-{ACTION}(e.g.,6-ADD,7-EDIT)- Automatically filtered based on user permissions
- Startup: All YAML/JSON configs loaded into Redis cache via
/api/storage/loadConfigs - Runtime: Configs retrieved from cache, parsed, and permission-filtered per user
- Query Building: Dynamic query construction with pagination, sorting, filtering
- Action Execution: Config-driven query execution with parameter binding
To add new functionality:
- Create SQL tables in
data/fs/setup-queries/ - Add list config YAML for data display
- Add form config YAML for data entry/editing
- Define access permissions in role claims
- Reload configs via API endpoint
No code changes required - everything is configuration-driven!
- YAML files in
/data/fs/configs/define API endpoints and form configurations - Dynamic query execution based on configuration files
- Use
loadConfigsendpoint after modifying YAML configurations
The BaseService class provides typed query execution methods:
// For single values
this.executeScalarQuery(query, params)
// For single rows
this.executeSingleRowQuery(query, params)
// For multiple rows
this.executeMultipleRowsQuery(query, params)- Use
npm run devfor TypeScript hot reloading viatsx - Docker volume mounts
/srcfor live code updates in containers - Database schema changes require calling
initializeDatabaseendpoint
/src/storage/db.ts- Database connection and query execution/src/storage/cache.ts- Redis connection management/data/fs/setup-queries/- SQL schema files (executed in specific order)/data/fs/configs/- YAML configuration files for dynamic endpoints/src/types/models.ts- TypeScript interfaces for database entities
This API manages a comprehensive business system including:
- User Management: Authentication, authorization, and role-based access
- Geographical Data: Country, state, city, district, and address management
- Inventory: Material management with categories, brands, and units of measure
- Warehouse Operations: Palette and warehouse management
- Trading: Broker and party management
- Order Processing: Purchase orders, sales orders, and picking lists
- Transport Management: Logistics and shipping coordination
The system implements a sophisticated permission model where resources are organized hierarchically, and users can have different access types and levels based on their assigned roles.
- YAML files in
/data/fs/configs/define API endpoints and form configurations - Dynamic query execution based on configuration files
- Use
loadConfigsendpoint after modifying YAML configurations
The BaseService class provides typed query execution methods:
// For single values
this.executeScalarQuery(query, params)
// For single rows
this.executeSingleRowQuery(query, params)
// For multiple rows
this.executeMultipleRowsQuery(query, params)- Use
npm run devfor TypeScript hot reloading viatsx - Docker volume mounts
/srcfor live code updates in containers - Database schema changes require calling
initializeDatabaseendpoint
/src/storage/db.ts- Database connection and query execution/src/storage/cache.ts- Redis connection management/data/fs/setup-queries/- SQL schema files (executed in specific order)/data/fs/configs/- YAML configuration files for dynamic endpoints/src/types/models.ts- TypeScript interfaces for database entities