pwshub.com

How to Create Database Documentation Using dbdocs with DBML

How to Create Database Documentation Using dbdocs with DBML

Database documentation plays a crucial role in maintaining and scaling systems. Clear and well-organized documentation can significantly improve communication between team members and enhance project longevity.

One of the most efficient ways to document a database is through dbdocs and DBML - an open sourced Database Markup Language.

In this guide, I’ll show you how to create database documentation using these tools, step by step.

What is dbdocs?

dbdocs is a platform that generates database documentation from your schema, easily shareable via a link. Using DBML (Database Markup Language), you can create clear, shareable, and updatable documentation of your database structure.

Prerequisites

Before we begin, ensure you have the following:

  • Basic knowledge of databases and SQL.

  • A database schema to document (we’ll use a PostgreSQL example in this guide).

Step 1: Install DBML CLI and dbdocs

Start by installing the DBML CLI, which helps convert your database schema into a DBML format. You also need the dbdocs CLI to generate and publish your documentation.

npm install -g dbdocs

Step 2: Export Your Database Schema to DBML

DB diagram

If you’re working with an existing database, you can export the schema into DBML using the DBML CLI tool.

For PostgreSQL, run the following command:

$ dbdocs db2dbml postgres <connection-string> -o database.dbml
✔ Connecting to database <db-name>... done.
✔ Generating DBML... done.
✔ Wrote to database.dbml

Extract DBML code from database connection

This command will export your database schema and save it into a file called database.dbml.

Here’s an example of how a generated DBML file might look:

Table users {
  id int [pk, increment]
  username varchar(50) [not null]
  email varchar(100) [not null, unique]
  created_at timestamp [not null]
}
Table orders {
  id int [pk, increment]
  user_id int [not null, ref: > users.id]
  total decimal [not null]
  created_at timestamp [not null]
}

In this example:

• The users and orders tables are defined.

• Fields are annotated with types and constraints.

• The relationship between orders.user_id and users.id is established using ref.

Step 3: Edit and Add Notes to the DBML File

You may want to clean it up or add extra documentation like table descriptions and field descriptions to communicate with other members in the team.

Add notes to generated DBML Code

Step 4: Generate Documentation with dbdocs

Once your DBML file is ready, the next step is to generate the documentation using dbdocs. First, you need to login to dbdocs:

dbdocs login

After logging in, publish the DBML file:

dbdocs build database.dbml

Generate database documentation from DBML file

This command will generate a shareable documentation link that you can access via the dbdocs platform. You can also set access permissions and collaborate with your team.

This seamless workflow ensures that your documentation always reflects the latest state of your database.

Benefits of Using dbdocs with DBML

Conclusion

In this tutorial, we explored how to export a database schema, customize it, and generate shareable documentation using dbdocs.

By incorporating this workflow into your development process, you’ll improve your team’s collaboration, enhance your project’s scalability, and ensure that everyone stays on the same page. Happy documenting!

Source: freecodecamp.org

Related stories
2 weeks ago - Entity Relationship (ER) Diagram is a visual representation of entities (people, objects, or concepts) and their relationships in a database, used to design and model database structures. ER Diagram simplifies database design by clearly...
2 days ago - Unlock insights instantly with Amazon Redshift's new zero-ETL integrations for Aurora PostgreSQL and DynamoDB, eliminating complex ETL pipelines.
1 day ago - Databases play a vital role in the development of applications across mobile and web platforms. Adequate knowledge of data interactions between the application structure and the database is essential for storing relevant application data....
1 week ago - Node.js v22.5.0 introduced a native SQLite module, which is is similar to what other JavaScript runtimes like Deno and Bun already have. The post Using the built-in SQLite module in Node js appeared first on LogRocket Blog.
1 month ago - Learn how to handle real-time geospatial data using Supabase Realtime and Flutter.
Other stories
32 minutes ago - Fixes 41 bugs (addressing 595 👍). node:http2 server and gRPC server support, ca and cafile support in bun install, Bun.inspect.table, bun build --drop, iterable SQLite queries, iterator helpers, Promise.try, Buffer.copyBytesFrom, and...
4 hours ago - This guide provides a foundational understanding of Redux and why you should use it for state management in a React app. The post Understanding Redux: A tutorial with examples appeared first on LogRocket Blog.
6 hours ago - Discover some of the best Node.js web scraping libraries, including Axios and Superagent, and techniques for how to use them. The post The best Node.js web scrapers for your use case appeared first on LogRocket Blog.
10 hours ago - Infinite runner games have been a favorite for gamers and developers alike due to their fast-paced action and replayability. These games often feature engaging mechanics like endless levels, smooth character movement, and dynamic...
12 hours ago - Yesterday, Elizabeth Siegle, a developer advocate for CLoudflare, showed off a really freaking cool demo making use of Cloudflare's Workers AI support. Her demo made use of WNBA stats to create a beautiful dashboard that's then enhanced...