{"id":31842603,"url":"https://github.com/luizcurti/redis_postgres_node","last_synced_at":"2026-04-12T00:43:54.169Z","repository":{"id":318214056,"uuid":"975571118","full_name":"luizcurti/redis_postgres_node","owner":"luizcurti","description":"This project demonstrates a simple integration between Node.js, Redis, and PostgreSQL to perform basic operations such as user creation, login authentication, and storing user data in Redis for fast access.","archived":false,"fork":false,"pushed_at":"2025-10-05T20:06:58.000Z","size":100,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-05T21:26:29.132Z","etag":null,"topics":["jwt","nodejs","postgresql","redis","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/luizcurti.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-04-30T14:30:09.000Z","updated_at":"2025-10-05T20:07:01.000Z","dependencies_parsed_at":"2025-10-06T11:01:55.949Z","dependency_job_id":null,"html_url":"https://github.com/luizcurti/redis_postgres_node","commit_stats":null,"previous_names":["luizcurti/redis_postgres_node"],"tags_count":null,"template":false,"template_full_name":null,"purl":"pkg:github/luizcurti/redis_postgres_node","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luizcurti%2Fredis_postgres_node","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luizcurti%2Fredis_postgres_node/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luizcurti%2Fredis_postgres_node/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luizcurti%2Fredis_postgres_node/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/luizcurti","download_url":"https://codeload.github.com/luizcurti/redis_postgres_node/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luizcurti%2Fredis_postgres_node/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279010484,"owners_count":26084757,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-12T02:00:06.719Z","response_time":53,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["jwt","nodejs","postgresql","redis","typescript"],"created_at":"2025-10-12T06:28:11.236Z","updated_at":"2026-04-12T00:43:54.132Z","avatar_url":"https://github.com/luizcurti.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Node.js + Redis + PostgreSQL REST API\n\nA REST API built with Node.js, Express, PostgreSQL, and Redis that handles user registration, authentication with JWT, and Redis-based profile caching.\n\n---\n\n## Tech Stack\n\n| Layer | Technology |\n|---|---|\n| Runtime | Node.js 20 + TypeScript |\n| Framework | Express 4 |\n| Database | PostgreSQL 15 (persistent storage) |\n| Cache | Redis 7 (session cache via ioredis) |\n| Auth | JWT (jsonwebtoken) |\n| Passwords | bcryptjs |\n| Testing | Jest + ts-jest + Supertest |\n| Containers | Docker + Docker Compose |\n\n---\n\n## Architecture Overview\n\n```\nClient\n  │\n  ▼\nExpress Router\n  │\n  ├── POST /users          → CreateUserController  → PostgreSQL (INSERT)\n  ├── POST /login          → LoginUserController   → PostgreSQL (SELECT) + Redis (SET)\n  └── GET  /users/profile/:id  → auth middleware → GetUserInfoController → Redis (GET)\n```\n\nAfter a successful login, the user's data is stored in Redis with a 1-hour TTL (`user-{id}`).  \n`GET /users/profile/:id` reads exclusively from the Redis cache — no database round-trip.\n\n---\n\n## API Endpoints\n\n### `POST /users` — Create a new user\n\n**Request body:**\n```json\n{\n  \"name\": \"newname\",\n  \"username\": \"newuser\",\n  \"email\": \"newuser@example.com\",\n  \"password\": \"newpassword\"\n}\n```\n\n**Responses:**\n\n| Status | Body |\n|---|---|\n| `201 Created` | `{ \"message\": \"User created successfully\", \"userId\": \"\u003cuuid\u003e\" }` |\n| `400 Bad Request` | `{ \"error\": \"Missing required fields.\" }` |\n| `409 Conflict` | `{ \"error\": \"Username already taken.\" }` |\n| `500 Internal Server Error` | `{ \"error\": \"Internal server error\" }` |\n\n---\n\n### `POST /login` — Authenticate a user\n\n**Request body:**\n```json\n{\n  \"username\": \"newuser\",\n  \"password\": \"newpassword\"\n}\n```\n\n**Responses:**\n\n| Status | Body |\n|---|---|\n| `200 OK` | `{ \"message\": \"Login successful\", \"token\": \"\u003cjwt\u003e\", \"user\": { \"id\", \"name\", \"username\", \"email\" } }` |\n| `400 Bad Request` | `{ \"error\": \"Username and password are required.\" }` |\n| `401 Unauthorized` | `{ \"error\": \"Invalid credentials.\" }` |\n| `500 Internal Server Error` | `{ \"error\": \"Internal server error.\" }` |\n\n---\n\n### `GET /users/profile/:id` — Get user profile (requires JWT)\n\n**Header:**\n```\nAuthorization: Bearer \u003ctoken\u003e\n```\n\n**Responses:**\n\n| Status | Body |\n|---|---|\n| `200 OK` | `{ \"id\", \"name\", \"username\", \"email\" }` |\n| `401 Unauthorized` | `{ \"error\": \"Token missing\" }` or `{ \"error\": \"Invalid token\" }` |\n| `404 Not Found` | `{ \"error\": \"User not found in cache.\" }` *(session expired or user never logged in)* |\n| `500 Internal Server Error` | `{ \"error\": \"Internal server error.\" }` |\n\n\u003e **Note:** This endpoint reads from Redis only. The cache is populated on login and expires after 1 hour. A 404 means the user needs to log in again.\n\n---\n\n## Getting Started\n\n### Prerequisites\n\n- [Docker](https://www.docker.com/) and Docker Compose\n- Node.js 20+ (for local development without Docker)\n\n### 1. Clone the repository\n\n```bash\ngit clone https://github.com/luizcurti/redis-nodis-pg.git\ncd redis-nodis-pg\n```\n\n### 2. Configure environment variables\n\nCreate a `.env` file in the project root:\n\n```env\n# Application\nPORT=3000\nJWT_SECRET=your_jwt_secret_key\n\n# PostgreSQL\nPOSTGRES_HOST=localhost\nPOSTGRES_PORT=5432\nPOSTGRES_USER=user\nPOSTGRES_PASSWORD=password\nPOSTGRES_DB=mydb\n\n# Redis\nREDIS_HOST=localhost\nREDIS_PORT=6379\n```\n\n\u003e When running via Docker Compose, `POSTGRES_HOST` should be `postgres` and `REDIS_HOST` should be `redis` (the service names defined in `docker-compose.yml`).\n\n### 3. Start with Docker Compose\n\n```bash\ndocker-compose up\n```\n\nThis starts:\n- **PostgreSQL** on port `5432` (creates the `users` table automatically via `database.sql`)\n- **Redis** on port `6379`\n- **Node.js app** on port `3000`\n\n### 4. Start locally (without Docker)\n\nMake sure PostgreSQL and Redis are running, then:\n\n```bash\nnpm install\nnpm run dev\n```\n\n---\n\n## Database Schema\n\n```sql\nCREATE TABLE IF NOT EXISTS users (\n  id       UUID PRIMARY KEY,\n  name     TEXT NOT NULL,\n  username TEXT UNIQUE NOT NULL,\n  password TEXT NOT NULL,\n  email    TEXT UNIQUE NOT NULL\n);\n```\n\n---\n\n## Available Scripts\n\n| Script | Description |\n|---|---|\n| `npm run dev` | Start development server with hot reload (tsx watch) |\n| `npm start` | Start production server (tsx) |\n| `npm run build` | Compile TypeScript to JavaScript |\n| `npm test` | Run all tests |\n| `npm run coverage` | Run tests with coverage report |\n| `npm run lint` | Run ESLint |\n| `npm run lint:fix` | Run ESLint with automatic fixes |\n| `npm run format` | Format code with Prettier |\n| `npm run format:check` | Check if code is properly formatted |\n\n---\n\n## Running Tests\n\n```bash\nnpm test\n```\n\nThe test suite uses Jest with mocked PostgreSQL, Redis, bcrypt, and JWT dependencies — no live services are required to run the tests.\n\n```\nTest Suites: 7 passed\nTests:       30 passed\n```\n\n---\n\n## CI/CD Pipeline\n\nThis project uses GitHub Actions for continuous integration. The pipeline runs on every push and pull request to `main` and includes:\n\n- ESLint code quality check\n- Prettier format validation\n- TypeScript type checking\n- Full test suite with coverage\n- Build verification\n\n**Services provisioned in CI:**\n- PostgreSQL 13\n- Redis Alpine\n\n---\n\n## Project Structure\n\n```\n.\n├── src/\n│   ├── server.ts                  # Express app entry point\n│   ├── routes.ts                  # Route definitions\n│   ├── postgres.ts                # PostgreSQL pool (pg)\n│   ├── redisConfig.ts             # Redis client (ioredis)\n│   ├── controllers/\n│   │   ├── CreateUserController.ts\n│   │   ├── LoginUserController.ts\n│   │   └── GetUserInfoController.ts\n│   ├── middleware/\n│   │   └── auth.ts                # JWT authentication middleware\n│   └── @types/\n│       └── express/index.d.ts     # Express Request type extension\n├── __tests__/                     # Jest test suites\n├── database.sql                   # PostgreSQL schema (auto-run by Docker)\n├── docker-compose.yml\n├── Dockerfile\n├── jest.config.js\n├── tsconfig.json\n└── eslint.config.js\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fluizcurti%2Fredis_postgres_node","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fluizcurti%2Fredis_postgres_node","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fluizcurti%2Fredis_postgres_node/lists"}