1.0.2 • Published 4 months ago

jidan_starter v1.0.2

Weekly downloads
-
License
ISC
Repository
github
Last release
4 months ago

šŸŽÆ Project Setup Guide


šŸ“Œ 1. Database Preparation

Before running the project, ensure that the MySQL database is properly configured. šŸ”¹ Check the migration file in migration/../migration.sql and adjust it if necessary. šŸ”¹ Configure .env with MySQL connection details matching your server environment.


šŸ›† 2. Install Dependencies

Run the following command to install all required dependencies:

npm install

āš™ļø 3. Generate Prisma Client

After installing dependencies, run the following command to generate the Prisma Client:

npx prisma generate

šŸ› ļø 4. Synchronize Database with Prisma

To ensure the database schema matches Prisma, execute the following commands:

npx prisma db push
npx prisma db pull

šŸ‘‰ db push applies the Prisma schema to the database. šŸ‘‰ db pull retrieves the schema from the database if any changes were made.


šŸ” 5. Check Database Connection

Use the following command to verify the database connection:

npx ts-node src/config/dbCheck.ts

šŸ‘Œ If the connection is successful, proceed to the next step.


šŸš€ 6. Run the Project

Once all configurations are complete, start the server with:

npm run dev

šŸŽ‰ If successful, the server will be available at http://localhost:3000.


šŸ”— API Endpoints

šŸ‘¤ User Management

  • šŸ’š Create a new user:

    POST http://localhost:3000/api/users

    Request Body:

    {
      "username": "jojo",
      "email": "john@example.com",
      "password": "1234578A@"
    }
  • šŸ”‘ User login:

    POST http://localhost:3000/api/users/login

    Request Body:

    {
      "email": "john@example.com",
      "password": "1234578A@"
    }

    Response:

    {
        "message": "Login successful",
        "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoxLCJlbWFpbCI6ImpvaG5AZXhhbXBsZS5jb20iLCJ1c2VybmFtZSI6Impvam8iLCJpYXQiOjE3NDExMzUyMzEsImV4cCI6MTc0MTIyMTYzMX0.JjIYECNDcr1hS5TH1eRJwTFwR8ZbzRfeYMyQtWxy0KE",
        "user": {
            "user_id": 1,
            "username": "jojo",
            "email": "john@example.com"
        }
    }
  • šŸ“ƒ Retrieve all users (requires authentication):

    GET http://localhost:3000/api/users/

    Authorization: Bearer Token

  • šŸ‘¤ Retrieve a user by ID (requires authentication):

    GET http://localhost:3000/api/users/10

    Authorization: Bearer Token

  • āœļø Update a user by ID (requires authentication):

    PUT http://localhost:3000/api/users/10

    Authorization: Bearer Token

  • šŸ›¢ļø Soft delete a user (set is_deleted column to true, requires authentication):

    DELETE http://localhost:3000/api/users/soft/6

    Authorization: Bearer Token

  • āŒ Delete a user permanently (requires authentication):

    DELETE http://localhost:3000/api/users/6

    Authorization: Bearer Token


šŸ“š API Usage Instructions

  1. šŸ“ Create an account first using the POST /api/users endpoint.
  2. šŸ”’ Login using the POST /api/users/login endpoint to obtain a Bearer Token.
  3. ⚔ Use the Bearer Token to access the following endpoints:
    • GET (Retrieve all users or a specific user by ID)
    • PUT (Update user information)
    • DELETE (Soft delete a user)
    • DELETE (Permanently delete a user)

šŸ”’ How to Use Bearer Token in Authorization

When making API requests that require authentication, include the Bearer Token in the request header:

Authorization: Bearer YOUR_TOKEN_HERE

Example using cURL:

curl -X GET "http://localhost:3000/api/users" -H "Authorization: Bearer YOUR_TOKEN_HERE"

āš ļø Notes

šŸš€ Ensure MySQL is running before starting the project. šŸš€ If there are schema changes, always run:

npx prisma generate
npx prisma db push

šŸ“‰ Security Reminder: Do not expose your .env file in public repositories.

šŸ“„ Happy Coding & Enjoy! šŸš€