{"id":25149285,"url":"https://github.com/jawherkl/node-api-postgres","last_synced_at":"2025-04-13T05:13:24.499Z","repository":{"id":250182210,"uuid":"833709601","full_name":"JawherKl/node-api-postgres","owner":"JawherKl","description":"This is a modern RESTful API built with Node.js and Express, designed to interact with a PostgreSQL database.","archived":false,"fork":false,"pushed_at":"2025-03-15T13:54:55.000Z","size":1490,"stargazers_count":58,"open_issues_count":0,"forks_count":18,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-04-13T05:13:16.235Z","etag":null,"topics":["express","nodejs","npm","postgresql"],"latest_commit_sha":null,"homepage":"https://medium.com/@jawherkl/express-node-js-api-with-postgresql-4b86bb6219c5","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/JawherKl.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-07-25T15:28:37.000Z","updated_at":"2025-03-31T18:05:39.000Z","dependencies_parsed_at":"2024-07-25T18:24:29.180Z","dependency_job_id":"b52fc4d7-752f-4e36-bfad-31bd2b94735b","html_url":"https://github.com/JawherKl/node-api-postgres","commit_stats":null,"previous_names":["jawherkl/node-api-postgres"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JawherKl%2Fnode-api-postgres","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JawherKl%2Fnode-api-postgres/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JawherKl%2Fnode-api-postgres/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JawherKl%2Fnode-api-postgres/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/JawherKl","download_url":"https://codeload.github.com/JawherKl/node-api-postgres/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248665746,"owners_count":21142123,"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","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":["express","nodejs","npm","postgresql"],"created_at":"2025-02-08T21:20:20.433Z","updated_at":"2025-04-13T05:13:24.475Z","avatar_url":"https://github.com/JawherKl.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Express - Node.js API with PostgreSQL\n\n![Repository Size](https://img.shields.io/github/repo-size/JawherKl/node-api-postgres)\n![Last Commit](https://img.shields.io/github/last-commit/JawherKl/node-api-postgres)\n![Issues](https://img.shields.io/github/issues-raw/JawherKl/node-api-postgres)\n![Forks](https://img.shields.io/github/forks/JawherKl/node-api-postgres)\n![Stars](https://img.shields.io/github/stars/JawherKl/node-api-postgres)\n\n![nodepost](https://github.com/user-attachments/assets/6f206c6e-dea0-4045-8baa-a04e74a5fbf8)\n\nThis is a modern RESTful API built with **Node.js** and **Express**, designed to interact with a **PostgreSQL** database. The API provides various endpoints for managing user data, with additional features like authentication, JWT protection, soft deletion, and automated testing. We've also integrated **Swagger** for auto-generated API documentation.\n\n![Express.js](https://img.shields.io/badge/express.js-%23404d59.svg?style=for-the-badge\u0026logo=express\u0026logoColor=%2361DAFB)\n![NodeJS](https://img.shields.io/badge/node.js-6DA55F?style=for-the-badge\u0026logo=node.js\u0026logoColor=white)\n![Postgres](https://img.shields.io/badge/postgres-%23316192.svg?style=for-the-badge\u0026logo=postgresql\u0026logoColor=white)\n![NPM](https://img.shields.io/badge/NPM-%23CB3837.svg?style=for-the-badge\u0026logo=npm\u0026logoColor=white)\n\n## Features 🚀\n- **User Management**:\n  - **Get All Users**: Retrieve a list of all users.\n  - **Get User by ID**: Retrieve a specific user by their ID.\n  - **Create User**: Add a new user to the database.\n  - **Update User**: Update details of an existing user.\n  - **Delete User**: Remove a user from the database (soft delete functionality).\n  \n- **Authentication \u0026 Authorization**:\n  - **User Authentication**: Secure API access using **JSON Web Tokens (JWT)**.\n  - **Role-based Access Control (RBAC)**: Control access to resources based on user roles (e.g., admin, user).\n\n- **Swagger API Documentation**:\n  - **Swagger** integrated for real-time API documentation and testing directly in the browser. Access the documentation at: [http://localhost:3000/api-docs](http://localhost:3000/api-docs).\n\n- **Database**:\n  - Integration with **PostgreSQL** for storing user data securely.\n  - **Soft delete functionality**: Mark users as deleted without removing their data.\n\n- **Unit Testing**:\n  - Comprehensive unit tests using **Mocha** and **Chai** to ensure the reliability of the application.\n  - **Test Cases**: Includes tests for user creation, update, deletion, and authentication.\n\n## Technologies Used ⚙️\n- **Node.js** (JavaScript runtime)\n- **Express** (Web framework)\n- **PostgreSQL** (Database)\n- **JSON Web Token (JWT)** (Authentication)\n- **Body-Parser** (Parsing JSON request bodies)\n- **Swagger** (API documentation)\n- **Mocha** (Testing framework)\n- **Chai** (Assertion library)\n\n## Installation 🛠️\n### Step 1: Clone the Repository\n```bash\ngit clone https://github.com/JawherKl/node-api-postgres.git\ncd node-api-postgres\n```\n\n### Step 2: Install Dependencies\n```bash\nnpm install\n```\n\n### Step 3: Set up PostgreSQL\nEnsure you have **PostgreSQL** installed and running. Create a new database and configure the connection.\n\n### Step 4: Configure Database Connection\nUpdate the `db.js` file to set up your PostgreSQL connection credentials.\n\n### Step 5: Generate JWT Secret (Optional)\nGenerate a random JWT secret key (recommended for production environments):\n```bash\nnode -e \"console.log(require('crypto').randomBytes(32).toString('hex'))\"\n```\n\n### Inject Table into PostgreSQL\n```sql\nCREATE TABLE users (\n  id SERIAL PRIMARY KEY,\n  name VARCHAR(100) NOT NULL,\n  email VARCHAR(255) UNIQUE NOT NULL,\n  password VARCHAR(255) NOT NULL,\n  picture VARCHAR(255) NULL,\n  role VARCHAR(20) DEFAULT 'user',  -- Role-based access control\n  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n  updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n  deleted_at TIMESTAMP NULL  -- For soft delete functionality\n);\n```\n\n```sql\nCREATE TABLE metrics (\n    id SERIAL PRIMARY KEY,\n    user_id INT NOT NULL,\n    metric_name VARCHAR(255) NOT NULL,\n    metric_value FLOAT NOT NULL,\n    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n    FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE\n);\n```\n\n### Column Explanation\n- `id`: Unique identifier for each user (auto-increment).\n- `name`: User's name (max 100 characters).\n- `email`: Unique email address (max 255 characters).\n- `password`: Hashed password for security.\n- `role`: User's role (e.g., admin, user).\n- `created_at`: Timestamp for record creation.\n- `updated_at`: Timestamp for last update (auto-updates on modification).\n- `deleted_at`: Nullable timestamp for soft deletion.\n\n## Usage 🏃‍♂️\n\n### Start the Server\n```bash\nnpm start\n```\nThe server will run on [http://localhost:3000].\n\n### Access Swagger API Docs\nOnce the server is running, you can access the auto-generated API documentation powered by Swagger at:\n[http://localhost:3000/api-docs](http://localhost:3000/api-docs).\n\n## API Endpoints 📡\n- **GET /** - Returns a simple welcome message.\n- **GET /users** - Get all users.\n- **GET /users/:id** - Get a user by ID.\n- **POST /users** - Create a new user (requires JSON body).\n- **PUT /users/:id** - Update an existing user by ID (requires JSON body).\n- **DELETE /users/:id** - Delete a user by ID.\n- **POST /login** - Authenticate a user and return a JWT (requires JSON body with email and password).\n\n[\u003cimg src=\"https://run.pstmn.io/button.svg\" alt=\"Run In Postman\" style=\"width: 128px; height: 32px;\"\u003e](https://app.getpostman.com/run-collection/31522917-54350f46-dd5e-4a62-9dc2-4346a7879692?action=collection%2Ffork\u0026source=rip_markdown\u0026collection-url=entityId%3D31522917-54350f46-dd5e-4a62-9dc2-4346a7879692%26entityType%3Dcollection%26workspaceId%3D212c8589-8dd4-4f19-9a53-e77403c6c7d9)\n\n## Example Requests 📝\n\n### Get All Users\n```bash\ncurl -X GET http://localhost:3000/users\n```\n\n### Create User\n```bash\ncurl -X POST http://localhost:3000/users -H \"Content-Type: application/json\" -d '{\"name\": \"John Doe\", \"email\": \"john@example.com\", \"password\": \"password\"}'\n```\n\n### Update User\n```bash\ncurl -X PUT http://localhost:3000/users/1 -H \"Content-Type: application/json\" -d '{\"name\": \"Jane Doe\"}'\n```\n\n### Delete User\n```bash\ncurl -X DELETE http://localhost:3000/users/1\n```\n\n### Authenticate User\n```bash\ncurl -X POST http://localhost:3000/login -H \"Content-Type: application/json\" -d '{\"email\": \"john@example.com\", \"password\": \"password\"}'\n```\n\n### Access Protected Route\n```bash\ncurl -X GET http://localhost:3000/users -H \"Authorization: Bearer your_jwt_token\"\n```\n\n## Unit Testing 🧪\nUnit tests are implemented using **Mocha** and **Chai**. To run tests:\n\n1. Install **test dependencies** (if not installed):\n   ```bash\n   npm install --save-dev mocha chai\n   ```\n\n2. Run the tests:\n   ```bash\n   npm test\n   ```\n   \nThis will run all tests and output the results to the console. You can find the test cases for different routes and operations in the `test` folder.\n\n## Contributing 🤝\nContributions are welcome! If you have suggestions, improvements, or bug fixes, please open an issue or submit a pull request.\n\n## License 📝\nThis project is licensed under the **MIT License**. See the [LICENSE](./LICENSE) file for details.\n\n## Acknowledgments 🙏\n- Special thanks to all contributors and the open-source community.\n- Gratitude to the maintainers of the libraries used in this project.\n\n## Stargazers over time\n[![Stargazers over time](https://starchart.cc/JawherKl/node-api-postgres.svg?variant=adaptive)](https://starchart.cc/JawherKl/node-api-postgres)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjawherkl%2Fnode-api-postgres","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjawherkl%2Fnode-api-postgres","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjawherkl%2Fnode-api-postgres/lists"}