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
  • Orientation
  • Sequelize Connection
  • Analysis
  • app.js
  1. js_library
  2. Node Server
  3. 04 - Database Intro
  4. 01 - Sequelize Intro

02 - Initialize

Previous01 - Sequelize introNext05 - Model View Controller

Last updated 7 years ago

In this module, we will use Sequelize to connect our server to PG Admin.

Orientation

Right now we'll be working with Sequelize and Postgres:

Sequelize Connection

//1
const Sequelize = require('sequelize');

      //2               //3       //4         //5            //6          
const sequelize = new Sequelize('workoutlog', 'postgres', 'Letmein1234!', {
    host: 'localhost', //7
    dialect: 'postgres'  ///8
});
    //9      //10         //11         
sequelize.authenticate().then(
    function() { //12
        console.log('Connected to workoutlog postgres database');
    },
    function(err){ //13
        console.log(err);
    }
);
                 //14
module.exports = sequelize;

Analysis

Let's do some analysis of the code above. You do not need to memorize all of this information. Read through it, and then use it as a reference for when you need it next:

Concept

Analysis

1

Import the Sequelize package.

2

Create an instance of Sequelize for use in the module with the sequelize variable.

3

Use the constructor to create a new Sequelize object.

4

Identify the db table to connect to.

5

The username for the db.

6

The password for the local db.

7

The host points to the local port for Sequelize. This is 5432.

8

Identify the QL dialect being used. Could be MSSQL, SQLLite, or others

9

Use the sequelize variable to access methods.

10

Call the authenticate() method.

11

authenticate() returns a promise. Use .then().

12

Fire a function that shows if we're connected.

13

Fire an error if there are any errors.

14

Export the module.

app.js

We also need to do some configuration in our app.js file.

Do the following: 1. Create a sequelize variable that imports the db file. 2. Use the variable and call .sync(). This method will ensure that we sync all defined models to the DB.

var express = require('express');
var app = express();
var test= require('./controllers/testcontroller')
//1
var sequelize = require('./db');

//2
sequelize.sync(); // tip: pass in {force: true} for resetting tables

app.use('/test', test)

NOTE: When you run your database with Sequelize, you may see the following message:

Newer versions of sequelize make use of the new Symbol datatype instead of relying on strings. You can still use strings for now; this message just means that in the future they may not. We haven't covered Symbols much, but feel free to do some research on your own and try them out.

To use Sequelize, we'll have to establish a connection. This is the standard approach from the , and it is often copy and pasted into projects for setup. Simply put, it's allowing us to connect from our project to the Postgres database. If you haven't already done so, create the file db.js inside your server folder and put this code inside of it:

docs
screenshot
depreciated Strings