Skip to content

glowiep/country-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 

Repository files navigation

🌎 United States Country API

A simple United States Country API created from scratch which runs on Express, and uses Prisma for database operations. This API provides information on states and cities in United States (mock data).

Express.js Prisma Postgres TypeScript

This API is structured around three main entities:

  • states
  • cities
  • relationship between them (represented in the cities_states table)

Goals 🏆

  • Building a simple country API to demonstrate knowledge on:
    • Data Access and Management
      • Set up and organize the backend effectively.
      • Implement CRUD (Create, Read, Update, Delete) operations for managing state and city data.
    • Integration and Scalability
      • Use TypeScript with Prisma to allow for seamless integration with Express JS.
    • Documentation and Developer Experience
      • Apply the principles of ORM in practice and document what I have learned through the process of developing an API from scratch.

What I learned 📚

🎨 Deep Dive into Prisma

Prisma is an ORM (Object-Relational Mapping) tool designed to simplify database interactions and data modeling in modern web development.

  • Abstraction of Database Operations
    • Prisma abstracts away low-level SQL queries and database interactions.
  • Model-Driven Approach
    • Prisma encourages a model-driven approach to data management, where database entities (tables) are represented as models in the code.
  • Database Agnostic Approach
    • Prisma is great as it supports multiple database systems, including PostgreSQL, MySQL, SQLite, and SQL Server - allowing developers to switch between databases without significant code changes.
  • Code Generation and Type Safety
    • Prisma's code generation capabilities create TypeScript/JavaScript client libraries based on the database schema, providing type safety and auto-completion in the IDE.
  • Prisma Studio (favorite discovery ♥)
    • I discovered Prisma Studio which provides a visual representation of your database schema, including tables, columns, relationships, and constraints.
    • This makes it easy to explore and understand the structure of your database without writing SQL queries or accessing the database directly.

🔍 Discovering TypeScript

I gained hands-on experience integrating TypeScript with Prisma for database operations.

  • Static Typing
    • I learned how TypeScript can help catch errors during development time rather than runtime. This enhances code reliability, reduces bugs, and improves overall code quality.
  • Improved Code Readability
    • I learned how to use TypeScript interfaces and types for defining API payloads.

📈 Overall Growth:

This project increased my understanding in understanding ORM (Object-Relational Mapping) concepts, including object-relational mapping, entity relationships, data abstraction, query generation, and data validation. This helped me gain valuable insights into modern JavaScript development practices, type systems, and tooling.

API Endpoints ✅

States Endpoints
- GET All States
Description: Retrieve all states from the database.
Endpoint: GET /api/v1/states

- GET State by ID
Description: Retrieve a state by its ID.
Endpoint: GET /api/v1/states/:id

- CREATE State
Description: Create a new state.
Endpoint: POST /api/v1/states

- UPDATE State
Description: Update an existing state by its ID.
Endpoint: PUT /api/v1/states/:id

- DELETE State
Description: Delete a state by its ID.
Endpoint: DELETE /api/v1/states/:id
Cities Endpoints
- GET All Cities
Description: Retrieve all cities from the database.
Endpoint: GET /api/v1/cities

- GET City by ID
Description: Retrieve a city by its ID.
Endpoint: GET /api/v1/cities/:id

- CREATE City
Description: Create a new city.
Endpoint: POST /api/v1/cities

- UPDATE City
Description: Update an existing city by its ID.
Endpoint: PUT /api/v1/cities/:id

- DELETE City
Description: Delete a city by its ID.
Endpoint: DELETE /api/v1/cities/:id

Future Enhancements ✨

Authentication and Security:

  • Secure API endpoints with authentication mechanisms such as JWT (JSON Web Tokens) to control access.

Running the Project 🏃

  1. Fork and clone this repo locally, then navigate to the root folder of the project.
  2. Install dependencies by running:
npm install
  1. Start postgresql. To create the country_api database, run the following in the terminal:
createdb movie_api -O <enter your db owner name>
  1. Start the express server:
npm run dev
  1. To reset seed data:
npx prisma migrate reset
  1. Then rerun seed files:
npm run seeds
  1. Open prisma studio to view the tables (it will run on http://localhost:5555/):
npx prisma studio

Prisma Studio Prisma Studio - State Data

  1. To experiment with the API requests I created in Insomnia, import the docs/Insomnia_APIRequests.json file. Insomnia API Requests Or simply browse to http://localhost:3001/api/v1/cities in a browser.

Dependencies 🔧

DevDependencies

🧠 This project was based on Dominic Tremblay's Lighthouse Labs Advanced Topic Lecture.

📖 Read my blog post about Prisma - A Modern ORM here.

Project hours logged