localgreenchain/lib/db/index.ts
Claude 3d2ccdc29a
feat(db): implement PostgreSQL database integration with Prisma ORM
Agent 2 - Database Integration (P0 Critical):

- Add Prisma ORM with PostgreSQL for persistent data storage
- Create comprehensive database schema with 20+ models:
  - User & authentication models
  - Plant & lineage tracking
  - Transport events & supply chain
  - Vertical farming (farms, zones, batches, recipes)
  - Demand & market matching
  - Audit logging & blockchain storage

- Implement complete database service layer (lib/db/):
  - users.ts: User CRUD with search and stats
  - plants.ts: Plant operations with lineage tracking
  - transport.ts: Transport events and carbon tracking
  - farms.ts: Vertical farm and crop batch management
  - demand.ts: Consumer preferences and market matching
  - audit.ts: Audit logging and blockchain integrity

- Add PlantChainDB for database-backed blockchain
- Create development seed script with sample data
- Add database documentation (docs/DATABASE.md)
- Update package.json with Prisma dependencies and scripts

This provides the foundation for all other agents to build upon
with persistent, scalable data storage.
2025-11-23 03:56:40 +00:00

156 lines
3.3 KiB
TypeScript

/**
* LocalGreenChain Database Service Layer
* Central export for all database operations
*/
// Prisma client singleton
export { default as prisma, prisma as db } from './prisma';
// Types and utilities
export * from './types';
// User operations
export * as users from './users';
export {
createUser,
getUserById,
getUserByEmail,
getUserByWalletAddress,
updateUser,
deleteUser,
getUsers,
getUsersByType,
updateLastLogin,
searchUsers,
getUserWithPlants,
getUserWithFarms,
getUserStats,
} from './users';
// Plant operations
export * as plants from './plants';
export {
createPlant,
getPlantById,
getPlantWithOwner,
getPlantWithLineage,
updatePlant,
updatePlantStatus,
deletePlant,
getPlants,
getPlantsByOwner,
getNearbyPlants,
searchPlants,
getPlantLineage,
clonePlant,
getPlantNetworkStats,
updatePlantBlockchain,
} from './plants';
// Transport operations
export * as transport from './transport';
export {
createTransportEvent,
getTransportEventById,
getTransportEventWithDetails,
updateTransportEvent,
updateTransportEventStatus,
deleteTransportEvent,
getTransportEvents,
getTransportEventsByPlant,
getPlantJourney,
getEnvironmentalImpact,
getUserCarbonFootprint,
createSeedBatch,
getSeedBatchById,
createHarvestBatch,
getHarvestBatchById,
} from './transport';
// Vertical farm operations
export * as farms from './farms';
export {
createVerticalFarm,
getVerticalFarmById,
getVerticalFarmWithZones,
updateVerticalFarm,
updateFarmStatus,
deleteVerticalFarm,
getVerticalFarms,
getVerticalFarmsByOwner,
createGrowingZone,
getGrowingZoneById,
getGrowingZoneWithBatch,
updateGrowingZone,
updateZoneStatus,
deleteGrowingZone,
getGrowingZonesByFarm,
updateZoneEnvironment,
createCropBatch,
getCropBatchById,
getCropBatchWithDetails,
updateCropBatch,
updateCropBatchStatus,
deleteCropBatch,
getCropBatches,
getActiveCropBatchesByFarm,
addCropBatchIssue,
createGrowingRecipe,
getGrowingRecipeById,
getGrowingRecipesByCrop,
incrementRecipeUsage,
recordResourceUsage,
getResourceUsage,
recordFarmAnalytics,
getFarmAnalytics,
} from './farms';
// Demand and market operations
export * as demand from './demand';
export {
upsertConsumerPreference,
getConsumerPreference,
getNearbyConsumerPreferences,
createDemandSignal,
getDemandSignalById,
getDemandSignals,
getActiveDemandSignals,
createSupplyCommitment,
getSupplyCommitmentById,
updateSupplyCommitment,
reduceCommitmentQuantity,
getSupplyCommitments,
findMatchingSupply,
createMarketMatch,
getMarketMatchById,
getMarketMatchWithDetails,
updateMarketMatchStatus,
getMarketMatches,
createSeasonalPlan,
getSeasonalPlanById,
updateSeasonalPlan,
updateSeasonalPlanStatus,
getSeasonalPlansByGrower,
createDemandForecast,
getLatestDemandForecast,
createPlantingRecommendation,
getPlantingRecommendations,
} from './demand';
// Audit and blockchain operations
export * as audit from './audit';
export {
createAuditLog,
getAuditLogs,
getEntityAuditLogs,
getUserAuditLogs,
getAuditLogsSummary,
createBlockchainBlock,
getBlockchainBlockByIndex,
getBlockchainBlockByHash,
getLatestBlockchainBlock,
getBlockchainBlocks,
verifyBlockchainIntegrity,
getBlockchainStats,
logEntityChange,
} from './audit';