Rumah >hujung hadapan web >tutorial js >Membina API RESTful dengan Prisma, Express, TypeScript dan PostgreSQL

Membina API RESTful dengan Prisma, Express, TypeScript dan PostgreSQL

WBOY
WBOYasal
2024-09-07 06:31:32880semak imbas

Building a RESTful API with Prisma, Express, TypeScript, and PostgreSQL

Table of Contents

  1. Introduction

    • Overview of Prisma, Express, TypeScript, and PostgreSQL
    • Why Prisma as an ORM?
    • Setting Up the Environment
  2. Initial Project Setup

    • Setting Up a New Node.js Project
    • Configuring TypeScript
    • Installing Required Packages
  3. Setting Up PostgreSQL

    • Installing PostgreSQL
    • Creating a New Database
    • Configuring Environment Variables
  4. Setting Up Prisma

    • Installing Prisma
    • Initializing Prisma in the Project
    • Configuring the Prisma Schema
  5. Defining the Data Model

    • Understanding Prisma Schema Language (PSL)
    • Creating Models for the API
    • Migrations with Prisma
  6. Integrating Prisma with Express

    • Setting Up the Express Server
    • Creating CRUD Operations with Prisma
    • Error Handling and Validation
  7. Using TypeScript for Type Safety

    • Defining Types with Prisma
    • Type-Safe Queries and Mutations
    • Leveraging TypeScript in API Development
  8. Testing the API

    • Writing Unit Tests for Prisma Models
    • Integration Testing with Supertest and Jest
    • Mocking the Database with Prisma
  9. Deployment Considerations

    • Preparing the API for Production
    • Deploying PostgreSQL
    • Deploying the Node.js Application
  10. Conclusion

    • Benefits of Using Prisma with Express and TypeScript
    • Final Thoughts and Next Steps

1. Introduction

Overview of Prisma, Express, TypeScript, and PostgreSQL

In modern web development, building robust, scalable, and type-safe APIs is crucial. Combining the power of Prisma as an ORM, Express for server-side logic, TypeScript for static typing, and PostgreSQL as a reliable database solution, we can create a powerful RESTful API.

Prisma simplifies database management by providing a modern ORM that supports type-safe queries, migrations, and seamless database schema management. Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications. TypeScript adds static type definitions to JavaScript, helping catch errors early in the development process. PostgreSQL is a powerful, open-source relational database system known for its reliability and feature set.

Why Prisma as an ORM?

Prisma offers several advantages over traditional ORMs like Sequelize and TypeORM:

  • Type-Safe Database Queries: Automatically generated types ensure that your database queries are type-safe and error-free.
  • Automated Migrations: Prisma provides a powerful migration system that keeps your database schema in sync with your Prisma schema.
  • Intuitive Data Modeling: The Prisma schema file (written in PSL) is easy to understand and maintain.
  • Extensive Ecosystem: Prisma integrates seamlessly with other tools and services, including GraphQL, REST APIs, and popular databases like PostgreSQL.

Setting Up the Environment

Before we dive into the code, ensure you have the following tools installed on your machine:

  • Node.js (LTS version recommended)
  • npm or Yarn (for package management)
  • TypeScript (for static typing)
  • PostgreSQL (as our database)

Once these tools are installed, we can start building our API.


2. Initial Project Setup

Setting Up a New Node.js Project

  1. Create a new project directory:
   mkdir prisma-express-api
   cd prisma-express-api
  1. Initialize a new Node.js project:
   npm init -y

This will create a package.json file in your project directory.

Configuring TypeScript

  1. Install TypeScript and Node.js types:
   npm install typescript @types/node --save-dev
  1. Initialize TypeScript in your project:
   npx tsc --init

This command creates a tsconfig.json file, which is the configuration file for TypeScript. Modify it as needed for your project. Here’s a basic setup:

   {
     "compilerOptions": {
       "target": "ES2020",
       "module": "commonjs",
       "strict": true,
       "esModuleInterop": true,
       "skipLibCheck": true,
       "forceConsistentCasingInFileNames": true,
       "outDir": "./dist"
     },
     "include": ["src/**/*"]
   }
  1. Create the project structure:
   mkdir src
   touch src/index.ts

Installing Required Packages

To start with Express and Prisma, you'll need to install some essential packages:

npm install express prisma @prisma/client
npm install --save-dev ts-node nodemon @types/express
  • express: The web framework for Node.js.
  • prisma: The Prisma CLI for database management.
  • @prisma/client: The Prisma client for querying the database.
  • ts-node: Runs TypeScript directly without the need for precompilation.
  • nodemon: Automatically restarts the server on file changes.
  • @types/express: TypeScript definitions for Express.

3. Setting Up PostgreSQL

Installing PostgreSQL

PostgreSQL can be installed via your operating system’s package manager or directly from the official website. For example, on macOS, you can use Homebrew:

brew install postgresql
brew services start postgresql

Creating a New Database

Once PostgreSQL is installed and running, you can create a new database for your project:

psql postgres
CREATE DATABASE prisma_express;

Replace prisma_express with your preferred database name.

Configuring Environment Variables

To connect to the PostgreSQL database, create a .env file in your project’s root directory and add the following environment variables:

DATABASE_URL="postgresql://<user>:<password>@localhost:5432/prisma_express"

Replace and with your PostgreSQL username and password. This connection string will be used by Prisma to connect to your PostgreSQL database.


4. Setting Up Prisma

Installing Prisma

Prisma is already installed in the previous step, so the next step is to initialize it within the project:

npx prisma init

This command will create a prisma directory containing a schema.prisma file and a .env file. The .env file should already contain the DATABASE_URL you specified earlier.

Configuring the Prisma Schema

The schema.prisma file is where you'll define your data models, which will be used to generate database tables.

Here’s a basic example schema:

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

model User {
  id        Int      @id @default(autoincrement())
  name      String
  email     String   @unique
  createdAt DateTime @default(now())
  posts     Post[]
}

model Post {
  id        Int      @id @default(autoincrement())
  title     String
  content   String?
  published Boolean  @default(false)
  authorId  Int
  author    User     @relation(fields: [authorId], references: [id])
}

In this schema, we have two models: User and Post. Each model corresponds to a database table. Prisma uses these models to generate type-safe queries for our database.


5. Defining the Data Model

Understanding Prisma Schema Language (PSL)

Prisma Schema Language (PSL) is used to define your database schema. It's intuitive and easy to read, with a focus on simplicity. Each model in the schema represents a table in your database, and each field corresponds to a column.

Creating Models for the API

In the schema defined earlier, we created two models:

  • User: Represents users in our application.
  • Post: Represents posts created by users.

Migrations with Prisma

To apply your schema changes to the database, you’ll need to run a migration:

npx prisma migrate dev --name init

This command will create a new migration file and apply it to your database, creating the necessary tables.


6. Integrating Prisma with Express

Setting Up the Express Server

In your src/index.ts, set up the basic Express server:

import express, { Request, Response } from 'express';
import { PrismaClient } from '@prisma/client';

const app = express();
const prisma = new PrismaClient();

app.use(express.json());

app.get('/', (req: Request, res: Response) => {
  res.send('Hello, Prisma with Express!');
});

const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
  console.log(`Server is running on port ${PORT}`);
});

This code sets up a simple Express server and initializes the Prisma client.

Creating CRUD Operations with Prisma

Next, let’s create some CRUD (Create, Read, Update, Delete) routes for our User model.

Create a new user:

app.post('/user', async (req: Request, res: Response) => {
  const { name, email } = req.body;
  const user = await prisma.user.create({
    data: { name, email },
  });
  res.json(user);
});

Read all users:


app.get('/users', async (req: Request, res: Response) => {
  const users = await prisma.user.findMany();
  res.json(users);
});

Update a user:

app.put('/user/:id', async (req: Request, res: Response) => {
  const { id } = req.params;
  const { name, email } = req.body;
  const user = await prisma.user.update({
    where: { id: Number(id) },
    data: { name, email },
  });
  res.json(user);
});

