Skip to content

Divyansh-007/shared-code-backend

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Shared Code Backend

A robust shared code service built with NestJS, Prisma, and MongoDB. This backend API provides a complete paste/code sharing platform with advanced features for managing code snippets.

Features

  • Create, read, update, and delete pastes
  • Password protection for pastes
  • Expiration dates for pastes
  • Public/private visibility settings
  • View counter tracking
  • Pagination support
  • Author IP tracking
  • Automatic cleanup of expired pastes
  • Swagger API documentation
  • Custom logger module
  • TypeScript support with strict typing
  • Comprehensive testing setup

Quick Start

Prerequisites

  • Node.js (v16 or higher)
  • MongoDB database (local or MongoDB Atlas)
  • Yarn package manager

Setup Instructions

  1. Clone and install dependencies

    yarn install
  2. Configure environment

    cp env-example .env

    Edit .env and set your MongoDB connection string:

    DATABASE_URL="mongodb://localhost:27017/sharedcode"
    
  3. Setup database

    yarn migrate
  4. Start development server

    yarn dev
  5. Test the API

    Create a paste:

    curl -X POST http://localhost:3000/pastes \
      -H "Content-Type: application/json" \
      -d '{
        "title": "Hello World",
        "content": "console.log(\"Hello, World!\");",
        "language": "javascript"
      }'

Available Scripts

Development

  • yarn dev - Start in development mode with hot reload
  • yarn start - Start the application
  • yarn build - Build the application for production
  • yarn start:prod - Start in production mode

Database

  • yarn migrate - Run database migrations and generate Prisma client
  • yarn generate - Generate Prisma client
  • yarn db:dev:console - Open Prisma Studio for database management
  • yarn db:dev:migrate - Push database schema and generate client
  • yarn db:test:console - Open Prisma Studio for test database
  • yarn db:test:migrate - Push test database schema and generate client

Code Quality

  • yarn lint:check - Check for linting errors
  • yarn lint:fix - Fix linting errors automatically
  • yarn format:check - Check code formatting
  • yarn format:fix - Fix code formatting
  • yarn format - Format all TypeScript files
  • yarn type-check - Run TypeScript type checking

Testing

  • yarn test - Run unit tests
  • yarn test:watch - Run tests in watch mode
  • yarn test:cov - Run tests with coverage report
  • yarn test:e2e - Run end-to-end tests
  • yarn test:debug - Run tests in debug mode

Project Structure

src/
β”œβ”€β”€ paste/                    # Paste module
β”‚   β”œβ”€β”€ dto/                 # Data Transfer Objects
β”‚   β”‚   β”œβ”€β”€ create-paste.dto.ts
β”‚   β”‚   β”œβ”€β”€ update-paste.dto.ts
β”‚   β”‚   └── index.ts
β”‚   β”œβ”€β”€ response/            # Response DTOs
β”‚   β”‚   β”œβ”€β”€ paste-response.dto.ts
β”‚   β”‚   β”œβ”€β”€ paste-list-response.dto.ts
β”‚   β”‚   β”œβ”€β”€ paginated-response.interface.ts
β”‚   β”‚   └── index.ts
β”‚   β”œβ”€β”€ docs/                # API documentation
β”‚   β”‚   β”œβ”€β”€ swagger.constants.ts
β”‚   β”‚   β”œβ”€β”€ api-examples.ts
β”‚   β”‚   β”œβ”€β”€ error-responses.ts
β”‚   β”‚   └── index.ts
β”‚   β”œβ”€β”€ paste.controller.ts  # REST API endpoints
β”‚   β”œβ”€β”€ paste.service.ts     # Business logic
β”‚   β”œβ”€β”€ paste.module.ts      # Module definition
β”‚   └── README.md           # Paste module documentation
β”œβ”€β”€ prisma/                  # Database module
β”‚   β”œβ”€β”€ prisma.service.ts   # Prisma client service
β”‚   └── prisma.module.ts    # Prisma module
β”œβ”€β”€ app.config.ts           # Application configuration
β”œβ”€β”€ app.module.ts           # Main application module
└── main.ts                 # Application entry point

libs/
└── logger/                  # Custom logger library
    └── src/
        β”œβ”€β”€ logger.service.ts
        β”œβ”€β”€ logger.module.ts
        β”œβ”€β”€ logger.middleware.ts
        └── index.ts

prisma/
└── schema.prisma           # Database schema definition

dist/                       # Compiled JavaScript output
test/                       # E2E tests

MongoDB Connection Examples

Local MongoDB:

DATABASE_URL="mongodb://localhost:27017/sharedcode"

MongoDB with authentication:

DATABASE_URL="mongodb://username:password@localhost:27017/sharedcode?authSource=admin"

MongoDB Atlas:

DATABASE_URL="mongodb+srv://username:password@cluster.mongodb.net/sharedcode?retryWrites=true&w=majority"

Environment Variables

Create a .env file based on env-example with the following variables:

  • DATABASE_URL: MongoDB connection string (required)
  • PORT: Server port (optional, defaults to 3000)
  • ENVIRONMENT: Environment mode (development/production)
  • ENABLE_LOGGER: For enabling logger libs to log all events on terminal

API Documentation

Swagger UI

Once the server is running, you can access the interactive API documentation at:

The Swagger documentation includes:

  • All available endpoints
  • Request/response schemas
  • Example requests and responses
  • Authentication requirements
  • Error response formats

API Testing

You can test the API using:

  • Swagger UI - Built-in interactive documentation at /docs
  • Postman - Import endpoints from Swagger documentation
  • cURL - Command line testing (examples provided above)
  • Thunder Client - VS Code extension
  • Insomnia - API client

Development

Code Style

This project uses:

  • ESLint for linting
  • Prettier for code formatting
  • TypeScript for type safety

Run yarn lint:fix and yarn format:fix before committing.

Pre-commit Hooks

The project is configured to run linting and formatting checks before commits.

Deployment

Production Build

yarn build
yarn start:prod

Environment Setup

  1. Set ENVIRONMENT=production
  2. Configure production MongoDB connection
  3. Ensure all required environment variables are set

Docker (Optional)

The application can be containerized using Docker. A Dockerfile is recommended for production deployments.

Troubleshooting

Common Issues

Database Connection Failed

  • Verify MongoDB is running
  • Check DATABASE_URL in your .env file
  • Ensure database user has proper permissions

Port Already in Use

  • Change the port in your .env file
  • Kill the process using the port: lsof -ti:3000 | xargs kill

Prisma Issues

  • Run yarn generate to regenerate Prisma client
  • Run yarn migrate to sync database schema

TypeScript Errors

  • Run yarn type-check to see detailed type errors
  • Ensure all dependencies are installed: yarn install

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Run tests and linting
  5. Submit a pull request

License

This project is licensed under UNLICENSED.

About

πŸš€ Robust NestJS backend API for code sharing platform with MongoDB, Prisma ORM, password protection, expiration dates, Swagger docs, and comprehensive testing suite.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors