Skip to content

Latest commit

 

History

History
93 lines (68 loc) · 2.4 KB

File metadata and controls

93 lines (68 loc) · 2.4 KB

CRUD Ops with Node.js, Fastify, and MongoDB using the Node.js Test Runner

Build Status

Description

This project demonstrates how to perform CRUD operations using Node.js with the Fastify framework and MongoDB. It includes unit tests that verify the functionality of the API endpoints and track code coverage.

Getting Started

Prerequisites

  • Docker and Docker compose
  • Node.js (version 20 or later)
  • MongoDB (local or cloud instance)

Installation

  1. Clone the repository:

    git clone https://github.com/ErickWendel/nodejs-fastify-mongodb-crud.git
    cd nodejs-fastify-mongodb-crud 
  2. Install the dependencies:

    npm ci

Running Tests

To run the tests and see code coverage, use:

docker-compose up -d mongodb
npm test

This will execute the tests defined in the project and provide a coverage report.

Running the Project

To initialize the MongoDB, run:

docker-compose up -d mongodb

To initialize the project, run:

npm start

The server will start, and you can access the API on http://localhost:9999 (or your specified port).

API Endpoints

Here are the available routes for performing CRUD operations on customers:

  1. Create a Customer (POST)

    curl -X POST http://localhost:9999/v1/customers \
    -H "Content-Type: application/json" \
    -d '{"name": "John Doe", "phone": "123456789"}'
  2. Retrieve All Customers (GET)

    curl http://localhost:9999/v1/customers
  3. Retrieve a Customer by ID (GET)

    curl http://localhost:9999/v1/customers/<customer_id>
  4. Update a Customer (PUT)

    curl -X PUT http://localhost:9999/v1/customers/<customer_id> \
    -H "Content-Type: application/json" \
    -d '{"name": "Jane Doe", "phone": "987654321"}'
  5. Delete a Customer (DELETE)

    curl -X DELETE http://localhost:9999/v1/customers/<customer_id>

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments