vpa-api
v1.0.1
Published
Prisma + TypeGraphQL API with Node.js, Express, TypeScript, and Apollo Server
Maintainers
Readme
VPA API
A modern Node.js API built with Prisma, TypeGraphQL, Express, and TypeScript.
Features
- 🚀 Express.js - Fast, unopinionated web framework
- 🗄️ Prisma - Next-generation ORM with type safety
- 🐘 PostgreSQL - Robust, open-source relational database
- 📊 TypeGraphQL - GraphQL API with TypeScript decorators
- 🔌 Apollo Server - Production-ready GraphQL server
- 📡 GraphQL Subscriptions - Real-time updates with WebSockets
- 🔧 TypeScript - Type-safe development experience
- 🎨 Prettier & ESLint - Code formatting and linting
- 🧪 Jest - Testing framework
- 🔒 Envalid - Environment variable validation
- 🆕 Node 22 - Latest LTS Node.js features
Prerequisites
- Node.js >= 22.0.0
- PostgreSQL >= 12.0
- npm or yarn
Installation
- Clone the repository:
git clone <your-repo-url>
cd vpa-api- Install dependencies:
npm install- Set up environment variables:
cp .env.example .env
# Edit .env with your PostgreSQL configuration- Set up the database:
npm run db:generate
npm run db:pushDevelopment
Start the development server:
npm run devThe API will be available at:
- GraphQL endpoint: http://localhost:4000/graphql
- GraphQL subscriptions: ws://localhost:4000/graphql
Available Scripts
npm run dev- Start development server with hot reloadnpm run build- Build the project for productionnpm run start- Start production servernpm run test- Run testsnpm run lint- Run ESLintnpm run format- Format code with Prettiernpm run db:generate- Generate Prisma clientnpm run db:push- Push schema changes to databasenpm run db:migrate- Run database migrationsnpm run db:deploy- Deploy migrations to productionnpm run db:reset- Reset database (development only)npm run db:studio- Open Prisma Studionpm run db:seed- Run database seedersnpm run clean- Clean build artifacts
Project Structure
src/
├── modules/ # Feature modules
│ └── user/ # User module with resolver
├── shared/ # Shared utilities and GraphQL setup
│ ├── env.config.ts # Environment configuration with validation
│ ├── graphql/ # GraphQL resolvers and schema
│ └── graphql-subscription/ # WebSocket subscriptions
└── app.ts # Main application entry point
prisma/
├── schema.prisma # Database schema
└── generated/ # Generated types (gitignored)
└── type-graphql # TypeGraphQL generated typesEnvironment Configuration
This project uses Envalid for environment variable validation. The configuration is centralized in src/shared/env.config.ts and provides:
- Type-safe environment variables
- Runtime validation
- Default values
- Choice validation for specific variables
- SCREAM_SNAKE_CASE in .env files, camelCase in code
Environment Variables (.env file)
Server Configuration
NODE_ENV- Environment (development/production/test)PORT- Server port (default: 4000)HOST- Server host (default: localhost)
PostgreSQL Configuration
POSTGRES_USER- Database username (default: postgres)POSTGRES_PASSWORD- Database password (default: password)POSTGRES_HOST- Database host (default: localhost)POSTGRES_PORT- Database port (default: 5432)POSTGRES_DB- Database name (default: vpa_api)POSTGRES_SCHEMA- Database schema (default: public)DATABASE_URL- Full database connection string with variable substitution
GraphQL Configuration
GRAPHQL_PATH- GraphQL endpoint path (default: /graphql)GRAPHQL_SUBSCRIPTIONS_PATH- WebSocket path (default: /graphql)
Other Configuration
CORS_ORIGIN- CORS allowed origin (default: http://localhost:3000)LOG_LEVEL- Logging level (error/warn/info/debug)
Code Usage (camelCase)
In your application code, you can access these values using camelCase:
import { config } from './shared/env.config';
// Server configuration
const port = config.port; // 4000
const host = config.host; // 'localhost'
// PostgreSQL configuration
const dbUser = config.postgresUser; // 'postgres'
const dbName = config.postgresDb; // 'vpa_api'
const dbUrl = config.databaseUrl; // Full connection string
// GraphQL configuration
const gqlPath = config.graphqlPath; // '/graphql'Database URL Configuration
The DATABASE_URL in your .env file uses environment variable substitution for a clean, maintainable configuration:
DATABASE_URL=postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}?schema=${POSTGRES_SCHEMA}This approach:
- ✅ Keeps all database configuration in one place
- ✅ Uses standard environment variable substitution
- ✅ Makes it easy to update individual components
- ✅ Maintains consistency across different environments
Database Setup
PostgreSQL Requirements
- Version: PostgreSQL 12.0 or higher
- Extensions: No special extensions required
- Permissions: User must have CREATE, ALTER, DROP privileges on the database
Initial Setup
- Create a PostgreSQL database:
CREATE DATABASE vpa_api;- Update your
.envfile with database credentials:
POSTGRES_USER=your_username
POSTGRES_PASSWORD=your_password
POSTGRES_HOST=localhost
POSTGRES_PORT=5432
POSTGRES_DB=vpa_api
POSTGRES_SCHEMA=public
DATABASE_URL=postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}?schema=${POSTGRES_SCHEMA}- Generate Prisma client and push schema:
npm run db:generate
npm run db:pushMigrations
For production deployments, use migrations:
npm run db:migrate # Create and apply migration
npm run db:deploy # Deploy existing migrationsGraphQL Schema
The API automatically generates a GraphQL schema from your Prisma models and TypeGraphQL resolvers. You can explore the schema using Apollo Studio at the GraphQL endpoint.
Adding New Modules
- Create a new directory in
src/modules/ - Add your resolver with TypeGraphQL decorators
- Export it from the module's
index.ts - Add it to the resolvers array in
src/shared/graphql/index.ts
Node 22 Compatibility
This project is optimized for Node.js 22+ and includes:
- Modern ES2022 TypeScript target
- Latest dependency versions
- Optimized build configuration
- Enhanced type safety
Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Run tests and linting
- Submit a pull request
License
MIT
