Skip to content

Domain Model

@nx/search không sở hữu bảng PostgreSQL nào. "Domain" của nó là tập các Typesense collection - projection đọc phi chuẩn hoá của các bảng nguồn trong schema public, pricing, và inventory. Định nghĩa schema nằm trong src/configurations/<entity>.collection.ts (cấu trúc dùng chung trong schema-fragments.ts).

1. ERD đầy đủ

Bảng nguồn CDC (trái) được chiếu vào Typesense collection (phải). Nét liền = nguồn document trực tiếp; nét đứt = chỉ cascade.

2. Entities (Collections)

Mỗi khối là một collection. Mỗi document Typesense có cấu trúc khá phẳng; i18n { en, vi } được làm phẳng thành field.en / field.vi. queryBy liệt kê các trường text có thể tìm; các trường facetable có thể lọc.

organizers

Thuộc tínhGiá trị
Sourcepublic.Organizer (trực tiếp)
Configsrc/configurations/organizer.collection.ts
Doc idOrganizer.id
queryByname.en, name.vi, description.en, description.vi, slug, identifier
Relationsparent (organizers, bù dữ liệu)
Embeddingtuỳ chọn

merchants

Thuộc tínhGiá trị
Sourcepublic.Merchant (trực tiếp)
Configsrc/configurations/merchant.collection.ts
Doc idMerchant.id
queryByname.en, name.vi, description.en, description.vi, slug, identifier
Relationsorganizer (join gốc), parent (merchants, bù dữ liệu)

categories

Thuộc tínhGiá trị
Sourcepublic.Category (trực tiếp)
Configsrc/configurations/category.collection.ts
Doc idCategory.id
queryByname.en, name.vi, description.en, description.vi
Relationsmerchant (join gốc)

devices

Thuộc tínhGiá trị
Sourcepublic.Device (trực tiếp)
Configsrc/configurations/device.collection.ts
Doc idDevice.id
queryByname.*, identifier, code
Relationsorganizer (join gốc), merchant (join gốc)

sale-channels

Thuộc tínhGiá trị
Sourcepublic.SaleChannel (trực tiếp)
Configsrc/configurations/sale-channel.collection.ts
Doc idSaleChannel.id
queryByname.en, name.vi, slug, identifier
Relationsmerchant (join gốc), parent (sale-channels, bù dữ liệu)

products

Thuộc tínhGiá trị
Sourcepublic.Product + public.ProductInfo (i18n) - cả hai trực tiếp (info là cập nhật một phần)
CascadeProductCategorycategoryIds[]
Configsrc/configurations/product.collection.ts (+ product-info-mapper)
Doc idProduct.id
queryByidentifier, slug, info.name.{en,vi}, info.description.{en,vi}, merchant_name.{en,vi}, organizer_name.{en,vi}, sale_channel_names
Facetsstatus, merchantId (ref merchants.id), categoryIds[], parentId, tên parent đã phi chuẩn hoá
Relationsmerchant (join gốc), parent (products, bù dữ liệu)
Embeddingtrường embedding từ [identifier, slug]

product-variants

Thuộc tínhGiá trị
Sourcepublic.ProductVariant (trực tiếp) + ProductVariantInfo
CascadeProductCategory (categoryIds), MetaLink (metaLinks), FareSet/Fare (fareSet, defaultPrice), ProductBundler (comboItems)
Configsrc/configurations/product-variant.collection.ts
Doc idProductVariant.id
Relationsproduct (join gốc), merchant (join gốc)

inventories

Thuộc tínhGiá trị
Sourceinventory.InventoryStock (trực tiếp, mỗi document một dòng)
CascadeInventoryItem, InventoryLocation, InventoryIdentifier, Material
Configsrc/configurations/inventory.collection.ts
Doc idInventoryStock.id (== inventoryStockId)
queryByitem.name.{en,vi}, location.name.{en,vi}, identifiers.value, stock.lotNumber, stock.serialNumber, item.slug, item.description.{en,vi}
Structurenhóm lồng item / location / stock; identifiers[] (không phụ thuộc scheme); flags[] (boolean dẫn xuất)
Notesflags.isExpired / isExpiringSoon có thể cũ - được làm mới bằng reindex định kỳ hoặc khoảng giá trị tại thời điểm truy vấn

3. Bất biến xuyên entity

Bất biếnCách thực thi
Document id luôn bằng id dòng nguồnMapper extractDocumentId / idField
Dòng nguồn bị soft-delete ⇒ document không tồn tạiMapper trả về nullCDCService xoá
Event CDC sai thứ tự không bao giờ ghi đè trạng thái mới hơnBộ chặn LSN (source_lsn) + so sánh deleted_at
products.categoryIds phản ánh mọi dòng ProductCategoryLan toả cascade + bổ sung dữ liệu (không phải mapper của dòng)
Document inventories là một-trên-mỗi-InventoryStock, item/location đã phi chuẩn hoáCascade từ InventoryItem/InventoryLocation
Trường reference của Typesense phải ở cấp cao nhấtSchema đặt merchantId, inventoryItemId, v.v. ở cấp cao nhất

4. Hành vi Soft-delete

Hành viChi tiết
Nguồn dữ liệu chínhPostgreSQL deletedAt (do service nguồn sở hữu)
Ảnh hưởng tới tìm kiếmMapper phát hiện deletedAt != null → phát lệnh xoá tới Typesense
Đọc mặc địnhDòng bị soft-delete không có document, nên không bao giờ xuất hiện trong kết quả
Xoá cứngMột op Debezium d cũng xoá document
Khôi phụcMột event CDC không-bị-xoá sau đó sẽ upsert lại document

5. Trang liên quan

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