Desishub Lessons
Node Js and Typescript
Test

A Beginner's Guide to Implementing API Documentation with Swagger (with Prisma Integration)

[... Previous content remains the same up to the "Using TypeScript Schemas in Swagger Documentation" section ...]

Using Prisma Schema in Swagger Documentation

If you're using Prisma, you already have a well-defined schema for your database models. We can leverage this schema to generate TypeScript types and use them in our Swagger documentation. Here's how to do it:

  1. First, make sure you have your Prisma schema defined in prisma/schema.prisma. For example:
// prisma/schema.prisma
 
model User {
  id    Int     @id @default(autoincrement())
  name  String
  email String  @unique
  age   Int?
}
  1. Generate Prisma client and TypeScript types:
npx prisma generate

This command generates TypeScript types based on your Prisma schema.

  1. Install @prisma/client if you haven't already:
npm install @prisma/client
  1. Create a file to export Prisma-generated types. Let's call it src/models/prismaTypes.ts:
// src/models/prismaTypes.ts
 
import { Prisma } from "@prisma/client";
 
export type User = Prisma.UserGetPayload<{}>;
export type CreateUserInput = Prisma.UserCreateInput;
  1. Now, we'll need to install a package that helps convert TypeScript to JSON Schema:
npm install typescript-json-schema
  1. Add a script to your package.json to generate JSON schemas:
"scripts": {
  "generate-schemas": "typescript-json-schema ./src/models/prismaTypes.ts '*' --out ./src/schemas.json"
}
  1. Run this script to generate the JSON schemas:
npm run generate-schemas
  1. Update your Swagger configuration in swagger.ts:
import swaggerJsdoc from "swagger-jsdoc";
import schemas from "./schemas.json";
 
const options = {
  definition: {
    openapi: "3.0.0",
    info: {
      title: "My API",
      version: "1.0.0",
      description: "A sample API using Prisma and Swagger",
    },
    servers: [
      {
        url: "http://localhost:3000",
        description: "Development server",
      },
    ],
    components: {
      schemas: schemas,
    },
  },
  apis: ["./src/routes/*.ts"],
};
 
export const specs = swaggerJsdoc(options);
  1. Update your route documentation to use these schemas:
// src/routes/userRoutes.ts
 
import express from "express";
import {
  createUser,
  getUsers,
  getUserById,
} from "../controllers/userController";
 
const router = express.Router();
 
/**
 * @swagger
 * /api/users:
 *   post:
 *     summary: Create a new user
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             $ref: '#/components/schemas/CreateUserInput'
 *     responses:
 *       201:
 *         description: Created
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/User'
 */
router.post("/users", createUser);
 
/**
 * @swagger
 * /api/users:
 *   get:
 *     summary: Retrieve a list of users
 *     responses:
 *       200:
 *         description: A list of users.
 *         content:
 *           application/json:
 *             schema:
 *               type: array
 *               items:
 *                 $ref: '#/components/schemas/User'
 */
router.get("/users", getUsers);
 
/**
 * @swagger
 * /api/users/{id}:
 *   get:
 *     summary: Get a user by ID
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *     responses:
 *       200:
 *         description: A single user.
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/User'
 *       404:
 *         description: User not found.
 */
router.get("/users/:id", getUserById);
 
export default router;

By following these steps, you're now using your Prisma schema to generate TypeScript types, which are then used to create JSON schemas for your Swagger documentation. This approach has several benefits:

  1. Your API documentation will always be in sync with your database schema.
  2. You can make changes to your Prisma schema and have them reflected in both your code and your documentation.
  3. It provides a single source of truth for your data models.
  4. You get the benefits of Prisma's powerful ORM along with comprehensive API documentation.

Remember to run the generate-schemas script whenever you make changes to your Prisma schema to keep your documentation up to date.

[... Rest of the content remains the same ...]