Skip to content

Repository files navigation

Gyaan Setu – Sync Backend

Backend for syncing the Gyaan Setu app’s local SQLite database with a cloud (MongoDB) database.

Setup

npm install
cp .env.example .env   # then set MONGO_URI and PORT
npm run dev

Server runs at http://localhost:8080 (or PORT from env). Use EXPO_PUBLIC_API_URL=http://localhost:8080/v1 in the app for local sync.

How the app syncs

The app (see Gyaan_Setu/src/services/sync.service.ts and api.service.ts) does:

  1. Push (process sync queue)
    For each row in the local sync_queue table:

    • CREATE → POST /v1/items with body { name, description?, data? } → backend returns { _id, createdAt, updatedAt, ... } → app updates the local items row with mongodb_id and is_synced = 1.
    • UPDATE → PUT /v1/items/:id (id = stored mongodb_id) with the changed fields → app sets is_synced = 1.
    • DELETE → DELETE /v1/items/:id (id = stored mongodb_id) → app then deletes the local row.
  2. Pull (get updates)
    GET /v1/items/updates?since=<timestamp> (timestamp in ms). Backend returns items with updatedAt > since. The app upserts them into the local items table by mongodb_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.

API (v1)

Base path: /v1

Items (used by app sync)

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 results (optional cloud backup)

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.

Health

Method Path Description
GET /health Liveness check.

Environment

  • PORT – Server port (default 8080).
  • MONGO_URI – MongoDB connection string.

App configuration

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

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages