A production-ready collaborative wiki backend built with Node.js, Express.js, and MongoDB. Features optimistic concurrency control (OCC), full-text search, aggregation analytics, JWT authentication, and schema migration strategies.
- JWT Authentication: Token-based auth protecting all mutating endpoints (POST, PUT, DELETE)
- Optimistic Concurrency Control (OCC): Prevents lost updates during collaborative editing without locking
- Full-Text Search: MongoDB
$textindex with relevance scoring, tag filtering, and pagination - Advanced Aggregations: Analytics endpoints for document edit counts and tag co-occurrences
- Schema Migrations: Dual strategy — lazy on-read transformation + background batch migration script
- Input Validation: Zod-based schema validation on all inputs
- Security Hardened: Helmet.js security headers and express-rate-limit
- Real Content Diffs: Uses the
difflibrary to generate proper unified diffs for revision history
Client → [JWT Auth] → Express.js API → MongoDB 7
↑
Migration Script (batch)
- Backend API: Express.js REST API with modular route handlers. All business logic (OCC, schema migration, analytics) is encapsulated in route modules.
- Database: MongoDB 7 in Docker. Documents collection stores content, metadata, and a capped revision history (last 20 entries) in a single document for optimized reads.
- Auth: JWT tokens issued via
POST /api/auth/token. Protected routes validate theAuthorization: Bearer <token>header.
- Docker and Docker Compose
-
Clone the repository:
git clone https://github.com/nnssprasad97/collaborative-document-store.git cd collaborative-document-store -
Start services:
docker-compose up --build
This will:
- Start MongoDB 7 with a health check
- Build and start the Node.js API on port 3000
- Auto-seed 10,000 documents on first run
- Create text and unique indexes
-
The API is available at
http://localhost:3000
| Method | Endpoint | Description |
|---|---|---|
| POST | /api/auth/token |
Get a JWT token (provide id, name, email) |
| Method | Endpoint | Auth | Description |
|---|---|---|---|
| POST | /api/documents |
✅ | Create a new document |
| GET | /api/documents/:slug |
❌ | Retrieve a document by slug |
| PUT | /api/documents/:slug |
✅ | Update with OCC (send version) |
| DELETE | /api/documents/:slug |
✅ | Delete a document |
| Method | Endpoint | Description |
|---|---|---|
| GET | /api/search?q=<term>&tags=<t1>,<t2>&page=1&limit=20 |
Full-text search with tag filtering and pagination |
| Method | Endpoint | Description |
|---|---|---|
| GET | /api/analytics/most-edited?limit=10 |
Top N most-edited documents |
| GET | /api/analytics/tag-cooccurrence?limit=50 |
Tag pair co-occurrence frequency |
Tests use Jest + Supertest and require a running MongoDB instance:
# With Docker MongoDB running:
npm test# Inside the Docker container:
docker-compose exec api node scripts/migrate_author_schema.js
# Or locally:
node scripts/migrate_author_schema.jsSee .env.example for all required variables:
| Variable | Description | Default |
|---|---|---|
MONGO_URI |
MongoDB connection string | mongodb://localhost:27017/collab_docs |
DATABASE_NAME |
Database name | collab_docs |
PORT |
API server port | 3000 |
JWT_SECRET |
Secret for JWT signing | (required) |
├── docker-compose.yml # Docker orchestration
├── Dockerfile # API container build
├── package.json # Dependencies and scripts
├── .env.example # Environment variable docs
├── src/
│ ├── server.js # Express app entry point
│ ├── db.js # MongoDB connection + index management
│ ├── seed.js # Database seeding (10,000 docs)
│ ├── middleware/
│ │ └── auth.js # JWT authentication middleware
│ └── routes/
│ ├── auth.js # Token generation endpoint
│ ├── documents.js # CRUD + OCC endpoints
│ ├── search.js # Full-text search endpoint
│ └── analytics.js # Aggregation pipeline endpoints
├── scripts/
│ └── migrate_author_schema.js # Background schema migration
└── tests/
└── api.test.js # Integration test suite