JS-301-NodeServer
  • Introduction
  • js_library
    • Node Server
      • 00 - Intro
        • 01 - Purpose
        • 02 - Back-End Setup
        • 03 - Terms Cheat Sheet
      • 01 - Server Set up
        • 01 - npm packages
        • 02 - Express Intro
        • 03 - Express code
      • 02 - Development Tools
        • 01 - Nodemon Intro
        • 02 - Postman Intro
        • 03 - Postman set up
      • 03 - Routes Intro
        • 01 - Routes intro
        • 02 - Express Router() intro
        • 03 - Challenge 1
        • 04 - Challenge 2
      • 04 - Database Intro
        • 00 - DB Intro and Set up
          • 00 - DB Intro
          • 01 - PostgreSQL Intro
          • 02 - Install
        • 01 - Sequelize Intro
          • 01 - Sequelize intro
          • 02 - Initialize
      • 05 - Model View Controller
        • 01 - MVC
          • 00 - MVC Intro
        • 02 - Models
          • 01 - Intro to Models
          • 02 - Test Model
        • 03 - Controllers
          • 00 - Controllers Intro
          • 01 - Controller Set up
          • 02 - Create Method
          • 03 - req.body()
          • 04 - Crafting the Response
          • 05 - Sending the Response
          • 06 - JSON Response
          • 07 - Error Handling
        • 04 - Conclusion
      • 06 - Tokenization
        • 01 - JWT Intro
          • 01 - JWT intro
        • 02 - User Create
          • 01 - User Create
          • 02 - Refactor
        • 03 - User Token
          • 01 - JWT Package
          • 02 - Adding JWT
          • 03 - ENV
      • 07 - Encryption
        • 01 - bcrypt
        • 02 - bcrypt setup
      • 08 - Session
        • 00 - Session Intro
        • 01 - Sign In Method
        • 02 - Sign In Bcrypt
        • 03 - Sign In JWT
      • 09 - Middleware
        • 01 - Test Client HTML
        • 02 - Test Client JS
        • 03 - Middleware intro
        • 04 - Headers intro
        • 05 - Server Update
        • 06 - Test Post
        • 07 - Test Post Refactor
        • 08 - Post Data
        • 09 - Fetch From One
      • 10 - Authenticated Routes
        • 01 - Intro to Authenticated Routes
        • 02 - Validate Session
        • 03 - Changes to app.js
        • 04 - authtestcontroller.js
        • 05 - Delete an Item
        • 06 - Update an Item
        • 07 - Postman Testing
      • 11 - Authenticated Requests
        • 00 - Additions to index
        • 01 - Anatomy of a Request
        • 02 - Create User
        • 03 - Getting a Token
        • 04 - Get Items From One User
        • 05 - Creating an Item for a User
        • 06 - Get one item
        • 07 - Update an Item
        • 08 - Deleting an Item
        • 09 - Deleting with a Custom Event
      • 12 - Workout Log Server
        • 00 - Intro
      • 13 - More Sequelize Functions
        • Migrations
          • 00 - Intro
          • 01 - init and config
          • 02 - Creating the First Migration
          • 03 - Running Migrations
          • 04 - Reverting Migrations
          • 05 - Seeds
          • 06 - Reverting Seeds
        • Queries
          • 00 - Intro
          • 01 - Queries
Powered by GitBook
On this page
  • File Setup
  • Test Model
  • Analysis
  1. js_library
  2. Node Server
  3. 05 - Model View Controller
  4. 02 - Models

02 - Test Model

In this module, we'll introduce you to a Sequelize model and use the .define() method to build a database model.

File Setup

Let's start by adding a models folder and a test.js file:

javascript-library
    └── 5-Express Server
        └── Server
            └── controllers
            └── models
                └── test.js
            └── app.js
            └── db.js

Test Model

Let's create our first model, a test model:

     //7                         //1  
module.exports = function (sequelize, DataTypes) {
                      //2   //3 
    return sequelize.define('test', { //4
        //5         //6
        testdata: DataTypes.STRING
    });
};

Analysis

  1. We run an anonymous function that has two parameters: sequelize and DataTypes. The function will return the value of what is created by sequelize.define.

  2. In the first argument of the define method, we pass in the string test. This will become a table called tests in Postgres (the table names are pluralized).

  3. Our second argument of the define function is an object. Any key/value pairs in the following object will become columns of the table. The syntax looks a little weird here. Just know that it's an object that we can pass in numerous properties to create numerous table columns.

  4. testdata is a key in our model object that will be a column in our database.

  5. The model is exported to allow Sequelize to create the tests table with the testdata column the next time the server connects to the database and a user makes a POST request that uses the model.

Previous01 - Intro to ModelsNext03 - Controllers

Last updated 7 years ago

We use the sequelize object to call the define method. .define() is a Sequelize method that will map model properties in the server file to a table in Postgres. You can read more about it .

DataTypes.STRING is our value for the testdata property. Because we define it as a STRING value in the model, any information to be held in that column MUST be a string data-type. Remember that DataTypes is a parameter in the function brought in through Sequelize. You can read more about Sequelize DataTypes . Although JavaScript is a loosely typed language, Postgres wants to know what data types we're adding to each of our columns. Sequelize is making us declare the data types that we'll be storing.

here
here