Entity types
TypeScript'da entity type'lar: BaseEntity, bog'lanishlarni id orqali saqlash, branded type'lar bilan id'larni chalkashtirmaslik, normalizatsiya (byId + allIds) va DTO bilan farqi.
91-dars
Entity types
Entity — bu o'z identifikatoriga (id) ega bo'lgan va vaqt o'tishi bilan o'zgarib turadigan obyekt: foydalanuvchi, buyurtma, mahsulot. Ikki entity'ning hamma maydoni bir xil bo'lsa ham, id farq qilsa, ular har xil narsa. Entity type'i ilovaning markaziy type'i bo'lib, DTO'lar (90-dars) unga aylantiriladi.
1. Umumiy bazaviy type
Deyarli har bir entity'da takrorlanadigan maydonlar bor:
type BaseEntity = {
id: number;
createdAt: Date;
updatedAt: Date;
};
type Product = BaseEntity & {
title: string;
price: number;
stock: number;
};
type Customer = BaseEntity & {
fullName: string;
phone: string;
};
2. Bog'lanishlar: id orqali yoki ichida
Entity'lar bir-biriga bog'langan. Ikki usul bor:
// a) faqat id saqlanadi (tavsiya etiladi)
type Order = BaseEntity & {
customerId: number;
items: OrderItem[];
status: "new" | "paid" | "shipped" | "cancelled";
};
// b) butun obyekt ichida
type OrderWithCustomer = Order & {
customer: Customer;
};
Asosiy type'da customerId saqlang. To'liq obyekt kerak bo'lgan joyda alohida type yasang. Aks holda bitta mijoz 10 ta buyurtma ichida 10 marta takrorlanadi va yangilanganda nusxalar eskirib qoladi.
3. id'larni chalkashtirmaslik
function getOrder(id: number) { /* ... */ }
getOrder(customer.id); // ⚠️ TS indamaydi, ikkalasi ham number
Buni hal qilish uchun branded type'lar ishlatiladi — oddiy number'ga ko'rinmas "yorliq" qo'shiladi:
type OrderId = number & { __brand: "OrderId" };
type CustomerId = number & { __brand: "CustomerId" };
type Order = BaseEntity & { id: OrderId; /* ... */ };
type Customer = BaseEntity & { id: CustomerId; /* ... */ };
function getOrder(id: OrderId) { /* ... */ }
getOrder(customer.id);
// ❌ Argument of type 'CustomerId' is not assignable to parameter of type 'OrderId'.
__brand maydoni faqat type darajasida mavjud, runtime'da yo'q — u shunchaki ikki number'ni bir-biridan ajratish uchun. Branded type'ni yaratishda esa as kerak bo'ladi (5 as OrderId), chunki oddiy number unga avtomatik mos kelmaydi.
Real misol: frontend store'da entity'lar
Ro'yxat ko'p bo'lsa, entity'lar id bo'yicha normalizatsiya qilinadi:
type EntityState<T extends { id: number }> = {
byId: Record<number, T>;
allIds: number[];
};
type StoreState = {
products: EntityState<Product>;
customers: EntityState<Customer>;
orders: EntityState<Order>;
};
function getOrderDetails(state: StoreState, orderId: number) {
const order = state.orders.byId[orderId];
if (!order) return null;
const customer = state.customers.byId[order.customerId];
return { ...order, customer }; // OrderWithCustomer
}
function updateProductPrice(state: StoreState, id: number, price: number) {
const product = state.products.byId[id];
if (product) {
state.products.byId[id] = { ...product, price, updatedAt: new Date() };
}
}
Mahsulot narxi bitta joyda o'zgaradi, uni ishlatgan hamma joy yangi narxni ko'radi.
DTO va Entity farqi
| DTO | Entity | |
|---|---|---|
| Qayerda | API chegarasida | Ilova ichida |
| Shakli | Server qanday bersa | Ilovaga qulay |
| Sana | string | Date |
| Asosiy belgisi | Tarmoq formati | id (identifikator) |
Eslab qolish uchun
- Entity =
id'ga ega, o'zgaradigan obyekt. - Umumiy maydonlarni
BaseEntity'ga chiqaring. - Bog'lanishni
xxxIdorqali saqlang. - Ko'p entity'ni
byId+allIdsko'rinishida saqlang.