Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson

Already subscribed? Sign In


    Sequelize: Model Definition Basics

    Mike FreyMike Frey

    Sequelize is an Object Relational Mapper (ORM), which simplifies interacting with relational databases by abstracting away the need to use SQL directly. This lesson introduces defining models with Sequelize in Node.js.

    You will need the Postgres database for this lesson! For Mac users, postgressapp makes this easy.



    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson


    00:01 I've preconfigured Sequelize for the database, MyDB, running on Postrest. I'm going to use the Sequelize define method to create a post model as if I'm creating a blog engine.

    00:12 Define needs two arguments, the model name -- in this case "Post," and on object defining the model's attributes. The first attribute will be ID. It's type will be "Integer," and it will be a Primary Key that auto-increments.

    00:34 Every blog post needs a title, and my title attribute will be a string.

    00:39 My post model will need an attribute to hold content, and since I don't want content to be constrained by the 8K row limit, I'll use the blob-type text instead of string.

    00:50 Lastly, I'll add a visible attribute to control whether or not the post is publicly visible, and it will default to "False."

    01:00 Now that my model is defined, I need to insure the table has been created if it doesn't exist already. The sync method on the post model will handle this for me. "Sync" is an asynchronous operation. All asynchronous operations in Sequelize return promises, which means I can wait for the promise to resolve using VEN.

    01:20 From here, I can go ahead and create some data to insert into my database. I'll only define title and content. The other attributes will be defaulted, and I'll use data to create a new instance of my post model, using the function "Create."

    01:37 Create will not only create an instance of post, it will also perform an insert into my posts table using the data I've provided it. Once the create is complete, I'm going to print out the resulting post instance to show that it was created.

    01:54 When I run the code in Node.js, I can see Sequelize has executed a "Create Table" query, a second query to describe the table, and finally an insert of the data I specified. Below that is the logged representation of the data in the posts table.

    02:14 Opening the Postrest console, I run a select query on the posts table to validate that my post has been created. It's worth noting here that although I named my model post, Sequelize used the plural version, "Posts," to create the table.

    02:29 Running the query shows data has been inserted into the table, and the visible field and the ID field have both been given default values.