DEV Community

Cover image for Sequelize Model Guide
Matthew Carpenter
Matthew Carpenter

Posted on

Sequelize Model Guide

Introduction to ORM

ORM or Object Relation Mapping, is a process of mapping between objects and relational database systems. Think of an ORM as the translator between your objects in code and the relational representation of your data. There are several benefits to using an ORM, one of which is DRY. You write your model in only one place, and it's easier to update, maintain, and reuse your code. You also don't have to use SQL code, which can be confusing for some developers.

Insert Sequelize

Sequelize is a promised based ORM for NodeJS. It supports the dialects PostgreSQL, MySQL, SQLite and MSSQL. Sequelize is easy to read and write, it uses javascript syntax, and easy to test using mocha.

ID    Name        Phone           Address             <—— ROWS
1     John Doe    444-444-4444    123 Database St.    <—— COLUMNS
2     Jane Doe    555-555-5555    456 SQL Dr.         <—— COLUMNS
Enter fullscreen mode Exit fullscreen mode

Sequelize Model

A sequelize model represents a table in the database. Instances of this class represent a database row. If you're not familiar with a relational database such as SQL, think excel. A relational database is a table of rows and columns. Sequalize helps manage that table, offering synchronization, association, validation. When setting up a model i'm predefining how I would like to store my data. Let's look at an example model below and how to set one up.

module.exports = function(sequelize, DataTypes) {

   var Todo = sequelize.define("Todo", {
       text: { //TABLE ROW
          type: DataTypes.STRING, //EXPECTING A STRING {COLUMN STRUCTURE}
          AllowNull: false, //THIS COLUMN CANNOT BE EMPTY
          validate: { //VALIDATE INFO GOING INTO THE TABLE
            len: [1, 140] //NEEDS TO BE BETWEEN 1 AND 140 CHARACTERS
          } 
        },
        complete: { //TABLE ROW
          type: DataTypes.BOOLEAN, //EXPECTING A BOOLEAN {COLUMN STRUCTURE}
          defaultValue: false //THIS COLUMN WILL HAVE A DEFAULT VALUE OF FALSE
        }
      });
      return Todo; //RETURN THE TABLE
    };
Enter fullscreen mode Exit fullscreen mode

Whats going on here?

To define mappings between a model and a table, use the define method. We then define our rows text and complete. Inside each of our objects we predefine the structure of that data we are expecting sequelize to pass to mySQL.

Translation Please

CREATE DATABASE todo_list;

USE todo_list

CREATE TABLE Todo(
    id INTEGER AUTO_INCREMENT NOT NULL,
    PRIMARY KEY(id),
    text VARCHAR(255) NOT NULL, 
    complete BOOLEAN DEFAULT FALSE
);
Enter fullscreen mode Exit fullscreen mode

*ID - Expecting an integer, Auto increment the ID column 1,2,3 .. etc
*The primary key for a table represents the column or set of columns that you use in your most vital queries.
*Text row, column is expected to be a STRING of 255 char or less
*complete, column is expected to be a BOOLEAN with a default of false

You made it!

All in all, sequalize is an awesome library with great documentation. Not only does it help structure your data, it also helps you become a better developer. Thanks for reading.

Sequelize Docs
MySQL Docs

Top comments (0)