“Live up to the time, the creation of non-stop, this article is participating in 2021 year-end summary essay competition”

Reading good open source software is one way to increase your coding skills. Open source software projects can be very large, and there are tricks to reading open source software.

There are plenty of nuggets articles out there, but this series is an attempt to share some of my own. This article begins with a brief explanation of the ERD.

An introduction to

The Entity Relationship (ER) model is usually the result of system analysis and is used to define and describe the content that is important to the process in the business domain. It does not define business processes; It presents the business data schema in graphical form only. It is usually drawn in graphical form as boxes (entities) connected by lines (relationships) that represent associations and dependencies between entities. ER model can also be expressed in the form of language, for example: a building can be divided into zero or more apartments, but an apartment can only be located in one building.

Entities can be represented not only by relationships, but also by additional attributes (attributes), including identifiers called “primary keys.” Diagrams created to represent attributes and entities and relationships can be called entity-attribute-relationship diagrams rather than entity-relationship models.

The ER model is typically implemented as a database. In a simple relational database implementation, each row of a table represents an instance of an entity type, and each field in the table represents an attribute type. In relational databases, relationships between entities are implemented by storing the primary key of one entity as a pointer or “foreign key” in another entity’s table.

ERD example

Entity Relationship Diagram (ERD) is the graphic representation of ER. ERD will help you understand ER more directly, and therefore better grasp some open source projects.

I use spree as an example.

Spree is a well-known e-commerce open source software similar to Shopify. Let’s generate an ERD.

git clone [email protected]:spree/spree_starter.git
cd spree_starter
bundle add rails-erd
bundle
Copy the code

Graphviz needs to be installed on ios

brew install graphviz
Copy the code

Execute it when it’s all installed

Change the corresponding config/database.yaml configuration database and create the corresponding database

Then perform

bundle exec rake db:migrate
Copy the code
bundle exec erd --notation=simple --direct  --orientation=vertical --splines=ortho --connected --attributes=false
Copy the code

You can see the following figure

If I execute this

 bundle exec erd --notation=simple --direct  --orientation=vertical --splines=ortho --connected
Copy the code

You’ll see this

The problem came, there were so many Entity models that it was hard to see who was connected to whom. I made a basic improvement: added color. The operation is as follows:

Change the root Gemfile to the rails-erd line

gem 'rails-erd', github: "williamhatch/rails-erd"
Copy the code

Save. Execute at the command line

bundle
bundle exec erd --notation=simple --direct  --orientation=vertical --splines=ortho --connected
Copy the code

Will get

If you execute

bundle exec erd --notation=simple --direct  --orientation=vertical --splines=ortho --connected --attributes=false
Copy the code

Will get

Is the color distinction clearer 🙂

Next steps:

  1. Define entities and relationships between them in YAML format, then automatically generate Rails models and generate ERDS (in color)
  2. Or use the UI (Web Base) to generate YAML by drag and drop, and then automatically generate the Rails Model and generate the ERD (in color). See StrAPI’s Admin Dashboard for this.
  3. Make a Web base UI (based on NuxT3), you can select (check) modules and only generate the ERD of the relevant modules.
  4. The ultimate goal is for users to simply drag and drop on the Web to model, automatically generate restful apis and graphQL apis, plus simple configuration to support authentication and logging. So the back-end is basically DDD (Domain Driven Design). I will introduce my experience in this respect in succession.

If you are interested, leave a comment below and follow me on Github.