<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Yuya Miyamoto</title>
    <description>The latest articles on DEV Community by Yuya Miyamoto (@ymmy).</description>
    <link>https://dev.to/ymmy</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2760975%2F93a4f36f-8301-4a9b-aef4-41729b0bc4ad.png</url>
      <title>DEV Community: Yuya Miyamoto</title>
      <link>https://dev.to/ymmy</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ymmy"/>
    <language>en</language>
    <item>
      <title>[First Post] Created a CLI Tool to Infer Relationships from DB Schemas and Convert Them to DBML</title>
      <dc:creator>Yuya Miyamoto</dc:creator>
      <pubDate>Sun, 26 Jan 2025 01:33:06 +0000</pubDate>
      <link>https://dev.to/ymmy/first-post-created-a-cli-tool-to-infer-relationships-from-db-schemas-and-convert-them-to-dbml-22i7</link>
      <guid>https://dev.to/ymmy/first-post-created-a-cli-tool-to-infer-relationships-from-db-schemas-and-convert-them-to-dbml-22i7</guid>
      <description>&lt;p&gt;Hello! I’m Ymmy, a web engineer born in 2001.&lt;br&gt;&lt;br&gt;
I’ve just published my first npm package, so I wrote this article to introduce it.&lt;/p&gt;
&lt;h2&gt;
  
  
  Motivation
&lt;/h2&gt;

&lt;p&gt;When working on both professional and personal projects, I often find myself wanting an ER diagram to understand the database schema more clearly. However, drawing it by hand every time can be tedious, and there are so many tools out there that it can be confusing to choose.&lt;br&gt;&lt;br&gt;
I became interested in automatically generating ER diagrams using &lt;strong&gt;Database Markup Language (DBML)&lt;/strong&gt;. But since I couldn’t find many tools that can infer relationships from existing table and column names and generate DBML automatically, I decided to &lt;strong&gt;create my own CLI tool and publish it on npm&lt;/strong&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  Overview of the Tool
&lt;/h2&gt;

