User IMS a RESTful API developed for managing user information such as name, email, phone that utilizes Node.js platform.
Explore the API documentation using Swagger UI:
Following API endpoints can be used through tools such as Postman:
-
Get all the Users
- Endpoint: GET /users
- Description: Retrieve a list of users.
-
Get User by ID
- Endpoint: GET /users/{id}
- Description: Retrieve a user by their unique identifier i.e id.
-
Create a User
- Endpoint: POST /users
- Description: Create a new user.
-
Update User
- Endpoint: PUT /users/{id}
- Description: Update an existing user by their unique identifier i.e id.
-
Delete User
- Endpoint: DELETE /users/{id}
- Description: Delete a user by their unique identifier i.e id.
- Live Demo
- Table of Contents
- Getting Started
- Architectural Decisions
- Challenges Faced
- Additional Features
- Usage
- Database
- Testing
- Deployment
- Built With
- Acknowledgments
- Add a new user
- Retrieve the list of all the users
- Retrieve data of a particular user using unique identifier i.e id
- Update a particular user using unique identifier i.e id
- Delete a particular user using unique identifier i.e id
- Input validation for each of the user fields
- Error handling with proper HTTP status code
- Unit Testing for each of the API endpoints
- Node.js and npm
- MySQL Database
-
Familiarity: Given my prior experience with Eloquent ORM, I have chosen Sequelize as the optimal choice for this project due to the comfort and familiarity with the tool.
-
Validation: Upon exploring the official documentation, I discovered Sequelize's built-in validation mechanism that enforces constraints and validation rules at the model level, making it one of the points for choosing Sequelize.
-
Security: By using Sequelize's methods over raw SQL queries, SQL injection can be prevented due to the sanitization and parameterization implemented by the Sequelize methods.
Other than these, Sequelize has a huge market share and community which guided me to learn and implement Sequelize on the project through the rich official documentation and various blogs.
Since, the requirement was to utilize relational database, I had chosen MySQL
Familiarity: I have been utilizing MySQL database for a certain period of time and have been very familiar with the syntax and the usage.
However, other SQL databases such as PostgreSQL and sqlite can also be implemented as the ORM supports various SQL based databases with minimal configuration changes.
-
Testing: Writing test cases and unit testing has been one of the valuable skills that I have acquired in this project. As I had little to no prior experience of writing test cases, I chose Jest as the Testing Framework due to ease of setup and usage.
-
Error Handling: For an API to be reliable, proper error handling is a must. While developing this project, I understood about the various HTTP status codes to indicate the nature of the error (400 for Bad Request, 409 Conflict).
- As an additional feature of this project, I have integrated Swagger UI, which is user-friendly and interactive API documentation tool. Through Swagger UI, it is much easier for users to understand all the API endpoints and the various HTTP status code and response that the API endpoints provides. Moreover, it allows to view the request and responses with appropriate examples and also test the functionality of the API.
git clone https://github.com/rajeevmandongol/user-ims.git
cd user-ims
npm install
npm install body-parser express mysql2 nodemon sequelize sequelize-cli swagger-jsdoc swagger-ui-express
mysql> CREATE DATABASE `user_ims`;
npx sequelize-cli db:migrate
Before performing the test, install the jest as follows:
npm install --save-dev jest
npm jest
node server.js
nodemon server.js
Access the API using postman at http://127.0.0.1:3000
HTTP method | URL | Status | Description |
---|---|---|---|
POST |
/users |
201 or (409 Conflict) if ValidationError Occurs | Create a new user. |
GET |
/users |
200 or (204 No Content) if no user found | Retrieve a list of all users. |
GET |
/users/:id |
200 or (404 Not Found) if User not found | Retrieve a user by their ID. |
PUT |
/users/:id |
200 (404 Not Found) if User not found | Update a user by their ID. |
PATCH |
/users/:id |
200 (404 Not Found) if User not found | Partially update a user by their ID. |
DELETE |
/users/:id |
200 or (404 Not Found) if User not found | Delete a user by their ID. |
GET /users/
{
"success": true,
"message": "Users found",
"data": {
"users": [
{
"id": 1,
"name": "Some Name",
"email": "[email protected]",
"phone": "9812345678",
"createdAt": "YYYY-MM-DD HH:mm:ss. SSS Z",
"updatedAt": "YYYY-MM-DD HH:mm:ss. SSS Z"
}
]
}
}
POST /users/
Parameter | Type | Description | Required |
---|---|---|---|
name |
string |
Must only contains alphabets and whitespaces |
true |
email |
string |
Must be a valid e-mail address |
true |
phone |
string |
Must only contain 10 digits or 7 for landline numbers |
true |
{
"success": true,
"message": "User has been added successfully!",
"data": {
"user": {
"id": 1,
"name": "Some Name",
"email": "[email protected]",
"phone": "9812345678",
"createdAt": "YYYY-MM-DD HH:mm:ss. SSS Z",
"updatedAt": "YYYY-MM-DD HH:mm:ss. SSS Z"
}
}
}
GET /users/:id
Parameter | Type | Description | Required |
---|---|---|---|
id |
integer |
Must be a valid user id |
true |
{
"success": true,
"message": "User found",
"data": {
"user": {
"id": 1,
"name": "Some Name",
"email": "[email protected]",
"phone": "9812345678",
"createdAt": "YYYY-MM-DD HH:mm:ss. SSS Z",
"updatedAt": "YYYY-MM-DD HH:mm:ss. SSS Z"
}
}
}
PUT /users/:id
Parameter | Type | Description | Required |
---|---|---|---|
id |
integer |
Must be a valid user id |
true |
name |
string |
Must only contains alphabets and whitespaces |
true |
email |
string |
Must be a valid e-mail address |
true |
phone |
string |
Must only contain 10 digits or 7 for landline numbers |
true |
{
"success": true,
"message": "User updated successfully",
"data": {
"user": {
"id": 1,
"name": "New Name",
"email": "[email protected]",
"phone": "9812345699",
"createdAt": "YYYY-MM-DD HH:mm:ss. SSS Z",
"updatedAt": "YYYY-MM-DD HH:mm:ss. SSS Z"
}
}
}
PATCH /users/:id
Parameter | Type | Description | Required |
---|---|---|---|
id |
integer |
Must be a valid user id |
true |
name |
string |
Must only contains alphabets and whitespaces |
false |
email |
string |
Must be a valid e-mail address |
false |
phone |
string |
Must only contain 10 digits or 7 for landline numbers |
false |
{
"success": true,
"message": "User updated successfully",
"data": {
"user": {
"id": 1,
"name": "New Name",
"email": "[email protected]",
"phone": "9812345699",
"createdAt": "YYYY-MM-DD HH:mm:ss. SSS Z",
"updatedAt": "YYYY-MM-DD HH:mm:ss. SSS Z"
}
}
}
DELETE /users/:id
Parameter | Type | Description | Required |
---|---|---|---|
id |
integer |
Must be a valid user id otherwise returns status 204 No Content |
true |
{
"success": true,
"message": "User deleted successfully!",
"data": {}
}
- Node.js
- Express.js
- Sequelize
- Jest
- Swagger UI