Skip to content

nnssprasad97/collaborative-document-store

Repository files navigation

Collaborative Document Store

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.

Features

  • 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 $text index 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 diff library to generate proper unified diffs for revision history

Architecture

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 the Authorization: Bearer <token> header.

Prerequisites

  • Docker and Docker Compose

Quick Start

  1. Clone the repository:

    git clone https://github.com/nnssprasad97/collaborative-document-store.git
    cd collaborative-document-store
  2. 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
  3. The API is available at http://localhost:3000

API Endpoints

Authentication

Method Endpoint Description
POST /api/auth/token Get a JWT token (provide id, name, email)

Documents

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

Search

Method Endpoint Description
GET /api/search?q=<term>&tags=<t1>,<t2>&page=1&limit=20 Full-text search with tag filtering and pagination

Analytics

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

Running Tests

Tests use Jest + Supertest and require a running MongoDB instance:

# With Docker MongoDB running:
npm test

Running the Migration Script

# Inside the Docker container:
docker-compose exec api node scripts/migrate_author_schema.js

# Or locally:
node scripts/migrate_author_schema.js

Environment Variables

See .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)

Project Structure

├── 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

About

Production-ready collaborative wiki backend with MongoDB, OCC, JWT auth, full-text search, and schema migrations

Topics

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors