This document provides instructions for setting up and using the database for the Debt Cycles Dashboard.
The application uses PostgreSQL with Prisma ORM to cache data from the FRED API. This improves performance and reduces API calls to external services.
The database schema includes the following models:
- FredSeries - Information about FRED data series
- CachedFredData - The actual data points for each series
- LastFetchTimestamp - When each series was last updated
- PostgreSQL installed and running
- Node.js and npm
- Make sure your PostgreSQL server is running
- Update the
.envfile with your database connection URL:
DATABASE_URL="postgresql://username:password@localhost:5432/debt_cycles?schema=public"
Replace username, password with your actual PostgreSQL credentials.
Run the following commands to set up the database:
# Install dependencies
npm install
# Generate Prisma client and run migrations
npm run db:setup
# Seed the database with initial data
npm run seedThe database service provides several functions for working with FRED data:
getCachedFredData(seriesId)- Retrieves cached data for a FRED seriescacheFredData(seriesId, data, metricInfo)- Stores FRED data in the databaseshouldFetchFromApi(seriesId)- Determines if new data should be fetched from the APIgetFredSeriesForMetric(metricId)- Gets the FRED series ID for a metric
# Generate Prisma client after schema changes
npm run db:generate
# Run Prisma migrations for schema changes
npm run db:migrate
# Open Prisma Studio to view/edit database
npm run db:studio
# Run database seeding
npm run seedThe database service has comprehensive tests:
# Run all tests
npm test
# Run only database tests
npm test -- -t "FRED.*Database"If you encounter issues with the database:
- Check that PostgreSQL is running
- Verify the DATABASE_URL in your
.envfile - Try running
npm run db:setupagain - Check the Prisma logs for more details