Skip to main content

Visualizing Sequelize Schemas

Using an Entity-Relationship Diagram (ERD) tool to visualize a database schema offers a clear and intuitive depiction of the database's structure, simplifying the process of understanding the relationships and dependencies among various entities.

With Atlas, you can easily visualize your Sequelize schema.

Getting started with Atlas and Sequelize

Before we continue, ensure you have installed the Atlas Sequelize Provider on your Sequelize project.

To set up, follow along the getting started guide for Sequelize and Atlas.

Project Setup

Suppose we have the following Sequelize models directory, with two models user and task:

'use strict';
module.exports = (sequelize, DataTypes) => {
const Task = sequelize.define('Task', {
complete: {
type: DataTypes.BOOLEAN,
defaultValue: false,
}
});

Task.associate = (models) => {
Task.belongsTo(models.User, {
foreignKey: {
name: 'userID',
allowNull: false
},
as: 'tasks'
});
};

return Task;
};

Above we see 2 models: User and Task, with a one-to-many relationship between them.

Config File

Before we begin testing, create a config file named atlas.hcl.

In this file we will create an environment, specify the source of our schema, and a URL for our dev database.

note

Be sure to update the --path in the config file with the correct path, as well as --dialect and dev depending on which database you are using.

atlas.hcl
data "external_schema" "sequelize" {
program = [
"npx",
"@ariga/atlas-provider-sequelize",
"load",
"--path", "./path/to/models",
"--dialect", "postgres", // mysql | mariadb | sqlite | mssql
]
}

env "sequelize" {
src = data.external_schema.sequelize.url
dev = "docker://postgres/16/dev?search_path=public"
}

Visualizing

Now that we are all setup, we can visualize our Sequelize models by running the inspect command with the -w flag:

 atlas schema inspect --env sequelize --url env://src -w

If you are not logged in, the output should be similar to:

? Where would you like to share your schema visualization?:
▸ Publicly (gh.atlasgo.cloud)
Your personal workspace (requires 'atlas login')

Our browser should open:

sequelize-visualization-in-atlas-cloud

Amazing! Now you can easily view and share your schema. Logged in users can also privately create schemas and save them for future use.