Delete a user:

app.delete('/user/:id', async (req: Request, res: Response) => {
  const { id } = req.params;
  const user = await prisma.user.delete({
    where: { id: Number(id) },
  });
  res.json(user);
});

Error Handling and Validation

To enhance the robustness of your API, consider adding error handling and validation:

app.post('/user', async (req: Request, res: Response) => {
  try {
    const { name, email } = req.body;
    if (!name || !email) {
      return res.status(400).json({ error: 'Name and email are required' });
    }
    const user = await prisma.user.create({
      data: { name, email },
    });
    res.json(user);
  } catch (error) {
    res.status(500).json({ error: 'Internal Server Error' });
  }
});

7. Using TypeScript for Type Safety

Defining Types with Prisma

Prisma automatically generates TypeScript types for your models based on your schema. This ensures that your database queries are type-safe.

For example, when creating a new user, TypeScript will enforce the shape of the data being passed:

const user = await prisma.user.create({
  data: { name, email }, // TypeScript ensures 'name' and 'email' are strings.
});

Type-Safe Queries and Mutations

With TypeScript, you get autocomplete and type-checking for all Prisma queries, reducing the chance of runtime errors:

const users: User[] = await prisma.user.findMany();

Leveraging TypeScript in API Development

Using TypeScript throughout your API development helps catch potential bugs early, improves code readability, and enhances overall development experience.


8. Testing the API

Writing Unit Tests for Prisma Models

Testing is an essential part of any application development. You can write unit tests for your Prisma models using a testing framework like Jest:

npm install jest ts-jest @types/jest --save-dev

Create a jest.config.js file:

module.exports = {
  preset: 'ts-jest',
  testEnvironment: 'node',
};

Example test for creating a user:

import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient();

test('should create a new user', async () => {
  const user = await prisma.user.create({
    data: {
      name: 'John Doe',
      email: 'john.doe@example.com',
    },
  });
  expect(user).toHaveProperty('id');
  expect(user.name).toBe('John Doe');
});

Integration Testing with Supertest and Jest

You can also write integration tests using Supertest:

npm install supertest --save-dev

Example integration test:

import request from 'supertest';
import app from './app'; // Your Express app

test('GET /users should return a list of users', async () => {
  const response = await request(app).get('/users');
  expect(response.status).toBe(200);
  expect(response.body).toBeInstanceOf(Array);
});

Mocking the Database with Prisma

For testing purposes, you might want to mock the Prisma client. You can do this using tools like jest.mock() or by creating a mock instance of the Prisma client.


9. Deployment Considerations

Preparing the API for Production

Before deploying your API, ensure you:

  • Remove all development dependencies.
  • Set up environment variables correctly.
  • Optimize the build process using tools like tsc and webpack.

Deploying PostgreSQL

You can deploy PostgreSQL using cloud services like AWS RDS, Heroku, or DigitalOcean. Make sure to secure your database with proper authentication and network settings.

Deploying the Node.js Application

For deploying the Node.js application, consider using services like:

  • Heroku: For simple, straightforward deployments.
  • AWS Elastic Beanstalk: For more control over the infrastructure.
  • Docker: To containerize the application and deploy it on any cloud platform.

10. Conclusion

Benefits of Using Prisma with Express and TypeScript

Using Prisma as an ORM with Express and TypeScript provides a powerful combination for building scalable, type-safe, and efficient RESTful APIs. With Prisma, you get automated migrations, type-safe queries, and an intuitive schema language, making database management straightforward and reliable.

Congratulations!! You've now built a robust RESTful API using Prisma, Express, TypeScript, and PostgreSQL. From setting up the environment to deploying the application, this guide covered the essential steps to get you started. As next steps, consider exploring advanced Prisma features like nested queries, transactions, and more complex data models.

Happy coding!

Atas ialah kandungan terperinci Membina API RESTful dengan Prisma, Express, TypeScript dan PostgreSQL. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel sebelumnya:Devtool tidak diperlukanArtikel seterusnya:Devtool tidak diperlukan