This is a Next.js project bootstrapped with create-next-app.
First, run the development server:
npm run dev
# or
yarn dev
# or
pnpm dev
# or
bun devOpen http://localhost:3000 with your browser to see the result.
This application periodically fetches random user data from an external API and stores it in a PostgreSQL database. It uses a backend implemented with Next.js API routes and a cron job for scheduled data fetching.
- Periodic User Data Fetching: Fetches user data every 5 minutes from the Random User API.
- Data Storage: Saves user details, including name, email, gender, and location, into a PostgreSQL database.
- Frontend Integration: Displays the fetched and stored user data using a client-side React application.
- Error Handling: Logs any errors during data fetching or database operations for debugging.
- Backend API (Next.js):
- API Route (
/api/cron-fetch):- Schedules a cron job using the
node-cronlibrary. - Fetches data from the Random User API.
- Processes the response and inserts user and location data into the database.
- Schedules a cron job using the
- API Route (
- Database Integration:
- Uses a PostgreSQL database with a connection pool (
pglibrary). - Two tables:
users: Stores user information.locations: Stores location details associated with users.
- Uses a PostgreSQL database with a connection pool (
-
Cron Job (
node-cron):- Executes every 5 minutes using the cron schedule
*/5 * * * *. - Fetches 5 random user records from the Random User API.
- Inserts the following into the database:
- User details (name, email, gender).
- Associated location (city, country).
- Executes every 5 minutes using the cron schedule
-
Error Handling:
- Logs errors during the cron job (e.g., API call failure or database insertion issues).
- Provides meaningful error messages in the console for debugging.
- Fetches data from the backend API using
axios. - Handles rendering and error display in case of data inconsistencies.
- Backend:
- Frontend:
- React with Next.js for SSR and frontend rendering.
axiosfor API calls.
| Column | Type | Description |
|---|---|---|
id |
SERIAL |
Primary key. |
name |
VARCHAR |
User's full name. |
email |
VARCHAR |
User's email address. |
gender |
VARCHAR |
User's gender. |
createdAt |
TIMESTAMP |
Timestamp of record creation. |
| Column | Type | Description |
|---|---|---|
id |
SERIAL |
Primary key. |
userId |
INTEGER |
Foreign key from users. |
city |
VARCHAR |
User's city. |
country |
VARCHAR |
User's country. |
-
Clone the Repository:
git clone <repository_url> cd <repository_directory>
-
Install Dependencies:
npm install
-
Set Up Environment Variables: Create a
.env.localfile in the root directory with the following variables:DATABASE_URL=<your_postgres_connection_string>
-
Run Database Migrations: Ensure the database is set up and migrate the schema:
CREATE TABLE users ( id SERIAL PRIMARY KEY, name VARCHAR(255), email VARCHAR(255), gender VARCHAR(50), createdAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); CREATE TABLE locations ( id SERIAL PRIMARY KEY, userId INTEGER REFERENCES users(id), city VARCHAR(255), country VARCHAR(255) );
-
Start the Development Server:
npm run dev
-
View the Application:
- Open http://localhost:3000 in your browser to view the application.
- API route for cron job:
/api/cron-fetch.
- Hydration Errors:
- Ensure no browser-specific code (e.g.,
window,localStorage) runs on the server during SSR.
- Ensure no browser-specific code (e.g.,
- Database Errors:
- Verify the database schema and ensure the
createdAtcolumn exists in theuserstable.
- Verify the database schema and ensure the
- API Errors:
- Check the Random User API endpoint and network connectivity.
- Pagination: Add pagination support for displaying users on the frontend.
- Authentication: Secure API routes with authentication.
- Frontend Enhancements: Add a more user-friendly UI for displaying and managing fetched data.
- Your Name: Owais
This documentation serves as a guide for understanding and running the project while highlighting key features and configurations.