&lt;p&gt;The npm package I’ve released is called &lt;strong&gt;&lt;a href="https://www.npmjs.com/package/@ymmy/dbml-relationalizer" rel="noopener noreferrer"&gt;@ymmy/dbml-relationalizer&lt;/a&gt;&lt;/strong&gt;.&lt;br&gt;&lt;br&gt;
Here are its main features:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Automatically generate &lt;strong&gt;DBML&lt;/strong&gt; by fetching schema information from an existing database
&lt;/li&gt;
&lt;li&gt;Read a custom &lt;code&gt;relations.yml&lt;/code&gt; file to define additional relationships
&lt;/li&gt;
&lt;li&gt;Infer relationships (through a built-in inference feature) based on table and column naming patterns
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can find the source code on GitHub:&lt;br&gt;&lt;br&gt;
&lt;a href="https://github.com/Ymmy833y/dbml-relationalizer" rel="noopener noreferrer"&gt;https://github.com/Ymmy833y/dbml-relationalizer&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Key Features
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Inference&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Automatically determines relationships such as &lt;code&gt;users.id ↔ orders.user_id&lt;/code&gt; based on table and column names.  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You can select the inference strategy (&lt;code&gt;default&lt;/code&gt; or &lt;code&gt;identical&lt;/code&gt;) to match your naming conventions.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Manual Relationship Definitions&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
If needed, you can specify custom column-to-column relationships in a &lt;code&gt;relations.yml&lt;/code&gt; file.  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Supports flexible pattern matching using wildcards (&lt;code&gt;%&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Control Over Self-Referencing Relationships&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
By toggling &lt;code&gt;ignoreSelfReferences&lt;/code&gt;, you can easily include or exclude self-referencing relationships.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;
  
  
  Installation
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Via npm
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-g&lt;/span&gt; @ymmy/dbml-relationalizer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h2&gt;
  
  
  Usage
&lt;/h2&gt;
&lt;h3&gt;
  
  
  1. (Optional) Create &lt;code&gt;relations.yml&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Here’s an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;inference&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;enabled&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
  &lt;span class="na"&gt;strategy&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;default&lt;/span&gt;

&lt;span class="na"&gt;relations&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;parentQualifiedColumn&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;users.id"&lt;/span&gt;
    &lt;span class="na"&gt;childQualifiedColumns&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;orders.user_id"&lt;/span&gt;

&lt;span class="na"&gt;ignoreSelfReferences&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;inference.enabled&lt;/strong&gt;: Whether to enable inference
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;inference.strategy&lt;/strong&gt;: The inference mode that matches your naming conventions

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;default&lt;/code&gt;: e.g. &lt;code&gt;users.id&lt;/code&gt; → &lt;code&gt;orders.user_id&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;identical&lt;/code&gt;: e.g. &lt;code&gt;users.user_id&lt;/code&gt; → &lt;code&gt;orders.user_id&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;relations&lt;/strong&gt;: Manually added relationship definitions
&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;ignoreSelfReferences&lt;/strong&gt;: Whether to exclude self-referencing relationships&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;For more details, check out the &lt;a href="https://github.com/Ymmy833y/dbml-relationalizer/blob/master/README.md" rel="noopener noreferrer"&gt;GitHub repository&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Run the CLI
&lt;/h3&gt;

&lt;p&gt;Just run the following command to generate DBML:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;relation2dbml mysql mysql://user:pass@localhost:3306/dbname &lt;span class="nt"&gt;-o&lt;/span&gt; schema.dbml
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;First argument&lt;/strong&gt;: Connection type (see &lt;a href="https://dbml.dbdiagram.io/js-module/connector/" rel="noopener noreferrer"&gt;here&lt;/a&gt; for details)
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Second argument&lt;/strong&gt;: Connection string (see &lt;a href="https://dbml.dbdiagram.io/js-module/connector/" rel="noopener noreferrer"&gt;here&lt;/a&gt; for details)
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-o, --out-file&lt;/code&gt;: Specifies the output file (if omitted, the DBML is printed to stdout)
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-i, --input-file&lt;/code&gt;: Path to &lt;code&gt;relations.yml&lt;/code&gt; (default: &lt;code&gt;./relations.yml&lt;/code&gt;)
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-v, --verbose&lt;/code&gt;: Outputs detailed logs&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When you run this command, a file named &lt;code&gt;schema.dbml&lt;/code&gt; is generated, containing your tables and relationships in DBML format.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example: Generated DBML
&lt;/h2&gt;

&lt;p&gt;Below is an example of the kind of DBML you might end up with (just for illustration):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Table "user" {
  "id" int [pk, not null, increment]
  ...
}

Table "order" {
  "order_id" int [pk, not null, increment]
  "user_id" int [not null]
  ...
}

Ref "infer_fk_user_order_id":"user"."id" &amp;lt; "order"."user_id"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can load this into a database design tool to visualize an ER diagram, which is quite handy.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;&lt;a href="https://www.npmjs.com/package/@ymmy/dbml-relationalizer" rel="noopener noreferrer"&gt;@ymmy/dbml-relationalizer&lt;/a&gt;&lt;/strong&gt; CLI tool:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Fetches your DB schema and converts it to DBML&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Infers relationships automatically, with optional manual overrides&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Makes it easy to visualize everything as an ER diagram&lt;/strong&gt; (for example, by using &lt;a href="https://github.com/softwaretechnik-berlin/dbml-renderer" rel="noopener noreferrer"&gt;dbml-renderer&lt;/a&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This workflow may be particularly helpful for those who want a straightforward way to generate and maintain database documentation for personal or medium-scale projects.  &lt;/p&gt;

&lt;p&gt;If you have any questions or suggestions, feel free to leave a comment or open an issue on GitHub. Your feedback is welcome!&lt;/p&gt;




&lt;p&gt;Thank you for reading my very first post!&lt;br&gt;&lt;br&gt;
If you find it interesting, please give it a try. I plan to keep improving it little by little.  &lt;/p&gt;

&lt;p&gt;See you in the next article!&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
