Skip to content

alex-chaliy/task-api-pure-sql

Repository files navigation

Task API

A GraphQL-based API built with NestJS, TypeORM, and PostgreSQL, featuring user authentication with JWT tokens.

Features

  • GraphQL API with Apollo Server
  • User Authentication (Sign-up & Sign-in)
  • JWT Token-based Authentication
  • PostgreSQL Database with TypeORM
  • TypeScript for type safety
  • Password Hashing with bcryptjs
  • Input Validation with class-validator

Prerequisites

  • Node.js v22+
  • PostgreSQL 18+
  • npm or yarn

Project setup

npm install
  1. Set up environment variables

    cp .env.example .env

    Update .env with your PostgreSQL credentials:

    DATABASE_HOST=localhost
    DATABASE_PORT=5432
    DATABASE_USER=postgres
    DATABASE_PASSWORD=your_password
    DATABASE_NAME=task_api_db
    JWT_SECRET=your_jwt_secret_key
    
  2. Create PostgreSQL database

    psql -U postgres 
    createdb task_api_db

If psql command not found: https://stackoverflow.com/a/79875872/7455192

  1. Check connection to PostgreSQL
    nc -vz <hostname> <port_number>
    for example, in our case:
    nc -vz 127.0.0.1 5432

Compile and run the project

# development
npm run start

# watch mode
npm run start:dev

# production mode
npm run build
npm run start:prod

GraphQL Playground

The GraphQL playground is available at http://localhost:5002/graphql

Example Mutations

Sign Up:

mutation {
  signUp(input: {
    email: "user@example.com"
    password: "password123"
    firstName: "John"
    lastName: "Doe"
  }) {
    accessToken
    id
    email
    firstName
    lastName
  }
}

Sign In:

mutation {
  signIn(input: {
    email: "user@example.com"
    password: "password123"
  }) {
    accessToken
    id
    email
    firstName
    lastName
  }
}

Get User (after Sign In):

query {
  user {
    id
    email
    firstName
    lastName
    createdAt
    updatedAt
  }
}

Note: The user query requires JWT authentication. Include the accessToken from the Sign In response in the Authorization header:

Authorization: Bearer <accessToken>

Get User by ID:

query {
  getUserById(id: "user-uuid-here") {
    id
    email
    firstName
    lastName
    createdAt
    updatedAt
  }
}

query {
  getUserById(id: "af23-4234-e0a9-8b3b4-af23-4234-e0a9-8b3b4") {
    id
    email
    firstName
    lastName
    createdAt
    updatedAt
  }
}

Note: The getUserById query does not require authentication.

Run tests

# unit tests
npm run test

# e2e tests
npm run test:e2e

# test coverage
npm run test:cov

Project Structure

src/
├── auth/                 # Authentication module
│   ├── strategies/      # JWT strategy
│   ├── dto/             # Auth DTOs
│   ├── auth.service.ts
│   ├── auth.resolver.ts
│   └── auth.module.ts
├── users/               # Users module
│   ├── entities/        # User entity
│   ├── users.service.ts
│   ├── users.resolver.ts
│   └── users.module.ts
├── config/              # Configuration
│   └── database.config.ts
├── app.module.ts        # Main app module
└── main.ts              # Entry point

Deployment

Deployment configuration depends on your target environment (AWS, Heroku, Docker, etc.).

License

MIT licensed

About

Task api | PURE SQL | Nest.js, PostgreSQL, GraphQL

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published