Skip to content

API Events

@nx/search là một CDC consumer. Nó đăng ký 18 Debezium topic và chỉ phát message tới một dead-letter queue. Không có business event outbound, không WebSocket, không BullMQ. Tên topic theo quy ước Debezium {prefix}.{schema}.{table} với prefix nx.seller.

1. Inbound - Kafka (CDC)

Tất cả topic được định nghĩa trong SearchCollections.CDC (src/common/kafka-topics.ts); consumer đăng ký ALL_CDC_TOPICS. Handler là CDCService.handleBatch() cho mọi topic.

Nguồn document trực tiếp (ánh xạ trong TableToCollectionMap)

TopicSchema.Table→ Collection
nx.seller.public.Organizerpublic.Organizerorganizers
nx.seller.public.Merchantpublic.Merchantmerchants
nx.seller.public.Categorypublic.Categorycategories
nx.seller.public.Devicepublic.Devicedevices
nx.seller.public.SaleChannelpublic.SaleChannelsale-channels
nx.seller.public.Productpublic.Productproducts
nx.seller.public.ProductInfopublic.ProductInfoproducts (i18n partial)
nx.seller.public.ProductVariantpublic.ProductVariantproduct-variants
nx.seller.inventory.InventoryStockinventory.InventoryStockinventories

Nguồn chỉ cascade (không phải nguồn document trực tiếp - lan toả qua CDCCascadeService)

TopicSchema.TableTính lại
nx.seller.public.ProductCategorypublic.ProductCategoryproducts + product-variants categoryIds
nx.seller.public.MetaLinkpublic.MetaLinkproduct → variant productMetaLinks; variant metaLinks
nx.seller.pricing.FareSetpricing.FareSetvariant fareSet + defaultPrice
nx.seller.pricing.Farepricing.Farevariant fareSet + defaultPrice
nx.seller.public.ProductBundlerpublic.ProductBundlervariant comboItems
nx.seller.inventory.InventoryIteminventory.InventoryItemnhóm item của inventories
nx.seller.inventory.InventoryLocationinventory.InventoryLocationnhóm location của inventories
nx.seller.inventory.InventoryIdentifierinventory.InventoryIdentifierinventories identifiers[]
nx.seller.inventory.Materialinventory.Materialitem của inventories (kiểu Material)

Mã op Debezium

OpÝ nghĩaHành động
ctạoupsert document
ucập nhậtupsert document
dxoáxoá document
rđọc snapshotupsert document (nạp lần đầu)

Bất kỳ op nào mà mapper trả về null (đã soft-delete) đều trở thành lệnh xoá trên Typesense.

2. Outbound - Kafka

TopicKích hoạtBên tiêu thụPayload
nx.seller.cdc.dlq (mặc định, ghi đè qua APP_ENV_CDC_DLQ_TOPIC)Một message CDC xử lý thất bại sau khi thử lạiOps / công cụ replay thủ côngMessage CDC gốc + metadata lỗi

3. Inbound - BullMQ

N/A - không có BullMQ consumer. (Thiết kế embedding-queue trong selection report là một phương án tương lai, chưa được triển khai.)

4. Outbound - BullMQ

N/A - không có BullMQ producer.

5. WebSocket Emissions

N/A - thư viện không phát WebSocket event nào. Cập nhật UI thời gian thực là việc của host service.

6. Payload Schemas

Payload CDC là Debezium envelope, được giải mã bằng avsc. Search không sở hữu các schema này - chúng phản chiếu bảng nguồn. Cấu trúc (rút gọn):

ts
// Debezium change event (Avro-decoded)
interface DebeziumEnvelope<TBefore, TAfter> {
  op: 'c' | 'u' | 'd' | 'r';
  before: TBefore | null;
  after: TAfter | null;
  source: {
    schema: string;     // public | pricing | inventory
    table: string;      // vd "Product"
    lsn: number;        // → chặn replay sai thứ tự (SearchVersionFields.SOURCE_LSN)
    ts_ms: number;
  };
}

Tham số truy vấn search (phía consumer, ISearchParams):

ts
interface ISearchParams {
  q?: string;                    // mặc định '*'
  limit?: number;                // mặc định 10, max 250
  offset?: number;
  where?: unknown;               // kiểu Ignis → filter_by
  order?: string | string[];     // "createdAt DESC" → sort_by
  include?: IIncludeSpec[];      // bù dữ liệu quan hệ / native join
  useCache?: boolean;
  cacheTtl?: number;
  disableSemanticSearch?: boolean; // bỏ embedding khỏi query_by (thuần từ khoá)
}

7. Idempotency & Ordering

Loại topicPhân phốiThứ tựPhục hồi
Tất cả topic CDCat-least-once (tắt auto-commit; commit sau batch)theo key qua Debezium; bộ chặn LSN loại bỏ event cũđọc lại offset chưa commit sau khi khởi động lại / circuit breaker đóng
DLQgửi-rồi-quên-replay thủ công
Bộ chặnTrườngTác dụng
LSNsource_lsn (SearchVersionFields.SOURCE_LSN)Bỏ qua event nếu LSN của nó ≤ LSN document đã lưu
Tombstonedeleted_at (SearchVersionFields.DELETED_AT)So sánh để giữ lệnh xoá không bị upsert cũ hoàn tác

8. Trang liên quan

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