Prisma ERD

repository·master·Indexed 18 days ago

https://github.com/skn0tt/prisma-erd

A tool for visualizing Prisma database schemas as Entity-Relationship Diagrams (ERDs). It provides a web-based interface and an API endpoint to transform Prisma schemas into Mermaid diagram syntax, which can then be rendered as SVGs and exported as files.

Tokens
947
Snippets
3
Records
4
Agent score
13%

What's inside prisma-erd

  1. Visualize Prisma schemas as Entity-Relationship Diagrams

    master

    Prisma ERD is a tool designed to take Prisma schemas and generate visual Entity-Relationship Diagrams (ERDs). You can use the web-based version of the tool at https://prisma-erd.simonknott.de.

    If you prefer to integrate this functionality directly into your Prisma workflow as a generator, you can use the prisma-erd-generator built by @keonik.

  2. Render Mermaid syntax to SVG

    master

    Once you have received the Mermaid diagram string, you can render it into an HTML element using the mermaid.mermaidAPI.render method.

    In this implementation, the renderWithMermaid function takes the Mermaid input string, renders it using a specific svgId, and injects the resulting SVG string into a container element (graphDiv). After rendering, the SVG attributes for height and width are cleared to allow for responsive scaling.

    function renderWithMermaid(input: string) {
      mermaid.mermaidAPI.render(
        svgId,
        input,
        (svg: string) => (graphDiv.innerHTML = svg)
      );
    
      const svgEl = document.getElementById(svgId);
      svgEl.setAttribute("height", undefined);
      svgEl.setAttribute("width", undefined);
    }
  3. Transform Prisma schema to Mermaid diagram via API

    master

    The application provides a way to transform a Prisma schema into a Mermaid diagram by sending a POST request to the / .netlify/functions/render-dml-to-mermaid endpoint.

    To perform the transformation, you must send the raw schema text in the body of a POST request with the Content-Type set to text/plain. The server returns the Mermaid diagram syntax as a plain text string.

    fetch("/.netlify/functions/render-dml-to-mermaid", {
      method: "POST",
      body: schemaInput.value,
      headers: {
        "Content-Type": "text/plain",
      },
    });
  4. Export rendered Mermaid diagram as SVG file

    master

    The application allows downloading the rendered Mermaid diagram as an SVG file. This is achieved by serializing the rendered SVG element, ensuring the correct XML namespaces (xmlns="http://www.w3.org/2000/svg" and xmlns:xlink="http://www.w3.org/1999/xlink") are present, and prepending an XML declaration.

    The resulting XML string is then encoded as a Base64 data URI and assigned to the href attribute of a download anchor element.

    function updateDownloadButton() {
      const el = document.getElementById(svgId);
      const serializer = new XMLSerializer();
      let source = serializer.serializeToString(el);
    
      // Ensure namespaces are present for valid SVG export
      if (!source.match(/^<svg[^>]+xmlns="http:\/\/www\.w3\.org\/2000\/svg"/)) {
        source = source.replace(/^<svg/, '<svg xmlns="http://www.w3.org/2000/svg"');
      }
      if (!source.match(/^<svg[^>]+"http:\/\/www\.w3\.org\/1999\/xlink"/)) {
        source = source.replace(
          /^<svg/,
          '<svg xmlns:xlink="http://www.w3.org/1999/xlink"'
        );
      }
    
      source = '<?xml version=