JunpStart – Refactoring using Sequelize

postgresSQL

Today, we will be refactoring our mongodb books api to work with postgresSQL DB

DBMS has been around since 60s
click here for more history
SQLite is a lightweight db
MariaDB is forked from mySQL

Sequelize is a promise-based Node.js ORM for postgres, etc
(That means it converts our mongodb books api to SQL queries)

ER diagrams

An Entity Relationship (ER) Diagram is a type of flowchart that illustrates how “entities” such as people, objects or concepts relate to each other within a system.

ER
Crow’s Foot

Crow’s Foot/Martin/Information Engineering style

 

 

Steps for sequelize:

Create the model schema in sequelize.define()

export all your models to index.js in models folder

module.exports = (sequelize, type) => {}

in index.js, import sequelize, and create an instance

const sequelize = new Sequelize(“books-api”, “username”, “password”, {
dialect:”postgres”
});
import models, and associate the models
Object.keys(models).forEach(key => {
if (“associate”inmodels[key]) {
models[key].associate(models);
}
});
export both sequelize and models

then sync it in your server

//Wrap sequelize sync around the app.listen (force: true drops the tables if exists, so only use in dev )
sequelize.sync({ force: true }).then(() => {
createAuthorsAndBooks();
//your app.listen here
});
cross-env for deploying on mac and windows, etc

Leave a comment

Design a site like this with WordPress.com
Get started