npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@eng-tools/zod-to-d2

v0.0.26

Published

A library that extends Zod types and provides tools to convert schemas to D2 diagrams.

Readme

zod-to-d2

CI npm version

zod-to-d2 is a TypeScript library that extends the Zod schema validation library, enabling you to annotate your schemas and automatically generate D2 diagrams. This helps visualize data models, relationships, and schema structures directly from your Zod definitions.

Features

  • Schema Annotation: Add metadata (primary keys, foreign keys, notes, table names) to Zod schemas.
  • Automatic Diagram Generation: Convert annotated Zod schemas into D2 diagrams.
  • Relationship Mapping: Visualize foreign keys, primary keys, and other relationships.
  • Extensible: Supports custom extensions for notes, table names, and more.
  • CLI Tool: Generate diagrams from files or directories via command line.
  • TypeScript Support: Fully typed for safe and predictable usage.

Installation

pnpm add @eng-tools/zod-to-d2
# or
npm install @eng-tools/zod-to-d2
# or
yarn add @eng-tools/zod-to-d2

Usage

  1. Annotate your Zod schemas

Sample schemas here

import { z } from "zod";
import "@eng-tools/zod-to-d2";

export const userSchema = z
  .object({
    id: z.string().primaryKey(), //<-- marks the 'id' property as a PK
    name: z.string().notes("The name of the user"), //<-- includes comments on this property on the output diagram
    email: z.string(),
  })
  .tableName("users"); //<-- give the table/entity a name

export const postSchema = z
  .object({
    id: z.string().primaryKey(),
    authorId: z.string().foreignKey(userSchema, "id"), //<- creates a FK relationship to the 'id' property of the 'userSchema'
    content: z.string(),
  })
  .tableName("posts"); //<-- give the table/entity a name
  1. Generate a D2 diagram (programmatic API)
import { generateDiagramText } from "@eng-tools/zod-to-d2";

const diagram = generateDiagramText([userSchema, postSchema]);
console.log(diagram); // D2 diagram source

This outputs the following D2 text:

direction: down
title: |md Sample Diagram | {near: top-center}
users: {
  shape: sql_table
  "id": string {constraint: [primary_key]}
  "name": string # The name of the user
  "email": string
}

posts: {
  shape: sql_table
  "id": string {constraint: [primary_key]}
  "authorId": string {constraint: [foreign_key]}
  "content": string
}

posts.authorId <-> users.id: {
  source-arrowhead: {shape: cf-many}
  target-arrowhead: {shape: cf-one-required}
}

You can find how this looks on the D2 Playground here, or as shown in the image below.

!Sample Diagram

  1. Generate a D2 diagram (CLI)
> pnpm zod2d2 --file-paths src/schemas/user.ts src/schemas/post.ts --output-path diagram.d2 --title "My Diagram"
# or
> pnpm zod2d2 --directory src/schemas --output-path diagram.d2

License

MIT

Contributing

Contributions and suggestions are welcome! Please open issues or pull requests on GitHub.