Sitemap

Defining Ash Framework resources from Mermaid ER diagrams with ChatGPT

3 min readApr 17, 2023

ChatGPT understands Mermaid ER diagrams! Let’s try an example:

erDiagram
ORGANIZATION ||--|{ USER : has
ORGANIZATION ||--|{ TEAM : has
USER ||--|{ USER-ROLE: has
USER-ROLE }|--|| ROLE: role
TEAM ||--|{ TEAM-USER : has
TEAM-USER }|--|| USER: user
TEAM ||--|{ TEAM-REPORT : has
TEAM-REPORT ||--|{ USER-REPORT : has
USER-REPORT }|--|| USER : user
Diagram created by mermaid.live

First, I type to ChatGPT:

I have the following ER diagram as defined by Mermaid: erDiagram ORGANIZATION … (cut for brevity, see definition above)

ChatGPT says:

I see. That is a valid ER diagram syntax for Mermaid. Do you want me to generate a visual representation of it for you?

I then say (see the Ash Framework in case you’re unfamiliar):

Create Ash Framework resources from this diagram using AshPostgres.DataLayer

ChatGPT responds:

defmodule MyApp.Organization do
use Ash.Resource,
data_layer: AshPostgres.DataLayer

attributes do
attribute :name, :string
end

relationships do
has_many :users, MyApp.User
has_many :teams, MyApp.Team
end

# add validations, actions, policies, or extensions here
end

I add:

Continue defining the additional resources.

ChatGPT replies:

Okay, I’ll try to define the additional resources based on your ER diagram. Here is an example of how to define a schema module for User:

defmodule MyApp.User do
use Ash.Resource,
data_layer: AshPostgres.DataLayer

attributes do
attribute :name, :string
attribute :email, :string
end

relationships do
belongs_to :organization, MyApp.Organization
has_many :user_roles, MyApp.UserRole
has_many :roles, MyApp.Role, through: [:user_roles]
has_many :team_users, MyApp.TeamUser
has_many :teams, MyApp.Team, through: [:team_users]
has_many :user_reports, MyApp.UserReport
end

# add validations, actions, policies, or extensions here
end

You can follow the same pattern for the other entities in your diagram. Do you want me to generate more resources for you?

ChatGPT changed the resource names to CamelCase on its own! ChatGPT’s output is astonishing considering that Ash Framework is bleeding edge technology.

Our jobs are safe for now, but for how long? The “big thinking” involved creating the ER diagram (by me, a human). From there, ChatGPT can take over and define Ash Resources, Postgres tables, and more.

And more!

Me:

Add the attribute ‘name’ to all entities. Add the attribute ‘id’ with type ‘uuid’ to all entities. Output the diagram as a Mermaid document.

ChatGPT:

Sure, I can output the diagram as Mermaid document. Here is the result:


erDiagram
ORGANIZATION {
id uuid
name string
}
ORGANIZATION ||--|{ USER : has
USER {
id uuid
name string
}
ORGANIZATION ||--|{ TEAM : has
TEAM {
id uuid
name string
}
USER ||--|{ USER-ROLE: has
USER-ROLE {
id uuid
name string
}
USER-ROLE }|--|{ ROLE: role
ROLE {
id uuid
name string
}
TEAM ||--|{ TEAM-USER : has
TEAM-USER {
id uuid
name string
}
TEAM-USER }|--|| USER: user
TEAM ||--|{ TEAM-REPORT : has
TEAM-REPORT {
id uuid
name string
}
TEAM-REPORT ||--|| USER-REPORT : has
USER-REPORT {
id uuid
name string
}
USER-REPORT }|--|| USER : user

--

--

Terris Linenbach
Terris Linenbach

Written by Terris Linenbach

Coder since 1980. Always seeking the Best Way. CV: https://terris.com/cv

Responses (1)