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:
@@ -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");
|
||||
@@ -193,9 +193,14 @@ model Property {
|
||||
valuations Valuation[]
|
||||
media PropertyMedia[]
|
||||
|
||||
// --- Single-column indexes ---
|
||||
@@index([propertyType])
|
||||
@@index([district, city])
|
||||
@@index([location], type: Gist)
|
||||
|
||||
// --- Compound indexes (query optimization) ---
|
||||
@@index([district, propertyType])
|
||||
@@index([district, city, propertyType])
|
||||
}
|
||||
|
||||
model PropertyMedia {
|
||||
@@ -242,6 +247,7 @@ model Listing {
|
||||
transactions Transaction[]
|
||||
inquiries Inquiry[]
|
||||
|
||||
// --- Single-column indexes ---
|
||||
@@index([status])
|
||||
@@index([transactionType])
|
||||
@@index([priceVND])
|
||||
@@ -252,6 +258,13 @@ model Listing {
|
||||
@@index([createdAt])
|
||||
@@index([featuredUntil])
|
||||
@@index([expiresAt])
|
||||
|
||||
// --- Compound indexes (query optimization) ---
|
||||
@@index([sellerId, status, publishedAt(sort: Desc)])
|
||||
@@index([agentId, status])
|
||||
@@index([status, createdAt(sort: Desc)])
|
||||
@@index([status, publishedAt(sort: Desc)])
|
||||
@@index([transactionType, status, createdAt(sort: Desc)])
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
|
||||
Reference in New Issue
Block a user