This project demonstrates real-time synchronization between Google Sheets and a PostgreSQL database using Next.js for the frontend, Express.js for the backend, and Prisma as the ORM for PostgreSQL. Any changes made in Google Sheets will be reflected in the database, and vice versa. This project also features CRUD operations from the frontend, which will automatically update both the database and Google Sheets in real time using WebSockets.
This project implements a full-stack solution that keeps Google Sheets in sync with a PostgreSQL database. It allows the user to make changes either directly in Google Sheets or through a web-based frontend, and the changes are synchronized in real-time between Google Sheets and the database.
The project uses Google Sheets API to interact with Google Sheets and Prisma ORM to handle PostgreSQL database interactions. It also incorporates WebSocket connections for real-time data updates in the frontend.
- Next.js (Frontend)
- Express.js (Backend)
- PostgreSQL (Database)
- Prisma (ORM for database)
- Google Sheets API (for synchronization with Google Sheets)
- Socket.IO (WebSocket connection for real-time updates)
- Real-time Synchronization: Data is synced between Google Sheets and PostgreSQL in real time.
- CRUD Operations: Create, Read, Update, and Delete operations can be performed from the frontend, which are synced to both Google Sheets and the database.
- Google Sheets Triggers: A trigger is set up within Google Sheets to detect changes and automatically call the backend API to sync the data with the database.
- WebSockets: Real-time updates are handled through WebSocket connections, ensuring instant updates on the frontend when changes are made in Google Sheets or the database.
To run this project locally, follow these steps:
-
Clone the repository:
git clone https://github.com/StackItHQ/vit-ramtanniru cd vit-ramtanniru
-
Set up Google Sheets API:
- Go to the Google Cloud Console, create a project, and enable the Google Sheets API.
- Create credentials and download the
credentials.json
file. - Place this file in your
backend
folder.
-
Install Dependencies: In both the frontend and backend directories, run:
npm install
-
Set up environment variables: Create a
.env
file in the backend directory and add the necessary environment variables, including the PostgreSQL database connection URL and Google Sheets credentials. -
Run the project:
- Start the backend:
npm start:backend
- Start the frontend:
npm run dev:frontend
- Start the backend:
-
Set up Google Sheets Trigger: In the Google Sheets script editor, add a function that triggers on changes and sends data to the backend.
Once the project is running:
- Any changes made in the Google Sheets will automatically sync with the database.
- You can perform CRUD operations in the frontend, and the changes will be synced to both the database and Google Sheets.
-
Google Sheets API Setup:
- I first set up the Google Sheets API by creating a project in Google Cloud and downloading the API credentials.
- Configured the backend to interact with Google Sheets, fetching and updating data as required.
-
Backend Setup:
- I defined the Prisma schema to match the structure of the Google Sheets data.
- The backend handles CRUD operations on the database and Google Sheets sync through API endpoints and WebSockets.
-
WebSocket for Real-Time Updates:
- I implemented WebSocket connections to enable real-time updates on the frontend whenever the database or Google Sheets data changes.
-
Trigger Setup:
- I set up an onChange trigger in Google Sheets to call the backend API whenever data in the sheet is modified. This allows for seamless updates between Google Sheets and the database.
One of the main challenges I encountered was working with Google Sheets. Since it lacks built-in WebSocket support, I initially used polling to detect changes. However, this caused a 'Gauxis error' due to exceeding the maximum read quota from the Google Sheets API.
To resolve this issue, I considered using debouncing or batching API calls to reduce the frequency of reads, but I found a better approach. Instead of relying on frequent polling, I used Google Sheets triggers. These triggers, specifically the onChange event, notify the backend only when a change occurs in Google Sheets, significantly reducing the number of API calls and avoiding quota errors.