Joining an existing project with a large codebase can be a daunting task.
Understanding the data model of an application is key for developers to start working on an existing project. One commonly used tool to help overcome this challenge, and enable developers to grasp an application's data model is an ER (Entity Relation) diagram.
ER diagrams provide a visual representation of your data model, and details each field of the entities. Many tools can help create these, where one example is Jetbrains DataGrip, that can generate an ER diagram by connecting to and inspecting an existing database:
DataGrip ER diagram example
Ent, a simple, yet powerful entity framework for Go, was originally developed inside Facebook specifically for dealing with projects with large and complex data models. This is why Ent uses code generation - it gives type-safety and code-completion out-of-the-box which helps explain the data model and improves developer velocity. On top of all of this, wouldn't it be great to automatically generate ER diagrams that maintain a high-level view of the data model in a visually appealing representation? (I mean, who doesn't love visualizations?)
entviz is an ent extension that automatically generates a static HTML page that visualizes your data graph.
Entviz example output
If you want to know more about how entviz was implemented, checkout the implementation section.
First, let's add the entviz extension to our entc.go file:
If you are not familiar with
entc you're welcome to read entc documentation to learn more about it.
Let's say we have a simple schema with a user entity and some fields:
Now, entviz will automatically generate a visualization of our graph everytime we run:
You should now see a new file called
schema-viz.html in your ent directory:
Open the html file with your favorite browser to see the visualization
Next, let's add another entity named Post, and see how our visualization changes:
Now we add an (O2M) edge from User to Post:
Finally, regenerate the code:
Refresh your browser to see the updated result!
Entviz was implemented by extending ent via its extension API.
The Ent extension API lets you aggregate multiple templates, hooks, options and annotations.
For instance, entviz uses templates to add another go file,
entviz.go, which exposes the
ServeEntviz method that can be used as an http handler, like so:
We define an extension struct which embeds the default extension, and we export our template via the
The template file is the code that we want to generate:
That's it! now we have a new method in ent package.
We saw how ER diagrams help developers keep track of their data model. Next, we introduced entviz - an Ent extension that automatically generates an ER diagram for Ent schemas. We saw how entviz utilizes Ent's extension API to extend the code generation and add extra functionality. Finally, you got to see it in action by installing and use entviz in your own project. If you like the code and/or want to contribute - feel free to checkout the project on github.
Have questions? Need help with getting started? Feel free to join our Slack channel.