ErdDocs

Sequelize models to ER diagram

Paste your models and get the ER diagram of the tables Sequelize creates. Table names come from tableName and column names from field. The diagram also shows the foreign keys your associations imply but your attributes never mention. Nothing to install, nothing uploaded.

Your schema

Three of the columns in that diagram appear nowhere in the model definitions above (see below). Paste your own models over it.

The columns you never wrote

A Sequelize schema is spread across three places, and only one of them is the attribute object. The table name is in the options. The column names may be there too, behind field. The foreign keys are often in neither place. They come from association calls that may sit at the bottom of the file, or in a different file entirely.

In the example above, venue_id, event_id and customer_id are declared by belongsTo and by nothing else. Sequelize will create those columns. A diagram that only reads the attribute objects will not show them. The picture then has no relations in it.

What the parser reads

  • Both declaration forms: sequelize.define(...) and Model.init(...), in one file or several pasted together.
  • The names the database gets: tableName over the model name, field over the attribute key.
  • Column properties: the DataTypes with their size, primaryKey, autoIncrement, unique, defaults, and allowNull, which defaults to true just as nullability does in SQL.
  • Associations. belongsTo and hasMany become relations, with the foreign key column added where the association implies one.

Without booting the project

ErdDocs reads the file itself. Paste the models and get the diagram, with nothing to install and nothing to boot. The npm packages that answer this question inspect a live Sequelize instance. You install them, wire them into your app, run it, and get a picture. That works when the project is yours and already runs. It is no help when you have been handed a repository, or when the app needs a database it cannot reach. Nor does it help when you only want to read the model layer before deciding whether to take the work at all. ErdDocs covers all three.

FAQ

Does it understand both sequelize.define and Model.init?

Yes, and a file that uses both. Either way, the real table name lives in the options object. tableName: "ticket_types" wins over the model name, exactly as it does when Sequelize creates the table.

My foreign keys are not in the attributes. They come from belongsTo.

That is the normal case, and it is handled. Sequelize creates the column from the association, not from the attribute list. So Order.belongsTo(Customer, { foreignKey: "customer_id" }) means the orders table has a customer_id column that appears nowhere in your model definition. The diagram adds it, because the database will have it.

What about camelCase attributes and snake_case columns?

field: "starts_at" is what the database sees, so that is what the diagram draws. The JavaScript name stays in your code.

Do I need to run my project?

No. ErdDocs never runs it. It reads the models as text in your browser, so a file with a missing import, a half-finished refactor, or a snippet pasted out of a pull request still draws. Tools that inspect a live Sequelize instance need the project to boot first, which is a much bigger ask than pasting a file.

Is it free?

Yes for schemas up to 25 tables, with no account and no install. $9 once lifts the limit, removes the mark from exports, and unlocks the data dictionary.

Related