Skip to content

Configuration

Tổng quan

Gói Core cung cấp quản lý cấu hình tập trung qua biến môi trường, constants, định nghĩa kênh sự kiện, và định nghĩa queue. Mọi service kế thừa các cấu hình này cho hành vi nhất quán trên toàn nền tảng BANA.

Liên kết nhanh

PhầnMô tả
ConstantsHằng tĩnh, enum, và định nghĩa schema
Environment VariablesTham chiếu biến môi trường đầy đủ
EventsĐịnh nghĩa sự kiện và kiểu payload cho giao tiếp xuyên service
Cross-Package Messaging (Kafka / CDC)Hợp đồng topic sự kiện domain Kafka + CDC Debezium

Cấu trúc thư mục

packages/core/src/common/
├── index.ts              # Mọi export
├── environments.ts       # Định nghĩa key môi trường (re-export qua keys.ts)
├── constants.ts          # Hằng tĩnh
├── decorators.ts         # Decorator tuỳ chỉnh
├── validations.ts        # Tiện ích validation
├── events/               # Định nghĩa sự kiện
│   ├── index.ts
│   ├── commerce-events.ts
│   ├── ledger-events.ts
│   ├── payment-events.ts
│   └── websocket-events.ts
├── kafka/                # Hợp đồng topic + message Kafka
│   ├── index.ts
│   ├── topics.ts         # KafkaTopics + CDCKafkaTopics
│   └── types.ts
└── cdc/                  # Hằng bảng CDC Debezium
    ├── index.ts
    └── tables.ts

Biến môi trường

Lớp EnvironmentKeys mở rộng base keys của IGNIS Framework với cấu hình riêng application:

typescript
import { EnvironmentKeys as BaseEnvironmentKeys } from '@venizia/ignis';

export class EnvironmentKeys extends BaseEnvironmentKeys {
  // Application
  static readonly APP_ENV_NODE_ENV = 'APP_ENV_NODE_ENV';
  static readonly APP_ENV_APPLICATION_EXPLORER_URL = 'APP_ENV_APPLICATION_EXPLORER_URL';

  // Database
  static readonly APP_ENV_POSTGRES_HOST = 'APP_ENV_POSTGRES_HOST';
  static readonly APP_ENV_POSTGRES_PORT = 'APP_ENV_POSTGRES_PORT';
  // ... thêm key
}

Phân loại

Phân loạiVí dụMô tả
ApplicationAPP_ENV_NODE_ENV, APP_ENV_APPLICATION_NAMECài đặt app cốt lõi
DatabaseAPP_ENV_POSTGRES_HOST, APP_ENV_POSTGRES_PORTKết nối PostgreSQL
Redis/BullMQAPP_ENV_BULLMQ_REDIS_HOSTKết nối job queue
StorageAPP_ENV_MINIO_HOST, APP_ENV_MINIO_ACCESS_KEYLưu trữ MinIO/S3
MailAPP_ENV_MAIL_HOST, APP_ENV_MAIL_USERCấu hình SMTP
ID GenerationAPP_ENV_SNOWFLAKE_WORKER_IDCấu hình Snowflake ID
ServicesAPP_ENV_IDENTITY_SERVICE_BASE_URLURL liên service

Xem mọi Environment Variables -->

Constants

Giá trị cấu hình tĩnh dùng khắp application:

PostgreSQL Schemas

typescript
export class PostgresSchemas {
  static readonly PUBLIC = 'public';
  static readonly PRICING = 'pricing';
  static readonly ALLOCATION = 'allocation';
  static readonly SALE = 'sale';
  static readonly INVENTORY = 'inventory';
  static readonly FINANCE = 'finance';
  static readonly PAYMENT = 'payment';
}

Fixed User Roles

typescript
export class FixedUserRoles {
  static readonly SUPER_ADMIN = '999-super-admin';
  static readonly ADMIN = '998-admin';
  static readonly OPERATOR = '997-operator';
  static readonly ORGANIZER_OWNER = '899-organizer-owner';
  static readonly EMPLOYEE = '898-employee';

  static readonly ALWAYS_ALLOW_ROLES = new Set([
    FixedUserRoles.SUPER_ADMIN,
    FixedUserRoles.ADMIN,
  ]);
}

Xem mọi Constants -->

Events

Định nghĩa sự kiện có kiểu cho giao tiếp xuyên service, publish/subscribe qua event bus (IEventBus, adapter Redis Pub/Sub). Định nghĩa trong src/common/events/: commerce-events.ts, ledger-events.ts, payment-events.ts, websocket-events.ts.

Danh sách event-class chính xác bên dưới là một ảnh chụp một phần — xem Events và file source cho tập hiện tại.

Tiện ích WebSocket

LớpMô tả
WebSocketRoomsDựng định danh room chuẩn hoá (wr:{prefix}/{paths})
WebSocketTopicsDựng định danh topic chuẩn hoá (ws:{paths})

Xem mọi Events -->

Cross-Package Messaging (Kafka / CDC)

Đường nối xuyên package là Kafka (sự kiện domain) và Debezium CDC, không phải một hệ thống queue BullMQ. Hằng topic nằm trong src/common/kafka/topics.ts.

LớpMục đích
KafkaTopicsTên topic sự kiện domain + prefixer build({ paths }) (nx.seller)
CDCKafkaTopicsTên topic CDC Debezium dựng từ CdcTables

Xem Cross-Package Messaging -->

Cách dùng

Truy cập biến môi trường

typescript
import { applicationEnvironment } from '@venizia/ignis';
import { EnvironmentKeys } from '@nx/core';

// Lấy biến bắt buộc
const dbHost = applicationEnvironment.get<string>(EnvironmentKeys.APP_ENV_POSTGRES_HOST);

// Lấy với mặc định
const port = applicationEnvironment.get<number>(EnvironmentKeys.APP_ENV_POSTGRES_PORT) ?? 5432;

Dùng Constants

typescript
import { FixedUserRoles, DeviceTypes, PostgresSchemas } from '@nx/core';

// Kiểm tra role
if (userRoles.includes(FixedUserRoles.SUPER_ADMIN)) {
  // Logic super admin
}

// Validate kiểu thiết bị
if (DeviceTypes.isValid(device.type)) {
  // Kiểu thiết bị hợp lệ
}

// Tham chiếu schema
const schema = PostgresSchemas.PRICING;

Luồng cấu hình

Thực hành tốt

1. Dùng Key an toàn kiểu

typescript
// Tốt -- an toàn kiểu
const host = applicationEnvironment.get<string>(EnvironmentKeys.APP_ENV_POSTGRES_HOST);

// Tệ -- dễ lỗi
const host = process.env.APP_ENV_POSTGRES_HOST;

2. Validate cấu hình bắt buộc

typescript
// Lúc khởi động application
function validateConfig() {
  const required = [
    EnvironmentKeys.APP_ENV_POSTGRES_HOST,
    EnvironmentKeys.APP_ENV_POSTGRES_DATABASE,
    EnvironmentKeys.APP_ENV_SNOWFLAKE_WORKER_ID,
  ];

  for (const key of required) {
    if (!applicationEnvironment.get(key)) {
      throw new Error(`Missing required environment variable: ${key}`);
    }
  }
}

3. Dùng Constants cho giá trị magic

typescript
// Tốt -- dùng constant
if (user.role === FixedUserRoles.SUPER_ADMIN) { }

// Tệ -- chuỗi magic
if (user.role === '999-super-admin') { }

Tài liệu liên quan

Proprietary and Confidential. Unauthorized copying, distribution, or use of this software is strictly prohibited.