feat: API versioning, compound indexes, and new exports

- Add global /api/v1/ prefix with health/ready exclusions
- Add compound indexes on Property and Listing for query optimization
- Export CsrfMiddleware and UploadedFile type from shared infra
- New Prisma migration for compound indexes

Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
Ho Ngoc Hai
2026-04-09 01:27:17 +07:00
parent 60830d00d0
commit 45ebc6cf1d
4 changed files with 78 additions and 4 deletions

View File

@@ -0,0 +1,52 @@
-- AddCompoundIndexes: query optimization for Listing and Property tables
-- Addresses missing compound indexes identified in database audit (TEC-1566)
-- =============================================================================
-- LISTING COMPOUND INDEXES
-- =============================================================================
-- 1. Seller dashboard: WHERE sellerId = ? [AND status = ?] ORDER BY publishedAt DESC
-- Covers: findBySellerId(), getUserDetail() seller listings, admin user detail
-- Leading column sellerId has high cardinality; status + publishedAt enable
-- efficient range scans for "my active listings, newest first"
CREATE INDEX "Listing_sellerId_status_publishedAt_idx"
ON "Listing" ("sellerId", "status", "publishedAt" DESC NULLS LAST);
-- 2. Agent portfolio: WHERE agentId = ? AND status = ?
-- Covers: agent listing management, agent performance queries
-- agentId is nullable so Postgres skips NULLs in B-tree naturally
CREATE INDEX "Listing_agentId_status_idx"
ON "Listing" ("agentId", "status");
-- 3. Browse / search: WHERE status = ? ORDER BY createdAt DESC
-- Covers: search(), findByStatus(), getModerationQueue(), getDashboardStats() counts,
-- reindexAll() WHERE status = 'ACTIVE' ORDER BY publishedAt
-- Most listing queries filter on status first then paginate by time
CREATE INDEX "Listing_status_createdAt_idx"
ON "Listing" ("status", "createdAt" DESC);
-- 4. Active listings feed: WHERE status = 'ACTIVE' ORDER BY publishedAt DESC
-- The search indexer and public listing feeds sort by publishedAt for active listings
CREATE INDEX "Listing_status_publishedAt_idx"
ON "Listing" ("status", "publishedAt" DESC NULLS LAST);
-- 5. Transaction type filtering: WHERE transactionType = ? AND status = ? ORDER BY createdAt DESC
-- Search queries commonly filter by SALE/RENT combined with status
CREATE INDEX "Listing_transactionType_status_createdAt_idx"
ON "Listing" ("transactionType", "status", "createdAt" DESC);
-- =============================================================================
-- PROPERTY COMPOUND INDEXES
-- =============================================================================
-- 6. Location + type search: WHERE district = ? AND propertyType = ?
-- Extends existing (district, city) index for the very common search pattern
-- that filters by district AND propertyType (via Listing -> Property relation)
-- Note: district and propertyType live on Property, not Listing
CREATE INDEX "Property_district_propertyType_idx"
ON "Property" ("district", "propertyType");
-- 7. Full location + type search: WHERE district = ? AND city = ? AND propertyType = ?
-- Covers the search() query pattern that filters all three together
CREATE INDEX "Property_district_city_propertyType_idx"
ON "Property" ("district", "city", "propertyType");