Unlocking the Power of Prisma: Getting Prisma Types into JSDoc and Displaying them in DocDash
Image by Kanti - hkhazo.biz.id

Unlocking the Power of Prisma: Getting Prisma Types into JSDoc and Displaying them in DocDash

Posted on

Are you tired of manually documenting your Prisma models and struggling to keep your documentation up-to-date? Look no further! In this comprehensive guide, we’ll walk you through the process of integrating Prisma types with JSDoc and displaying them in DocDash, the ultimate documentation hub. Get ready to take your documentation to the next level!

What is Prisma?

For those new to Prisma, let’s start with a brief introduction. Prisma is a powerful ORM (Object-Relational Mapping) tool that simplifies database interactions and provides a robust, type-safe API for your database. It supports a wide range of databases, including PostgreSQL, MySQL, and MongoDB. With Prisma, you can define your database schema using a simple, intuitive data model, and generate a robust, type-safe API for your application.

What is JSDoc?

JSDoc, on the other hand, is a popular documentation generator for JavaScript projects. It extracts documentation comments from your code and generates beautiful, readable documentation in various formats, including HTML, Markdown, and JSON. JSDoc is widely used in the JavaScript community and is an essential tool for maintaining accurate, up-to-date documentation.

What is DocDash?

DocDash is a modern, web-based documentation hub that allows you to create, manage, and display documentation for your projects. It supports a wide range of documentation formats, including JSDoc, Markdown, and HTML. With DocDash, you can easily create a centralized documentation portal for your projects, making it easier for developers, users, and stakeholders to access and understand your documentation.

The Challenge: Integrating Prisma with JSDoc and DocDash

So, why is integrating Prisma with JSDoc and DocDash a challenge? The main issue is that Prisma generates TypeScript definitions for your models, which don’t play nicely with JSDoc out of the box. However, with a few clever tricks and configurations, we can bridge the gap between Prisma, JSDoc, and DocDash. In the following sections, we’ll walk you through the process step-by-step.

Step 1: Generate Prisma Types

First, let’s generate the Prisma types for your models. Assuming you have a `prisma` folder in your project root, containing your Prisma schema file (`schema.prisma`), run the following command:

npx prisma generate

This command generates TypeScript definitions for your models in the `generated` folder. These definitions will serve as the foundation for our JSDoc integration.

Step 2: Configure JSDoc to Read Prisma Types

To configure JSDoc to read the Prisma types, we need to create a `jsdoc.json` configuration file in our project root. Add the following content to the file:

{
  "source": {
    "include": ["generated"]
  },
  "opts": {
    "recurse": true
  },
  "plugins": ["jsdoc-prisma"]
}

This configuration tells JSDoc to include the `generated` folder, which contains our Prisma types, and enables recursion to traverse the folder hierarchy. The `jsdoc-prisma` plugin is a custom plugin we’ll create later to handle Prisma type annotations.

Step 3: Create a Custom JSDoc Plugin for Prisma Types

Create a new file called `jsdoc-prisma.js` in your project root, and add the following content:

const jsdoc = require('jsdoc');

jsdoc.tagDictionary.defineTag('prisma’, {
  mustHaveValue: true,
  onTagged: (doclet, tag) => {
    const { name, description } = tag;
    doclet.prismatype = { name, description };
  }
});

This plugin defines a custom `@prisma` tag that extracts the Prisma type information from our generated TypeScript definitions.

Step 4: Update Prisma Models with JSDoc Comments

Now, let’s update our Prisma models with JSDoc comments that include the `@prisma` tag. Open your `schema.prisma` file and add the following comments:

model User {
  id       String   @id @default(cuid())
  name     String
  email    String   @unique
  createdAt DateTime @default(now())
  updatedAt DateTime @default(now())

  /**
   * @prisma { type: "User", description: "Represents a user entity" }
   */
}

These comments will be picked up by JSDoc and used to generate documentation for our Prisma models.

Step 5: Run JSDoc and Generate Documentation

With our configuration and plugin in place, let’s run JSDoc to generate our documentation:

npx jsdoc -c jsdoc.json

This command generates documentation for our Prisma models, including the extracted type information.

Step 6: Display Documentation in DocDash

Finally, let’s display our generated documentation in DocDash. Create a new file called `docdash.json` in your project root, and add the following content:

{
  "name": "My Prisma Project",
  "description": "Documentation for my Prisma project",
  "docs": ["jsdocoutput"]
}

This configuration tells DocDash to display our JSDoc-generated documentation. Run the following command to start DocDash:

npx docdash -c docdash.json

Open your web browser and navigate to http://localhost:8080 to see your beautifully generated documentation, complete with Prisma type information!

Prisma Model JSDoc Comment Generated Documentation
User /** @prisma { type: "User", description: "Represents a user entity" } */
  • Type: User
  • Description: Represents a user entity

Conclusion

We’ve successfully integrated Prisma with JSDoc and DocDash, unlocking a world of possibilities for generating and displaying robust, type-safe documentation for our Prisma models. By following these steps, you can easily maintain accurate, up-to-date documentation for your projects, making it easier for developers, users, and stakeholders to understand and work with your code.

Final Thoughts

Remember to keep your Prisma schema up-to-date and re-run the `prisma generate` command whenever you make changes to your schema. This ensures that your JSDoc-generated documentation stays accurate and reflects the latest changes to your Prisma models.

With this comprehensive guide, you’re now equipped to take your Prisma project to the next level, leveraging the power of JSDoc and DocDash to create a robust, maintainable documentation ecosystem. Happy documenting!

Frequently Asked Question

Got questions about getting Prisma types into JSDoc and displaying them in DocDash? We’ve got answers!

How do I generate JSDoc annotations for my Prisma schema?

You can use the `prisma generate` command with the `–jsdoc` flag to generate JSDoc annotations for your Prisma schema. This will create a `jsdoc.json` file in your Prisma schema directory, which contains the generated JSDoc annotations.

How do I integrate Prisma types with my existing JSDoc setup?

You can integrate Prisma types with your existing JSDoc setup by including the generated `jsdoc.json` file in your JSDoc configuration. You can do this by adding the `prisma/generated/jsdoc.json` file to your JSDoc `include` pattern or by using a JSDoc plugin like `jsdoc-prisma` to automatically include the Prisma types.

How do I customize the display of Prisma types in DocDash?

You can customize the display of Prisma types in DocDash by using JSDoc tags and templates. For example, you can use the `@typedef` tag to define a custom type definition for your Prisma model, and then use a template to customize the display of that type in DocDash.

Do I need to manually update my JSDoc annotations when my Prisma schema changes?

No, you don’t need to manually update your JSDoc annotations when your Prisma schema changes. The `prisma generate` command will automatically regenerate the JSDoc annotations for your Prisma schema, so you can simply re-run the command to update your JSDoc annotations.

Can I use Prisma types with other documentation tools besides DocDash?

Yes, you can use Prisma types with other documentation tools besides DocDash. Since Prisma types are generated as JSDoc annotations, you can use them with any documentation tool that supports JSDoc, such as JSDoc, Dox, or typedoc.