Productivity
This 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.
User Management:
Authentication & Authorization:
Swagger API Documentation:
Database:
Unit Testing:
git clone https://github.com/JawherKl/node-api-postgres.git
cd node-api-postgres
npm install
Ensure you have PostgreSQL installed and running. Create a new database and configure the connection.
Update the db.js
file to set up your PostgreSQL connection credentials.
Generate a random JWT secret key (recommended for production environments):
node -e "console.log(require('crypto').randomBytes(32).toString('hex'))"
CREATE TABLE users (
id SERIAL PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(255) UNIQUE NOT NULL,
password VARCHAR(255) NOT NULL,
picture VARCHAR(255) NULL,
role VARCHAR(20) DEFAULT 'user', -- Role-based access control
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
deleted_at TIMESTAMP NULL -- For soft delete functionality
);
CREATE TABLE metrics (
id SERIAL PRIMARY KEY,
user_id INT NOT NULL,
metric_name VARCHAR(255) NOT NULL,
metric_value FLOAT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE
);
id
: Unique identifier for each user (auto-increment).name
: User's name (max 100 characters).email
: Unique email address (max 255 characters).password
: Hashed password for security.role
: User's role (e.g., admin, user).created_at
: Timestamp for record creation.updated_at
: Timestamp for last update (auto-updates on modification).deleted_at
: Nullable timestamp for soft deletion.node index.js
The server will run on [http://localhost:3000].
Once the server is running, you can access the auto-generated API documentation powered by Swagger at: http://localhost:3000/api-docs.
curl -X GET http://localhost:3000/users
curl -X POST http://localhost:3000/users -H "Content-Type: application/json" -d '{"name": "John Doe", "email": "john@example.com", "password": "password"}'
curl -X PUT http://localhost:3000/users/1 -H "Content-Type: application/json" -d '{"name": "Jane Doe"}'
curl -X DELETE http://localhost:3000/users/1
curl -X POST http://localhost:3000/login -H "Content-Type: application/json" -d '{"email": "john@example.com", "password": "password"}'
curl -X GET http://localhost:3000/users -H "Authorization: Bearer your_jwt_token"
Unit tests are implemented using Mocha and Chai. To run tests:
Install test dependencies (if not installed):
npm install --save-dev mocha chai
Run the tests:
npm test
This 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.
Contributions are welcome! If you have suggestions, improvements, or bug fixes, please open an issue or submit a pull request.
This project is licensed under the MIT License. See the LICENSE file for details.
Give me stars! Thank you!!!