Backend for syncing the Gyaan Setu app’s local SQLite database with a cloud (MongoDB) database.
npm install
cp .env.example .env # then set MONGO_URI and PORT
npm run devServer runs at http://localhost:8080 (or PORT from env). Use EXPO_PUBLIC_API_URL=http://localhost:8080/v1 in the app for local sync.
The app (see Gyaan_Setu/src/services/sync.service.ts and api.service.ts) does:
-
Push (process sync queue)
For each row in the localsync_queuetable:- CREATE →
POST /v1/itemswith body{ name, description?, data? }→ backend returns{ _id, createdAt, updatedAt, ... }→ app updates the localitemsrow withmongodb_idandis_synced = 1. - UPDATE →
PUT /v1/items/:id(id = storedmongodb_id) with the changed fields → app setsis_synced = 1. - DELETE →
DELETE /v1/items/:id(id = storedmongodb_id) → app then deletes the local row.
- CREATE →
-
Pull (get updates)
GET /v1/items/updates?since=<timestamp>(timestamp in ms). Backend returns items withupdatedAt > since. The app upserts them into the localitemstable bymongodb_id.
Sync runs when the device has suitable network (e.g. WiFi or fast cellular) and at least 30 seconds have passed since the last sync.
Base path: /v1
| Method | Path | Description |
|---|---|---|
POST |
/v1/items |
Create item. Body: { name, description?, data? }. Returns ApiItem (_id, createdAt, updatedAt, …). |
PUT |
/v1/items/:id |
Update item by MongoDB _id. Body: same as create (partial). |
DELETE |
/v1/items/:id |
Delete item by MongoDB _id. |
GET |
/v1/items/updates?since=<ms> |
Items with updatedAt > since (for pull sync). |
Quiz attempts are stored locally in SQLite. These endpoints allow backing them up to the cloud (you can later extend the app to push/pull quiz results the same way as items).
| Method | Path | Description |
|---|---|---|
POST |
/v1/quiz-results |
Create quiz result. Body: { quiz_id, score, total_questions, answers_json, created_at? }. |
GET |
/v1/quiz-results/updates?since=<ms> |
Quiz results with updatedAt > since. |
| Method | Path | Description |
|---|---|---|
GET |
/health |
Liveness check. |
PORT– Server port (default8080).MONGO_URI– MongoDB connection string.
In the app, set the API base URL so sync uses this backend:
- Development:
EXPO_PUBLIC_API_URL=http://<your-machine-ip>:8080/v1(use your LAN IP so the device/emulator can reach the server). - Production:
EXPO_PUBLIC_API_URL=https://your-api-host.com/v1.
The app’s ApiService (Gyaan_Setu/src/services/api.service.ts) uses this for all sync requests.
Gyaan_Setu_Backend
├─ package.json
├─ README.md
├─ src
│ ├─ controllers
│ │ └─ quizResults.controllers.ts
│ ├─ helpers
│ │ └─ quiz.helpers.ts
│ ├─ models
│ │ ├─ chapter.models.ts
│ │ ├─ language.models.ts
│ │ ├─ microlesson.models.ts
│ │ ├─ picture.models.ts
│ │ ├─ quiz.models.ts
│ │ ├─ quizResult.models.ts
│ │ ├─ subject.models.ts
│ │ └─ topic.models.ts
│ ├─ routes
│ │ ├─ index.routes.ts
│ │ └─ quizResults.routes.ts
│ ├─ server.ts
│ └─ types
│ └─ api.types.ts
└─ tsconfig.json